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

Module:FIFARanking

Από τη Βικιπαίδεια, την ελεύθερη εγκυκλοπαίδεια

Lua error in Module:Documentation at line 869: message: type error in message cfg.testcases-run-link-display (string expected, got nil).

local p = {}
local lang = mw.getContentLanguage()
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"] = ""
	},
}

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

-- returns year, month, day from timestamp resembling ISO 8601
-- see member time at https://www.wikidata.org/wiki/Special:ListDatatypes#time
local function _timeToYMD(dateStr)
	return dateStr:match('([+-].*)-(.*)-(.*)T')
end

-- Return e.g. "3 Οκτωβρίου 2017" given "+2017-10-03T00:00:00Z" on [elwiki]
local function dateText(timestamp)
	return lang:formatDate('j F Y', timestamp)
end

-- Checks whether time string is within given number of seconds
-- see also https://www.wikidata.org/wiki/Special:ListDatatypes#time → [time]
local function _dateIsRecent(dateStr, seconds)
	-- match year, month, day fields
	local y, m, d = _timeToYMD(dateStr)

	-- default 126230400 seconds (4 years) as suggested by
	-- https://en.wikipedia.org/wiki/Wikipedia_talk:Lua#Module_help_(2)
	local delta = seconds or 126230400
	return os.time() - os.time({year = y, month = m, day = d}) < delta
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')
	local hasRecentRanking = false
	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
				}
				if _dateIsRecent(rankings[#rankings][2]) then
					hasRecentRanking = true
				end
			end
		end
	end
	if hasRecentRanking and 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 == 'max' then
		rankWanted = rankBest
	elseif want == 'min' 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