మాడ్యూల్:Infobox television

--- @module
local p = {}

local maintenance_categories = {
	incorrectly_formatted = "[[Category:Pages using infobox television with incorrectly formatted values|%s]]",
	image_values_without_an_image = "[[Category:Pages using infobox television with image-related values without an image]]",
	unnecessary_title_parameter = "[[Category:Pages using infobox television with unnecessary name parameter]]",
	non_matching_title = "[[Category:Pages using infobox television 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 with unnecessary manual displaytitle]]",
}

--- Returns a maintenance category if the italic_title value is not "no".
---
--- Infobox parameters checked:
--- - |italic_title=
---
--- @param args table
--- @return string
local function is_italic_title_valid_value(args)
	if args.italic_title and args.italic_title ~= "no" then
		return string.format(maintenance_categories.incorrectly_formatted, "Italic title")
	end
	return ""
end

--- Returns a maintenance category if the dates are not formatted correctly with
--- {{Start date}} and {{End date}} templates.
---
--- Infobox parameters checked:
--- - |first_aired=
--- - |released=
--- - |last_aired=
---
--- @param start_date string The start date value.
--- @param end_date string The end date value.
--- @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 {{Italic title}} or {{DISPLAYTITLE}} template is used.
---
--- Testing parameters:
--- - |page_test= - a real Wikipedia page.
---
--- @param args table
--- @return string
local function has_display_title(args)
	local page_text
	if args.page_test then
		page_text = mw.title.new(args.page_test):getContent()
	else
		page_text = mw.title.getCurrentTitle():getContent()
	end

	if string.find(page_text, "{{[Ii]talic title") or string.find(page_text, "DISPLAYTITLE") then
		return maintenance_categories.manual_display_title
	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 the values are linked or formatted.
---
--- Infobox parameters checked:
--- - |language=
--- - |website=
--- - |production_website=
---
--- The function currently checks if the following values are present:
--- - ] - links.
--- - {{URL - use the {{URL}} template.
---
--- @param args table
--- @return string
local function are_values_linked_or_formatted(args)
	args = {language = args.language, website = args.website, production_website = args.production_website}
	for key, value in pairs(args) do
		for _, bad_value in pairs({"]", "{{URL"}) do
			if string.find(value, bad_value, 1, true) then
				return string.format(maintenance_categories.incorrectly_formatted, key)
			end
		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:
--- - |name=
---
--- The function currently checks if the infobox title is equal to the article title while ignoring styling such as:
--- - nowrap spans.
--- - line breaks.
---
--- Testing parameters:
--- - |page_title_test= - the title of the page being checked.
---
--- @param frame table
--- @param args table
--- @return string
local function is_infobox_title_equal_to_article_title(frame, args)
	if not args.name 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 page_name, _ = string.gsub(article_title, "%s+%b()$", "")

	-- Remove nowrap span.
	if string.find(args.name, "nowrap") then
		local title = frame:expandTemplate{title = "Strip tags", args = {args.name}}
		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.name, "<br%s?/?>") then
		local title, _ = string.gsub(args.name, "<br%s?/?>", "")
		title, _ = string.gsub(title, "  ", " ")
		if title == page_name then
			return ""
		end
		return maintenance_categories.non_matching_title
	end

	if args.name == 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}} 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, has_display_title(args))
	table.insert(categories, are_image_auxiliary_values_used_for_no_image(args))
	table.insert(categories, are_values_linked_or_formatted(args))
	table.insert(categories, has_flag_icon(args))
	table.insert(categories, are_dates_formatted_correctly(args.first_aired or args.released, args.last_aired))
	table.insert(categories, is_italic_title_valid_value(args))

	return table.concat(categories, "")
end


return p