Jump to content

Module:Infobox television episode

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gonnym (talk | contribs) at 09:01, 12 September 2021. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--- @module
local p = {}

local maintenance_categories = {
	incorrectly_formatted = "[[Category:Pages using infobox television episode with incorrectly formatted values]]",
	unlinked_values = "[[Category:Pages using infobox television episode with unlinked values]]",
	image_values_without_an_image = "[[Category:Pages using infobox television episode with image-related values without an image]]",
	unnecessary_title_parameter = "[[Category:Pages using infobox television episode with unnecessary title parameter]]",
	non_matching_title = "[[Category:Pages using infobox television episode with non-matching title]]",
	flag_icon = "[[Category:Pages using infobox television with flag icon]]",
	dates_incorrectly_formatted = "[[Category:Pages using infobox television with nonstandard dates]]",
	manual_display_title = "[[Category:Pages using infobox television episode with unnecessary manual displaytitle]]",
}

--- Returns a maintenance category if the dates aren't formatted correctly with
--- {{Start date}} and {{End date}} templates.
---
--- @param start_date string The start date parameter.
--- @param end_date string The end date parameter.
--- @return string
local function are_dates_formatted_correctly(start_date, end_date)
	if start_date and not string.find(start_date, "dtstart") then
		return maintenance_categories.dates_incorrectly_formatted
	end

	if end_date and (not string.find(end_date, "dtend") and end_date ~= "present") then
		return maintenance_categories.dates_incorrectly_formatted
	end

	return ""
end

--- Returns a maintenance category if a flag icon is used.
---
--- All the infobox values are checked.
---
--- @param args table
--- @return string
local function has_flag_icon(args)
	for _, v in pairs(args) do
		if string.find(v, "flagicon") then
			return maintenance_categories.flag_icon
		end
	end
	return ""
end

--- Returns a maintenance category if unwanted values are found in the text.
---
--- Infobox parameters checked:
--- - |episode=
--- - |season=
--- - |series_no=
--- - |episode_list=
---
--- The function currently checks if the following values are present:
--- - ]] - links.
--- - '' - italics or bold.
---
--- @param args table
--- @return string
local function are_values_unlinked_and_unformatted(args)
	for _, parameter in pairs({args.episode, args.season, args.series_no, args.episode_list}) do
		for _, bad_value in pairs({"]]", "''"}) do
			if string.find(parameter, bad_value, 1, true) then
				return maintenance_categories.incorrectly_formatted
			end
		end
	end
	return ""
end

--- Returns a maintenance category if the values use additional overall numbering.
---
--- Infobox parameters checked:
--- - |episode=
--- - |season=
--- - |series_no=
---
--- The function currently checks if the following values are present:
--- - overall - unsupported series overall numbering.
---
--- @param args table
--- @return string
local function are_values_using_overall(args)
	for _, parameter in pairs({args.episode, args.season, args.series_no}) do
		if string.find(parameter, "overall") then
			return maintenance_categories.incorrectly_formatted
		end
	end

	return ""
end

--- Returns a maintenance category if the values are unlinked and if additional characters are found in the text.
---
--- Infobox parameters checked:
--- - |series=
--- - |prev=
--- - |next=
---
--- The function currently checks if a value is unlinked or if there is any additional character
--- before or after the linked text.
---
--- @param args table
--- @return string
local function are_values_links_only(args)
	for _, v in pairs({args.series, args.prev, args.next}) do
		-- Check whether the values are linked.
		if not string.find(v, "%[%[.*%]%]") then
			return maintenance_categories.unlinked_values
		end

		-- Check whether the values have anything before or after link brackets.
		if string.gsub(v, "(%[%[.*%]%])", "") ~= "" then
			return maintenance_categories.incorrectly_formatted
		end
	end

	return ""
end

--- Returns a maintenance category if there is no image file while image auxiliary values are present.
---
--- Infobox parameters checked:
--- - |image=
--- - |image_size=
--- - |image_upright=
--- - |image_alt=
--- - |alt=
--- - |caption=
---
--- @param args table
--- @return string
local function are_image_auxiliary_values_used_for_no_image(args)
	if args.image then
		return ""
	end

	if args.image_size or args.image_upright or args.image_alt or args.alt or args.caption then
		return maintenance_categories.image_values_without_an_image
	end

	return ""
end

--- Returns a maintenance category if the infobox title is equal to the article title
---
--- Infobox parameters checked:
--- - |title=
---
--- The function currently checks if the infobox title is equal to the article title while ignoring styling such as:
--- - nowrap spans.
--- - line breaks.
--- - Leading and trailing apostrophe spaces.
---
--- |page_title_test= is used for testing only.
---
--- @param frame table
--- @param args table
--- @return string
local function is_infobox_title_equal_to_article_title(frame, args)
	if not args.title then
		return ""
	end

	local article_title = args.page_title_test
	if not args.page_title_test then
		article_title = mw.title.getCurrentTitle().text
	end

	-- Remove disambiguation.
	local _
	article_title, _ = string.gsub(article_title, "%s+%b()$", "")

	local space_single = frame:expandTemplate{title = "Space+single"}
	local single_space = frame:expandTemplate{title = "Single+space"}

	-- Convert the page name to use the "Space+single" and "Single+space" templates if a leading or trailing apostrophe is used.
	local page_name, _ = string.gsub(string.gsub(article_title, "^'", space_single), "'$", single_space)

	-- Remove nowrap span.
	if string.find(args.title, "nowrap") then
		local title = frame:expandTemplate{title = "Strip tags", args = {args.title}}
		if title == page_name then
			return ""
		end
		return maintenance_categories.non_matching_title
	end

	-- Remove line breaks and additional spaces as a result.
	if string.find(args.title, "<br%s?/?>") then
		local title, _ = string.gsub(args.title, "<br%s?/?>", "")
		title, _ = string.gsub(title, "  ", " ")
		if title == page_name then
			return ""
		end
		return maintenance_categories.non_matching_title
	end

	if args.title == page_name then
		return maintenance_categories.unnecessary_title_parameter
	end

	-- Article and infobox titles do not match.
	return maintenance_categories.non_matching_title
end

--- Returns the relevant maintenance categories based on the Infobox television episode values validated.
---
--- @param frame table
--- @return string
function p.validate_values(frame)
	local getArgs = require("Module:Arguments").getArgs
	local args = getArgs(frame)

	local categories = {}
	table.insert(categories, is_infobox_title_equal_to_article_title(frame, args))
	table.insert(categories, are_image_auxiliary_values_used_for_no_image(args))
	table.insert(categories, are_values_links_only(args))
	table.insert(categories, are_values_using_overall(args))
	table.insert(categories, are_values_unlinked_and_unformatted(args))
	table.insert(categories, has_flag_icon(args))
	table.insert(categories, are_dates_formatted_correctly(args.airdate or args.released, nil))

	return table.concat(categories, "")
end

--- Returns an {{Italic dab2}} instance if title qualifies and a maintenance category if conditions are met.
---
--- The article's title is italicized if the series name is included in the article's title disambiguation.
--- No italicization happens if one of the following conditions is met:
---
--- - |no_italic= is set.
--- - The article's title does not use disambiguation.
--- - No |series= value is set.
--- - The article's disambiguation is not equal or does not include the series name.
---
--- The page is added to a maintenance category if the title is italicized and there is already an Italic dab
--- or DISPLAYTITLE template.
---
--- Infobox parameters checked:
--- - |series=
--- - |no_italic=
---
--- @param frame table
--- @return string, string
function p.italic_title(frame)
	local getArgs = require("Module:Arguments").getArgs
	local args = getArgs(frame)

	-- In case the page does not need to be italicized, any value will disable it.
	if args.no_italic then
		return "", ""
	end

	local article_title = args.page_title_test
	if not args.page_title_test then
		article_title = mw.title.getCurrentTitle().text
	end

	local match = require("Module:String")._match
	local disambiguation = match(article_title, "%s%((.-)%)", 1, -1, false, "")

	-- The title is not italicized if the title does not use disambiguation or if the series parameter isn't used.
	if disambiguation == "" or not args.series then
		return "", ""
	end

	local delink = require("Module:Delink")._delink
	local series_name = delink({args.series})

	-- Check if the page already has an Italic dab or DISPLAYTITLE template.
	local page_text = mw.title.getCurrentTitle():getContent()
	local maintenance_category = ""
	if string.find(page_text, "[Ii]talic dab") or string.find(page_text, "DISPLAYTITLE") then
		maintenance_category = maintenance_categories.manual_display_title
	end

	-- Check if the disambiguation equals the series name or if the series name can be found in the disambiguation.
	local italic_dab
	if disambiguation == series_name then
		italic_dab = frame:expandTemplate{title = "Italic dab2"}
	elseif string.find(disambiguation, series_name) then
		italic_dab = frame:expandTemplate{title = "Italic dab2", args = {string = series_name}}
	else
		return "", ""
	end

	if args.page_title_test and italic_dab then
		italic_dab = "italic_dab"
	end
	return italic_dab, maintenance_category

end

--- Returns a maintenance category if a flag icon is used.
---
--- This function is currently used by other television infobox templates.
---
--- @param frame table
--- @return string
function p.check_for_flag_icon(frame)
	local getArgs = require("Module:Arguments").getArgs
	local args = getArgs(frame)
	return has_flag_icon(args)
end

--- Returns a maintenance category if the dates aren't formatted correctly with
--- {{Start date}} and {{End date}} templates.
---
--- Infobox parameters checked for Television and Season infoboxes:
--- - |first_aired=
--- - |released=
--- - |last_aired=
---
--- Infobox parameters checked for Crossover infoboxes:
--- - |airdate_overall=
---
--- @param frame table
--- @return string
function p.validate_dates(frame)
	local getArgs = require("Module:Arguments").getArgs
	local args = getArgs(frame)

	if args.type == "crossover" then
		return are_dates_formatted_correctly(args.airdate_overall, nil)
	end

	return are_dates_formatted_correctly(args.first_aired or args.released, args.last_aired)
end

return p