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(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 -- Things like 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("game")(rows) 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  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 = "Series", desc = "A game code. See Module:UtilsGame.", },		plural = { type = "boolean", desc = "If true, retrieves the plural form." },	} }

p.Documentation = { {		name = "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