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
Register
mNo edit summary
m (fix bug related to MediaWiki:Gadget-EditToolbarButtons.js)
Line 7: Line 7:
 
local utilsGame = require("Module:UtilsGame")
 
local utilsGame = require("Module:UtilsGame")
 
local utilsMarkup = require("Module:UtilsMarkup")
 
local utilsMarkup = require("Module:UtilsMarkup")
  +
local utilsPage = require("Module:UtilsPage")
 
local utilsString = require('Module:UtilsString')
 
local utilsString = require('Module:UtilsString')
 
local utilsTable = require('Module:UtilsTable')
 
local utilsTable = require('Module:UtilsTable')
Line 36: Line 37:
 
function p.Singular(frame)
 
function p.Singular(frame)
 
local args = getArgs(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
return p.printTerm(args)
 
 
end
 
end
   
 
function p.Plural(frame)
 
function p.Plural(frame)
 
local args = getArgs(frame)
 
local args = getArgs(frame)
return p.printTerm(args, true)
+
return p.printTerm(args, true, not utilsPage.inNamespace({"User", "MediaWiki"}))
 
end
 
end
   
Line 51: Line 52:
 
end
 
end
   
function p.printTerm(args, plural)
+
function p.printTerm(args, plural, printErrorCategories)
 
local validationErrors = utilsArg.validator({
 
local validationErrors = utilsArg.validator({
 
page = {
 
page = {
Line 68: Line 69:
   
 
local term, fetchErrors = p.fetchTerm(args.page, args.game, plural or args.plural)
 
local term, fetchErrors = p.fetchTerm(args.page, args.game, plural or args.plural)
local errors = utilsTable.concat(validationErrors or {}, fetchErrors or {})
 
 
 
  +
local errorCategories = ""
local result = utilsMarkup.categories(errors)
 
  +
if printErrorCategories then
 
local errors = utilsTable.concat(validationErrors or {}, fetchErrors or {})
 
errorCategories = utilsMarkup.categories(errors)
  +
end
  +
local result = ""
 
if not term then
 
if not term then
 
local errLink = utilsMarkup.sectionLink(args.page, args.section, args.display)
 
local errLink = utilsMarkup.sectionLink(args.page, args.section, args.display)
Line 76: Line 81:
 
class = "facelift-term-invalid",
 
class = "facelift-term-invalid",
 
tooltip = "Invalid or missing term",
 
tooltip = "Invalid or missing term",
})(errLink) .. result
+
})(errLink)
 
elseif args.link == "link" then
 
elseif args.link == "link" then
 
local gameSub = utilsGame.AbbToBaseGame(args.game, true)
 
local gameSub = utilsGame.AbbToBaseGame(args.game, true)
Line 82: Line 87:
 
args.section = gameSub
 
args.section = gameSub
 
end
 
end
result = utilsMarkup.sectionLink(args.page, args.section, args.display or term) .. result
+
result = utilsMarkup.sectionLink(args.page, args.section, args.display or term)
 
else
 
else
result = utilsMarkup.class("term")(args.display or term) .. result
+
result = utilsMarkup.class("term")(args.display or term)
 
end
 
end
return result
+
return result .. errorCategories
 
end
 
end
   

Revision as of 03:46, 25 April 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"

-- 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 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.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 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({
			class = "facelift-term-invalid",
			tooltip = "Invalid or missing term",
		})(errLink)
	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
	if type(page) == "table" then --legacy
		game = page.game
		plural = page.plural
		page = page.term
	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 = "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