Zelda Wiki

OoT Navi.png
Hey! Listen!

This wiki contains spoilers! Read at your own risk!

READ MORE

Zelda Wiki
Zelda Wiki
10,263
pages
(+sort argument)
Line 8: Line 8:
 
function p._Main (frame)
 
function p._Main (frame)
 
local args = frame.args
 
local args = frame.args
return p.Main(args["game"], args["term"], args["link"], args["plural"], args["display"], args["section"])
+
return p.Main(args["game"], args["term"], args["link"], args["plural"], args["display"], args["section"], args["sort"])
 
end
 
end
   
function p.Main(game, term, link, plural, display, section)
+
function p.Main(game, term, link, plural, display, section, sort)
 
local returnedValue = ""
 
local returnedValue = ""
 
 
Line 54: Line 54:
 
returnedValue = returnedValue .. "</span>"
 
returnedValue = returnedValue .. "</span>"
 
end
 
end
  +
end
  +
if not utilsCode.IsEmpty(sort) then
  +
local sortValue = p.getSortValue(game, term, sort)
  +
returnedValue = string.format('data-sort-value="%s"|', sortValue) .. returnedValue
  +
mw.log(returnedValue)
 
end
 
end
 
return returnedValue
 
return returnedValue
  +
end
  +
  +
function p.getSortValue(game, term, sort)
  +
local queryResult = cargo.query("SortValues", "Value", {
  +
where= ("Game='%s' AND Term='%s' AND Hypernym='%s'"):format(game, term, sort),
  +
limit= 1,
  +
})
  +
local sortValue = queryResult[1] and queryResult[1]["Value"]
  +
if sortValue == nil then
  +
local err = ("No sort value of type <code>%s</code> found for term <code>%s</code> in game <code>%s</code>. Defaulting to 0."):format(sort, term, game)
  +
mw.addWarning(err)
  +
sortValue = "0"
  +
end
  +
return sortValue
 
end
 
end
   

Revision as of 23:38, 22 February 2020

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


local p = {}
local cargo = mw.ext.cargo
local utilsCode = require("Module:UtilsCode")
local utilsGame = require("Module:UtilsGame")
local utilsText = require('Module:UtilsText')
local utilsTable = require('Module:UtilsTable')

function p._Main (frame)
	local args = frame.args
	return p.Main(args["game"], args["term"], args["link"], args["plural"], args["display"], args["section"], args["sort"])
end

function p.Main(game, term, link, plural, display, section, sort)
	local returnedValue = ""
	
	local fetchedTerm = p.fetchRow(game, term)
	if utilsCode.IsEmpty(fetchedTerm) then
		
		local displayedTerm = term
		if not utilsCode.IsEmpty(display) then
			displayedTerm = display
		end
		
		local linkedTerm = term
		if not utilsCode.IsEmpty(section) then
			linkedTerm = linkedTerm .. "#" .. section
		end
		return "<span class='explain facelift-term-invalid' title='Invalid or missing term'>[[" .. term .. "|" .. displayedTerm .. "]]</span>[[Category:Pages with Invalid or Missing Terms]][[Category:" .. utilsGame.AbbToGame(game) .. " Pages with Invalid or Missing Terms]]"
	else
		if link == "link" then
			local gameSub = utilsGame.AbbToBaseGame(game, true)
			returnedValue = returnedValue .. "[[" .. term
			if not utilsCode.IsEmpty(section) then
				returnedValue = returnedValue .. "#" .. section
			elseif (gameSub ~= "Unknown" and game ~= "Series") then
				returnedValue = returnedValue .. "#" .. gameSub
			end
			returnedValue = returnedValue .. "|"
		else
			returnedValue = returnedValue .. "<span class='term'>"
		end
		
		if not utilsCode.IsEmpty(display) then
			returnedValue = returnedValue .. display
		elseif plural == "plural" then
			returnedValue = returnedValue .. fetchedTerm["plural"]
		else
			returnedValue = returnedValue .. fetchedTerm["term"]
		end
		
		if link == "link" then
			returnedValue = returnedValue .. "]]"
		else
			returnedValue = returnedValue .. "</span>"
		end
	end
	if not utilsCode.IsEmpty(sort) then
		local sortValue = p.getSortValue(game, term, sort)
		returnedValue = string.format('data-sort-value="%s"|', sortValue) .. returnedValue
		mw.log(returnedValue)
	end
	return returnedValue
end

function p.getSortValue(game, term, sort)
	local queryResult = cargo.query("SortValues", "Value", {
		where= ("Game='%s' AND Term='%s' AND Hypernym='%s'"):format(game, term, sort),
		limit= 1,
	})
	local sortValue = queryResult[1] and queryResult[1]["Value"]
	if sortValue == nil then
		local err = ("No sort value of type <code>%s</code> found for term <code>%s</code> in game <code>%s</code>. Defaulting to 0."):format(sort, term, game)
		mw.addWarning(err)
		sortValue = "0"
	end
	return sortValue
end

-- Returns raw term
function p.fetchTerm(game, page)
	return p.fetchRow(game, page)["term"]
end

function p._fetchTerm(frame)
	local args = frame.args
	return p.fetchTerm(args["game"], args["term"])
end

-- Returns raw plural
function p.fetchPlural(game, page)
	return p.fetchRow(game, page)["plural"]
end

function p._fetchPlural(frame)
	local args = frame.args
	return p.fetchPlural(args["game"], args["term"])
end

-- Returns the table row of the specified game for the specified page.
-- Returns the entry for "Series" if not found.
function p.fetchRow(game, page)
    local tables = 'Terminologies'
    local fields = 'games, term, plural'
    local queryArgs = {
        where = "_pageName = '" .. string.gsub(string.gsub(page, "&#39;", "''"), "'", "''") .. "'"
    }
    local result = cargo.query( tables, fields, queryArgs )
    
    --Looks for the game
	for _, row in pairs(result) do
		local games = utilsText.split(row.games)
		if utilsTable.keyOf(games, game) then
			return row
		end
	end
	
	--Else, looks for "Series"
	for _, row in pairs(result) do
		local games = utilsText.split(row.games)
		if utilsTable.keyOf(games, "Series") then
			return row
		end
	end
	
	return nil
end

return p