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.P1346 then
		mw.log("no claims")
		return
	end

	-- select P1346 with P1355
	local data = {}
	local max = 0
	for i, v in ipairs(entity.claims.P1346) do
		if (v.type == "statement") and (v.rank ~= "deprecated") -- not deprecated statement
		and v.qualifiers and v.qualifiers.P1355 -- with qualifier P1355
		and v.mainsnak.snaktype == "value" then -- to Q entity 
			local id = v.mainsnak.datavalue.value.id
			-- select MAX(P1355) although there is mostly one
			local victory = 0
			for _, e in ipairs(v.qualifiers.P1355) do
				if e.snaktype == "value" then
					local value = tonumber(e.datavalue.value.amount)
					if value and value > victory then
						victory = value
					end
				end
			end
			if victory > max then
				data = { id }
				max = victory
			elseif victory == max and victory > 0 then
				table.insert( data, id )
			end
		end
	end

	if #data == 0 then
		mw.log("there is no data with victory")
		return
	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 = {}
	-- select data with maximum victory
	for _, id in ipairs(data) do
		table.insert(result, makeResult(id) .. " (" .. max .. ")")
	end

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

return p