Aller au contenu

Module:Infobox/Cycliste

Cette page fait l’objet d’une mesure de semi-protection étendue.
Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 16 septembre 2018 à 10:41 et modifiée en dernier par Psemdel (discuter | contributions) (tentative 2). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.

 Documentation[voir] [modifier] [historique] [purger]

Cette page définit un module d'infobox. Pour les conseils sur l'usage de ce module, voyez Modèle:Infobox Cycliste.


local person = require "Module:Infobox/Fonctions/Personne"
local Wikidata = require "Module:Wikidata"
local wikibase = mw.wikibase
local Complexedate = require "Module:Date complexe"
local wikidataON=1 

-- ##### Functions from Cycling race module copied #####
--[[ Iterator to get all statements for an entity and property which are not deprecated and have a value]]
local function nextStatement(state, i)
	local s
	repeat
		i = i + 1
		local s = state[i]
		if s and s.rank ~= 'deprecated' and s.mainsnak.snaktype == 'value' then
			return i, s
		end
	until s == nil
end
local function statements(QID, PID)
	return nextStatement, wikibase.getAllStatements(QID, PID), 0
end

--[[ Get any value for a property which is not deprecated ]]
local function firstValue(QID, PID, field)
	local ss = wikibase.getAllStatements(QID, PID)
	for _, s in pairs(ss) do
		if s.rank ~= 'deprecated' and s.mainsnak.snaktype == 'value' then
			return field and s.mainsnak.datavalue.value[field] or s.mainsnak.datavalue.value
		end
	end
end

--[[ Go from season of a team to the team ]]
local function getParentID(teamID)
	return firstValue(teamID, 'P361', 'id') -- P361 is 'part of'
		or firstValue(teamID, 'P5138', 'id') -- P5138 is 'season of club or team'
end

--[[ Get sitelink with no wiki no formating ]]
local function getRawTeamLink(teamID)
	local sitelink
	local parentID = getParentID(teamID)
	if parentID then -- try parent team first
		sitelink = mw.wikibase.getSitelink(parentID)
	end
	if not sitelink then
		sitelink = mw.wikibase.getSitelink(teamID)
	end
	return sitelink
end

--[[ Get a Wikidata statement for an entity and property valid at the given timevalue ]]
local function getStatementForTime(ID, property, time)
	for _, s in statements(ID, property) do
		local start, startPrecision, END, endPrecision
		local q = s.qualifiers
		if q then
			if q.P580 and q.P580[1] and	q.P580[1].snaktype == 'value' then -- P580 is start time
				start = q.P580[1].datavalue.value.time
				startPrecision = q.P580[1].datavalue.value.precision
				if startPrecision == 9 then -- precision is years
					start = string.sub(start, 1, 5) -- Cut of everything after year
				elseif startPrecision == 10 then -- precision is months
					start = string.sub(start, 1, 8) -- Cut of everything after month
				end
			end
			if q.P582 and q.P582[1] and q.P582[1].snaktype == 'value' then -- P582 is end time
				END = q.P582[1].datavalue.value.time
				endPrecision = q.P582[1].datavalue.value.precision
			end
		end
		if not start or start <= time then
			if not END then
				return s
			end
			if endPrecision == 9 then -- precision 9 is 'years'
				END = string.sub(END, 1, 6) .. '13' -- Set month to 13
			elseif endPrecision == 10 then -- precision 10 is 'months'
				END = string.sub(END, 1, 9) .. '32' -- Set day to 32
			end
			if END > time then
				return s
			end
		end
	end
end

local function getOfficialName(teamID, timeOfRace)
	local p1448 = getStatementForTime(teamID, 'P1448', timeOfRace) -- P1448 is official name
	if p1448 then
		if available_lang_priority and p1448.qualifiers and p1448.qualifiers.P1448 then
			local q = p1448.qualifiers.P1448
			local wantedLanguages = {}
			local best = 999
			local name
			for i, lang in ipairs(translations.lang_priority) do
				wantedLanguages[lang] = i
			end
			for _, l in pairs(q) do
				if l.snaktype == 'value' then
					local lang = l.datavalue.value.language
					if wantedLanguages[lang] and wantedLanguages[lang] < best then
						best = wantedLanguages[lang]
						name = l.datavalue.value.text
					end
				end
			end
			if name	then return name, true end
		end
		return p1448.mainsnak.datavalue.value.text, false
	end
	return wikibase.getLabel(teamID) -- No official name, try label
end

--[[ Get sitelink, categoryID and maybe country for a team.
	Returns sitelink, team category ID, countryID (only countryID if country arg is true ]]
local function getTeamLinkCat(teamID, timeOfRace, country)
	local name, sitelink, parentID
	-- Find team category
	local catID = firstValue(teamID, 'P31', 'id') -- P31 is 'instance of'
	-- Find country if needed
	local countryID
	if country or catID == 'Q23726798' or catID == 'Q20738667' then
		countryID = firstValue(teamID, 'P17', 'id') -- P17 is country
	end
	if countryID and (catID == 'Q23726798' or catID == 'Q20738667') then
		-- It is a national cycling team
		name = getCountryName(countryID)
		if catID == 'Q20738667' then -- national cycling team U23
			local s
			if wiki == 'fr' then s = ' espoirs'
			elseif wiki == 'mk' then s = ' под 23 години'
			elseif wiki == 'ar' then s = ' تحت 23'
			elseif wiki == 'es' then s = ' sub-23'
			else s = ' U23'
			end
			name = name .. s
		end
		sitelink = getRawTeamLink(teamID)
	else
		-- It is not a national cycling team
		local isLocal
		parentID = getParentID(teamID)
		if parentID then -- try parent team first
			sitelink = wikibase.getSitelink(parentID)
			name, isLocal = getOfficialName(parentID, timeOfRace)
		end
		if not sitelink then
			sitelink = wikibase.getSitelink(teamID)
		end

		if not name or (not isLocal and available_lang_priority) then
			local partName, partIsLocal = getOfficialName(teamID, timeOfRace)
			if partName and (not name or partIsLocal) then
				name = partName
			end
		end
	end
	if sitelink then
		if name then
			sitelink = '[[' .. sitelink .. '|' .. name .. ']]'
		else
			sitelink = '[[' .. sitelink .. ']]'
		end
	else
		if name then
			sitelink = name
		else
			sitelink = (ParentID and wikibase.getLabel(ParentID)) or
				wikibase.getLabel(TeamID) or 'No name'
		end
	end
	return sitelink, catID, countryID
end

-- ##### Functions for the Infobox #####
--For the case with direct input of the parameters
local function teamValue( teamParam, dateParam )
	return function( localdata )
		local names, periods = localdata[ teamParam ], localdata[ dateParam ]
		if not names then
			return nil
		end
		if names then
			names =  mw.text.split(names, '<br />')
			periods = mw.text.split(periods or '', '<br />')
		end
		local tab =  mw.html.create('table')
		for i, name in pairs(names) do
			local row = mw.html.create('tr')
				:tag('td'):wikitext(periods[i] or ''):done()
				:tag('td'):wikitext(name):done()
			tab:node(row):done()
		end
		tab:done()
		return tostring(tab)
	end
end

local function presentTeam(item)
	local todaytable=os.date("*t")  
	local teamId, result
	if mw.ustring.len(todaytable["month"])==1 then todaytable["month"]='0'..todaytable["month"] end
	if mw.ustring.len(todaytable["day"])==1 then todaytable["day"]='0'..todaytable["day"] end	
	today='+'..todaytable["year"].."-"..todaytable["month"].."-"..todaytable["day"].."T00:00:00Z"
	
	itemID=item:getId()
	local p54=getStatementForTime(itemID, 'P54', today) 
	if p54  then
		teamId= p54.mainsnak.datavalue.value.id
		result=getTeamLinkCat(teamId, today)
		return  result 
	else 
		return nil
	end
end

local function dateObject(orig)
	local newobj = Complexedate.splitDate(orig)  
	local temp
	--if newobj.day ==0 or newobj.day==nil then
		if newobj.month == 0 or newobj.month==nil then
			temp=newobj.year
		else
			temp=newobj.month..'.'..newobj.year
		end
	--else
		--temp=newobj.day..'.'..newobj.month..'.'..newobj.year
	--end
	return temp 
end

local function listofTeam(item)
	--first we have to read P54 of the rider
	local starttime, endtime, teamId
	local riderteam={}
	
	for ii, p54 in statements(itemID, 'P54') do --itemID loaded in presentTeam
		if p54 then
			teamId=p54.mainsnak.datavalue.value.id
		else
			teamId=nil
		end
		local q = p54.qualifiers
		if q then
			if q.P580 and q.P580[1] and	q.P580[1].snaktype == 'value' then -- P580 is start time
				starttime =q.P580[1].datavalue.value.time
			elseif q.P585 and q.P585[1] and	q.P585[1].snaktype == 'value' then
				starttime =q.P585[1].datavalue.value.time
			else
				starttime =0
			end
			if q.P582 and q.P582[1] and q.P582[1].snaktype == 'value' then
				endtime =q.P582[1].datavalue.value.time
			elseif q.P585 and q.P585[1] and	q.P585[1].snaktype == 'value' then
				endtime =q.P585[1].datavalue.value.time
			else
				endtime=0
			end
			if q.P39 and q.P39[1] and	q.P39[1].snaktype == 'value' then
				stagiaire = q.P39[1].datavalue.value
			else
				stagiaire = 0
			end
			table.insert(riderteam,{teamId, starttime,  endtime, stagiaire})
		end
	end
	return  riderteam
end

local function comparedate(date1, beginorend1, date2, beginorend2)
	local date1smaller=1
	local year1, m1, d1
	local year2, m2, d2
	if date1==0 then
		if date2==0 then
			return 1--always smaller
		end
	else
		_, _, year1,m1,d1 = string.find(date1, "(%d+)-(%d+)-(%d+)")
		if m1 ==nil or m1=="00" then
			if beginorend1==0 then --begin
				m1="01"
				d1="01"
			else--end
				m1="12"
				d1="31"
			end		
		end
	end

	if date2==0 then
		return 1--always bigger
	else
		_, _, year2,m2,d2 = string.find(date2, "(%d+)-(%d+)-(%d+)")
		if m2 ==nil or m2=="00" then
			if beginorend2==0 then --begin
				m2="01"
				d2="01"
			else--end
				m2="12"
				d2="31"
			end		
		end
	end

	if year1==nil or year2 ==nil then
		date1smaller=0
	else
		if tonumber(year1)<tonumber(year2) then
			date1smaller=1
		elseif tonumber(year1)>tonumber(year2) then
			date1smaller=0
		else
			if tonumber(m1)<tonumber(m2) then
				date1smaller=1
			elseif tonumber(m1)>tonumber(m2) then
				date1smaller=0
			else
				if d1==nil or d2==nil then
				elseif tonumber(d1)<=tonumber(d2) then
					date1smaller=1
				elseif tonumber(d1)>tonumber(d2) then
					date1smaller=0
				end
			end
		end	
	end
	return date1smaller
end

local function sortteamtable(teamrider)
	iimax=#teamrider
	newteamrider=teamrider
	for i, v in pairs(teamrider) do
		jjmax=iimax-i
		table_sorted=1
		jj=1
		while jj<jjmax+1 do
			comparisonTemp=comparedate(teamrider[jj][4],0,teamrider[jj+1][4],0) 
			if comparisonTemp==0 then			
				temp1=teamrider[jj][1]
				temp2=teamrider[jj][2]
				temp3=teamrider[jj][3]
				temp4=teamrider[jj][4]
				temp5=teamrider[jj][5]
                newteamrider[jj][1]=teamrider[jj+1][1]
                newteamrider[jj][2]=teamrider[jj+1][2]
                newteamrider[jj][3]=teamrider[jj+1][3]
                newteamrider[jj][4]=teamrider[jj+1][4]
                newteamrider[jj][5]=teamrider[jj+1][5]
                newteamrider[jj+1][1]=temp1
                newteamrider[jj+1][2]=temp2
                newteamrider[jj+1][3]=temp3
                newteamrider[jj+1][4]=temp4
                newteamrider[jj+1][5]=temp5
                table_sorted=0
             end
             jj=jj+1
         end
    	if table_sorted==1 then
            break
        end
	end
	return newteamrider
end

local function formatdate(date1)
	if date1==0 then
		return ''
	else 
		return dateObject(date1)
	end	
end

local function listofteamname(teamrider)
local teamoutput={}	
local sitelink
	if teamrider==nil then
		return nil
	end

	for ii, v in pairs(teamrider) do
		local teamname={} --it is a temp
		local thisteam = teamrider[ii][1] 
		local starttime, endtime
		local teamlink=wikibase.getSitelink(thisteam)
		local p1448 = wikibase.getBestStatements(thisteam, 'P1448')
		if p1448[1] and p1448[1].mainsnak.snaktype == 'value' then
			for kk, v1 in statements(thisteam, 'P1448') do
				local q = v1.qualifiers
				if q then
					if q.P580 and q.P580[1] and	q.P580[1].snaktype == 'value' then -- P580 is start time
						starttime =q.P580[1].datavalue.value.time
					else
						starttime =0
					end
					if q.P582 and q.P582[1] and	q.P582[1].snaktype == 'value' then -- P582 is end time
						endtime =q.P582[1].datavalue.value.time
					else
						endtime =0
					end
				else
					starttime =0
					endtime =0
				end
	
				if teamlink==nil then
					sitelink=v1.mainsnak.datavalue.value.text
				else
					sitelink='[['..teamlink..'|'..v1.mainsnak.datavalue.value.text..']]'
				end
	
				if starttime==0 and endtime==0 then --Only one name of team
					table.insert(teamoutput,{sitelink, formatdate(teamrider[ii][2]),formatdate(teamrider[ii][3]),teamrider[ii][2],teamrider[ii][4]})
				elseif starttime==0 and comparedate(endtime,1,teamrider[ii][3],1)==1 then --no beginning for name of team (not logical)
					table.insert(teamoutput,{sitelink, formatdate(teamrider[ii][2]),formatdate(teamrider[ii][3]),teamrider[ii][2],teamrider[ii][4]})
				elseif comparedate(starttime, 0,teamrider[ii][2],0)==1 and endtime==0 then --no ending for the name, the name was already so when the rider arrived in the team
					table.insert(teamoutput,{sitelink, formatdate(teamrider[ii][2]),formatdate(teamrider[ii][3]),teamrider[ii][2],teamrider[ii][4]})
				elseif comparedate(starttime,0, teamrider[ii][2],0)==1 and comparedate(teamrider[ii][3],1,endtime,1)==1 then --the name was so before and after the rider was in the team
				   table.insert(teamoutput,{sitelink, formatdate(teamrider[ii][2]), formatdate(teamrider[ii][3]),teamrider[ii][2],teamrider[ii][4]})
				elseif comparedate(starttime, 0,teamrider[ii][2],0)==1 and comparedate(teamrider[ii][2],0,endtime,1)==1 then --the name started before arrival of the rider but changed then
					table.insert(teamoutput,{sitelink, formatdate(teamrider[ii][2]),formatdate(endtime),teamrider[ii][2],teamrider[ii][4]})
				elseif comparedate(starttime,0, teamrider[ii][3],1)==1 and comparedate(teamrider[ii][3],1, endtime, 1)==1 then --the name changed when the rider was already there and stayed the same afterwards
				    if teamrider[ii][3]==0 then
				    	table.insert(teamoutput,{sitelink,  formatdate(starttime), formatdate(endtime),starttime,teamrider[ii][4]})
				    else
				  		table.insert(teamoutput,{sitelink, formatdate(starttime),formatdate(teamrider[ii][3]),starttime,teamrider[ii][4]})
					end
				elseif comparedate(teamrider[ii][2], 0,starttime,0)==1 and comparedate( endtime, 1,teamrider[ii][3],1)==1 then --the name is "included" in the period when the rider was there
					table.insert(teamoutput,{sitelink, formatdate(starttime),formatdate(endtime),starttime,teamrider[ii][4]})
				end	
			end
		else --no official name
			if teamlink==nil then
				sitelink=wikibase.label(thisteam)  
			else
				sitelink='[['..teamlink..'|'..wikibase.label(thisteam)..']]'
			end
			table.insert(teamoutput,{sitelink, formatdate(teamrider[ii][2]),formatdate(teamrider[ii][3]),teamrider[ii][2],teamrider[ii][4]})
		end
	end
	
return sortteamtable(teamoutput)
end

local function teamsplit(teamrider)
local thisteam 
local teamamateur, teampro={},{}

	for ii, v in pairs(teamrider) do
		thisteam =teamrider[ii][1] 
		local p31 = wikibase.getAllStatements(thisteam, 'P31')
		if p31[1] and p31[1].mainsnak.snaktype == 'value' and p31[2] and p31[2].mainsnak.snaktype == 'value'then --at least two types
			teamid1=p31[1].mainsnak.datavalue.value
			teamid2=p31[2].mainsnak.datavalue.value
			if teamid1==20639848 or teamid1==20652655 or teamid1==26849121  then
				if teamid2==20639848 or teamid2==20652655 or teamid2==26849121 then
					table.insert(teamamateur, teamrider[ii])
				else
					table.insert(teampro, teamrider[ii])
				end
			else 
				table.insert(teampro, teamrider[ii])
			end
		elseif p31[1] and p31[1].mainsnak.snaktype == 'value' then	
			teamid=p31[1].mainsnak.datavalue.value
			if teamid==2466826 or teamid==20639847 or teamid==1756006 or teamid==20653566 then --pro women, pro men, GSIII, continental
				table.insert(teampro, teamrider[ii])
			elseif teamid==20639848 or teamid==20652655 or teamid==26849121  then
				table.insert(teamamateur, teamrider[ii])
			else --by default pro
				table.insert(teampro, teamrider[ii])
			end
		end
	end

return teampro,	teamamateur
end

local function listofTeamTable(localdata)
-- retourne une table de lignes contenant les anciens noms de l'équipe
	local teamrider = listofTeam(localdata.item)
	if not teamrider then
		return nil
	end
	--table sorted
	teampro, teamamateur =teamsplit(teamrider)
end	

local function TeamTable(teamrider, titlesing, titleplural)
--local rows = {}

local rows =  mw.html.create('table')
	if teamrider ~=nil then
		for i, period in pairs(teamrider) do --teamperiodbegin[i]..teamperiodend[i]
			if teamrider[i][2]==teamrider[i][3] then
				if teamrider[i][5]==0 then
					periodtemp=teamrider[i][2]
					nametemp=teamrider[i][1]
				else
					periodtemp=teamrider[i][2]
					nametemp=teamrider[i][1]..' (stagiaire)'
				end
			else
				if teamrider[i][5]==0 then  
					periodtemp=teamrider[i][2]..'-'..teamrider[i][3]
					nametemp=teamrider[i][1]
				else
					periodtemp=teamrider[i][2]..'-'..teamrider[i][3]
					nametemp=teamrider[i][1]..' (stagiaire)'
				end
			end
			local row = mw.html.create('tr')
			:tag('td'):wikitext(periodtemp or ''):done()
			:tag('td'):wikitext(nametemp):done()
			rows:node(row):done()
		end
	end
	
	local title = titleplural

	local temprows=tostring(rows)
	if teamrider ==nil or #teamrider==0 then
		temprows=nil
	else
		if #teamrider == 1 then
			title = titlesing
		end
	end
	return {type = 'table', title = title, rows = {{type = 'row', value = function () return temprows end}}}
end

local function AmateurTeamTable(localdata)
	-- teamamateur called before
	local NotUCInames = localdata['équipes non-UCI']
    local Amateurnames = localdata['équipes amateur']

	if NotUCInames or Amateurnames or localdata['équipes UCI'] or localdata['équipes pro'] then
	   wikidataON=0
	end

	if wikidataON==1 then
		if teamamateur==nil then
			listofTeamTable(localdata)
		end
		local teamrider=listofteamname(teamamateur)
		return TeamTable(teamrider, 'Équipe non-UCI','Équipes non-UCI')
	else
		if NotUCInames then
		    return {type = 'table', title = 'Équipes non-UCI', rows = {{type = 'row', value = teamValue( 'équipes non-UCI', 'années non-UCI' )}}}
		else
			return {type = 'table', title = 'Équipes amateurs', rows = {{type = 'row', value = teamValue( 'équipes amateur', 'années amateur' )}}}
		end
	end
end		

local function ProTeamTable(localdata)
    local UCInames = localdata['équipes UCI']
    local Pronames = localdata['équipes pro']
	
	if wikidataON==1 then
		if teampro==nil then
			listofTeamTable(localdata)
		end
		local teamrider=listofteamname(teampro)
		return TeamTable(teamrider, 'Équipe UCI','Équipes UCI')
	else
		if UCInames then
		    return {type = 'table', title = 'Équipes UCI', rows = {{type = 'row', value = teamValue( 'équipes UCI', 'années UCI' )}}}
		else
			return {type = 'table', title = 'Équipes professionnelles', rows = {{type = 'row', value = teamValue( 'équipes pro', 'années pro' )}}}
		end
	end
end

return 
	{
		maincolor = '#FFDF80',
		parts = {
			person.title('cyclisme'),
			person.mainimage('Article à illustrer Cycliste'),
			{type = 'table', title = 'Informations', rows = {
				person.othernames(),
				person.birth(),
				person.death(),
				person.placeofburial(),
				person.nationality(),
				{type = 'row', label = 'Équipe actuelle', value = 'équipe',
				wikidata = function ( item )
					return presentTeam(item)
					end,
				},
				{
					type = 'row',
					label = 'Spécialité',
					singularlabel = 'Spécialité',
					plurallabel = 'Spécialités',
					value = 'type coureur',
					property = 'P413'
				},
				{type = 'row', label = 'Latéralisation', property = 'P552'},
				--person.appearance(),
				{type = 'row', label = 'Groupe sanguin', property = 'P1853'},
				--person.family(),
				person.awards(),
				},
			},
			AmateurTeamTable,
			ProTeamTable,
			{type = 'table', title = 'Équipes dirigées', rows = {
				{type = 'row', value = teamValue( 'équipes dirigées', 'années direction' )}
			}},
			{type = 'table', title = 'Principales victoires', rows = {
				{type = 'row', value = 'victoires principales'},
			}},
		person.plaque(),
        person.tombe(),
		}
	}