Modul:Wikidata løbshistorie

Fra Wikipedia, den frie encyklopædi
Documentation icon Moduldokumentation[vis] [redigér] [historik] [opfrisk]

Dette modul laver en child-infoboks med løbshistorie for et cykelløb. Infoboksen er beregnet til at placere i {{Infoboks cykelløb}}.

Løbshistorien hentes fra Wikidata. Modulet bruger Modul:Infoboks til at danne infoboksen og Modul:Flag til at finde nationalitet og nationalflag for vindere på tidspunktet for løbet.

Brug[rediger kildetekst]

{{#invoke:Wikidata løbshistorie|historie}} eller {{Wikidata løbshistorie}}

Funktionsargumenter hentes fra den kaldende skabelon. Se {{Wikidata løbshistorie}} for beskrivelse af argumenter.

--[[ Modulet bruges af "Skabelon:Wikidata løbshistorie". Se skabelonen for beskrivelse. ]]

require('Modul:No globals')
local flag = require('Modul:Flag')
local wikibase = mw.wikibase
local frame = mw.getCurrentFrame()

local p = {}

local function get_countryID(qid, time)
	local country = flag.getStatementForTime(qid, 'P1532', time)	-- country for sport
		or flag.getStatementForTime(qid, 'P27', time)				-- or country of citizenship
	if country and country.mainsnak.snaktype == 'value' then
		return country.mainsnak.datavalue.value.id
	else
		return nil
	end
end

local function flag_and_link(qid, time)
	local label = wikibase.getLabel(qid)
	local sitelink = wikibase.getSitelink(qid)
	if sitelink then
		label = '[[' .. sitelink .. '|' .. label .. ']]'
	end
	local countryID = get_countryID(qid, time)
	if countryID then
		return flag.flag(countryID, time) .. ' ' .. label
	end
	return label
end

local function getQualifier(qualifiers, prop)
	if qualifiers and qualifiers[prop] then
		local first = qualifiers[prop][1]
		if first.snaktype == 'value' then
			return first.datavalue.value.id
		end
	end
end

local function getTime(raceID, pid)
	local time_list = wikibase.getBestStatements(raceID, pid)
	if time_list[1] and time_list[1].mainsnak.snaktype == 'value' then
		return time_list[1].mainsnak.datavalue.value.time
	else
		return nil
	end
end

local function getWinner(raceID)
	local winner_list = wikibase.getBestStatements(raceID, 'P1346')
	for _, winner in pairs(winner_list) do
		if winner.mainsnak.snaktype == 'value' then
			if getQualifier(winner.qualifiers, 'P642') == 'Q20882667' then
				-- winner 'for' 'overall winner general classification'
				return winner.mainsnak.datavalue.value.id
			end
		end
	end
end

function p.historie(frame)
	local args = (frame == mw.getCurrentFrame()) and frame:getParent().args or frame.args
	local qid = args.q or mw.wikibase.getEntityIdForCurrentPage()
	if not qid then
		return 'Fejl i Modul:Wikidata løbshistorie: Wikidata-entitet ikke angivet.'
	end
	local aflyst = tonumber(args.aflyst) or 0
	local this_year = tostring(os.date('+%Y'))

	local first_winner
	local first_winner_time = '+9999'
	local last_winner
	local last_winner_time = '+0000'
	local last_cancel_time = '+0000'
	local winners = {}
	local racelist = wikibase.getBestStatements(qid, 'P527') -- P527 = 'has part'
	local count = #racelist
	for _, race in pairs(racelist) do
		local raceID = (race.mainsnak.snaktype == 'value') and race.mainsnak.datavalue.value.id

		local time = getTime(raceID, 'P580')	-- Try 'start time'
			or getTime(raceID, 'P585')			-- or 'point in time'
		if not time then
			-- error message or category here
			error('Fejl: Løb [[d:' .. raceID .. ']] mangler datoangivelse på Wikidata')
		end

		local winnerID = raceID and getWinner(raceID)
		if winnerID == 'Q30108381' then
			--[[ Q30108381 ("aflyst") bruges nogen steder hvis løbet ikke kørt
				 ('novalue' med en forklarende kvalifikator ville være korrekt/logisk)
			]]
			count = count - 1
			aflyst = aflyst + 1 
			if time > last_cancel_time then
				last_cancel_time = time
			end
		elseif winnerID then
			if time < first_winner_time then
				first_winner, first_winner_time = winnerID, time
			end
			if time > last_winner_time then
				last_winner, last_winner_time = winnerID, time
			end
			if not winners[winnerID] then
				winners[winnerID] = {}
				winners[winnerID].count = 0
			end
			winners[winnerID].count = winners[winnerID].count + 1
			if time > (winners[winnerID].last_time or '') then
				winners[winnerID].last_time = time
			end
		else
			if time >= this_year then
				-- don't count future races
				count = count - 1
			end
		end
	end

	local most_wins = 0
	local most_wins_ID = {}
	for winnerID, winner in pairs(winners) do
		if winner.count > most_wins then
			most_wins = winner.count
			most_wins_ID = { winnerID }
		elseif winner.count == most_wins then
			most_wins_ID[#most_wins_ID + 1] = winnerID
		end
	end

	local infobox_args = {
		child = 'yes'
	}
	infobox_args.label1 = 'Første udgave'
	infobox_args.data1 = string.sub(first_winner_time, 2, 5)
	infobox_args.label2 = 'Udgaver'
	local fordate = math.max(string.sub(last_winner_time, 2, 5), string.sub(last_cancel_time, 2, 5))
	if aflyst == 0 then
		infobox_args.data2 = count .. ' (pr. ' .. fordate .. ')'
	elseif aflyst == 1 then
		infobox_args.data2 = (count + aflyst) .. ' (pr. ' .. fordate .. ' inkl. 1 aflyst)'
	else
		infobox_args.data2 = (count + aflyst) .. ' (pr. ' .. fordate .. ' inkl. ' .. aflyst .. ' aflyste)'
	end
	infobox_args.label3 = 'Første vinder'
	infobox_args.data3 = flag_and_link(first_winner, first_winner_time)
	if most_wins > 1 then
		infobox_args.label4 = 'Flest sejre'
		local most_wins_text = {}
		for _, most in pairs(most_wins_ID) do
			most_wins_text[#most_wins_text + 1] = flag_and_link(most, winners[most].last_time) .. ' (' .. most_wins .. ')'
		end
		infobox_args.data4 = table.concat(most_wins_text, '<br/>')
	end
	infobox_args.label5 = 'Regerende mester'
	infobox_args.data5 = flag_and_link(last_winner, last_winner_time)
		mw.logObject(infobox_args)
	return (require('Modul:Infoboks').infobox)(infobox_args)
end

return p