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 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 and (v.mainsnak.datatype == "wikibase-item") and v.mainsnak.datavalue -- consists of wikibase item
		and (v.mainsnak.datavalue.type == "wikibase-entityid") and v.mainsnak.datavalue.value and (v.mainsnak.datavalue.value["entity-type"] == "item") -- to Q entity
		then
			local id = v.mainsnak.datavalue.value.id
			-- select MAX(P393) although there is mostly one
			local edition = false
			for _, e in ipairs(v.qualifiers.P393) do
				if e and (e.snaktype == "value") and (e.datatype == "string") and e.datavalue and (e.datavalue.type == "string") and e.datavalue.value then
					local value = tonumber(e.datavalue.value)
					if value then
						if not edition then
							edition = value
						elseif value < edition then
							edition = value
						end
					end
				end
			end

			if edition then
				table.insert(data, { id, edition })
			end
		end
	end

	if #data == 0 then
		mw.log("there is no data with edition")
		return
	end
	
	-- select max edition
	local edition = data[1][2]
	for i = 2, #data do
		if data[i][2]-1 > edition then
			edition = data[i][2]
		end
	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 edition
	for _, v in ipairs(data) do
		if v[2] == edition then
			table.insert(result, makeResult(v[1]))
		end
	end

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

return p