Module:Sandbox/RexxS/Ranking

-- Module to return most recent ranking (P1352) from FIFA (Q180825)
-- making use of point in time (P585)
--

local p = {}

local i18n =
{
	["errors"] =
	{
		["entity-not-found"] = "Wikidata entity not found.",
		["claims-not-found"] = "Wikidata entity has no claims.",
		["property-not-found"] = "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"] =
	{
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	}
}

p.getRanking = function(frame)
	-- allow us to see all values if debugging - set dbg=true as parameter
	dbug = frame.args.dbg or false
	-- see if a qid was supplied for arbitrary access; make empty string into nil
	local qid = mw.text.trim(frame.args.qid or "")
	if qid and (#qid == 0) then qid = nil end
	
	local entity = mw.wikibase.getEntity(qid)
	if not entity then return i18n.errors["entity-not-found"] end
	if not entity.claims then return i18n.errors["claims-not-found"] end
	local props = entity.claims['P1352']
	if not props then return i18n.errors["property-not-found"] end
	
	local rank = {}
	local timestamp = {}
	for k, 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
				rank[#rank + 1] = v.mainsnak.datavalue.value.amount
				timestamp[#rank] = v.qualifiers["P585"][1].datavalue.value.time
			end
		end
	end
	
	if rank then
		local ts = ""
		local imax = 0
		for i, v in ipairs(timestamp) do
			if v > ts then
				ts = v
				imax = i
			end
		end
		
		local lastdate = tonumber(ts:sub(10, 11)) .. " " .. i18n.months[tonumber(ts:sub(7, 8))] .. " " .. tonumber(ts:sub(2, 5))
		local lastrank = tonumber(rank[imax])
		
		if dbug then
			return table.concat(rank, ", ") .. "<br>" .. table.concat(timestamp, ", ") .. "<br>Latest = " .. lastdate .. " rank is " .. lastrank
		else
			return lastrank .. " (" .. lastdate .. ")"
		end
	else
		return i18n.errors["ranks-not-found"]
	end
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.