Mòdul:Sports career

De la Viquipèdia, l'enciclopèdia lliure
local p = {}

-- dependencies
local Arguments = require('Module:Arguments')
local Wikidata = require('Module:Wikidades/proves')

function p._main(args)
	local id = args.item
	local lang = args.lang or Wikidata.lang()
	
	-- fetch data
	local teams_text = Wikidata.claim{item = id, lang = lang, formatting = 'table', separator = '<value>', editicon = 'no',
		property = 'P54', -- member of sports team
		qualifier = 'P580', -- start time
		qualifier2 = 'P582', -- end time
		qualifier3 = 'P2094', -- competition class
		rowformat = '$3<qual>$0<qual>$1<qual>$2'}
	
	local victories_text = Wikidata.claim{item = id, lang = lang, formatting = 'table', separator = '<value>', editicon = 'no',
		property = 'P2522', -- victory
		qualifier = 'P2094', -- competition class
		qualifier2 = 'P585', -- point in time
		colformat0 = 'sitelink',
		rowformat = '$1<qual>$2<qual>$0'}
	
	local wins_text = Wikidata.claim{item = id, lang = lang, formatting = 'table', separator = '<value>', editicon = 'no',
		property = 'P1355', -- number of wins
		qualifier = 'P642', -- of
		rowformat = '$1<qual>$0'}
	
	local starts_text = Wikidata.claim{item = id, lang = lang, formatting = 'table', separator = '<value>', editicon = 'no',
		property = 'P1350', -- number of matches played/races/starts
		qualifier = 'P642', -- of
		rowformat = '$1<qual>$0'}
	
	local points_text = Wikidata.claim{item = id, lang = lang, formatting = 'table', separator = '<value>', editicon = 'no',
		property = 'P6544', -- total points in career
		qualifier = 'P642', -- of
		qualifier2 = 'P2210', -- relative to
		colformat2 = 'raw',
		rowformat = '$1<qual>$2<qual>$0'}
	
	-- fill tables with data fetched
	local function splitToTable(text)
		local mytable = {}
		if text then
			local row
			for v in mw.text.gsplit(text, '<value>', true) do
				row = mw.text.split(v, '<qual>', true)
				table.insert(mytable, row)
			end
		end
		return mytable
	end
	
	local teams = splitToTable(teams_text) -- {competition, team, year1, year2}
	local victories = splitToTable(victories_text) -- {competition, year, victory}
	local wins = splitToTable(wins_text) -- {competition, wins}
	local starts = splitToTable(starts_text) -- {competition, starts}
	local points = splitToTable(points_text) -- {competition, header, points}
	
	-- competitions index
	local comp_dated, comp_index = {}, {}
	local function addComp(tab, index)
		for _, v in ipairs(tab) do
			if comp_dated[v[1]] == nil then
				comp_dated[v[1]] = index and v[index] or ''
				table.insert(comp_index, v[1])
			end
		end
	end
	addComp(teams, 3)
	addComp(victories, 1)
	addComp(wins)
	addComp(starts)
	addComp(points)
	table.sort(comp_index, function(a, b) return comp_dated[a] > comp_dated[b] end)
	
	-- competitions data
	local competitions = {} -- {competition, {team}, {victories}}}
	for i, v in ipairs(comp_index) do
		competitions[v] = {}
		competitions[v].team = {}
		competitions[v].victories = {}
	end
	for i, v in ipairs(teams) do
		table.insert(competitions[v[1]].team, v[2]..' ('..v[3]..'-'..v[4]..')')
	end
	for i, v in ipairs(victories) do
		table.insert(competitions[v[1]].victories, '[['..v[3]..'|'..v[2]..']]')
	end
	
	-- statistical points
	local stats = {} -- {competition, starts=, wins=, podiums=, poles=, flaps=, points=}
	local stats_headers = {}
	for i, v in ipairs(comp_index) do
		stats[v] = {}
	end
	if #starts > 0 then
		table.insert(stats_headers, 'P1350')
		for i, v in ipairs(starts) do
			stats[v[1]]['P1350'] = v[2]
		end
	end
	if #wins > 0 then
		table.insert(stats_headers, 'Q50000')
		for i, v in ipairs(wins) do
			stats[v[1]]['Q50000'] = v[2]
		end
	end
	local function contains(tab, val)
		for index, value in ipairs(tab) do
			if value == val then return true end
		end
	end
	for i, v in ipairs(points) do
		if not contains(stats_headers, v[2]) then
			table.insert(stats_headers, v[2])
		end
		stats[v[1]][v[2]] = v[3]
	end
	
	-- labels
	-- stats order: starts, wins, podiums, poles p., f.laps, points
	local stats_order = {['P1350']=1, ['Q50000']=2, ['Q5688743']=3, ['Q588596']=4, ['Q310258']=5, ['Q3393320']=6}
	table.sort(stats_headers, function(a, b) return (stats_order[a] or 9) < (stats_order[b] or 9) end)
	local labels_qid = mw.clone(stats_headers)
	table.insert(labels_qid, 'Q327245') -- team
	table.insert(labels_qid, 'Q50000') -- victory
	local labels = {}
	for _, v in ipairs(labels_qid) do
		labels[v] = Wikidata.getLabel{v, lang=lang, editicon='no'}
	end
	
	-- output formatted
	local background = args.background or '#E2F0E8'
	local ret = '<table class="collapsible" style="text-align: left; width: 100%; background: transparent; font-size: 95%;">'
	ret = ret .. '<tr><th colspan="2" style="text-align: center; color:#4b4b4b; background:' .. background .. ';" class="fn">'
	ret = ret .. 'Palmarès</th></tr>'
	for _, v in ipairs(comp_index) do
		ret = ret .. '<tr><th colspan="2" style="text-align: center; color:#4b4b4b; background:' .. background .. ';" class="fn">'
		ret = ret .. v .. '</th></tr>'
		if #competitions[v].team > 0 then
			ret = ret .. '<tr><th style="text-align: left;">' .. labels['Q327245'] .. '</th>'
			ret = ret .. '<td>' .. table.concat(competitions[v].team, ', ') .. '</td></tr>'
		end
		if #competitions[v].victories > 0 then
			ret = ret .. '<tr><th style="text-align: left;">' .. labels['Q50000'] .. '</th>'
			ret = ret .. '<td>' .. table.concat(competitions[v].victories, ', ') .. '</td></tr>'
		end
		if stats[v] ~= {} then
			ret = ret .. '<tr><td colspan="2">'
			ret = ret .. '<table style="padding: 0px; margin: 0px; width: 100%; background: transparent;"><tr>'
			local columns = 0
			for _ in pairs(stats[v]) do columns = columns + 1 end
			local width = math.floor(100 / columns) .. '%'
			for _, h in ipairs(stats_headers) do
				if stats[v][h] then
					ret = ret .. '<th style="width:' .. width .. ';text-align:center">' .. labels[h] .. '</th>'
				end
			end
			ret = ret .. '</tr><tr>'
			for _, h in ipairs(stats_headers) do
				if stats[v][h] then
					ret = ret .. '<td style=text-align:center>' .. stats[v][h] .. '</td>'
				end
			end
			ret = ret .. '</tr></table></td></tr>'
		end
	end
	ret = ret .. '</table>'
	return ret
end
	
function p.main(frame)
	local args = Arguments.getArgs(frame)
	return p._main(args)
end

return p