Module:Term

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 utilsString = require('Module:UtilsString') local utilsTable = require('Module:UtilsTable')

local CARGO_TABLE = "Terminologies"

-- Deprecated function p._Main(frame) local args = frame.args args = utilsTable.mapValues(utilsString.nilIfEmpty)(args) return p.printTerm({game = args["game"], page = args["term"], link = args["link"], plural = args["plural"], display = args["display"], section = args["section"]}) end

-- Deprecated function p.Main(args) args.page = args.term args = utilsTable.mapValues(mw.text.trim)(args) args = utilsTable.mapValues(utilsString.nilIfEmpty)(args) return p.printTerm(args) end

local function getArgs(frame) local args = frame:getParent.args args = utilsTable.mapValues(mw.text.trim)(args) args = utilsTable.mapValues(utilsString.nilIfEmpty)(args) 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) end

function p.Plural(frame) local args = getArgs(frame) return p.printTerm(args, true) end

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

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

local term, fetchErrors = p.fetchTerm(args.page, args.game, plural or args.plural) local errors = utilsTable.concat(validationErrors or {}, fetchErrors or {}) local result = utilsMarkup.categories(errors) if not term then local errLink = utilsMarkup.sectionLink(args.page, args.section, args.display) result = utilsMarkup.inline({			class = "facelift-term-invalid",			tooltip = "Invalid or missing term",		})(errLink) .. result 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) .. result else result = utilsMarkup.class("term")(args.display or term) .. result end return result end

function p.fetchTerm(page, game, plural) utilsArg.required(page, "page") if not page then return nil end if type(page) == "table" then --legacy game = page.game plural = page.plural page = page.term end local rows = utilsCargo.query(CARGO_TABLE, "term, games, plural", {		where = utilsCargo.allOf({_pageName = page})	}) local term = h.findTerm(rows, game) or h.findTerm(rows, "Series") local subtitle = utilsGame.AbbToGame(game or "Series") local 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 if not term then return nil, errCategories elseif plural and utilsString.isEmpty(term.plural) then utilsError.warn(string.format("Term  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

function h.findTerm(rows, game) for _, row in ipairs(rows) do		local games = utilsString.split(row.games) if not utilsString.isEmpty(utilsTable.keyOf(games, game)) then return row end 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 = "Series", desc = "A game code. See Module:UtilsGame.", },		plural = { type = "boolean", desc = "If true, retrieves the plural form." },	} }

p.Documentation = { {		name = "fetchTerm", outputOnly = true, 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 = { {				args = {"Dynalfos", "OoT"}, expected = { "Dinolfos", nil }, },			{				description = "Defaults to series term.", args = {"Dinolfos"}, expected = {"Dynalfos"}, },			{				description = "Defaults to series term when term does not exist for specified game.", args = {"Dinolfos", "ALttP"}, expected = {"Dynalfos"}, },			{				description = "Error when page does store any terms (game specified).", args = {"Flippityfloppito", "SS"}, expected = {nil, {"Pages with Invalid or Missing Terms", "Skyward Sword Pages with Invalid or Missing Terms"}} },			{				description = "Error when page does store any terms (no game specified).", args = {"Flippityfloppityfloo"}, expected = {nil, {"Pages with Invalid or Missing Terms", "The Legend of Zelda Series Pages with Invalid or Missing Terms"}} },			{				description = "Plural", args = {"Bubble", "Series", true}, expected = {"Bubbles", nil}, },			{				description = "Returns singular when no plural form exists.", args = {"A Brother's Roast", "BotW", true}, expected = { "A Brother's Roast", { "Pages with Invalid or Missing Terms", "Breath of the Wild Pages with Invalid or Missing Terms", }}			}		},	}, }

return p