Zelda Wiki

Want to contribute to this wiki?
Sign up for an account, and get started!

Come join the Zelda Wiki community Discord server!

READ MORE

Zelda Wiki
mNo edit summary
No edit summary
Line 12: Line 12:
   
 
local CARGO_TABLE = "Terminologies"
 
local CARGO_TABLE = "Terminologies"
 
-- Deprecated
 
function p.Main(args)
 
args.page = args.term
 
args = utilsTable.mapValues(args, utilsString.trim)
 
args = utilsTable.mapValues(args, utilsString.nilIfEmpty)
 
return p.printTerm(args)
 
end
 
   
 
local function getArgs(frame)
 
local function getArgs(frame)
Line 98: Line 90:
 
if not page then
 
if not page then
 
return nil
 
return nil
end
 
if type(page) == "table" then --legacy
 
game = page.game
 
plural = page.plural
 
page = page.term
 
 
end
 
end
 
 

Revision as of 17:40, 3 May 2020

Lua error in package.lua at line 80: module 'Module:UtilsGame' not found.


local p = {}
local h = {}

local utilsArg = require("Module:UtilsArg")
local utilsCargo = require("Module:UtilsCargo")
local utilsError = require("Module:UtilsError")
local utilsGame = require("Module:UtilsGame")
local utilsMarkup = require("Module:UtilsMarkup")
local utilsPage = require("Module:UtilsPage")
local utilsString = require('Module:UtilsString')
local utilsTable = require('Module:UtilsTable')

local CARGO_TABLE = "Terminologies"

local function getArgs(frame)
	local args = frame:getParent().args
	args = utilsTable.mapValues(args, utilsString.trim)
	args = utilsTable.mapValues(args, utilsString.nilIfEmpty)
	return {
		game = args[1],
		page = args[2],
		link = args[3],
		plural = args[4],
		display = args.display,
		section = args.section,
	}
end

function p.Singular(frame)
	local args = getArgs(frame)
	return p.printTerm(args, false, not utilsPage.inNamespace({"User", "MediaWiki"})) -- MediaWiki namespace is listed here to prevent a weird bug with MediaWiki:Gadget-EditToolbarButtons.js
end

function p.Plural(frame)
	local args = getArgs(frame)
	return p.printTerm(args, true, not utilsPage.inNamespace({"User", "MediaWiki"}))
end

function p._fetchTerm(frame)
	local args = frame.args
	args = utilsTable.mapValues(args, utilsString.trim)
	args = utilsTable.mapValues(args, utilsString.nilIfEmpty)
	return p.fetchTerm(args.term, args.game)
end

function p.printTerm(args, plural, printErrorCategories)
	local validationErrors = utilsArg.validate(args, {
		page = {
			nonEmpty = true,
		},
		game = {
			enum = utilsGame.getEnum()
		},
		link = {},
		plural = {
			deprecated = true
		},
		display = {},
		section = {},
	})

	local term, fetchErrors = p.fetchTerm(args.page, args.game, plural or args.plural)
	
	local errorCategories = ""
	if printErrorCategories then
		local errors = utilsTable.concat(validationErrors or {}, fetchErrors or {})
		errorCategories = utilsMarkup.categories(errors)
	end
	local result = ""
	if not term then
		local errLink = utilsMarkup.sectionLink(args.page, args.section, args.display)
		result = utilsMarkup.inline(errLink, {
			class = "facelift-term-invalid",
			tooltip = "Invalid or missing term",
		})
	elseif args.link == "link" then
		local gameSub = utilsGame.AbbToBaseGame(args.game, true)
		if not args.section and gameSub ~= "Unknown" and args.game ~= "Series" then
			args.section = gameSub
		end
		result = utilsMarkup.sectionLink(args.page, args.section, args.display or term)
	else
		result = utilsMarkup.class("term", args.display or term)
	end
	return result .. errorCategories
end

function p.fetchTerm(page, game, plural)
	utilsArg.required(page, "page")
	if not page then
		return nil
	end
	
	-- Things like {{PAGENAME}} return HTML entities. These have to be removed as the "#" character cannot be used in Cargo queries.
	page = mw.text.decode(page) 
	local rows = utilsCargo.query("Terminologies=terms, Terminologies__games=termGames", "termGames._value=game, terms.term=term, terms.plural=plural", {
		join = "terms._ID=termGames._rowID",
		where = utilsCargo.allOf(
			{ _pageName = page },
			utilsCargo.IN("termGames._value", {"Series", game})
		)
	})
	local termsByGame = utilsTable.keyBy(rows, "game")
	local term = termsByGame[game or "Series"] or termsByGame["Series"]
	local subtitle = utilsGame.AbbToGame(game or "Series")
	local errCategories = {}
	if mw.title.getCurrentTitle().nsText ~= "User" then
		table.insert(errCategories, "Pages with Invalid or Missing Terms")
		if subtitle ~= "Unknown" then
			table.insert(errCategories, string.format("%s Pages with Invalid or Missing Terms", subtitle))
		end
	end
	if not term then
		return nil, errCategories
	elseif plural and utilsString.isEmpty(term.plural) then
		utilsError.warn(string.format("Term <code>%s</code> has no plural form defined. Using singular form.", term.term))
		return term.term, errCategories
	elseif plural then
		return term.plural
	else
		return term.term
	end
end

p.Schemas = {
	fetchTerm = {
		page = {
			type = "string",
			required = true,
			desc = "The name of a wiki article from which to retrieve a term.",
		},
		game = {
			type = "string",
			default = mw.dumpObject("Series"),
			desc = "A game code. See [[Module:UtilsGame]].",
		},
		plural = {
			type = "boolean",
			desc = "If true, retrieves the plural form."
		},
	}
}

p.Documentation = {
	fetchTerm = {
		params = {"page", "game", "plural"},
		returns = {
			"The term for the given article and game, or nil if none found.",
			"An error category if no term was found.",
		},
		cases = {
			outputOnly = true,
			{
				args = {"Dynalfos", "OoT"},
				expect = { "Dinolfos", nil },
			},
			{
				desc = "Defaults to series term.",
				args = {"Dinolfos"},
				expect = {"Dynalfos"},
			},
			{
				desc = "Defaults to series term when term does not exist for specified game.",
				args = {"Dinolfos", "ALttP"},
				expect = {"Dynalfos"},
			},
			{
				desc = "Error when page does store any terms (game specified).",
				args = {"Flippityfloppito", "SS"},
				expect = {nil, {"Pages with Invalid or Missing Terms", "Skyward Sword Pages with Invalid or Missing Terms"}}
			},
			{
				desc = "Error when page does store any terms (no game specified).",
				args = {"Flippityfloppityfloo"},
				expect = {nil, {"Pages with Invalid or Missing Terms", "The Legend of Zelda Series Pages with Invalid or Missing Terms"}}
			},
			{
				desc = "Plural",
				args = {"Bubble", "Series", true},
				expect = {"Bubbles", nil},
			},
			{
				desc = "Returns singular when no plural form exists.",
				args = {"A Brother's Roast", "BotW", true},
				expect = { "A Brother's Roast", {
				  "Pages with Invalid or Missing Terms",
				  "Breath of the Wild Pages with Invalid or Missing Terms",
				}}
			}
		},
	},
}

return p