Zelda Wiki

OoT Navi.png

Hey! Listen!

This wiki contains spoilers! Read at your own risk!

READ MORE

Zelda Wiki
Zelda Wiki
10,154
pages
m
(refactor + validation + documentation)
Line 1: Line 1:
 
local p = {}
 
local p = {}
local cargo = mw.ext.cargo
+
local h = {}
  +
local utilsCode = require("Module:UtilsCode")
 
  +
local utilsArg = require("Module:UtilsArg")
  +
local utilsCargo = require("Module:UtilsCargo")
 
local utilsError = require("Module:UtilsError")
 
local utilsError = require("Module:UtilsError")
 
local utilsGame = require("Module:UtilsGame")
 
local utilsGame = require("Module:UtilsGame")
local utilsLocalization = require('Module:UtilsLocalization')
+
local utilsMarkup = require("Module:UtilsMarkup")
 
local utilsString = require('Module:UtilsString')
 
local utilsString = require('Module:UtilsString')
 
local utilsTable = require('Module:UtilsTable')
 
local utilsTable = require('Module:UtilsTable')
Line 10: Line 12:
 
local CARGO_TABLE = "Terminologies"
 
local CARGO_TABLE = "Terminologies"
   
  +
-- Deprecated
function p._Main (frame)
 
  +
function p._Main(frame)
 
local args = frame.args
 
local args = frame.args
  +
args = utilsTable.mapValues(utilsString.nilIfEmpty)(args)
return p.Main({game = args["game"], term = args["term"], link = args["link"], plural = args["plural"], display = args["display"], section = args["section"], sort = args["sort"]})
 
  +
return p.printTerm({game = args["game"], page = args["term"], link = args["link"], plural = args["plural"], display = args["display"], section = args["section"]})
 
end
 
end
   
  +
-- Deprecated
 
function p.Main(args)
 
function p.Main(args)
  +
args.page = args.term
local returnedValue = ""
 
  +
args = utilsTable.mapValues(mw.text.trim)(args)
 
  +
args = utilsTable.mapValues(utilsString.nilIfEmpty)(args)
local fetchedTerm = p.fetchRow({game = args["game"], term = args["term"]})
 
  +
return p.printTerm(args)
if utilsCode.IsEmpty(fetchedTerm) then
 
 
local displayedTerm = args["term"]
 
if not utilsCode.IsEmpty(args["display"]) then
 
displayedTerm = args["display"]
 
end
 
 
local linkedTerm = args["term"]
 
if not utilsCode.IsEmpty(args["section"]) then
 
linkedTerm = linkedTerm .. "#" .. args["section"]
 
end
 
local returnString = "<span class='explain facelift-term-invalid' title='Invalid or missing term'>[[" .. args["term"] .. "|" .. displayedTerm .. "]]</span>"
 
if mw.title.getCurrentTitle().isContentPage == true then
 
returnString = returnString .. "[[Category:Pages with Invalid or Missing Terms]][[Category:" .. utilsGame.AbbToGame(args["game"]) .. " Pages with Invalid or Missing Terms]]"
 
end
 
return returnString
 
else
 
if args["link"] == "link" then
 
local gameSub = utilsGame.AbbToBaseGame(args["game"], true)
 
returnedValue = returnedValue .. "[[" .. args["term"]
 
if not utilsCode.IsEmpty(args["section"]) then
 
returnedValue = returnedValue .. "#" .. args["section"]
 
elseif (gameSub ~= "Unknown" and args["game"] ~= "Series") then
 
returnedValue = returnedValue .. "#" .. gameSub
 
end
 
returnedValue = returnedValue .. "|"
 
else
 
returnedValue = returnedValue .. "<span class='term'>"
 
end
 
 
if not utilsCode.IsEmpty(args["display"]) then
 
returnedValue = returnedValue .. args["display"]
 
elseif args["plural"] == "plural" then
 
returnedValue = returnedValue .. fetchedTerm["plural"]
 
else
 
returnedValue = returnedValue .. fetchedTerm["term"]
 
end
 
 
if args["link"] == "link" then
 
returnedValue = returnedValue .. "]]"
 
else
 
returnedValue = returnedValue .. "</span>"
 
end
 
end
 
return returnedValue
 
 
end
 
end
   
  +
function p.Singular(frame)
-- Returns raw term, or nil if none found
 
  +
local args = frame:getParent().args
function p.fetchTerm(args)
 
  +
args = utilsTable.mapValues(mw.text.trim)(args)
local row = p.fetchRow({game = args["game"], term = args["term"]})
 
  +
args = utilsTable.mapValues(utilsString.nilIfEmpty)(args)
if row then
 
return row.term
+
return p.printTerm(args)
  +
end
else
 
  +
utilsError.warn(string.format("Invalid or missing term <code>%s</code> for game <code>%s</code>", args.term, args.game))
 
  +
function p.Plural(frame)
end
 
  +
local args = frame:getParent().args
  +
args = utilsTable.mapValues(mw.text.trim)(args)
  +
args = utilsTable.mapValues(utilsString.nilIfEmpty)(args)
  +
return p.printTerm(frame:getParent().args, true)
 
end
 
end
   
 
function p._fetchTerm(frame)
 
function p._fetchTerm(frame)
 
local args = frame.args
 
local args = frame.args
  +
args = utilsTable.mapValues(mw.text.trim)(args)
return p.fetchTerm({game = args["game"], term = args["term"]})
 
  +
args = utilsTable.mapValues(utilsString.nilIfEmpty)(args)
  +
return p.fetchTerm(args.term, args.game)
 
end
 
end
   
-- Returns raw plural
+
function p.printTerm(args, plural)
  +
local validationErrors = utilsArg.validator({
function p.fetchPlural(args)
 
  +
page = {
return p.fetchRow({game = args["game"], term = args["term"]})["plural"]
 
  +
nonEmpty = true,
end
 
  +
},
  +
game = {
  +
enum = utilsGame.getEnum()
  +
},
  +
link = {},
  +
plural = {
  +
deprecated = true
  +
},
  +
display = {},
  +
section = {},
  +
})(args)
   
  +
local term, fetchErrors = p.fetchTerm(args.page, args.game, plural or args.plural)
function p._fetchPlural(frame)
 
  +
local errors = utilsTable.concat(validationErrors or {}, fetchErrors or {})
local args = frame.args
 
  +
return p.fetchPlural({game = args["game"], term = args["term"]})
 
  +
local result = ""
  +
if mw.title.getCurrentTitle().isContentPage then
  +
result = utilsMarkup.categories(errors)
  +
end
  +
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
 
end
   
  +
function p.fetchTerm(page, game, plural)
-- Returns the table row of the specified game for the specified page.
 
  +
utilsArg.required(page, "page")
-- Returns the entry for "Series" if not found.
 
  +
if not page then
function p.fetchRow(args)
 
  +
return nil
local tables = CARGO_TABLE
 
local fields = 'games, term, plural'
 
local queryArgs = {
 
where = "_pageName = '" .. string.gsub(string.gsub(args["term"], "&#39;", "''"), "'", "''") .. "' AND lang = '" .. utilsLocalization.GetPageLanguage() .. "'"
 
}
 
local result = cargo.query( tables, fields, queryArgs )
 
 
--Looks for the game
 
for _, row in pairs(result) do
 
local games = utilsString.split(row.games)
 
if utilsTable.keyOf(games, args["game"]) then
 
return row
 
end
 
 
end
 
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 errCategories = {
--Else, looks for "Series"
 
  +
"Pages with Invalid or Missing Terms",
for _, row in pairs(result) do
 
  +
string.format("%s Pages with Invalid or Missing Terms", utilsGame.AbbToGame(game or "Series")),
  +
}
  +
if not term then
  +
utilsError.warn(string.format("No term exists for game <code>%s</code> on page <code>%s</code>.", game or "Series", page))
  +
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
  +
  +
function h.findTerm(rows, game)
  +
for _, row in ipairs(rows) do
 
local games = utilsString.split(row.games)
 
local games = utilsString.split(row.games)
if utilsTable.keyOf(games, "Series") then
+
if not utilsString.isEmpty(utilsTable.keyOf(games, game)) then
 
return row
 
return row
 
end
 
end
 
end
 
end
 
return nil
 
 
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
 
return p

Revision as of 20:12, 7 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 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

function p.Singular(frame)
	local args = frame:getParent().args
	args = utilsTable.mapValues(mw.text.trim)(args)
	args = utilsTable.mapValues(utilsString.nilIfEmpty)(args)
	return p.printTerm(args)
end

function p.Plural(frame)
	local args = frame:getParent().args
	args = utilsTable.mapValues(mw.text.trim)(args)
	args = utilsTable.mapValues(utilsString.nilIfEmpty)(args)
	return p.printTerm(frame:getParent().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 = ""
	if mw.title.getCurrentTitle().isContentPage then
		result = utilsMarkup.categories(errors)
	end
	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 errCategories = {
		"Pages with Invalid or Missing Terms",
		string.format("%s Pages with Invalid or Missing Terms", utilsGame.AbbToGame(game or "Series")),
	}
	if not term then
		utilsError.warn(string.format("No term exists for game <code>%s</code> on page <code>%s</code>.", game or "Series", page))
		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

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