Μετάβαση στο περιεχόμενο

Module:FIFARankingMin

Από τη Βικιπαίδεια, την ελεύθερη εγκυκλοπαίδεια
Documentation icon Τεκμηρίωση module[δημιουργία]
local p = {}

local i18n =
{
	["errors"] =
	{
		["entity-not-found"] = "Wikidata entity not found.",
		["claims-not-found"] = "Wikidata entity has no claims.",
		["property-not-found"] = "",
		["unknown-claim-type"] = "Unknown claim type.",
		["unknown-entity-type"] = "Unknown entity type.",
		["qualifier-not-found"] = "Qualifier not found.",
		["site-not-found"] = "Wikimedia project not found.",
		["unknown-datetime-format"] = "Unknown datetime format.",
		["ranks-not-found"] = "No ranks found."
	},
	["months"] =
	{
		"Ιανουαρίου", "Φεβρουαρίου", "Μαρτίου", "Απριλίου", "Μαΐου", "Ιουνίου", "Ιουλίου", "Αυγούστου", "Σεπτεμβίου", "Οκτωβρίου", "Νοεμβρίου", "Δεκεμβρίου"
	}
}

local function stripToNil(text)
	-- If text is a non-empty string, return its trimmed content.
	-- Otherwise, return nothing (text is an empty string or is not a string).
	if type(text) == 'string' then
		return text:match('(%S.-)%s*$')
	end
end

local function dateText(ts)
	-- Return formatted date from timestamp such as "+2017-10-16T00:00:00Z".
	return tonumber(ts:sub(10, 11)) .. " " .. i18n.months[tonumber(ts:sub(7, 8))] .. " " .. ts:sub(2, 5)
end

local function getRankings(qid)
	-- Return table of all rankings or nil if none found.
	-- Each entry is a table { RANK, TIMESTAMP } where RANK is a number.
	local rankings = {}
	local props = mw.wikibase.getBestStatements(qid, 'P1352')
	if not props then return nil end
	for _, v in pairs(props) do
		if v.qualifiers and v.qualifiers["P585"] and v.qualifiers["P459"] then
			if v.qualifiers["P459"][1].datavalue.value.id == "Q180825" then
				rankings[#rankings + 1] = {
					tonumber(v.mainsnak.datavalue.value.amount),  -- rank number or nil
					v.qualifiers["P585"][1].datavalue.value.time  -- timestamp
				}
			end
		end
	end
	if next(rankings) then  -- if table rank is not empty
		return rankings
	end
	return nil
end

function p.main(frame)
	local args = frame.args
	-- see if a qid was supplied for arbitrary access
	local qid = args.qid
	if qid == nil or qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
	local rankings = getRankings(qid)
	if not rankings then return i18n.errors["ranks-not-found"] end

	-- For best and worst, second item should be a table of timestamps.
	local rankCurrent = { 0, "" }  -- ranking/timestamp of entry with most-recent (largest) timestamp
	local rankBest = { 1e6, "" }   -- ranking/timestamp of entry with best (smallest) ranking
	local rankWorst = { 0, "" }    -- ranking/timestamp of entry with worst (largest) ranking
	for i, v in ipairs(rankings) do
		if v[2] > rankCurrent[2] then
			rankCurrent = v
		end
		if v[1] < rankBest[1] then
			rankBest = v
		end
		if v[1] > rankWorst[1] then
			rankWorst = v
		end
	end
	local rankWanted
	local want = stripToNil(args[1])
	if want == 'min' then
		rankWanted = rankBest
	elseif want == 'max' then
		rankWanted = rankWorst
	else
		rankWanted = rankCurrent
	end
	local function text(r)
		return r[1] .. " (" .. dateText(r[2]) .. ")"
	end
	if stripToNil(args.dbg) then  -- show all values for debugging if have parameter dbg=anything
		local lines = {}
		for _, v in ipairs(rankings) do
			lines[#lines + 1] = text(v) .. "<br>"
		end
		return table.concat(lines) .. "Wanted = " .. text(rankWanted)
	end
	return text(rankWanted)
end

return p