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 17:37, 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 franchise = "franchise"
local season = "season"
local radio = "radio"

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 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 = ""

-- Validate that the year entered is a valid year.
local function validateYear(year)
	return true -- not implemented yet.
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
	else
		return false
	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

	-- 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)

		return (isYearValid and isAdjectiveValid), "Debug: Using a valid format with an extended Year and Country - " .. (isYearValid and isAdjectiveValid) .. "."
		
	-- Check if extended disambiguation has year.
	elseif (extendedDisambiguation:match('^%d+$')) then
		year = extendedDisambiguation:match('^%d+')
		-- call to validate year
		isYearValid = validateYear(year)
		return isYearValid, "Debug: Using a valid format with an extended Year - " .. isYearValid .. "."
		
	-- Check if extended disambiguation has country adjective.
	elseif (extendedDisambiguation:match('^%D+$')) then
		adjective = extendedDisambiguation
		-- call to validate country adjective
		isAdjectiveValid = validateCountryAdjective(adjective)
		return isAdjectiveValid, "Debug: Using a valid format with an extended Country - " .. isAdjectiveValid .. "."

	-- 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

local function _main(args)
	local title = args[1]
	
	-- Exit module if the parameter has no value.
	if (isEmpty(title)) then
		return ""
	end
	
	-- Get the disambiguation.
	local disambiguation = getDisambiguation(title)

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

	-- Exit module if the title has brackets that are part of the title (not disambiguation).	
	if (isOnExceptionList(title)) then
		return ""
	end
	
	-- Check if the disambiguation is valid.
	--local isValid = validateDisambiguation(disambiguation)
	local isValid, debugString = validateDisambiguation(disambiguation)
	
	-- Check if the disambiguation is using "franchise".
	if (string.match(disambiguation, franchise)) then
		isValid = true
		category = categoryList["franchise"]
	end

	-- Check if the disambiguation is using "season".
	if (string.match(disambiguation, season)) then
		isValid = true
		category = categoryList["season"]
	end

	-- Check if the disambiguation is using "radio".
	if (string.match(disambiguation, radio)) then
		isValid = true
		category = categoryList["radio"]
	end
	
	-- 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)
	return _main(args)
end

return p