Module:Fiction-based redirects to list entries category handler/RedirectType

From blackwiki
Jump to navigation Jump to search

Module:Fiction-based redirects to list entries category handler/RedirectType handles the RedirectType class which is used in Module:Fiction-based redirects to list entries category handler.

Usage

local redirectType = require('Module:Fiction-based redirects to list entries category handler/RedirectType')
local typeList = redirectType.getRedirectTypes()
local defaultType = redirectType.getDefaultType(typeList)

Use local categoryType = typeList[i] (i = a number between 1 and 4) to retrieve the wanted categoryType. Once the categoryType has been retrieved, you can access the following functions:

categoryType:getMainCategory(mainCategory)
categoryType:getTopLevelCategory(media)
categoryType:getCategory(category)
categoryType:setTemplateAsArrowverse(articleTitle)

local libraryUtil = require('libraryUtil')

local p = {}

local franchiseList = {
	[1] = "Module:Arrowverse redirect category handler"
}

--[[
RedirectType class.
--]]
local function RedirectType(o)
	local RedirectType = o or {redirectType = "", defaultCategory = "", topLevelCategory = "", template = "", from = "", suffix = "", isFranchise = false}
	local checkSelf = libraryUtil.makeCheckSelfFunction('Fiction-based redirects to list entries category handler', 'RedirectType', RedirectType, 'Fiction-based redirects to list entries category handler object')

--[[
Public function which is used to retrieve the main category for a specific type.
--]]
	function RedirectType:getMainCategory(mainCategory)
		return RedirectType:getCategory(mainCategory)
	end
	
--[[
Public function which is used to retrieve the top level category (or categories) for a specific type and media format.
If no media format is passed, the function returns the default top level category.
--]]
	function RedirectType:getTopLevelCategory(mediaList)
		if (mediaList and #mediaList > 0) then
			local categories = ""
			for i = 1, #mediaList do
				local category
				if ((RedirectType.redirectType == "episode") and (string.lower(mediaList[i]) == "television")) then
					category = RedirectType:getCategory(RedirectType.defaultCategory)
				else
					category = RedirectType:getCategory(RedirectType.topLevelCategory)
				end
				categories = categories .. category:gsub("{media}", mediaList[i])
			end
			return categories
		else
			return RedirectType.defaultCategory
		end
	end	

--[[
Public function which is used to retrieve a category for a specific type.
--]]
	function RedirectType:getCategory(category)
		return category:gsub("{type}", RedirectType.redirectType)
	end

--[[
Public function which is used to check if the category belongs to a special franchise, such as the Arrowverse.
If the category is part of that franchise, returns the series name, else returns nil.
--]]
	function RedirectType:isCategorySpecialFranchise(series)
		local seriesParameter
		if (RedirectType.redirectType == "character" or RedirectType.redirectType == "element" or RedirectType.redirectType == "location") then
			
			for i, v in pairs(franchiseList) do
				local seriesModule = require(v)
				local seriesList = seriesModule.getSeriesList()
				for i, v in pairs(seriesList) do
					local currentTVSeries = seriesList[i]
					if (series == currentTVSeries.seriesName) then
						seriesParameter = currentTVSeries.argName
						break
					end
				end
			
				local franchise = seriesModule.getFranchiseName()
				if (series == franchise) then
					seriesParameter = franchise
					RedirectType.isFranchise = true
				end
				
				if (seriesParameter) then
					if (i == 1) then
						RedirectType:setTemplateAsArrowverse()
					end
					break
				end
			end
		end
		
		if (seriesParameter) then
			return seriesParameter
		else
			return nil
		end
	end

--[[
Public function which is used to change the type of template to the Arrowverse specific version.
--]]	
	function RedirectType:setTemplateAsArrowverse()
		RedirectType.template = "Arrowverse " .. RedirectType.redirectType .. " redirect"
	end

	return RedirectType
end

--[[
Public function which is used to retrieve the list of redirect types.
--]]
function p.getRedirectTypes()
	return {
		RedirectType{
			redirectType = "character",
			defaultCategory = "[[Category:Fictional character redirects]]",
			topLevelCategory = "[[Category:{media} character redirects to lists]]",
			template = "Fictional character redirect",
			from = "fictional characters",
			suffix = "redirects to lists"
		},
		RedirectType{
			redirectType = "element",
			defaultCategory = "[[Category:Fictional element redirects]]",
			topLevelCategory = "[[Category:{media} element redirects to lists]]",
			template = "Fictional element redirect",
			from = "fictional elements",
			suffix = "redirects to lists"
		},
		RedirectType{
			redirectType = "episode",
			defaultCategory = "[[Category:Episode redirects to lists]]",
			topLevelCategory = "[[Category:{media} episode redirects to lists]]",
			template = "Television episode redirect handler",
			from = "episodes",
			suffix = "redirects to lists"
		},
		RedirectType{
			redirectType = "location",
			defaultCategory = "[[Category:Fictional location redirects]]",
			topLevelCategory = "[[Category:{media} location redirects]]",
			template = "Fictional location redirect",
			from = "fictional locations or settings",
			suffix = "redirects"
		}
	}

end

--[[
Public function which is used to retrieve a default redirect type.
--]]
function p.getDefaultType(typeList)
	return typeList[3]
end

return p