Module:FileInfo/File Redirect: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 8: Line 8:
local utilsArg = require("Module:UtilsArg")
local utilsArg = require("Module:UtilsArg")
local utilsTable = require("Module:UtilsTable")
local utilsTable = require("Module:UtilsTable")
local CATEGORY_INVALID_ARGS = "[[Category:"..require("Module:Constants/category/invalidArgs").."]]"


function p.Main(frame)
function p.Main(frame)
local args, err = utilsArg.parse(args, p.Templates["File Redirect"])
local args, err = utilsArg.parse(frame:getParent().args, p.Templates["File Redirect"])
local categories = err and err.categoryText or ""
local categories = err and err.categoryText or ""


Line 17: Line 19:
end
end
if args.game and not args.type then
if args.game and not args.type then
local utilsError = require("Module:UtilsError")
utilsError.warn("<code>type</code> must be set when <code>game</code> is set.")
utilsError.warn("<code>type</code> must be set when <code>game</code> is set.")
categories = categories..CATEGORY_INVALID_ARGS
categories = categories..CATEGORY_INVALID_ARGS
Line 25: Line 28:
end
end
local type = args.type
local type = args.type
local typeCategory = Data.types[type] and Data.types[type].cat
local typeCategory = Data.types[type] and Data.types[type].category
if gameName and typeCategory then
if gameName and typeCategory then
categories = categories..string.format("[[Category:%s %s]]", gameName, typeCategory)
categories = categories..string.format("[[Category:%s %s]]", gameName, typeCategory)

Latest revision as of 21:56, 28 October 2022

This is the main module for the following templates:
-- This was separated from Module:File and Module:FileInfo for job queue optimization
-- See [[Guidelines:Modules#Performance Optimization]]
local p = {}

local Data = mw.loadData("Module:FileInfo/Data")

local Franchise = require("Module:Franchise")
local utilsArg = require("Module:UtilsArg")
local utilsTable = require("Module:UtilsTable")

local CATEGORY_INVALID_ARGS = "[[Category:"..require("Module:Constants/category/invalidArgs").."]]"

function p.Main(frame)
	local args, err = utilsArg.parse(frame:getParent().args, p.Templates["File Redirect"])
	local categories = err and err.categoryText or ""

	if not args.game and not args.type then
		categories = categories.."[[Category:File Redirects]]"
	end
	if args.game and not args.type then
		local utilsError = require("Module:UtilsError")
		utilsError.warn("<code>type</code> must be set when <code>game</code> is set.")
		categories = categories..CATEGORY_INVALID_ARGS
	end
	local gameName = args.game and Franchise.shortName(args.game)
	if gameName then
		categories = categories.."[[Category:"..gameName.." File Redirects]]"
	end
	local type = args.type
	local typeCategory = Data.types[type] and Data.types[type].category
	if gameName and typeCategory then
		categories = categories..string.format("[[Category:%s %s]]", gameName, typeCategory)
	end
	return categories
end

p.Templates = {
	["File Redirect"] = {
		purpose = "This template is added to [[Guidelines:Redirects#File Redirects|file redirects]] to properly categorize them.",
		params = {
			[1] = {
				name = "game",
				desc = "A [[Data:Franchise|game code]].",
				type = "string",
				enum = Franchise.enum({ includeSeries = true }),
				trim = true,
				nilIfEmpty = true,
			},
			[2] = {
				name = "type",
				desc = "The [[Guidelines:Files#Filename|file type]]. This should be set if <code>game</code> is set.",
				type = "string",
				enum = utilsTable.merge({ reference = "[[Module:FileInfo/Data]]" }, utilsTable.keys(Data.types)),
				trim = true,
				nilIfEmpty = true,
			},
		},
		examples = {
			{"MM", "Model"},
			{},
			{
				desc = "Error handling",
				args = {"not a game", "Model"},
			},
			{"MM", "not a type"},
			{"MM"},
		},
	}
}

return p