Module:Προηγούμενοςπρωταθλητής

Documentation icon Τεκμηρίωση module[δημιουργία]
local p = {}

function p.main(frame)
	local entity = mw.wikibase.getEntity()
	if not entity or not entity.claims or not entity.claims.P527 or not entity.claims.P1346 then
		mw.log("no claims")
		return
	end

	-- select P527 with P393
	local data = {}
	for i, v in ipairs(entity.claims.P527) do
		if (v.type == "statement") and (v.rank ~= "deprecated") and v.qualifiers and v.qualifiers.P393 -- not deprecated statement with qualifier P393
		and (v.mainsnak.snaktype == "value") then
			-- select MAX(P393) although there is mostly one
			local edition = 0
			for _, e in ipairs(v.qualifiers.P393) do
				if e.snaktype == "value" then
					local value = tonumber(e.datavalue.value)
					if value and value > edition then
						edition = value
					end
				end
			end

			local id = v.mainsnak.datavalue.value.id
			table.insert(data, { id, edition })
		end
	end

	if #data < 2 then
		mw.log("there is not enough data with edition")
		return
	end
	
	table.sort(data, function(first, second)
		return first[2] > second[2]
	end)

	local function makeResult(id)
		local sitelink = mw.wikibase.sitelink(id)
		local label = mw.wikibase.label(id)
		if sitelink and label and (sitelink ~= label) then
			return "[["..sitelink.."|"..label.."]]"
		elseif sitelink then
			return "[["..sitelink.."]]"
		elseif label then
			return label
		else
			return id
		end
	end

	local result = {}
	for i, v in ipairs(entity.claims.P1346) do
		if (v.type == "statement") and (v.rank ~= "deprecated") and v.qualifiers and v.qualifiers.P2522 -- not deprecated statement with qualifier P2522
		and (v.mainsnak.snaktype == "value") then
			for _, e in ipairs(v.qualifiers.P2522) do
				if e.snaktype == "value" then
					if data[2][1] == e.datavalue.value.id then
						table.insert(result, makeResult(v.mainsnak.datavalue.value.id))
					end
				end
			end
		end
	end

	mw.log(table.concat(result, ", "))
	return table.concat(result, ", ")
end

return p