https://en.seminaverbi.bibleget.io/w/index.php?title=Module:Television_infoboxes_disambiguation_check&feed=atom&action=historyModule:Television infoboxes disambiguation check - Revision history2024-03-29T10:35:02ZRevision history for this page on the wikiMediaWiki 1.41.0https://en.seminaverbi.bibleget.io/w/index.php?title=Module:Television_infoboxes_disambiguation_check&diff=22093&oldid=prevJohnrdorazio: 1 revision imported2020-09-02T19:59:05Z<p>1 revision imported</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<tr class="diff-title" lang="en">
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 19:59, September 2, 2020</td>
</tr><tr><td colspan="2" class="diff-notice" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>Johnrdoraziohttps://en.seminaverbi.bibleget.io/w/index.php?title=Module:Television_infoboxes_disambiguation_check&diff=22092&oldid=preven>Gonnym: added support for "year, country TV series, season #" styles2019-11-08T13:40:05Z<p>added support for "year, country TV series, season #" styles</p>
<p><b>New page</b></p><div>local libraryUtil = require('libraryUtil')<br />
<br />
--=============================================================--<br />
-- DisambiguationPattern class.<br />
local function DisambiguationPattern(o)<br />
local DisambiguationPattern = o or {pattern = "", type = ""}<br />
local checkSelf = libraryUtil.makeCheckSelfFunction( 'Television infoboxes disambiguation check', 'DisambiguationPattern', DisambiguationPattern, 'Television infoboxes disambiguation check object' )<br />
<br />
return DisambiguationPattern<br />
end<br />
<br />
--=============================================================--<br />
<br />
-- Constants.<br />
local DAB_VALID = {<br />
[true] = "valid",<br />
[false] = "invalid"<br />
}<br />
<br />
local CATEGORY_INCORRECT = "[[Category:Television articles with incorrect naming style]]"<br />
<br />
local validationTypeList = {<br />
["VALIDATION_TYPE_YEAR_COUNTRY"] = 1,<br />
["VALIDATION_TYPE_YEAR"] = 2,<br />
["VALIDATION_TYPE_COUNTRY"] = 3,<br />
["VALIDATION_TYPE_YEAR_SEASON_NUMBER"] = 4,<br />
["VALIDATION_TYPE_COUNTRY_SEASON_NUMBER"] = 5,<br />
["VALIDATION_TYPE_SEASON_NUMBER"] = 6,<br />
["VALIDATION_TYPE_COUNTRY_SEASON"] = 7,<br />
["VALIDATION_TYPE_YEAR_COUNTRY_SEASON_NUMBER"] = 8<br />
}<br />
<br />
local debugMessageList = {<br />
["DEBUG_EMPTY_TITLE"] = "Debug: Error: Empty title.",<br />
["DEBUG_NO_DAB"] = "Debug: No disambiguation.",<br />
["DEBUG_TITLE_ON_EXCEPTION"] = "Debug: Title on exception list.",<br />
["DEBUG_VALID_FORMAT"] = "Debug: Using a valid format.",<br />
["DEBUG_NOT_VALID_FORMAT"] = "Debug: Not a valid format.",<br />
["DEBUG_YEAR_COUNTRY"] = "Debug: Using a valid format with an extended Year and Country - {}.",<br />
["DEBUG_YEAR"] = "Debug: Using a valid format with an extended Year - {}.",<br />
["DEBUG_COUNTRY"] = "Debug: Using a valid format with an extended Country - {}.",<br />
["DEBUG_INCORRECT_STYLE"] = "Debug: Using a valid format but using an incorrect extended style.",<br />
["DEBUG_INCORRECT_INFOBOX"] = "Debug: Using incorrect infobox - {}.",<br />
["DEBUG_YEAR_SEASON_NUMBER"] = "Debug: Using a valid format with an extended Year and Season number - {}.", <br />
["DEBUG_COUNTRY_SEASON_NUMBER"] = "Debug: Using a valid format with an extended Country and Season number - {}.", <br />
["DEBUG_SEASON_NUMBER"] = "Debug: Using a valid format with a Season number - {}.",<br />
["DEBUG_COUNTRY_SEASON"] = "Debug: Using a valid format with a Country and the word Season - {}.",<br />
["DEBUG_YEAR_COUNTRY_SEASON_NUMBER"] = "Debug: Using a valid format with an extended Year, Country and Season number - {}."<br />
}<br />
<br />
-- Local function which checks if both booleans are true or not.<br />
local function validateTwoParameters(isValid1, isValid2)<br />
if (isValid1 and isValid2) then<br />
return true<br />
else<br />
return false<br />
end<br />
end<br />
<br />
-- Validate that the season number entered is a valid number -<br />
-- that it does not start with a leading zero (0).<br />
local function validateSeasonNumber(seasonNumber)<br />
if (tonumber(string.sub(seasonNumber, 1, 1)) == 0) then<br />
return false<br />
else<br />
return true<br />
end<br />
end<br />
<br />
-- Validate that the year entered is a valid year.<br />
local function validateYear(year)<br />
if (string.len(year) == 4) then<br />
return true<br />
else<br />
return false<br />
end<br />
end<br />
<br />
-- Validate that the text entered is a supported country adjective.<br />
local function validateCountryAdjective(adjective)<br />
local data = mw.loadData('Module:Country adjective')<br />
<br />
-- Search for a country corresponding to the given text.<br />
if (data.getCountryFromAdj[adjective]) then<br />
return true<br />
else<br />
return false<br />
end<br />
end<br />
<br />
-- Checks pages using by validating the disambiguation patterns.<br />
local function validatePatterns(disambiguation, disambiguationPatternList)<br />
local year = ""<br />
local adjective = ""<br />
local seasonNumber = ""<br />
local isYearValid<br />
local isAdjectiveValid<br />
local isSeasonNumberValid<br />
<br />
for i, v in ipairs(disambiguationPatternList) do<br />
local currentDisambiguationPattern = disambiguationPatternList[i]<br />
if (disambiguation:match(currentDisambiguationPattern.pattern)) then<br />
<br />
-- Year and Country styles: "1999 American TV series"<br />
if (currentDisambiguationPattern.type == validationTypeList["VALIDATION_TYPE_YEAR_COUNTRY"]) then<br />
year, adjective = disambiguation:match(currentDisambiguationPattern.pattern)<br />
isYearValid = validateYear(year)<br />
isAdjectiveValid = validateCountryAdjective(adjective)<br />
<br />
local isValid = validateTwoParameters(isYearValid, isAdjectiveValid)<br />
return isValid, debugMessageList["DEBUG_YEAR_COUNTRY"]:gsub("{}", DAB_VALID[isValid])<br />
<br />
-- Year styles: "1999 TV series"<br />
elseif (currentDisambiguationPattern.type == validationTypeList["VALIDATION_TYPE_YEAR"]) then<br />
year = disambiguation<br />
isYearValid = validateYear(year)<br />
return isYearValid, debugMessageList["DEBUG_YEAR"]:gsub("{}", DAB_VALID[isYearValid])<br />
<br />
-- Country styles: "American TV series"<br />
elseif (currentDisambiguationPattern.type == validationTypeList["VALIDATION_TYPE_COUNTRY"]) then<br />
adjective = disambiguation<br />
isAdjectiveValid = validateCountryAdjective(adjective)<br />
return isAdjectiveValid, debugMessageList["DEBUG_COUNTRY"]:gsub("{}", DAB_VALID[isAdjectiveValid])<br />
<br />
-- Year and Season number styles: "1999 TV series, season 1"<br />
elseif (currentDisambiguationPattern.type == validationTypeList["VALIDATION_TYPE_YEAR_SEASON_NUMBER"]) then<br />
year, seasonNumber = disambiguation:match(currentDisambiguationPattern.pattern)<br />
isYearValid = validateYear(year)<br />
isSeasonNumberValid = validateSeasonNumber(seasonNumber)<br />
<br />
local isValid = validateTwoParameters(isYearValid, isSeasonNumberValid)<br />
return isValid, debugMessageList["DEBUG_YEAR_SEASON_NUMBER"]:gsub("{}", DAB_VALID[isValid])<br />
<br />
-- Country and Season number styles: "American season 1" and "American TV series, season 1"<br />
elseif (currentDisambiguationPattern.type == validationTypeList["VALIDATION_TYPE_COUNTRY_SEASON_NUMBER"]) then<br />
adjective, seasonNumber = disambiguation:match(currentDisambiguationPattern.pattern)<br />
isAdjectiveValid = validateCountryAdjective(mw.text.trim(adjective))<br />
isSeasonNumberValid = validateSeasonNumber(seasonNumber)<br />
<br />
local isValid = validateTwoParameters(isAdjectiveValid, isSeasonNumberValid)<br />
return isValid, debugMessageList["DEBUG_COUNTRY_SEASON_NUMBER"]:gsub("{}", DAB_VALID[isValid])<br />
<br />
-- Country and the word season: "American season"<br />
elseif (currentDisambiguationPattern.type == validationTypeList["VALIDATION_TYPE_COUNTRY_SEASON"]) then<br />
adjective = disambiguation:match(currentDisambiguationPattern.pattern)<br />
isAdjectiveValid = validateCountryAdjective(mw.text.trim(adjective))<br />
return isAdjectiveValid, debugMessageList["DEBUG_COUNTRY_SEASON"]:gsub("{}", DAB_VALID[isAdjectiveValid])<br />
<br />
--Season number styles: "season 1"<br />
elseif (currentDisambiguationPattern.type == validationTypeList["VALIDATION_TYPE_SEASON_NUMBER"]) then<br />
seasonNumber = disambiguation:match(currentDisambiguationPattern.pattern)<br />
isSeasonNumberValid = validateSeasonNumber(seasonNumber)<br />
return isSeasonNumberValid, debugMessageList["DEBUG_SEASON_NUMBER"]:gsub("{}", DAB_VALID[isSeasonNumberValid])<br />
<br />
-- Year, Country and Season number styles: "Gladiators (2008 British TV series, series 2)"<br />
elseif (currentDisambiguationPattern.type == validationTypeList["VALIDATION_TYPE_YEAR_COUNTRY_SEASON_NUMBER"]) then<br />
year, adjective, seasonNumber = disambiguation:match(currentDisambiguationPattern.pattern)<br />
isYearValid = validateYear(year)<br />
isAdjectiveValid = validateCountryAdjective(mw.text.trim(adjective))<br />
isSeasonNumberValid = validateSeasonNumber(seasonNumber)<br />
<br />
local isValid = validateTwoParameters(isYearValid, isAdjectiveValid)<br />
isValid = validateTwoParameters(isValid, isSeasonNumberValid)<br />
return isValid, debugMessageList["DEBUG_YEAR_COUNTRY_SEASON_NUMBER"]:gsub("{}", DAB_VALID[isValid])<br />
<br />
-- Not a valid supported style.<br />
else<br />
-- Do nothing.<br />
end<br />
else<br />
-- Do nothing.<br />
end<br />
end<br />
return false, debugMessageList["DEBUG_INCORRECT_STYLE"]<br />
end<br />
<br />
-- Validate that the disambiguation type is one of the supported types.<br />
local function validateDisambiguationType(disambiguation, validDisambiguationTypeList)<br />
local extendedDisambiguation<br />
local count = 0<br />
<br />
for i, v in ipairs(validDisambiguationTypeList) do<br />
extendedDisambiguation, count = disambiguation:gsub(v, '')<br />
extendedDisambiguation = mw.text.trim(extendedDisambiguation)<br />
if (count ~= 0) then<br />
-- Disambiguation was a valid type; Exit loop.<br />
break<br />
end<br />
end<br />
<br />
count = count ~= 0 <br />
return count, extendedDisambiguation<br />
<br />
end<br />
<br />
-- Validate that the complete disambiguation is using a supported style.<br />
local function validateDisambiguation(invoker, disambiguation, validDisambiguationTypeList, validDisambiguationPatternList)<br />
-- Check if the list is empty.<br />
if (table.getn(validDisambiguationTypeList) ~= 0) then<br />
local isDisambiguationValid, extendedDisambiguation = validateDisambiguationType(disambiguation, validDisambiguationTypeList)<br />
<br />
-- Exit module if the disambiguation type is not a supported style.<br />
if (not isDisambiguationValid) then<br />
return false, debugMessageList["DEBUG_NOT_VALID_FORMAT"]<br />
end<br />
<br />
-- Check if there is no extended disambiguation.<br />
if (extendedDisambiguation == '') then<br />
return true, debugMessageList["DEBUG_VALID_FORMAT"]<br />
end<br />
<br />
-- A bit of hack so I won't need to refactor a ton of code.<br />
if (invoker ~= "infobox television season") then<br />
disambiguation = extendedDisambiguation<br />
end<br />
end<br />
<br />
return validatePatterns(disambiguation, validDisambiguationPatternList)<br />
end<br />
<br />
-- Check if the page is using disambiguation style that belongs to a different infobox.<br />
local function isPageUsingIncorrectInfobox(disambiguation, otherInfoboxList)<br />
for k, v in pairs(otherInfoboxList) do<br />
if (string.match(disambiguation, k)) then<br />
return true, v, debugMessageList["DEBUG_INCORRECT_INFOBOX"]:gsub("{}", k)<br />
end<br />
end<br />
return false<br />
end<br />
<br />
-- Validate that the title has brackets that are part of the title and not part of disambiguation.<br />
local function isOnExceptionList(title, exceptionList)<br />
for _, v in ipairs(exceptionList) do<br />
if (v == title) then<br />
return true<br />
elseif (string.match(title, v)) then<br />
return true<br />
end<br />
end<br />
return false<br />
end<br />
<br />
-- Get the disambiguation text and make sure that if the title has more than 1 pair of brackets, it returns the last one.<br />
local function getDisambiguation(title)<br />
local match = require("Module:String")._match<br />
return match(title, "%s%((.-)%)", 1, -1, false, "")<br />
-- return (string.match (title, '%s*%b()$') or ''):gsub('[%(%)]', '')<br />
end<br />
<br />
-- Validate that arg is not nill and not empty.<br />
local function isEmpty(arg)<br />
if (not arg or arg == "") then<br />
return true<br />
else<br />
return false<br />
end<br />
end<br />
<br />
-- Returns two objects:<br />
--- The first is either an empty string or a tracking category which will appear when using the live version.<br />
--- The second is a debug string which will appear when using /testcases.<br />
local function main(title, invoker, validDisambiguationTypeList, validDisambiguationPatternList, exceptionList, otherInfoboxList, invalidTitleStyleList)<br />
-- Exit module if the parameter has no value.<br />
if (isEmpty(title)) then<br />
return "", debugMessageList["DEBUG_EMPTY_TITLE"]<br />
end<br />
<br />
-- Exit module if the title has brackets that are part of the title (not disambiguation).<br />
if (isOnExceptionList(title, exceptionList)) then<br />
return "", debugMessageList["DEBUG_TITLE_ON_EXCEPTION"]<br />
end<br />
<br />
if (invoker == "infobox television season") then<br />
if (#invalidTitleStyleList ~= 0) then<br />
for i = 1, #invalidTitleStyleList do<br />
if (string.find(title, invalidTitleStyleList[i])) then<br />
return CATEGORY_INCORRECT, debugMessageList["DEBUG_NOT_VALID_FORMAT"]<br />
end<br />
end<br />
end<br />
end<br />
<br />
<br />
-- Get the disambiguation.<br />
local disambiguation = getDisambiguation(title)<br />
<br />
-- Exit module if the title has no disambiguation.<br />
if (isEmpty(disambiguation)) then<br />
return "", debugMessageList["DEBUG_NO_DAB"]<br />
end<br />
<br />
-- Exit module if the disambiguation belongs to a different infobox.<br />
local isValid, category, debugString = isPageUsingIncorrectInfobox(disambiguation, otherInfoboxList)<br />
if (isValid) then<br />
return category, debugString<br />
end<br />
<br />
-- Check if the disambiguation is valid.<br />
isValid, debugString = validateDisambiguation(invoker, disambiguation, validDisambiguationTypeList, validDisambiguationPatternList)<br />
<br />
-- Check if the disambiguation is not valid and add category.<br />
if (not isValid) then<br />
category = CATEGORY_INCORRECT<br />
end<br />
<br />
return category, debugString<br />
end<br />
<br />
return {<br />
main = main,<br />
DisambiguationPattern = DisambiguationPattern<br />
}</div>en>Gonnym