Jump to content

Module:Infobox television disambiguation check

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gonnym (talk | contribs) at 10:35, 22 May 2019 (Updated code. Now checks if the country text is using a valid country adjective and if the year is correctly set to 4 digits.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module requires the use of Module:Arguments.
local getArgs = require('Module:Arguments').getArgs

local p = {}

-- Constants.
local DAB_VALID = "valid"
local DAB_INVALID = "invalid"

local debugMessageList = {
	["DEBUG_EMPTY_TITLE"] = "Debug: Error: Empty title.",
	["DEBUG_NO_DAB"] = "Debug: No disambiguation.",
	["DEBUG_TITLE_ON_EXCEPTION"] = "Debug: Title on exception list.",
	["DEBUG_VALID_FORMAT"] = "Debug: Using a valid format.",
	["DEBUG_NOT_VALID_FORMAT"] = "Debug: Not a valid format.",
	["DEBUG_YEAR_COUNTRY"] = "Debug: Using a valid format with an extended Year and Country - {}.",
	["DEBUG_YEAR"] = "Debug: Using a valid format with an extended Year - {}.",
	["DEBUG_COUNTRY"] = "Debug: Using a valid format with an extended Country - {}.",
	["DEBUG_INCORRECT_STYLE"] = "Debug: Using a valid format but using an incorrect extended style.",
	["DEBUG_INCORRECT_INFOBOX"] = "Debug: Using incorrect infobox - {}."
}

local validDisambiguationTypeList = {
	"TV series",
	"TV program",
	"TV programme",
	"TV film",
	"film",
	"miniseries",
	"serial",
	"game show",
	"talk show",
	"web series"
}

local exceptionList = {
	"The (206)",
	"Cinderella (Apakah Cinta Hanyalah Mimpi?)",
	"How to Live with Your Parents (For the Rest of Your Life)",
	"I (Almost) Got Away With It",
	"Monty Python: Almost the Truth (Lawyers Cut)",
	"Randall and Hopkirk (Deceased)",
}

local otherInfoboxList = {
	"franchise",
	"season",
	"radio"
}

local categoryList = {
	["CATEGORY_INCORRECT"] = "[[Category:Television articles with incorrect naming style]]",
	["franchise"] = "[[Category:Television articles using incorrect infobox|F]]",
	["season"] = "[[Category:Television articles using incorrect infobox|S]]",
	["radio"] = "[[Category:Television articles using incorrect infobox|R]]"
}

-- Validate that the year entered is a valid year.
local function validateYear(year)
	if (string.len(year) == 4) then
		return true, DAB_VALID
	else
		return false, DAB_INVALID
	end
end

-- Validate that the text entered is a supported country adjective.
local function validateCountryAdjective(dabString)
	local data = mw.loadData('Module:Country adjective')

	-- Search for a country corresponding to the given text.
	if (data.getCountryFromAdj[dabString]) then
		return true, DAB_VALID
	else
		return false, DAB_INVALID
	end
end

-- Validate that the disambiguation type is one of the supported types.
local function validateDisambiguationType(disambiguation)
	local extendedDisambiguation
	local count = 0
	
	for i, v in ipairs(validDisambiguationTypeList) do
		extendedDisambiguation, count = disambiguation:gsub(v .. '$', '')
		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(disambiguation)
	local isDisambiguationValid, extendedDisambiguation = validateDisambiguationType(disambiguation)
	
	-- Exit module if the disambiguation type is not a supported style.
	if (not isDisambiguationValid) then
		return false, debugMessageList["DEBUG_NOT_VALID_FORMAT"]
	end
 
  	-- Remove trailing white space if there is any.
	extendedDisambiguation = mw.text.trim(extendedDisambiguation)
	
 	-- Check if there is no extended disambiguation.
	if (extendedDisambiguation == '') then
		return true, debugMessageList["DEBUG_VALID_FORMAT"]
	end

	local year = ""
	local adjective = ""
	local isYearValid
	local isAdjectiveValid
	local debugString = ""

	-- Check if extended disambiguation has both year and country adjective.
	if (extendedDisambiguation:match('^%d+ %D+')) then
		year, adjective = extendedDisambiguation:match('^(%d+) (%D+)')
		-- call to validate year
		isYearValid = validateYear(year)
		-- call to validate country adjective
		isAdjectiveValid = validateCountryAdjective(adjective)
		
		local isValid
		if (isYearValid and isAdjectiveValid) then
			isValid = true
			debugString = DAB_VALID
		else
			isValid = false
			debugString = DAB_INVALID
		end

		return isValid, debugMessageList["DEBUG_YEAR_COUNTRY"]:gsub("{}", debugString)
		
	-- Check if extended disambiguation has year.
	elseif (extendedDisambiguation:match('^%d+$')) then
		year = extendedDisambiguation:match('^%d+')
		-- call to validate year
		isYearValid, debugString = validateYear(year)
		return isYearValid, debugMessageList["DEBUG_YEAR"]:gsub("{}", debugString)
		
	-- Check if extended disambiguation has country adjective.
	elseif (extendedDisambiguation:match('^%D+$')) then
		adjective = extendedDisambiguation
		-- call to validate country adjective
		isAdjectiveValid, debugString = validateCountryAdjective(adjective)
		return isAdjectiveValid, debugMessageList["DEBUG_COUNTRY"]:gsub("{}", debugString)

	-- The disambiguation is not a supported style of one of the above.
	else
		return false, debugMessageList["DEBUG_INCORRECT_STYLE"]
	end
end

-- Check if the page is using disambiguation style that belongs to a different infobox.
local function isPageUsingIncorrectInfobox(disambiguation)
	for i, v in ipairs(otherInfoboxList) do
		if (string.match(disambiguation, v)) then
			category = categoryList[v]
			return true, category, debugMessageList["DEBUG_INCORRECT_INFOBOX"]:gsub("{}", v)
		end
	end
	return false
end

-- Validate that the title has brackets that are part of the title and not part of disambiguation.
local function isOnExceptionList(title)
	for _, v in ipairs(exceptionList) do
		if (v == title) then
			return true
		end
	end
	return false
end

-- 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, "")
end

-- Validate that arg is not nill and not empty.
local function isEmpty(arg)
	if (not arg or arg == "") then
		return true
	else
		return false
	end
end

-- 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 the sandbox version.
local function _main(args)
	local title = args[1]

	-- Exit module if the parameter has no value.
	if (isEmpty(title)) then
		return "", debugMessageList["DEBUG_EMPTY_TITLE"]
	end
	
	-- Get the disambiguation.
	local disambiguation = getDisambiguation(title)

	-- Exit module if the title has no disambiguation.
	if (isEmpty(disambiguation)) then
		return "", debugMessageList["DEBUG_NO_DAB"]
	end

	-- Exit module if the title has brackets that are part of the title (not disambiguation).	
	if (isOnExceptionList(title)) then
		return "", debugMessageList["DEBUG_TITLE_ON_EXCEPTION"]
	end

	-- Exit module if the disambiguation belongs to a different infobox.
	local isValid, category, debugString = isPageUsingIncorrectInfobox(disambiguation)
	if (isValid) then
		return category, debugString
	end
	
	-- Check if the disambiguation is valid.
	isValid, debugString = validateDisambiguation(disambiguation)
	
	-- Check if the disambiguation is not valid and add category.
	if (not isValid) then
		category = categoryList["CATEGORY_INCORRECT"]
	end

	return category, debugString
end

function p.main(frame)
	local args = getArgs(frame)
	local test = args['test']
	
	local category, debugString = _main(args)
	
	if (test) then
		return debugString
	else
		return category
	end
end

return p