Jump to content

Module:Infobox television/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gonnym (talk | contribs) at 16:28, 27 September 2021 (Create sandbox version of Module:Infobox television). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
--- @module
local p = {}

local maintenance_categories = {
	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 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 DISPLAYTITLE template is used.
---
---@return string
local function has_display_title()
	local page_text = mw.title.getCurrentTitle():getContent()
	if 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 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.
---
--- |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.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.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}} 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())
	table.insert(categories, are_image_auxiliary_values_used_for_no_image(args))
	table.insert(categories, has_flag_icon(args))
	table.insert(categories, are_dates_formatted_correctly(args.first_aired or args.released, args.last_aired))

	return table.concat(categories, "")
end


return p