Difference between revisions of "Module:Term"

From Zelda Wiki, the Zelda encyclopedia
Jump to: navigation, search
(Moved to Module:Sort Value)
Line 55: Line 55:
 
returnedValue = returnedValue .. "</span>"
 
returnedValue = returnedValue .. "</span>"
 
end
 
end
end
 
if not utilsCode.IsEmpty(args["sort"]) then
 
local sortValue = p.getSortValue({game = args["game"], term = args["term"], sort = args["sort"]})
 
returnedValue = string.format('data-sort-value="%s"|', sortValue) .. returnedValue
 
 
end
 
end
 
return returnedValue
 
return returnedValue
Line 111: Line 107:
 
return nil
 
return nil
 
end
 
end
 
function p.getSortValue(args)
 
local queryResult = cargo.query("SortValues", "Value", {
 
where= ("Game='%s' AND Term='%s' AND OrderBy='%s'"):format(args["game"], args["term"], args["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(args["sort"], args["term"], args["game"])
 
mw.addWarning(err)
 
sortValue = "0"
 
end
 
return sortValue
 
end
 
 
   
 
return p
 
return p

Revision as of 15:11, 24 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 utilsLocalization = require('Module:UtilsLocalization')
local utilsText = require('Module:UtilsText')
local utilsTable = require('Module:UtilsTable')

function p._Main (frame)
	local args = frame.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"]})
end

function p.Main(args)
	local returnedValue = ""
	
	local fetchedTerm = p.fetchRow({game = args["game"], term = args["term"]})
	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
		return "<span class='explain facelift-term-invalid' title='Invalid or missing term'>[[" .. args["term"] .. "|" .. displayedTerm .. "]]</span>[[Category:Pages with Invalid or Missing Terms]][[Category:" .. utilsGame.AbbToGame(args["game"]) .. " Pages with Invalid or Missing Terms]]"
	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

-- Returns raw term
function p.fetchTerm(args)
	return p.fetchRow({game = args["game"], term = args["term"]})["term"]
end

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

-- Returns raw plural
function p.fetchPlural(args)
	return p.fetchRow({game = args["game"], term = args["term"]})["plural"]
end

function p._fetchPlural(frame)
	local args = frame.args
	return p.fetchPlural({game = args["game"], term = 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(args)
    local tables = 'Terminologies'
    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 = utilsText.split(row.games)
		if utilsTable.keyOf(games, args["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