وحدة:Cycling race/infobox

من أرابيكا، الموسوعة الحرة
اذهب إلى التنقل اذهب إلى البحث
local p = {}

function p.infobox(frame)
	local t = {is_a="", class="", stage="", starttime="", endtime="", time="", date="", dates="", length="", length_unit="", 
	startplace="", endplace="", nr_teams="", nr_participants_start="", nr_participants_end="", speed="", speed_unit="", cost="", cost_unit="", 
	first="", first_team="", second="", second_team="", third="", third_team="", winner_point="", winner_point_team="", winner_mountain="", winner_mountain_team="", 
	winner_sprint="", winner_sprint_team="", winner_young="", winner_young_team="", winner_fighting="", winner_fighting_team="", winner_combination="", 
	winner_combination_team="", winner_metas_volantes="", winner_metas_volantes_team="", winner_regularity="", winner_regularity_team="", 
	winner_fuga_pinarello="", winner_fuga_pinarello_team="", winner_azzurri_ditalia="", winner_azzurri_ditalia_team="", winner_team="", 
	winner_team_points="", previous="", next=""}
	local ref = {stage="", starttime="", endtime="", time="", date="", dates="", length="", nr_participants_start="", nr_participants_end="", 
	speed="", speed_unit="", cost="", first="", second="", third="", winner_point="", winner_mountain="", winner_sprint="", winner_young="", 
	winner_fighting="", winner_metas_volantes="", winner_regularity="", winner_combination="", winner_fuga_pinarello="", winner_azzurri_ditalia="", 
	winner_team="", winner_team_points=""}
	local tt = {icon="", picture="", caption="", name="", nr="", map="" }
	local ttt = {tour={}, country={}}
	local tttt = {special_1a="", special_1b="", special_2a="", special_2b=""}
	
	local entity = ""
	local y, m, d = "", "", ""
	local y2, m2, d2 = "", "", ""
	local a, b, c, k, num, i
	local entityId = frame.args[1]
	local lang = mw.language.getContentLanguage()
	local results = false
	local a1, b1, c1, a2, b2, c2
	local timeofrace

	if type( entityId ) ~= 'string' then error( 'parameter must be a string') end
	if not entityId:match( 'Q%d+' ) then error ( 'parameter must be a valid Wikidata item (ex: Q42)' ) end
	local entity = mw.wikibase.getEntity( entityId )

	local localFrame 
	if string.match(frame:getParent():getTitle(), '%P+') == mw.site.namespaces.Template.name 
	then localFrame = frame:getParent() else localFrame = frame end
	
	if localFrame.args[infobox_translate(2)] then tt.nr = localFrame.args[infobox_translate(2)] end 
	if localFrame.args[infobox_translate(29)] then tt.picture = localFrame.args[infobox_translate(29)] end 
	if localFrame.args[infobox_translate(30)] then tt.caption = localFrame.args[infobox_translate(30)] end 
	if localFrame.args[infobox_translate(31)] then tt.map = localFrame.args[infobox_translate(31)] end 

	if  entity:getLabel('ar') ~= nil then tt.name = entity:getLabel('ar')
	elseif entity:getLabel( 'en' ) ~= nil then tt.name = entity:getLabel( 'en' )
	elseif entity:getLabel( 'fr' ) ~= nil then tt.name = entity:getLabel( 'fr' ).. " (fr)"
	else tt.name = "Wikidata doesn´t know"  
	end

	a, b = pcall(fooA, 'numeric-id', entity, 'P641', 1) 
	if a == true then -- icon for road bicycle racing, Q3609 (road bicycle racing)
		if b == 3609 then tt.icon = " [[File:Cycling (road) pictogram.svg|35px]]" end 
	end
	a, b = pcall(fooA, 'value', entity, 'P18', 1) 
	if a == true and tt.picture == "" then 
		local v
		tt.picture = b
		if pcall(foo3, entity, "P18", 1, "P2096", 1) then
			for _, v in pairs(entity.claims["P18"]) do
				for num=1,#v.qualifiers["P2096"] do
					if v.qualifiers["P2096"][num].datavalue.value.language == lang:getCode() then
					tt.caption = v.qualifiers["P2096"][num].datavalue.value.text end
				end
			end
		end
	end
	
	-- For FR Wiki and Wikidata, exception that permit to display 1er, 2e... for the edition number ; 
	-- for RU Wiki, -й is written after the value of P393
	a, b = pcall(fooA, 'value', entity, 'P393', 1)
	if a == true and tt.nr == "" then
		tt.nr = b .. ". "
		if wiki == "fr" then
			if b == 1  then tt.nr = "1<sup>re</sup> " else tt.nr = b .."<sup>e</sup> " end end
		if wiki == "nl" then tt.nr = b .."e " end
		if wiki == "ru" then tt.nr = b .."-й " end
		if wiki == "eo" then tt.nr = b .."-a " end
		if wiki == "cs" then tt.nr = b ..". " end
		if wiki == "hu" then tt.nr = b .."-ik " end
	end
	-- Class of a cycling race. Class is: 1.UWT, 2.UWT, 1.HC, ... add new classes, no problem
	local class = { "Q22231106", "Q22231107", "Q22231108", "Q22231109", "Q22231110", "Q22231111", "Q22231112",
		"Q22231113", "Q22231114", "Q22231115", "Q22231116", "Q22231117", "Q22231118", "Q22231119", "Q23015458", "Q23005601", "Q23005603" } 
	a, b = pcall(fooA, 'numeric-id', entity, 'P31', 1) -- first value for P31
	c, d = pcall(fooA, 'numeric-id', entity, 'P31', 2) -- second value for P31
	if a == true and c == true then -- for two values
		for i=1,#class do -- #class = number of elements in class
   			if "Q"..b == class[i] then t.class = "c"..b t.is_a = "Q"..d break end
   			if "Q"..d == class[i] then t.class = "c"..d t.is_a = "Q"..b break end  		
   		end
 	else -- if there is only one value, assume it is not a class
		if a == true or c == true then t.is_a = "Q"..b end
	end

	a, b = pcall(fooA, 'numeric-id', entity, 'P361', 1) -- Competition of the cycling race : UCI World Tour 2016, UCI Europe Tour 2016...
	if a == true then --ttt.tour = "Q" .. b end
		local i, v
		for i, v in pairs(entity.claims["P361"]) do
			ttt.tour[i] = "T" .. v.mainsnak.datavalue.value["numeric-id"]
		end
	end

	if t.stage == "" then
		if pcall(function() -- This function count the number of value for P527 to determine the number of stages
			t.stage = table.getn(entity.claims["P527"])
		end
		) then else t.stage = "" end
	end

	a1, b1, c1 = pcall(fooA, 'time', entity, 'P580', 1) 
	if a1 == true and t.starttime == "" then i = 1
		while a1 == true and c1 == 'deprecated' do 
			i = i + 1 
			a1, b1, c1 = pcall(fooA, 'time', entity, 'P580', i) 
		end  
		--b1 = "+2015-07-04T00:00:00Z"
		_, _, y, m, d = string.find(b1, "(%d+)-(%d+)-(%d+)")  
	end
	a2, b2, c2 = pcall(fooA, 'time', entity, 'P582', 1) 
	if a2 == true and t.endtime == "" then i = 1
		while a2 == true and c2 == 'deprecated' do 
			i = i + 1 
			a2, b2, c2 = pcall(fooA, 'time', entity, 'P582', i) 
		end  
		--b2 = "+2015-07-05T00:00:00Z"
		_, _, y2, m2, d2 = string.find(b2, "(%d+)-(%d+)-(%d+)")
	end
	if a1 == true and t.starttime == "" then 
		if y ~= y2 then t.starttime = lang:formatDate( "j F Y", b1 ) 
		elseif m ~= m2 then t.starttime = lang:formatDate( "j F", b1 )
		else t.starttime = lang:formatDate( "j", b1 )
		end
		if wiki == "ar" then 
			if y ~= y2 then t.starttime = lang:formatDate( "d F Y", b1 ) 		
			elseif m ~= m2 then t.starttime = lang:formatDate( "d F", b1 )	
			else t.starttime = lang:formatDate( "d F", b1 )
			end	end		
		if wiki == "br" then 
			if y ~= y2 then t.starttime = lang:formatDate( "j", b1 ) .." a viz ".. lang:formatDate( "F Y", b1 )		
			elseif m ~= m2 then t.starttime = lang:formatDate( "j", b1 ) .." a viz ".. lang:formatDate( "F", b1 )		
			else t.starttime = lang:formatDate( "j", b1 ) .." "	
			end	end	
		if wiki == "ca" or wiki == "es" then 
			if y ~= y2 then t.starttime = lang:formatDate( "j", b1 ) .." de ".. lang:formatDate( "F", b1 ) .." de ".. lang:formatDate( "Y", b1 )
			elseif m ~= m2 then t.starttime = lang:formatDate( "j", b1 ) .." de ".. lang:formatDate( "F", b1 )		
			else t.starttime = lang:formatDate( "j", b1 ) .." "	
			end	end	
		if wiki == "cs" then 
			if y ~= y2 then t.starttime = lang:formatDate( "j. xg Y", b1 )
			elseif m ~= m2 then t.starttime = lang:formatDate( "j. xg", b1 )
			else t.starttime = lang:formatDate( "j", b1 )
			end	end	
		if wiki == "de" or wiki == "da" or wiki == "fi" or wiki == "fo" or wiki == "no" then  
			if y ~= y2 then t.starttime = lang:formatDate( "j. F Y", b1 ) 
			elseif m ~= m2 then t.starttime = lang:formatDate( "j. F", b1 )
			else t.starttime = lang:formatDate( "j.", b1 )
			end	end			
		if wiki == "en" then 
			if y ~= y2 then t.starttime = lang:formatDate( "F d, Y", b1 ) 		
			elseif m ~= m2 then t.starttime = lang:formatDate( "F d", b1 )	
			else t.starttime = lang:formatDate( "F d", b1 )
			end	end			
		if wiki == "eo" then 
			if y ~= y2 then t.starttime = lang:formatDate( "j", b1 ) .."-a de ".. lang:formatDate( "F Y", b1 )		
			elseif m ~= m2 then t.starttime = lang:formatDate( "j", b1 ) .."-a de ".. lang:formatDate( "F", b1 )		
			else t.starttime = lang:formatDate( "j", b1 ) .."-a "	
			end	end	
		if wiki == "eu" then 
			if y ~= y2 then t.starttime = lang:formatDate( "Y", b1 ) ..".eko ".. lang:formatDate( "F", b1 ) .."k ".. lang:formatDate( "j", b1 ) 		
			elseif m ~= m2 then t.starttime = lang:formatDate( "F", b1 ) .."k ".. lang:formatDate( "j", b1 ) 		
			else t.starttime = lang:formatDate( "F", b1 ) .."k ".. lang:formatDate( "j", b1 )
			end	end	
		if wiki == "hu" then 
			if y ~= y2 then t.starttime = lang:formatDate( "Y", b1 ) ..". ".. lang:formatDate( "F j", b1 ) .."." 		
			elseif m ~= m2 then t.starttime = lang:formatDate( "F j", b1 ) .."."
			else t.starttime = lang:formatDate( "F j", b1 ) .."."
			end	end	
		if wiki == "ja" then 
			if y ~= y2 then t.starttime = lang:formatDate( "Y年m月d日", b1 ) 		
			elseif m ~= m2 then t.starttime = lang:formatDate( "Y年m月d日", b1 )	
			else t.starttime = lang:formatDate( "Y年m月d日", b1 )
			end	end			
		if wiki == "lv" then 
			if y ~= y2 then t.starttime = lang:formatDate( "Y. \\g\\a\\d\\a j. F", b1 ) 		
			elseif m ~= m2 then t.starttime = lang:formatDate( "Y. \\g\\a\\d\\a j. F", b1 )	
			else t.starttime = lang:formatDate( "Y. \\g\\a\\d\\a j.", b1 )
			end	end
		if wiki == "pl" then 
			local date_pl = {"stycznia", "lutego", "marca", "kwietnia", "maja", "czerwca", "lipca", "sierpnia", "września", "października", "listopada", "grudnia"}
			if y ~= y2 then t.starttime = lang:formatDate( "j ", b1 ) .. date_pl[tonumber(lang:formatDate( "n", b1 ))] .. lang:formatDate( " Y", b1 ) 
			elseif m ~= m2 then t.starttime = lang:formatDate( "j ", b1 ) .. date_pl[tonumber(lang:formatDate( "n", b1 ))]
			else t.starttime = lang:formatDate( "j", b1 )
			end	end				
	end -- if a1
	if a2 == true and t.endtime == "" then 
		t.endtime = lang:formatDate( "j F Y", b2 ) 
		timeofrace = b2
		if wiki == "ar" then
			if y ~= y2 then t.endtime = lang:formatDate( "d F Y", b2 ) 		
			elseif m ~= m2 then t.endtime = lang:formatDate( "d F Y", b2 )	
			else t.endtime = lang:formatDate( "d، Y", b2 )	
			end	end	
		if wiki == "br" then t.endtime = lang:formatDate( "j", b2 ) .." a viz ".. lang:formatDate( "F Y", b2 ) end
		if wiki == "ca" or wiki == "es" then t.endtime = lang:formatDate( "j", b2 ) .." de ".. lang:formatDate( "F", b2 ) .." de ".. lang:formatDate( "Y", b2 ) end
		if wiki == "cs" then t.endtime = lang:formatDate( "j. xg Y", b2 ) end
		if wiki == "de" or wiki == "da" or wiki == "fi" or wiki == "fo" or wiki == "no" then t.endtime = lang:formatDate( "j. F Y", b2 ) end
		if wiki == "en" then
			if y ~= y2 then t.endtime = lang:formatDate( "F d, Y", b2 ) 		
			elseif m ~= m2 then t.endtime = lang:formatDate( "F d, Y", b2 )	
			else t.endtime = lang:formatDate( "d, Y", b2 )	
			end	end	
		if wiki == "eo" then t.endtime = lang:formatDate( "j", b2 ) .."-a de ".. lang:formatDate( "F Y", b2 ) end		
		if wiki == "eu" then t.endtime = lang:formatDate( "Y", b2 ) ..".eko ".. lang:formatDate( "F", b2 ) .."k ".. lang:formatDate( "j", b2 ) end
		if wiki == "hu" then t.endtime = lang:formatDate( "Y", b2 ) ..". ".. lang:formatDate( "F j", b2 ) .."." end
		if wiki == "ja" then 
			if y ~= y2 then t.endtime = lang:formatDate( "Y年m月d日", b2 ) 		
			elseif m ~= m2 then t.endtime = lang:formatDate( "m月d日", b2 )	
			else t.endtime = lang:formatDate( "d日", b2 )	
			end	end	
		if wiki == "lv" then 
			if y ~= y2 then t.endtime = lang:formatDate( "Y. \\g\\a\\d\\a j. F", b2 ) 		
			elseif m ~= m2 then t.endtime = lang:formatDate( "j. F", b2 )	
			else t.endtime = lang:formatDate( "j. F", b2 )	
			end	end
		if wiki == "pl" then 
			local date_pl = {"stycznia", "lutego", "marca", "kwietnia", "maja", "czerwca", "lipca", "sierpnia", "września", "października", "listopada", "grudnia"}
			t.endtime = lang:formatDate( "j ", b2 ) .. date_pl[tonumber(lang:formatDate( "n", b2 ))] .. lang:formatDate( " Y", b2 ) end				
	end -- if a2

-- This function give a format to dates when P585 (date) is used in a single day race
	a1, b1, c1 = pcall(fooA, 'time', entity, 'P585', 1) 
	if a1 == true and t.time == "" then i = 1
		while a1 == true and c1 == 'deprecated' do 
			i = i + 1 
			a1, b1, c1 = pcall(fooA, 'time', entity, 'P585', i) 
		end  		
		t.time = func_date (b1, 'long')
		timeofrace = b1
	end

	-- This function permits to display an unity (the kilometer) for a distance, with exceptions for macedionan, russian and japanese 
	-- that have other symbols 
	a1, b1, c1 = pcall(fooA, 'amount', entity, 'P3157', 1) 
	if a1 == true and t.length == "" then i = 1
		while a1 == true and c1 == 'deprecated' do 
			i = i + 1 
			a1, b1, c1 = pcall(fooA, 'amount', entity, 'P3157', i) 
		end   
		t.length = b1
		a1, b1, _ = pcall(fooA, 'unit', entity, 'P3157', 1)
		if b1 == "http://www.wikidata.org/entity/Q828224" then
			t.length_unit = 'km'
			if  wiki == "ar" then t.length_unit = 'كم' end
			if  wiki == "mk" then t.length_unit = 'км' end
			if  wiki == "ru" then t.length_unit = 'км' end
			if  wiki == "ja" then t.length_unit = 'キロメートル' end
		end
	end

	a1, b1, c1 = pcall(fooA, 'amount', entity, 'P2043', 1) 
	if a1 == true and t.length == "" then i = 1
		while a1 == true and c1 == 'deprecated' do 
			i = i + 1 
			a1, b1, c1 = pcall(fooA, 'amount', entity, 'P2043', i) 
		end   
		t.length = b1
		a1, b1, _ = pcall(fooA, 'unit', entity, 'P2043', 1)
		if b1 == "http://www.wikidata.org/entity/Q828224" then
			t.length_unit = 'km'
			if  wiki == "ar" then t.length_unit = 'كم' end
			if  wiki == "mk" then t.length_unit = 'км' end
			if  wiki == "ru" then t.length_unit = 'км' end
			if  wiki == "ja" then t.length_unit = 'キロメートル' end
		end
	end

	a, b = pcall(fooA, 'numeric-id', entity, 'P17', 1) -- This function gives countries where the race take place
	if a == true then --and ttt.country == "" then ttt.country = "Q" .. b end
		local i, v
		for i, v in pairs(entity.claims["P17"]) do
   			ttt.country[i] = "C" .. v.mainsnak.datavalue.value["numeric-id"]
   		end
 	end			
	-- Function that give the starting place of a race 
	a1, b1, c1 = pcall(fooA, 'numeric-id', entity, 'P1427', 1) 
	if a1 == true and t.startplace == "" then i = 1
		while a1 == true and c1 == 'deprecated' do 
			i = i + 1 
			a1, b1, c1 = pcall(fooA, 'numeric-id', entity, 'P1427', i) 
		end   
		t.startplace = "Q" .. b1 
	end	
	-- Function that give the finishing place of a race
	a1, b1, c1 = pcall(fooA, 'numeric-id', entity, 'P1444', 1) 
	if a1 == true and t.endplace == "" then i = 1
		while a1 == true and c1 == 'deprecated' do 
			i = i + 1 
			a1, b1, c1 = pcall(fooA, 'numeric-id', entity, 'P1444', i) 
		end   
		t.endplace = "Q" .. b1 
	end	
	-- This function counts the number of value for P1923 (participating teams) and give the number of team that participate at the race
    if pcall(function() 
         t.nr_teams = table.getn(entity.claims["P1923"])
         end        	
      ) then else t.nr_teams = "" end   

	-- Function that give the number of cyclists at the beginning and at the finishing of a race
	a1, b1, c1 = pcall(fooA, 'amount', entity, 'P1132', 1) 
	if a1 == true and t.nr_participants_start == "" and t.nr_participants_end == "" then i = 1
		while a1 == true and c1 == 'deprecated' do 
			i = i + 1 
			a1, b1, c1 = pcall(fooA, 'amount', entity, 'P1132', i) 
		end 
		local v
		for _, v in pairs(entity.claims["P1132"]) do
			if pcall(function()
		   		if v.qualifiers["P276"][1].datavalue.value["numeric-id"] == 529711 then
				t.nr_participants_start = string.gsub(v.mainsnak.datavalue.value.amount, "+", "") end
				end
			) then else t.nr_participants_start = "" end
			if pcall(function()
				if v.qualifiers["P276"][1].datavalue.value["numeric-id"] == 12769393 then
				t.nr_participants_end = string.gsub(v.mainsnak.datavalue.value.amount, "+", "") end
			end
			) then else t.nr_participants_end = "" end
		end	-- for 	
	end -- if a1

	-- This function permits to display an unity (the kilometer per hour) for an average speed, with exceptions for macedionan, russian and japanese 
	-- that have other symbols
	a1, b1, c1 = pcall(fooA, 'amount', entity, 'P2052', 1) 
	if a1 == true and t.speed == "" then i = 1
		while a1 == true and c1 == 'deprecated' do 
			i = i + 1 
			a1, b1, c1 = pcall(fooA, 'amount', entity, 'P2052', i) 
		end 
		t.speed = b1
		a1, b1 = pcall(fooA, 'unit', entity, 'P2052', 1)
		if b1 == "http://www.wikidata.org/entity/Q180154" then
			t.speed_unit = 'km/h'
			if  wiki == "ar" then t.speed_unit = 'كم/س' end
			if  wiki == "da" or wiki == 'fo' or wiki == 'no' then t.speed_unit = 'km/t' end
			if  wiki == "nl" then t.speed_unit = 'km/u' end
			if  wiki == "mk" then t.speed_unit = 'км/ч' end
			if  wiki == "ru" then t.speed_unit = 'км/ч' end
			if  wiki == "ja" then t.speed_unit = 'キロメートル毎時' end			
		end
	end	-- if a1
 	-- This function permits to display an unity for the cost of a cycling race
	a, b = pcall(fooA, 'amount', entity, 'P2130', 1)
	if a == true and t.cost == "" then 
		t.cost = b 
		a, b = pcall(fooA, 'unit', entity, 'P2130', 1)
		if b == "http://www.wikidata.org/entity/Q4916" then t.cost_unit = '€' end	
		if b == "http://www.wikidata.org/entity/Q4917" then t.cost_unit = '$' end	
	end

	a, b = pcall(fooA, 'value', entity, 'P242', 1) -- locator map image
	if a == true and tt.map == "" then tt.map = b end
	a, b = pcall(fooA, 'numeric-id', entity, 'P155', 1) 
	if a == true and t.previous == "" then t.previous = "Q"..b end	
	a, b = pcall(fooA, 'numeric-id', entity, 'P156', 1) 
	if a == true and t.next == "" then t.next = "Q"..b end
	
	local note, qual, id, id_team
	-- By entering a value just below, the Wikipedia added will have Wikidata logos associated with cyclists in the infobox	
	if wiki == "mk" or wiki == "ja" or wiki == "ru" then note = 'q' 
	else note = 'f'
	end 

	-- By entering a value just below, the Wikipedia added will show the team of the winner
	if pcall(foo3, entity, "P1346", 1, "P642", 1) then
		local k, v, vv
		for k, v in pairs(entity.claims["P1346"]) do
			id_team = '' 
			--id_team = 't26973671'
			a, b = pcall(fooC, 'numeric-id', entity, 'P1346', k, 'P54', 1)
			if a == true then id_team = 't'..b end
		
			if pcall(function() id=v.mainsnak.datavalue.value['numeric-id'] end) then else id=nil end -- id = rider id
			if id ~= nil then
				--timeofrace = "+2016-03-00T00:00:00Z" 
				-- function timeStartEnd finds the team of the rider at the time (timeofrace) of the race
				if id_team == '' and timeofrace ~= nil and timeofrace ~= '' then 
					a, b = timeStartEnd('Q'..id, 'P54', 'numeric-id', timeofrace) 
					if a == true and b ~= nil then id_team = 't' .. b end
				end
			end
--			pcall(function() id=v.mainsnak.datavalue.value['numeric-id'] end) -- rider ID
			
			-- looks into race item if the winner has a P54 statement for beeng a member of a national team 
			pcall(function() 			
				for _, vv in pairs(entity.claims["P1346"][k].qualifiers['P54']) do 
					id_team = '2t'.. ' ('.. national_team('Q'..id, timeofrace, entity, 'P1346', k).. ')' 
				end
			end)

			-- looks into race item if the winner has a P642 statement for showing the type of winner (points, mountain, ..)
			pcall(function() 			
				for _, vv in pairs(entity.claims["P1346"][k].qualifiers['P642']) do 
					qual=vv.datavalue.value['numeric-id']

		if id~=nil and qual~=nil and v.rank~='deprecated' then 
			if qual == 20882667 and t.first=="" then t.first=note..id t.first_team=id_team ref.first=references(entity,"P1346",k) results=true end
			if qual == 20882668 and t.second=="" then t.second=note..id t.second_team=id_team ref.second=references(entity,"P1346",k) results=true end
			if qual == 20882669 and t.third=="" then t.third=note..id t.third_team=id_team ref.third=references(entity,"P1346",k) results=true end
			if qual == 20883007 and t.winner_point=="" then t.winner_point=note..id t.winner_point_team=id_team ref.winner_point=references(entity,"P1346",k) results=true end
			if qual == 20883212 and t.winner_mountain=="" then t.winner_mountain=note..id t.winner_mountain_team=id_team ref.winner_mountain=references(entity,"P1346",k) results=true end   			
  			if qual == 20883328 and t.winner_sprint=="" then t.winner_sprint=note..id t.winner_sprint_team=id_team ref.winner_sprint=references(entity,"P1346",k) results=true end
			if qual == 20883139 and t.winner_young=="" then t.winner_young=note..id t.winner_young_team=id_team ref.winner_young=references(entity,"P1346",k) results=true end  
			if qual == 20893983 and t.winner_fighting=="" then t.winner_fighting=note..id t.winner_fighting_team=id_team ref.winner_fighting=references(entity,"P1346",k) results=true end
			if qual == 27067359 and t.winner_metas_volantes=="" then t.winner_metas_volantes=note..id t.winner_metas_volantes_team=id_team ref.winner_metas_volantes=references(entity,"P1346",k) results=true end
			if qual == 27067170 and t.winner_regularity=="" then t.winner_regularity=note..id t.winner_regularity_team=id_team ref.winner_regularity=references(entity,"P1346",k) results=true end
			if qual == 20893979 and t.winner_combination=="" then t.winner_combination=note..id t.winner_combination_team=id_team ref.winner_combination=references(entity,"P1346",k) results=true end
			if qual == 27907715 and t.winner_fuga_pinarello=="" then t.winner_fuga_pinarello=note..id t.winner_fuga_pinarello_team=id_team ref.winner_fuga_pinarello=references(entity,"P1346",k) results=true end
			if qual == 27907747 and t.winner_azzurri_ditalia=="" then t.winner_azzurri_ditalia=note..id t.winner_azzurri_ditalia_team=id_team ref.winner_azzurri_ditalia=references(entity,"P1346",k) results=true end
			if qual == 20882921 and t.winner_team == "" then
				a, b = pcall(fooA, 'numeric-id', mw.wikibase.getEntity( 'Q'..id ), 'P31', 1)
				if a == true and (b == 20738667 or b == 23726798) then -- test if 'national cycling team U23' or 'national cycling team'
					t.winner_team = '2t' .." "..national_team('Q'..id, timeofrace, entity, 'P1346', k)
				else
					t.winner_team = note .. id ref.winner_team=references(entity,"P1346",k) results = true
				end
			end
			if qual == 27104269 and t.winner_team_points == "" then t.winner_team_points = note .. id ref.winner_team_points=references(entity,"P1346",k) results = true end
 		end
				end
			end) 
		end	-- for
	end  -- end if pcall

	-- code for references
	local list = {} -- list[1][1] = value from ref.first, list [1][2] = 'first'
	local k
	for k in pairs(ref) do 
		if ref[k]~='' then table.insert(list, {ref[k], k}) end 
	end
	local w, ww 	
	num = #list
	for w=num,1,-1 do
		for ww=1,w do
			if w ~= ww then 
				if list[w][1] == list[ww][1] then list[w][1] = frame:extensionTag{name='ref', args={name=entityId..ww}} break end	
			else list[w][1] = frame:extensionTag{name='ref', content=list[w][1], args={name=entityId..ww}} end 
		end
	end
	for i=1,num do 
		ref[ list[i][2] ] = list[i][1] --list[num+1-i][1] 
	end

	-- transform Q/q-number into flag / WPlink / WDlink
	local k
	local team = true
	local wd = " [[File:Wikidata-logo S.svg|24px|link=d:"	

	for k in pairs(ttt) do -- "^C" ttt.country, "^T" ttt.tour
		if ttt[k][1]~=nil and string.find(ttt[k][1], "^C") then for i=1,#ttt[k] do ttt[k][i] = string.gsub(ttt[k][i], "C", "Q") ttt[k][i] = flag(ttt[k][i], timeofrace).." ".. WPlink( ttt[k][i] ) end end
		if ttt[k][1]~=nil and string.find(ttt[k][1], "^T") then for i=1,#ttt[k] do ttt[k][i] = string.gsub(ttt[k][i], "T", "Q") ttt[k][i] = WPlink( ttt[k][i], 'race') end end
	end	
	for k in pairs(t) do 
		if string.find(t[k], "^Q") then --t[k], k = startplace, endplace, ... find variables with an item as value
			if pcall(foo1, t[k]) then t[k] = WPlink( t[k] )	end
		end
		if string.find(t[k], "^q") then -- find variables with an item as value (in section Résultats)
			t[k] = mw.ustring.upper( t[k] )
			--look into P27 (country of citizenship) to get the Qnumber of that country
			a, b = timeStartEnd(t[k], "P27", 'numeric-id', timeofrace)
			if a == true and pcall(foo1, t[k]) then t[k] = flag('Q'..b, timeofrace).." "..WPlink( t[k] )..ref[k]..wd..t[k].."]]"
			else
				--look into P17 (country) to get the Qnumber of that country
				a, b = pcall(fooA, 'numeric-id', mw.wikibase.getEntity( t[k] ), 'P17', 1)
				if a == true and pcall(foo1, t[k]) then t[k] = flag("Q"..b, timeofrace).." "..WPlink( t[k] )..ref[k]..wd..t[k].."]]" end
			end
		end
		if string.find(t[k], "^f") then -- no WDitem, frWP
			t[k] = string.gsub(t[k], "f", "Q")
			--look into P27 (country of citizenship) to get the Qnumber of that country
			a, b = timeStartEnd(t[k], "P27", 'numeric-id', timeofrace)
			if a == true and pcall(foo1, t[k]) then t[k] = flag('Q'..b, timeofrace).." "..WPlink( t[k] )..ref[k]
			else	
				--look into P17 (country) to get the Qnumber of that country
				a, b = pcall(fooA, 'numeric-id', mw.wikibase.getEntity( t[k] ), 'P17', 1)
				if a == true and pcall(foo1, t[k]) then t[k] = flag("Q"..b, timeofrace).." "..WPlink( t[k], 'team', timeofrace )..ref[k] else t[k] = WPlink( t[k], 'team', timeofrace )..ref[k] end
			end
		end	
		if string.find(t[k], "^c") then -- find "class"-items, first asci = "c"
			t[k] = string.gsub(t[k], "c", "Q")
			if pcall(foo1, t[k]) then t[k] = WPlink( t[k], "Q22348500" ) end
		end
		if string.find(t[k], "^t") then 
			t[k] = string.gsub(t[k], "t", "Q")
			--timeofrace = "+2008-04-09T00:00:00Z"
			if pcall(foo1, t[k]) then t[k] = " (" .. WPlink( t[k], 'team', timeofrace ) .. ")" end
		end
		if string.find(t[k], "^2t") then t[k] = string.gsub(t[k], "2t", "") end		
	end

    --local nl ="&#10;"
    -- local tooltip = infobox_bottom(2) .. nl .. mw.text.encode( property(1) .. ' (P393)' ) .. nl .. mw.text.encode( property(2) ..' (P17)')
    
    if t.length ~= "" then 
    	t.length, _ = string.gsub(t.length, "[+]", "") -- delets "+" sign from t.length 
    	t.length = lang:formatNum(tonumber(t.length)) -- formats t.length in the way it is done in the local Wiki
    	if wiki == 'fo' then t.length = string.gsub(t.length, "%.", ",") end
    end    
    if t.speed ~= "" then 
    	t.speed, _ = string.gsub(t.speed, "[+]", "")
    	t.speed = lang:formatNum(tonumber(t.speed))
    	if wiki == 'fo' then t.speed = string.gsub(t.speed, "%.", ",") end
    end
    if t.cost ~= "" then 
    	t.cost, _ = string.gsub(t.cost, "[+]", "")
    	t.cost = lang:formatNum(tonumber(t.cost))
    	if wiki == 'fo' then t.cost = string.gsub(t.cost, "%.", ",") end
    end
    
   	local span1, span2 = "<span style='color:#3366CC'>&#x25C0; </span>", "<span style='color:#3366CC'> &#x25B6;</span>"
    local k = t.previous
	local i, _ = string.find(k, "|")
	local j, _ = string.find(k, "]")
	if i ~= nil and j ~= nil then 
		t.previous = string.sub(k, 1, i) .. span1 .. (string.match(string.sub(k, i+1, j-1), "%d%d%d%d") or '') .. string.sub(k, j) 
	else 
		if string.find(k, "%d%d%d%d") == nil then t.previous = '' else t.previous = string.sub(k, string.find(k, "%d%d%d%d")) end
		if t.previous ~= "" then t.previous = span1 .. t.previous end 
	end

	k = t.next
	i, _ = string.find(k, "|")
	j, _ = string.find(k, "]")
	if i ~= nil and j ~= nil then 
		t.next = string.sub(k, 1, i) .. (string.match(string.sub(k, i+1, j-1), "%d%d%d%d") or '') .. span2 .. string.sub(k,j) 
	else 
		if string.find(k, "%d%d%d%d") == nil then t.next = "" else t.next = string.sub(k, string.find(k, "%d%d%d%d")) end
		if t.next ~= "" then t.next = t.next .. span2 end 
	end

	tt.caption = string.gsub(tt.caption, "{{(%w+)}}", function (n) return frame:expandTemplate{ title = n }  end)
	-- for overwriting Wikidata content with data in Wikipedia articles. numbers are from function infobox_translate
	local z = {stage=5, date=6, dates=7, length=8, startplace=11, endplace=12, nr_teams=13, nr_participants_start=14, nr_participants_end=15, 
	speed=16, cost=17, first=19, second=20, third=21, winner_point=22, winner_mountain=23, winner_sprint=24, winner_young=25, winner_fighting=26, 
	winner_combination=27, winner_team=28, winner_team_points=37, winner_metas_volantes=35, winner_regularity=36, winner_fuga_pinarello=38,
	winner_azzurri_ditalia=39}
	
	local i, k, s
	for i,k in pairs(z) do
		s = localFrame.args[mw.ustring.gsub(infobox_translate(k), "&#173;", "")] -- filter soft hyphen out
		if s~="" and s~=nil then
			t[i] = s
			if i=="length" then t.length_unit="" end
			if i=="speed" then t.speed_unit="" end
			if i=="cost" then t.cost_unit="" end
		else
			if k>=19 and k<=27 then t[i] = t[i].. t[i..'_team'] end
			if k>=35 and k<=36 then t[i] = t[i].. t[i..'_team'] end
			if k>=38 and k<=39 then t[i] = t[i].. t[i..'_team'] end
		end
	end

	if localFrame.args[infobox_translate(32)]~="" and localFrame.args[infobox_translate(32)]~=nil then
	i, k = string.find(localFrame.args[infobox_translate(32)] , ":")
		tttt.special_1a = string.sub(localFrame.args[infobox_translate(32)], 1, i-1)
		tttt.special_1b = string.sub(localFrame.args[infobox_translate(32)], k+1)
	end
	if localFrame.args[infobox_translate(33)]~="" and localFrame.args[infobox_translate(33)]~=nil then
	i, k = string.find(localFrame.args[infobox_translate(33)] , ":")
		tttt.special_2a = string.sub(localFrame.args[infobox_translate(33)], 1, i-1)
		tttt.special_2b = string.sub(localFrame.args[infobox_translate(33)], k+1)
	end

	if wiki == "fi" then
		if t.starttime ~= "" then t.starttime = mw.ustring.gsub(t.starttime, "%a+", "%0ta") end
		if t.endtime ~= "" then t.endtime = mw.ustring.gsub(t.endtime, "%a+", "%0ta") end
		if t.time ~= "" then t.time = mw.ustring.gsub(t.time, "%a+", "%0ta") end
	end

 	tab = "<table cellpadding='4' cellspacing='0' style='float:left; width:300px; border:1px solid rgb(200,200,200);"
    tab = tab .. "margin:0 0 0.5em 0.5em; background-color:rgb(255, 255, 255); color:black; padding:5px; clear:left; "
    tab = tab .. "text-align:right; font-size:85%; line-height:1.6em;'>"
	if wiki == "eo" then tab = "<table style='width:23em; border:1px solid rgb(200,200,200);"
    	tab = tab .. "margin:0 0 0.5em 0.5em; background-color:rgb(255, 255, 255); color:black; padding:5px;"
    	tab = tab .. "text-align:right; font-size:85%; line-height:1.6em;' class='infobox' >"
    end
	tab = tab .. "<tr><td colspan='2' style='border-bottom:5px solid white; font-size:175%; background:#FFDF80; text-align:center'>"
	tab = tab .. "<table style='width:100%'><tr><td>" .. tt.name .."</td><td>" .. tt.icon .. "</td></tr></table></td></tr>"
	if tt.picture ~= "" then tab = tab .. "<tr><td colspan='2' style='text-align:center'>".." [[File:".. tt.picture .."|center|300px]]</td></tr>" end
	if tt.picture ~= "" and tt.caption ~= "" then tab = tab .. "<tr><td colspan='2' style='text-align:center; font-size:80%'>"..tt.caption.."</td></tr>" end
	tab = tab .. "<tr><td colspan='2' style='background-color:#FFDF80; text-align:center'>'''"..infobox_translate(1).."'''</td></tr>"
	if tt.nr ~= "" and t.is_a ~= "" then tab = tab.."<tr><td style='width:40%'>'''"..infobox_translate(2).."'''</td><td>" .. tt.nr .. " " .. t.is_a .. "</td></tr>" end
	if ttt.tour[1] ~= nil then tab = tab.."<tr><td style= 'vertical-align:top'>'''"
		if #ttt.tour == 1 then tab = tab..infobox_translate(3).."'''</td><td>" else tab = tab..infobox_translate(4).."'''</td><td>" end	
		for i=1,#ttt.tour do tab = tab .. ttt.tour[i] if i == 1 then tab = tab .. " " ..t.class end tab = tab .. "<br>" end
		tab = tab.."</td></tr>" end		
	if t.stage ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(5).."'''</td><td>" .. t.stage.."</td></tr>" end	
	if (t.starttime~="" or t.endtime~="") and t.date=="" and t.dates=="" then tab = tab.."<tr><td>'''"..infobox_translate(7).."'''</td><td>" .. t.starttime.." – " ..t.endtime .."</td></tr>" end
	--if t.endtime ~= "" then tab = tab.."<tr><td>".."Date de fin".."</td><td>" .. t.endtime .. "</td></tr>" end	
	if t.time~="" and t.date=="" and t.dates=="" then tab = tab.."<tr><td>'''"..infobox_translate(6).."'''</td><td>" .. t.time.."</td></tr>" end	
	if t.date~="" then tab = tab.."<tr><td>'''"..infobox_translate(6).."'''</td><td>" .. t.date.."</td></tr>" end
	if t.dates~="" then tab = tab.."<tr><td>'''"..infobox_translate(7).."'''</td><td>" .. t.dates.."</td></tr>" end	
	if t.length ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(8).."'''</td><td>" .. t.length.." "..t.length_unit.."</td></tr>" end	
	if ttt.country[1] ~= nil then tab = tab.."<tr><td style= 'vertical-align:top'>'''"
		if #ttt.country == 1 then tab = tab..infobox_translate(9).."'''</td><td>" else tab = tab..infobox_translate(10).."'''</td><td>" end
		for i=1,#ttt.country-1 do tab = tab .. ttt.country[i].."<br>" end tab = tab .. ttt.country[#ttt.country].."</td></tr>" end	
	if t.startplace ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(11).."'''</td><td>" .. t.startplace .. "</td></tr>" end	
	if t.endplace ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(12).."'''</td><td>" .. t.endplace .. "</td></tr>" end
	if t.nr_teams ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(13).."'''</td><td>" .. t.nr_teams .. "</td></tr>" end
	if t.nr_participants_start ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(14).."'''</td><td>" .. t.nr_participants_start .. "</td></tr>" end
	if t.nr_participants_end ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(15).."'''</td><td>" .. t.nr_participants_end .. "</td></tr>" end	
	if t.speed ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(16).."'''</td><td>" .. t.speed .. " "..t.speed_unit.."</td></tr>" end		
	if t.cost ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(17).."'''</td><td>" .. t.cost .. " "..t.cost_unit.. "</td></tr>" end	
	if tttt.special_1a ~= "" then tab = tab.."<tr><td>'''"..tttt.special_1a.."'''</td><td>" .. tttt.special_1b.. "</td></tr>" end	
	if tttt.special_2a ~= "" then tab = tab.."<tr><td>'''"..tttt.special_2a.."'''</td><td>" .. tttt.special_2b.. "</td></tr>" end
	
	if results then tab = tab.."<tr><td colspan='2' style='border-bottom:5px solid white; background-color:#FFDF80; text-align:center'>'''"..infobox_translate(18).."'''</td></tr>" end
	if t.first ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(19).."'''</td><td>" .. t.first .. "</td></tr>" end
	if t.second ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(20).."'''</td><td>" .. t.second .. "</td></tr>" end
	if t.third ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(21).."'''</td><td>" .. t.third .. "</td></tr>" end
	if t.winner_point ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(22).."'''</td><td>" .. t.winner_point .. "</td></tr>" end
	if t.winner_mountain ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(23).."'''</td><td>" .. t.winner_mountain .. "</td></tr>" end
	if t.winner_sprint ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(24).."'''</td><td>" .. t.winner_sprint .. "</td></tr>" end
	if t.winner_young ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(25).."'''</td><td>" .. t.winner_young .. "</td></tr>" end
	if t.winner_fighting ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(26).."'''</td><td>" .. t.winner_fighting .. "</td></tr>" end
	if t.winner_metas_volantes ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(35).."'''</td><td>" .. t.winner_metas_volantes .. "</td></tr>" end
	if t.winner_regularity ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(36).."'''</td><td>" .. t.winner_regularity .. "</td></tr>" end
	if t.winner_fuga_pinarello ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(38).."'''</td><td>" .. t.winner_fuga_pinarello .. "</td></tr>" end
	if t.winner_azzurri_ditalia ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(39).."'''</td><td>" .. t.winner_azzurri_ditalia .. "</td></tr>" end
	if t.winner_combination ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(27).."'''</td><td>" .. t.winner_combination .. "</td></tr>" end
	if t.winner_team ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(28).."'''</td><td>" .. t.winner_team .. "</td></tr>" end
	if t.winner_team_points ~= "" then tab = tab.."<tr><td>'''"..infobox_translate(37).."'''</td><td>" .. t.winner_team_points .. "</td></tr>" end
	if tt.map ~= "" then tab = tab .. "<tr><td colspan='2' style='text-align:center'>".." [[File:".. tt.map .."|center|300px]]".."</td></tr>" end

	tab = tab.."<tr><td style='text-align:right'>"..t.previous.."</td><td style='text-align:left'>" .. t.next .. "</td></tr>"
	--tab = tab.."<tr><td>"..t.previous.."</td><td style='text-align:right'>" .. t.next .. "</td></tr>"
	local s = "d:Wikidata:WikiProject Cycling/Documentation/infobox"
	tab = tab .. "<tr><td colspan='2' style='text-align:left; border-top:3px solid #FFDF80; font-size:75%'>" .. "[[" .. s .. "|" .. infobox_translate(34) .. "]] [[File:Wikidata-logo S.svg|24px|link=d:".. frame.args[1]  .. "]]" --infobox_bottom(1) 
	--tab = tab .. "<span style='float:right' title='".. tooltip .. "'>[[File:High-contrast-help-browser.svg|10px|right|link=]]"	
	tab = tab .. "</span></td></tr>"
	tab= tab.."</table>"
	return tab
end

return p