Jump to content

Module:Infobox television disambiguation check/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gonnym (talk | contribs) at 18:45, 21 May 2019. 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 = {}

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 franchise = "franchise"
local season = "season"
local radio = "radio"

local categoryList = {
	["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]]"
}

local category = ""

-- Check if the page is using disambiguation style that belongs to a different infobox.
local function isPageUsingIncorrectInfobox(disambiguation, otherDisambiguationStyle)
	if (string.match(disambiguation, otherDisambiguationStyle)) then
		category = categoryList[otherDisambiguationStyle]
		return true
	else
		return false
	end
end

-- Validate that the year entered is a valid year.
local function validateYear(year)
	if (string.len(year) == 4) then
		return true, "valid"
	else
		return false, "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, "valid"
	else
		return false, "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, "Debug: Not a 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, "Debug: Using a 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, debugString = validateYear(year)
		-- call to validate country adjective
		isAdjectiveValid, debugString = validateCountryAdjective(adjective)

		return (isYearValid and isAdjectiveValid), "Debug: Using a valid format with an extended Year and Country - " .. 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, "Debug: Using a valid format with an extended Year - " .. 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, "Debug: Using a valid format with an extended Country - " .. debugString .. "."

	-- The disambiguation is not a supported style of one of the above.
	else
		return false, "Debug: Using a valid format but using an incorrect extended style."
	end
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 "", "Debug: Error: Empty title."
	end
	
	-- Get the disambiguation.
	local disambiguation = getDisambiguation(title)

	-- Exit module if the title has no disambiguation.
	if (isEmpty(disambiguation)) then
		return "", "Debug: No disambiguation."
	end

	-- Exit module if the title has brackets that are part of the title (not disambiguation).	
	if (isOnExceptionList(title)) then
		return "", "Debug: Title on exception list."
	end
	
	-- Check if the disambiguation is valid.
	local isValid, debugString = validateDisambiguation(disambiguation)
	
	-- Check if the disambiguation is using "franchise".
	isValid = isPageUsingIncorrectInfobox(disambiguation, franchise)

	-- Check if the disambiguation is using "season".
	isValid = isPageUsingIncorrectInfobox(disambiguation, season)

	-- Check if the disambiguation is using "radio".
	isValid = isPageUsingIncorrectInfobox(disambiguation, radio)
	
	-- Check if the disambiguation is not valid and add category.
	if (not isValid) then
		category = categoryList["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