Module:Infobox television episode
![]() | This Lua module is used on approximately 13,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
![]() | This module depends on the following other modules: |
Module:Infobox television episode is used to validate parameter values for Template:Infobox television episode and Template:Infobox television crossover episode.
Functions
above_title
{{#invoke:Infobox television episode|above_title}}
Returns the text used for the |above=
field of the infobox.
episode_list
{{#invoke:Infobox television episode|episode_list}}
Returns the text used for the |episode_list=
field of the infobox.
italic_title
{{#invoke:Infobox television episode|italic_title}}
Returns an {{Italic dab2}} instance if title qualifies. Also returns 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:
|italic_title=no
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}}, {{Italic title}} or {{DISPLAYTITLE}} template.
Infobox parameters checked:
|series=
|italic_title=
validate_values
{{#invoke:Infobox television episode|validate_values}}
Returns the relevant maintenance categories based on the {{Infobox television episode}} values validated.
Infobox television crossover episode function
validate_values_crossover
{{#invoke:Infobox television episode|validate_values_crossover}}
Returns the relevant maintenance categories based on the {{Infobox television crossover episode}} values validated.
See also
Maintenance categories
The module places relevant pages in the following maintenance categories:
- Category:Pages using infobox television with flag icon (0)
- Category:Pages using infobox television episode with image-related values without an image (5)
- Category:Pages using infobox television episode with incorrectly formatted values (50)
- Category:Pages using infobox television episode with non-matching title (9)
- Category:Pages using infobox television episode with nonstandard dates (14)
- Category:Pages using infobox television episode with unlinked values (1,085)
- Category:Pages using infobox television episode with unnecessary list markup (3,601)
- Category:Pages using infobox television episode with unnecessary manual displaytitle (11)
- Category:Pages using infobox television episode with unnecessary title parameter (9)
--- @module
local p = {}
local maintenance_categories = {
incorrectly_formatted = "[[Category:Pages using infobox television episode with incorrectly formatted values|%s]]",
unlinked_values = "[[Category:Pages using infobox television episode with unlinked values|%s]]",
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 the text after removing line breaks (br tags) and additional spaces as a result.
---
--- @param text string
--- @return string
local function get_name_with_br_fixes(text)
local title, _ = string.gsub(text, "<br%s?/?>", "")
title, _ = string.gsub(title, " ", " ")
return title
end
--- Returns the page name after fixing it to use the {{Space+single}} and {{Single+space}} templates
--- if a leading or trailing apostrophe is used.
---
--- @param frame table
--- @param article_title string
--- @return string
local function get_page_name_with_apostrophe_fixes(frame, article_title)
local space_single = frame:expandTemplate{title = "Space+single"}
local single_space = frame:expandTemplate{title = "Single+space"}
local page_name, _ = string.gsub(string.gsub(article_title, "^'", space_single), "'$", single_space)
return page_name
end
--- Returns the series name after de-linking it and other modifications.
---
--- @param series string
--- @return string
local function get_series_name(series)
local delink = require("Module:Delink")._delink
local series_name = delink({series})
-- Escape the character "-" as it is needed for string.find() to work.
local _
series_name, _ = string.gsub(series_name, "-", "%%-")
return series_name
end
--- Returns a table consisting of the episode's title parts.
---
--- The return table's properties:
--- - title - The episode's title.
--- - disambiguation - the disambiguation text without parentheses.
---
--- Note: could potentially be moved to an outside module for other template and module uses.
---
--- @param text string
--- @return table
local function get_title_parts(text)
local title, disambiguation = string.match(text, "^(.+) (%b())$")
local title_parts = {title = title, disambiguation = disambiguation}
if not title then
title_parts.title = text
return title_parts
end
if not disambiguation then
return title_parts
end
-- Remove outside parentheses from names which use parentheses as part of the name such as "episode (Randall and Hopkirk (Deceased))".
disambiguation = string.sub(disambiguation, 2, -2)
title_parts.disambiguation = disambiguation
return title_parts
end
--- Returns the title used in the {{Lowercase title}} template and an optional maintenance category.
---
--- @param page_text string
--- @param args table
--- @param title_parts table
--- @return string | nil
local function get_lowercase_template_status(page_text, args, title_parts)
local lowercase_template = string.match(page_text, "{{[Ll]owercase title.-}}")
if lowercase_template then
local lowercase_title, _ = string.gsub(title_parts.title,"^%u", string.lower)
if args.title then
if args.title == lowercase_title then
return maintenance_categories.unnecessary_title_parameter
else
return maintenance_categories.non_matching_title
end
return ""
end
return lowercase_title
end
return nil
end
--- Returns the title used in the {{Correct title}} template and an optional maintenance category.
---
--- @param page_text string
--- @param args table
--- @param return_category boolean
--- @return string | nil
local function get_correct_title_value(page_text, args, return_category)
local correct_title_template_pattern = "{{[Cc]orrect title|title=(.*)|reason=.*}}"
local correct_title = string.match(page_text, correct_title_template_pattern)
if not correct_title then
correct_title_template_pattern = "{{[Cc]orrect title|(.*)|reason=.*}}"
correct_title = string.match(page_text, correct_title_template_pattern)
end
if not correct_title then
return nil
end
local correct_title_title_parts = get_title_parts(correct_title)
if not correct_title_title_parts.disambiguation then
-- If the correct title value has no disambiguation, check if the title used in the infobox is the same as the title used for the correct title value.
if return_category and args.title then
if args.title == correct_title_title_parts.title then
return maintenance_categories.unnecessary_title_parameter
else
return maintenance_categories.non_matching_title
end
end
return correct_title_title_parts.title
end
local series_name = get_series_name(args.series)
if series_name ~= "" and (correct_title_title_parts.disambiguation == series_name or string.find(correct_title_title_parts.disambiguation, series_name)) then
if return_category and args.title then
if args.title == correct_title_title_parts.title then
return maintenance_categories.unnecessary_title_parameter
else
return maintenance_categories.non_matching_title
end
end
return correct_title_title_parts.title
end
-- Can't determine if the text in parentheses is disambiguation or part of the title since |series= isn't used.
if return_category then
return ""
end
return correct_title
end
--- 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 date is not formatted correctly with a {{Start date}} template.
---
--- Infobox parameters checked:
--- - |airdate=
--- - |released=
--- - |airdate_overall=
---
--- @param start_date string
--- @return string
local function are_dates_formatted_correctly(start_date)
if start_date and not string.find(start_date, "dtstart") 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 the values are linked or formatted.
---
--- 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_linked_or_formatted(args)
args = {episode = args.episode, season = args.season, series_no = args.series_no, episode_list = args.episode_list}
for key, value in pairs(args) do
for _, bad_value in pairs({"]]", "''"}) 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 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)
args = {episode = args.episode, season = args.season, series_no = args.series_no}
for key, value in pairs(args) do
if string.find(value, "overall") then
return string.format(maintenance_categories.incorrectly_formatted, key)
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)
args = {series = args.series, prev = args.prev, next = args.next}
for key, value in pairs(args) do
-- Check whether the values are linked.
if not string.find(value, "%[%[.*%]%]") then
return string.format(maintenance_categories.unlinked_values, key)
end
-- Check whether the values have anything before or after link brackets.
if string.gsub(value, "(%[%[.*%]%])", "") ~= "" then
return string.format(maintenance_categories.incorrectly_formatted, key)
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=
--- - |series=
---
--- 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.
---
--- 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.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
local title_parts = get_title_parts(article_title)
if title_parts.disambiguation then
local series_name = get_series_name(args.series)
series_name = get_name_with_br_fixes(series_name)
if series_name ~= "" and (title_parts.disambiguation == series_name or string.find(title_parts.disambiguation, series_name)) then
-- Remove disambiguation.
article_title = title_parts.title
end
end
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 title includes a hash sign ("#") get the title from the {{Correct title}} template.
if string.find(args.title, "#") then
local correct_title = get_correct_title_value(page_text, args, true)
if correct_title then
return correct_title
end
end
local lowercase_title = get_lowercase_template_status(page_text, args, title_parts)
if lowercase_title then
return lowercase_title
end
local page_name = get_page_name_with_apostrophe_fixes(frame, article_title)
-- 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 = get_name_with_br_fixes(args.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_linked_or_formatted(args))
table.insert(categories, has_flag_icon(args))
table.insert(categories, are_dates_formatted_correctly(args.airdate or args.released))
table.insert(categories, is_italic_title_valid_value(args))
-- TODO: temp code to see usage.
if args.rtitle then
table.insert(categories, "[[Category:Pages using infobox television episode with rtitle]]")
end
return table.concat(categories)
end
--- Returns an {{Italic dab2}} instance if title qualifies. Also returns 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:
---
--- - |italic_title= is set to "no".
--- - 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}}, {{Italic title}} or {{DISPLAYTITLE}} template.
---
--- Infobox parameters checked:
--- - |series=
--- - |italic_title=
---
--- Testing parameters:
--- - |page_test= - a real Wikipedia page.
--- - |page_title_test= - the title of the page being checked.
---
--- @param frame table
--- @return string, string
function p.italic_title(frame)
local getArgs = require("Module:Arguments").getArgs
local args = getArgs(frame)
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
local maintenance_category = ""
-- In case the page does not need to be italicized or can't be automatically done, a "no" value will disable both
-- the italicization and the error handling.
if args.italic_title == "no" then
return "", maintenance_category
end
local article_title = args.page_title_test
if not args.page_title_test then
article_title = mw.title.getCurrentTitle().text
end
-- Check if the page already has an {{Italic dab}}, {{Italic title}} or {{DISPLAYTITLE}} template.
local hasItalicDab, _ = string.find(page_text, "{{[Ii]talic dab")
local hasItalicTitle, _ = string.find(page_text, "{{[Ii]talic title")
local hasDisplayTitle, _ = string.find(page_text, "{{DISPLAYTITLE")
if hasItalicDab or hasItalicTitle or hasDisplayTitle then
maintenance_category = maintenance_categories.manual_display_title
end
local title_parts = get_title_parts(article_title)
-- The title is not italicized if the title does not use disambiguation or if the series parameter isn't used.
if not title_parts.disambiguation or not args.series then
return "", maintenance_category
end
local delink = require("Module:Delink")._delink
local series_name = delink({args.series})
series_name = get_name_with_br_fixes(series_name)
-- Check if the disambiguation equals the series name or if the series name can be found in the disambiguation.
local italic_dab
if title_parts.disambiguation == series_name then
italic_dab = frame:expandTemplate{title = "Italic dab2"}
elseif string.find(title_parts.disambiguation, series_name) then
italic_dab = frame:expandTemplate{title = "Italic dab2", args = {string = series_name}}
else
return "", maintenance_category
end
if args.page_title_test and italic_dab then
italic_dab = "italic_dab"
end
return italic_dab, maintenance_category
end
--- Returns the text used for the |above= field of the infobox.
---
--- Infobox parameters checked:
--- - |rtitle=
--- - |title=
--- - |series=
---
--- Testing parameters:
--- - |page_test= - a real Wikipedia page.
--- - |page_title_test= - the title of the page being checked.
---
--- @param frame table
--- @return string
function p.above_title(frame)
local getArgs = require("Module:Arguments").getArgs
local args = getArgs(frame)
if args.rtitle then
local title_pattern = '"(.*)" and "(.*)"'
if string.find(args.rtitle, title_pattern) then
local episode1, episode2 = string.match(args.rtitle, title_pattern)
local title_format = "\"'''%s'''\" and \"'''%s'''\""
return string.format(title_format, episode1, episode2)
end
local title_pattern_br = '"(.*)" and%s?<br%s?/?>%s?"(.*)"'
if string.find(args.rtitle, title_pattern_br) then
local episode1, episode2 = string.match(args.rtitle, title_pattern_br)
local title_format = "\"'''%s'''\" and<br/> \"'''%s'''\""
return string.format(title_format, episode1, episode2)
end
return string.format("'''%s'''", args.rtitle)
end
local title_format = "\"'''%s'''\""
if args.title then
return string.format(title_format, args.title)
end
local page
if args.page_test then
page = mw.title.new(args.page_test)
else
page = mw.title.getCurrentTitle()
end
local page_text = page:getContent()
local correct_title = get_correct_title_value(page_text, args, false)
if correct_title then
return string.format(title_format, correct_title)
end
local article_title = args.page_title_test
if not args.page_title_test then
article_title = page.text
end
local title_parts = get_title_parts(article_title)
local lowercase_title = get_lowercase_template_status(page_text, args, title_parts)
if lowercase_title then
return string.format(title_format, lowercase_title)
end
if not title_parts.disambiguation then
return string.format(title_format, get_page_name_with_apostrophe_fixes(frame, title_parts.title))
end
local series_name = get_series_name(args.series)
if series_name ~= "" and (title_parts.disambiguation == series_name or string.find(title_parts.disambiguation, series_name)) then
return string.format(title_format, get_page_name_with_apostrophe_fixes(frame, title_parts.title))
end
return string.format(title_format, get_page_name_with_apostrophe_fixes(frame, article_title))
end
--- Returns the relevant maintenance categories based on the {{Infobox television crossover episode}} values validated.
---
--- @param frame table
--- @return string
function p.validate_values_crossover(frame)
local getArgs = require("Module:Arguments").getArgs
local args = getArgs(frame)
local categories = {}
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.airdate_overall))
for i = 1, 5 do
if not args["series" .. i] then
break
end
local nested_args = {
series = args["series" .. i],
episode = args["episode_no" .. i],
season = args["season" .. i],
airdate = args["airdate" .. i],
prev = args["prev" .. i],
next = args["next" .. i],
episode_list = args["episode_list" .. i],
}
table.insert(categories, are_values_links_only(nested_args))
table.insert(categories, are_values_using_overall(nested_args))
table.insert(categories, are_values_linked_or_formatted(nested_args))
table.insert(categories, are_dates_formatted_correctly(nested_args.airdate))
end
return table.concat(categories, "")
end
return p