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 10:59, 31 August 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]]",

}

--- 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_single_links(args)
	for _, v in pairs({args.series, args.prev, args.next}) do
		-- Check whether the values are linked.
		if 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 values checked.
--- @param frame table
--- @return string
function p.verify_values(frame)
	local getArgs = require("Module:Arguments").getArgs
	local args = getArgs(frame)

	local categories = ""
	categories = categories .. is_infobox_title_equal_to_article_title(frame, args)
	categories = categories .. are_image_auxiliary_values_used_for_no_image(args)
	categories = categories .. are_values_single_links(args)
	categories = categories .. are_values_using_overall(args)
	categories = categories .. are_values_unlinked_and_unformatted(args)

	return categories
end

return p