require("strict")
local libraryUtil = require("libraryUtil")
--
=---- DisambiguationPattern class.local function DisambiguationPattern(o) local DisambiguationPattern = o or local checkSelf = libraryUtil.makeCheckSelfFunction("Television infoboxes disambiguation check", "DisambiguationPattern", DisambiguationPattern, "Television infoboxes disambiguation check object")
return DisambiguationPatternend
--
=--
-- Constants.local DAB_VALID =
local CATEGORY_INCORRECT = ""
local validationTypeList =
local debugMessageList =
-- Local function which checks if both booleans are true or not.local function validateTwoParameters(isValid1, isValid2) if (isValid1 and isValid2) then return true else return false endend
-- Validate that the season number entered is a valid number --- that it does not start with a leading zero (0).local function validateSeasonNumber(seasonNumber) if (tonumber(string.sub(seasonNumber, 1, 1))
-- Validate that the year entered is a valid year.local function validateYear(year) if (string.len(year)
-- Validate that the text entered is a supported country adjective.local function validateCountryAdjective(adjective) local data = mw.loadData("Module:Country adjective")
-- Search for a country corresponding to the given text. if (data.getCountryFromAdj[adjective]) then return true else return false endend
-- Checks pages using by validating the disambiguation patterns.local function validatePatterns(disambiguation, disambiguationPatternList) local year = "" local adjective = "" local seasonNumber = "" local isYearValid local isAdjectiveValid local isSeasonNumberValid
for i, v in ipairs(disambiguationPatternList) do local currentDisambiguationPattern = disambiguationPatternList[i] if (disambiguation:match(currentDisambiguationPattern.pattern)) then -- Year and Country styles: "1999 American TV series" if (currentDisambiguationPattern.type
local isValid = validateTwoParameters(isYearValid, isAdjectiveValid) return isValid, debugMessageList["DEBUG_YEAR_COUNTRY"]:gsub("", DAB_VALID[isValid])
-- Year styles: "1999 TV series" elseif (currentDisambiguationPattern.type
-- Country styles: "American TV series" elseif (currentDisambiguationPattern.type
-- Year and Season number styles: "(1999 TV series) season 1" elseif (currentDisambiguationPattern.type
local isValid = validateTwoParameters(isYearValid, isSeasonNumberValid) return isValid, debugMessageList["DEBUG_YEAR_SEASON_NUMBER"]:gsub("", DAB_VALID[isValid])
-- Country and Season number styles: "(American TV series) season 1" elseif (currentDisambiguationPattern.type
--Season number styles: "season 1" elseif (currentDisambiguationPattern.type
-- Year, Country and Season number styles: "Gladiators (2008 British TV series) series 2" elseif (currentDisambiguationPattern.type
-- Validate that the disambiguation type is one of the supported types.local function validateDisambiguationType(disambiguation, validDisambiguationTypeList) local extendedDisambiguation local count = 0 for i, v in ipairs(validDisambiguationTypeList) do extendedDisambiguation, count = disambiguation:gsub(v, "") extendedDisambiguation = mw.text.trim(extendedDisambiguation) if (count ~= 0) then -- Disambiguation was a valid type; Exit loop. break end end count = count ~= 0 return count, extendedDisambiguation
end
-- Validate that the complete disambiguation is using a supported style.local function validateDisambiguation(invoker, disambiguation, validDisambiguationTypeList, validDisambiguationPatternList) -- Check if the list is empty. if (table.getn(validDisambiguationTypeList) ~= 0) then local isDisambiguationValid, extendedDisambiguation = validateDisambiguationType(disambiguation, validDisambiguationTypeList) -- Exit module if the disambiguation type is not a supported style. if (not isDisambiguationValid) then return false, debugMessageList["DEBUG_NOT_VALID_FORMAT"] end -- Check if there is no extended disambiguation. if (extendedDisambiguation
-- Check if the page is using disambiguation style that belongs to a different infobox.local function isPageUsingIncorrectInfobox(disambiguation, otherInfoboxList) for k, v in pairs(otherInfoboxList) do if (string.match(disambiguation, k)) then return true, v, debugMessageList["DEBUG_INCORRECT_INFOBOX"]:gsub("", k) end end return falseend
-- Validate that the title has brackets that are part of the title and not part of disambiguation.local function isOnExceptionList(title, exceptionList) for _, v in ipairs(exceptionList) do if (v
-- Get the disambiguation text and make sure that if the title has more than 1 pair of brackets, it returns the last one.local function getDisambiguation(title) local match = require("Module:String")._match return match(title, "%s%((.-)%)", 1, -1, false, "")-- return (string.match (title, "%s*%b$") or ""):gsub("[%(%)]", "")end
-- Validate that arg is not nill and not empty.local function isEmpty(arg) if (not arg or arg
-- Returns two objects:--- The first is either an empty string or a tracking category which will appear when using the live version.--- The second is a debug string which will appear when using /testcases.local function main(title, invoker, validDisambiguationTypeList, validDisambiguationPatternList, exceptionList, otherInfoboxList, invalidTitleStyleList) -- Exit module if the parameter has no value. if (isEmpty(title)) then return "", debugMessageList["DEBUG_EMPTY_TITLE"] end
-- Exit module if the title has brackets that are part of the title (not disambiguation). if (isOnExceptionList(title, exceptionList)) then return "", debugMessageList["DEBUG_TITLE_ON_EXCEPTION"] end if (invoker
-- Exit module if the title has no disambiguation. if (isEmpty(disambiguation)) then return "", debugMessageList["DEBUG_NO_DAB"] end
-- Exit module if the disambiguation belongs to a different infobox. local isValid, category, debugString = isPageUsingIncorrectInfobox(disambiguation, otherInfoboxList) if (isValid) then return category, debugString end -- Check if the disambiguation is valid. isValid, debugString = validateDisambiguation(invoker, disambiguation, validDisambiguationTypeList, validDisambiguationPatternList) -- Check if the disambiguation is not valid and add category. if (not isValid) then category = CATEGORY_INCORRECT end
return category, debugStringend
return