وحدة:Cycling race/stageinfobox

من ويكيبيديا، الموسوعة الحرة
local p = {}

function p.stageinfobox(frame)
	local vvv= ''
	local tab
	local entityId = frame.args[1]
	local lang = mw.language.getContentLanguage()
	local timeofrace
	local a1, b1, c1, a2, b2, a3, b3
	local jerseyWPID, jersey_name
	local t_g = {show=true, name='', icon='', picture='', caption='', course= '', race={}, is_a='', startplace='', endplace='', time='', 
		length='', length_unit='', speed='', speed_unit='', nr_participants_start="", nr_participants_end='', country={}, 
		series_ordinal_previous="", series_ordinal_next="", map='', sectional_view='',previous="", next=""}
	local t_s = {order={'results', 'classement_gen', 'classement_annex'},
		results={show=false, header=15, order = {'first','second','third','winner_fighting','winner_fighting2','cima_coppi','cima_pantani'},
			-- name rider, stageinfobox_translate number, gender, jersey, team name, time, time gap, points, speed, rank
			first={"", 16, "", '', "", "", '', '', '', ''},
			second={"", 17, "", '', '', "", '', '', '', ''},
			third={"", 18, "", '', '', "", '', '', '', ''},
			winner_fighting={"", 19, "", '', '', '', '', '', '', ''},
			winner_fighting2={"", 19, "", '', '', '', '', '', '', ''}, -- two winner_fighting possible
			cima_coppi={"", 40, "", '', '', '', '', '', '', ''},
			cima_pantani={"", 41, "", '', '', '', '', '', '', ''}},
		
		classement_gen={show=false, header=20, order = {"leader", "deuxieme", "troisieme"},
			-- name rider, stageinfobox_translate number, gender, jersey, team name, time, time gap, points, speed, rank
			leader={"", 21, "", '', "", "", '', '', '', ''},
			deuxieme={"", 22, "", '', '', "", '', '', '', ''},
			troisieme={"", 23, "", '', '', "", '', '', '', ''}},

		classement_annex={show=false, header=24, order={"points","montagne","sprints","jeune","super_combatif","combine",
			"stage_volantes","regularite","azzurri_ditalia","fuga_pinarello","equipe","equipe_points"},
			-- name rider, stageinfobox_translate number, gender, jersey, team name, time, time gap, points, speed, rank
			points={"", 25, "", '', "", '', "", '', '', ''},
			montagne={"", 26, "", '', '', '', "", '', '', ''},
			sprints={"", 27, "", '', '', '', "", '', '', ''},
			jeune={"", 28, "", '', '', '', '', '', '', ''},
			super_combatif={"", 29, "", '', "", "", '', '', '', ''},
			combine={"", 30, "", '', '', "", '', '', '', ''},
			stage_volantes={"",31,'','','','','','', '', ''},
			regularite={"",32,'','','','','','', '', ''},
			azzurri_ditalia={"",42,'','','','','','', '', ''},
			fuga_pinarello={"",43,'','','','','','', '', ''},
			equipe={"", 33, "", '', '', "", '', '', '', ''},
			equipe_points={"", 34, "", '', '', '', '', '', '', ''}}
		}

	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 )

	if entity:getLabel('ar') ~= nil then t_g.name = entity:getLabel('ar')
	else t_g.name = entity:getLabel('ar') or entity:getLabel('en') or '' end
	if wiki == 'fr' and t_g.name ~= '' then t_g.name= mw.ustring.gsub(t_g.name, "^(%d+)([re]+)", "%1<sup>%2</sup> ") end
	t_g.name= mw.ustring.gsub(t_g.name, "^(%a)",function (x) return mw.ustring.upper(x) end) 
	
	a1, b1 = pcall(fooA, 'numeric-id', entity, 'P31', 1) -- If stage is a "stage" or a timetrial stage
	if a1 == true and b1~=18131152 then t_g.is_a = 'Q'.. b1 end

	if t_g.is_a=='Q2266066' or t_g.is_a=='Q2348250' or t_g.is_a=='Q485321' then t_g.icon = " [[File:Cycling (track) pictogram.svg|35px]]" 
	else t_g.icon = " [[File:Cycling (road) pictogram.svg|35px]]" end 

	a1, b1 = pcall(fooA, 'value', entity, 'P18', 1) -- picture, legende
	if a1 == true and t_g.picture == "" then 
		local v
		t_g.picture = b1
		if pcall(foo3, entity, "P18", 1, "P2096", 1) then
			for _, v in pairs(entity.claims["P18"]) do
				pcall(function()
					for num=1,#v.qualifiers["P2096"] do	-- find right language
						if v.qualifiers["P2096"][num].datavalue.value.language == lang:getCode() then
							t_g.caption = v.qualifiers["P2096"][num].datavalue.value.text end
					end
				end) 
			end
		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_g.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_g.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_g.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_g.endplace = "Q" .. b1 
	end	

-- 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_g.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_g.time = func_date (b1, 'long') 
		timeofrace = b1
	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_g.nr_participants_start == "" and t_g.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_g.nr_participants_start = string.gsub(v.mainsnak.datavalue.value.amount, "+", "") end
				end
			) then else t_g.nr_participants_start = "" end
			if pcall(function()
				if v.qualifiers["P276"][1].datavalue.value["numeric-id"] == 12769393 then
				t_g.nr_participants_end = string.gsub(v.mainsnak.datavalue.value.amount, "+", "") end
			end
			) then else t_g.nr_participants_end = "" end
		end	
	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_g.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_g.length = b1
		a1, b1, _ = pcall(fooA, 'unit', entity, 'P3157', 1)
		if b1 == "http://www.wikidata.org/entity/Q828224" then
			t_g.length_unit = 'km'
			if  wiki == "ar" then t_g.length_unit = 'كم' end
			if  wiki == "mk" then t_g.length_unit = 'км' end
			if  wiki == "ru" then t_g.length_unit = 'км' end
			if  wiki == "ja" then t_g.length_unit = 'キロメートル' end
		end
	end

	a1, b1, c1 = pcall(fooA, 'amount', entity, 'P2043', 1) 
	if a1 == true and t_g.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_g.length = b1
		a1, b1, _ = pcall(fooA, 'unit', entity, 'P2043', 1)
		if b1 == "http://www.wikidata.org/entity/Q828224" then
			t_g.length_unit = 'km'
			if  wiki == "ar" then t_g.length_unit = 'كم' end
			if  wiki == "mk" then t_g.length_unit = 'км' end
			if  wiki == "ru" then t_g.length_unit = 'км' end
			if  wiki == "ja" then t_g.length_unit = 'キロメートル' end
		end
	end
	-- 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_g.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_g.speed = b1
		a1, b1 = pcall(fooA, 'unit', entity, 'P2052', 1)
		if b1 == "http://www.wikidata.org/entity/Q180154" then
			t_g.speed_unit = 'km/h'
			if  wiki == "da" then t_g.speed_unit = 'km/t' end
			if  wiki == "fo" then t_g.speed_unit = 'km/t' end
			if  wiki == "nl" then t_g.speed_unit = 'km/u' end
			if  wiki == "no" then t_g.speed_unit = 'km/t' end
			if  wiki == "ar" then t_g.speed_unit = 'كم/س' end
			if  wiki == "mk" then t_g.speed_unit = 'км/ч' end
			if  wiki == "ru" then t_g.speed_unit = 'км/ч' end
			if  wiki == "ja" then t_g.speed_unit = 'キロメートル毎時' end			
		end
	end	-- if a1

	a1, b1 = pcall(fooA, 'numeric-id', entity, 'P17', 1) -- This function gives countries where the race take place
	if a1 == true then
		for i, v in pairs(entity.claims["P17"]) do
   			t_g.country[i] = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
   		end
 	end

	a1, b1 = pcall(fooA, 'value', entity, 'P242', 1) -- locator map image
	if a1 == true and t_g.map == "" then t_g.map = b1 end
	a1, b1 = pcall(fooA, 'value', entity, 'P2713', 1) -- sectional view
	if a1 == true and t_g.sectional_view == "" then t_g.sectional_view = b1 end	

	a1, b1 = pcall(fooA, 'numeric-id', entity, 'P155', 1) 
	if a1 == true and t_g.previous == "" then 
		t_g.previous = "Q"..b1 
		a2, b2 = pcall(fooA, 'value', mw.wikibase.getEntity( t_g.previous ), 'P1545', 1)
		if a2 == true then t_g.series_ordinal_previous = b2 end		
	end	
	a1, b1 = pcall(fooA, 'numeric-id', entity, 'P156', 1) 
	if a1 == true and t_g.next == "" then 
		t_g.next = "Q"..b1 
		a2, b2 = pcall(fooA, 'value', mw.wikibase.getEntity( t_g.next ), 'P1545', 1)
		if a2 == true then t_g.series_ordinal_next = b2 end		
	end

	local t_P642 = {[20882747]={'results', 'first'}, [20882748]={'results', 'second'}, [20882749]={'results', 'third'}, [21686770]={'results', 'winner_fighting'},
			[2250962]={'results', 'cima_coppi'}, [10452933]={'results', 'cima_pantani'},
			[20882763]={'classement_gen', 'leader'}, [20882764]={'classement_gen', 'deuxieme'}, [20882765]={'classement_gen', 'troisieme'},
			[20883213]={'classement_annex', 'montagne'}, [20883140]={'classement_annex', 'jeune'}, [20883008]={'classement_annex', 'points'}, 
			[20883329]={'classement_annex', 'sprints'}, [20893984]={'classement_annex', 'super_combatif'}, [20965880]={'classement_annex', 'combine'}, 
			[27104688]={'classement_annex', 'stage_volantes'}, [27104684]={'classement_annex', 'regularite'}, [20882922]={'classement_annex', 'equipe'}, 
			[27104271]={'classement_annex', 'equipe_points'},
			[20882667]={'classement_gen', 'leader'}, [20882668]={'classement_gen', 'deuxieme'}, [20882669]={'classement_gen', 'troisieme'},
			[20883212]={'classement_annex', 'montagne'}, [20883139]={'classement_annex', 'jeune'}, [20883007]={'classement_annex', 'points'}, 
			[20883328]={'classement_annex', 'sprints'}, [20893983]={'classement_annex', 'super_combatif'}, [20893979]={'classement_annex', 'combine'}, 
			[27067359]={'classement_annex', 'stage_volantes'}, [27067170]={'classement_annex', 'regularite'}, 
			[27907747]={'classement_annex', 'azzurri_ditalia'}, [27907748]={'classement_annex', 'azzurri_ditalia'}, 			
			[27907714]={'classement_annex', 'fuga_pinarello'}, [27907715]={'classement_annex', 'fuga_pinarello'}, 
			[20882921]={'classement_annex', 'equipe'}, [27104269]={'classement_annex', 'equipe_points'}
			}

	a1, b1 = pcall(fooA, 'numeric-id', entity, "P1346", 1)
	if a1 == true then
		local deprecated, qual, id, id_team, id_time, id_time_gap, id_speed, id_points_a, id_points_b
		for k, v in pairs(entity.claims["P1346"]) do
			id_team, id_speed, id_time, id_time_gap, id_points_a, id_points_b = '', '', '', '', '', ''
			a, b = pcall(fooC, 'numeric-id', entity, 'P1346', k, 'P54', 1)
			if a == true then id_team = 'Q'..b  end

			if pcall(function() 
				id=v.mainsnak.datavalue.value['numeric-id'] -- id = rider id
				deprecated = entity.claims["P1346"][k].rank
				end) then
			else id=nil end
			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 = 'Q' .. b end
				end

				-- 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 = national_team('Q'..id, timeofrace, entity, 'P1346', k) -- timeofrace -> startOfSeason ?????
						if id_team == nil then id_team = '' end
					end
				end)

		   		pcall(function() -- for property 'race time'
					for _, v in pairs(entity.claims['P1346'][k].qualifiers['P2781']) do
			   			id_time = tonumber(v.datavalue.value.amount)
					end
				end)

	   			pcall(function() -- for property 'time gap'
					for _, v in pairs(entity.claims['P1346'][k].qualifiers['P2911']) do
	   					id_time_gap = tonumber(v.datavalue.value.amount)
					end
				end)

		   		pcall(function() -- for property 'speed'
					for _, v in pairs(entity.claims['P1346'][k].qualifiers['P2052']) do
			   			id_speed = tonumber(v.datavalue.value.amount)
					end
				end)

				pcall(function() -- for property 'points for'
					for _, v in pairs(entity.claims['P1346'][k].qualifiers['P1358']) do
		   				id_points_a = tonumber(v.datavalue.value.amount)
					end
				end)

				pcall(function() -- for property 'number of points/goals/set scored'
					for _, v in pairs(entity.claims['P1346'][k].qualifiers['P1351']) do
		   				id_points_b = tonumber(v.datavalue.value.amount)
					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==21686770 and t_s['results']['winner_fighting'][1] ~= '' then t_P642[qual][2] = 'winner_fighting2' end
							pcall(function() -- inserts WD data into table t_s
								t_s[t_P642[qual][1]][t_P642[qual][2]][1]='Q'..id
								t_s[t_P642[qual][1]][t_P642[qual][2]][5]=id_team
								t_s[t_P642[qual][1]][t_P642[qual][2]][10]=deprecated
								if id_time~='' then t_s[t_P642[qual][1]][t_P642[qual][2]][6]=id_time end
								if id_time_gap~='' then t_s[t_P642[qual][1]][t_P642[qual][2]][7]=id_time_gap end
								if id_points_a~='' then t_s[t_P642[qual][1]][t_P642[qual][2]][8]=id_points_a end
								if id_points_b~='' then t_s[t_P642[qual][1]][t_P642[qual][2]][8]=id_points_b end
								if id_speed~='' then t_s[t_P642[qual][1]][t_P642[qual][2]][9]=id_speed end
							end)
							if qual==27104271 and t_s.classement_annex.equipe_points[1]=='' then t_s.classement_annex.equipe_points[1]='Q'..id end
							if qual==20882922 and t_s.classement_annex.equipe[1]=='' then t_s.classement_annex.equipe[1]='Q'..id end

							a1, b1 = pcall(fooA, 'numeric-id', mw.wikibase.getEntityObject('Q'..id), 'P21', 1)  -- detect gender
							if a1 == true then
								if b1 == 6581097 then t_s[t_P642[qual][1]][t_P642[qual][2]][3] = 'm' -- men
								elseif b1 == 6581072 then t_s[t_P642[qual][1]][t_P642[qual][2]][3] = 'f' -- female
								end
							else t_s[t_P642[qual][1]][t_P642[qual][2]][3] = 'f' -- female
							end
	 					end
					end
				end)
			end
		end
	end
	local rank, deprecated
	local order = {'first', 'second', 'third'}
	local property = 'P2417'
	a1, b1 = pcall(fooA, 'numeric-id', entity, property, 1)
	if a1 == true then
	for k, _ in pairs(entity.claims[property]) do -- look into P2417, stage classification
		for _, v in pairs(entity.claims[property][k].qualifiers['P1352']) do -- for property 'rank'
			rank = tonumber(v.datavalue.value.amount)
			deprecated = entity.claims[property][k].rank
		end
		if rank == 1 or rank == 2 or rank == 3 then
			t_s.results[order[rank]][10] = deprecated
			if t_s.results[order[rank]][1] == '' or t_s.results[order[rank]][10] == 'normal'  then -- name or rider / team		
				t_s.results[order[rank]][1] = 'Q' .. entity.claims[property][k].mainsnak.datavalue.value['numeric-id']
				if t_s.results[order[rank]][7] == '' and t_s.results[order[rank]][6] == '' then
		   			pcall(function()
						for _, v in pairs(entity.claims[property][k].qualifiers['P2911']) do -- for property 'time gap'
		   					t_s.results[order[rank]][7] = tonumber(v.datavalue.value.amount)
						end
					end)
				end
				if t_s.results[order[rank]][7] == '' and t_s.results[order[rank]][6] == '' then
		   			pcall(function()
						for _, v in pairs(entity.claims[property][k].qualifiers['P2781']) do -- for property 'race time'
		   					t_s.results[order[rank]][6] = tonumber(v.datavalue.value.amount)
						end
					end)
				end
				if t_s.results[order[rank]][9] == '' then
	   				pcall(function()
						for _, v in pairs(entity.claims[property][k].qualifiers['P2052']) do -- for property 'speed'
		   					t_s.results[order[rank]][9] = tonumber(v.datavalue.value.amount)
						end
					end)
				end
				a1, b1 = pcall(fooA, 'numeric-id', mw.wikibase.getEntityObject(t_s.results[order[rank]][1]), 'P21', 1)  -- detect gender
				if a1 == true then
					if b1 == 6581097 then t_s.results[order[rank]][3] = 'm' -- men
					elseif b1 == 6581072 then t_s.results[order[rank]][3] = 'f' -- female
					end
				else t_s.results[order[rank]][3] = 'f' -- female
				end
			end
		end
	end
	end
	order = {'leader', 'deuxieme', 'troisieme'}
	property = 'P2321'
	a1, b1 = pcall(fooA, 'numeric-id', entity, property, 1)
	if a1 == true then
	for k, _ in pairs(entity.claims[property]) do -- look into P2321, stage classification
		for _, v in pairs(entity.claims[property][k].qualifiers['P1352']) do -- for property 'rank'
			rank = tonumber(v.datavalue.value.amount)
			deprecated = entity.claims[property][k].rank
		end
		if rank == 1 or rank == 2 or rank == 3 then
			t_s.classement_gen[order[rank]][10] = deprecated
			if t_s.classement_gen[order[rank]][1] == '' or t_s.classement_gen[order[rank]][10] == 'normal'  then -- name or rider / team
				t_s.classement_gen[order[rank]][1] = 'Q' .. entity.claims[property][k].mainsnak.datavalue.value['numeric-id']
				if t_s.classement_gen[order[rank]][7] == '' and t_s.classement_gen[order[rank]][6] == '' then
	   				pcall(function()
						for _, v in pairs(entity.claims[property][k].qualifiers['P2911']) do -- for property 'time gap'
		   					t_s.classement_gen[order[rank]][7] = tonumber(v.datavalue.value.amount)
						end
					end)
				end
				if t_s.classement_gen[order[rank]][7] == '' and t_s.classement_gen[order[rank]][6] == '' then
	   				pcall(function()
						for _, v in pairs(entity.claims[property][k].qualifiers['P2781']) do -- for property 'race time'
		   					t_s.classement_gen[order[rank]][6] = tonumber(v.datavalue.value.amount)
						end
					end)
				end
				if t_s.classement_gen[order[rank]][9] == '' then
	   				pcall(function()
						for _, v in pairs(entity.claims[property][k].qualifiers['P2052']) do -- for property 'speed'
		   					t_s.classement_gen[order[rank]][9] = tonumber(v.datavalue.value.amount)
						end
					end)
				end
				a1, b1 = pcall(fooA, 'numeric-id', mw.wikibase.getEntityObject(t_s.classement_gen[order[rank]][1]), 'P21', 1)  -- detect gender
				if a1 == true then
					if b1 == 6581097 then t_s.classement_gen[order[rank]][3] = 'm' -- men
					elseif b1 == 6581072 then t_s.classement_gen[order[rank]][3] = 'f' -- female
					end
				else t_s.classement_gen[order[rank]][3] = 'f' -- female
				end
			end
		end
	end
	end

    if t_g.length ~= "" then
    	t_g.length, _ = string.gsub(t_g.length, "[+]", "") -- delets "+" sign
    	t_g.length = lang:formatNum(tonumber(t_g.length)) -- formats t_g.length in the way it is done in the local Wiki
    	if wiki == 'fo' then t_g.length = string.gsub(t_g.length, "%.", ",") end
    end
    if t_g.speed ~= "" then
    	t_g.speed, _ = string.gsub(t_g.speed, "[+]", "")
    	t_g.speed = lang:formatNum(tonumber(t_g.speed))
    	if wiki == 'fo' then t_g.speed = string.gsub(t_g.speed, "%.", ",") end
    end

	local a, b
   	local span1, span2 = "<span style='color:#3366CC'>&#x25C0; </span>", "<span style='color:#3366CC'> &#x25B6;</span>"

	if t_g.series_ordinal_previous ~= '' then
		a, _ = string.gsub(t_g.series_ordinal_previous, "%a", "") -- 20, not 20a
		if string.find(t_g.series_ordinal_previous, "%a") then b = string.sub(t_g.series_ordinal_previous, string.find(t_g.series_ordinal_previous, "%a"))
		else b = "" end
		local label_previous = stageLink(t_g.series_ordinal_previous, a, b)
		if t_g.series_ordinal_previous == "0" then label_previous = func_prologue() end
		if mw.wikibase.sitelink( t_g.previous ) ~= nil then
   			local k = transform(t_g.previous,'Q', timeofrace)
			local i, _ = string.find(k, "|")
			local j, _ = string.find(k, "]")
			if i ~= nil and j ~= nil then
				t_g.previous = string.sub(k, 1, i) .. span1 .. label_previous .. string.sub(k, j)
			else t_g.previous = span1 .. label_previous end
		else t_g.previous = span1 .. label_previous end
    else t_g.previous = ''
	end

	if t_g.series_ordinal_next~= '' then
		a, _ = string.gsub(t_g.series_ordinal_next, "%a", "") -- 20, not 20a
		if string.find(t_g.series_ordinal_next, "%a") then b = string.sub(t_g.series_ordinal_next, string.find(t_g.series_ordinal_next, "%a"))
		else b = "" end
		local label_next = stageLink(t_g.series_ordinal_next, a, b)
		if t_g.series_ordinal_next == "0" then label_next = func_prologue() end
		if mw.wikibase.sitelink( t_g.next ) ~= nil then
   			local k = transform(t_g.next,'Q', timeofrace)
			local i, _ = string.find(k, "|")
			local j, _ = string.find(k, "]")
			if i ~= nil and j ~= nil then
				t_g.next = string.sub(k, 1, i) .. label_next.. span2 .. string.sub(k, j)
			else t_g.next = label_next.. span2 end
		else t_g.next = label_next.. span2 end
    else t_g.next = ''
	end

	if t_g.course== '' then
		a1, b1 = pcall(fooA, 'value', entity, 'P1545', 1)
		if a1 == true then
			a, _ = string.gsub(b1, "%a", "") -- 20, not 20a
			if string.find(b1, "%a") then b = string.sub(b1, string.find(b1, "%a")) else b = "" end
			local label = stageLink(b1, a, b)
			if b1 == "0" then label = func_prologue() end
    		t_g.course = label
			a2, b2 = pcall(fooA, 'numeric-id', entity, 'P361', 1)
			if a2 == true then
				t_g.course = t_g.course .. ', '.. WPlink('Q'..b2,'pure')
				--t_g.race = 'Q'..b2
				
				local entity_race = mw.wikibase.getEntity('Q'..b2) 
				a3, b3 = pcall(fooA, 'numeric-id', entity_race, 'P31', 1) 
 				if a3 == true then 
					for k, v in pairs(entity_race.claims["P31"]) do	
						t_g.race[k] = 'Q'..v.mainsnak.datavalue.value["numeric-id"] 
					end
				end
			end
    	end
	end

	if t_s.results.first[1]~='' or t_s.results.second[1]~='' or t_s.results.third[1]~='' or t_s.results.winner_fighting[1]
		then t_s.results.show = true end
	if t_s.classement_gen.leader[1]~='' or t_s.classement_gen.deuxieme[1]~='' or t_s.classement_gen.troisieme[1]~='' 
		then t_s.classement_gen.show = true end
	if t_s.classement_annex.points[1]~='' or t_s.classement_annex.montagne[1]~='' or t_s.classement_annex.sprints[1]~='' or t_s.classement_annex.jeune[1]~='' or 
		t_s.classement_annex.super_combatif[1]~='' or t_s.classement_annex.combine[1]~='' or t_s.classement_annex.stage_volantes[1]~='' or 
		t_s.classement_annex.regularite[1]~='' or t_s.classement_annex.azzurri_ditalia[1]~='' or t_s.classement_annex.fuga_pinarello[1]~='' or 
		t_s.classement_annex.equipe[1]~='' or t_s.classement_annex.equipe_points[1]~='' then t_s.classement_annex.show = true end		

	function stage_icon(x)
 		if x=='Q20646667' then return "[[File:Plainstage.svg|20px]] " end
 		if x=='Q20646670' then return "[[File:Hillystage.svg|20px]] " end
  		if x=='Q20680270' then return "[[File:Mediummountainstage.svg|20px]] " end
  		if x=='Q20646668' then return "[[File:Mountainstage.svg|20px]] " end
  		if x=='Q20679712' then return "[[File:Mountain Time Trial Stage.svg|20px]] " end
		if x=='Q2348250' then return "[[File:Team Time Trial Stage.svg|20px]] " end
   		if x=='Q2266066' then return "[[File:Time Trial.svg|20px]] " end
   		if x=='Q485321'  then return "[[File:Time Trial.svg|20px]] " end
   		return ''
	end

	local width= '320px' -- size standard 320px, special 340px
	if t_s.classement_annex.show == true and (wiki == 'no' or wiki == '..') then width= '340px' end
	
 	tab = "<table border='0' cellpadding='4' cellspacing='0' style='float:left; max-width:"..width.."; 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;'>"
	tab = tab .. "<tr><td colspan='2' style='border-bottom:5px solid white; font-size:175%; background:#FFE7A0; text-align:center'>"
	tab = tab .. "<table style='width:100%'><tr><td>" .. t_g.name .."</td><td>" .. t_g.icon .. "</td></tr></table></td></tr>"

	if t_g.picture ~= "" then tab = tab .. "<tr><td colspan='2' style='text-align:center'>".." [[File:".. t_g.picture .."|center|300px]]</td></tr>" end
	if t_g.picture ~= "" and t_g.caption ~= "" then tab = tab .. "<tr><td colspan='2' style='text-align:center; font-size:80%'>"..t_g.caption.."</td></tr>" end
	if t_g.course~="" or t_g.is_a~="" or t_g.time~="" or t_g.length~="" or t_g.startplace~="" or t_g.endplace~="" then 
		tab=tab.."<tr><td colspan='2' style='background-color:#FFE7A0; text-align:center'>'''"..stageinfobox_translate(1).."'''</td></tr>" end
	if t_g.course ~= "" then tab = tab.."<tr><td>'''"..stageinfobox_translate(2).."'''</td><td>" .. t_g.course .. "</td></tr>" end	
	if t_g.is_a ~= "" then tab = tab.."<tr><td>'''"..stageinfobox_translate(3).."'''</td><td>" ..stage_icon(t_g.is_a).. WPlink(t_g.is_a,'pure').. "</td></tr>" end	
	if t_g.time~="" then tab = tab.."<tr><td>'''"..stageinfobox_translate(4).."'''</td><td>" .. t_g.time.."</td></tr>" end	
	if t_g.length ~= "" then tab = tab.."<tr><td>'''"..stageinfobox_translate(6).."'''</td><td>" .. t_g.length.." "..t_g.length_unit.."</td></tr>" end	
	if t_g.startplace ~= "" then tab = tab.."<tr><td>'''"..stageinfobox_translate(9).."'''</td><td>" .. WPlink(t_g.startplace,'pure').. "</td></tr>" end	
	if t_g.endplace ~= "" then tab = tab.."<tr><td>'''"..stageinfobox_translate(10).."'''</td><td>" .. WPlink(t_g.endplace,'pure').. "</td></tr>" end
	if t_g.country ~= nil then for i=1,#t_g.country do t_g.country[i] = flag(t_g.country[i], timeofrace).." ".. WPlink( t_g.country[i],'pure') end end
	if t_g.country[1] ~= nil then tab = tab.."<tr><td style= 'vertical-align:top'>'''"
		if #t_g.country == 1 then tab = tab..stageinfobox_translate(7).."'''</td><td>" else tab = tab..stageinfobox_translate(8).."'''</td><td>" end
		for i=1,#t_g.country-1 do tab = tab .. t_g.country[i].."<br>" end tab = tab .. t_g.country[#t_g.country].."</td></tr>" end	
	if t_g.nr_participants_start ~= "" then tab = tab.."<tr><td>'''"..stageinfobox_translate(11).."'''</td><td>" .. t_g.nr_participants_start .. "</td></tr>" end
	if t_g.nr_participants_end ~= "" then tab = tab.."<tr><td>'''"..stageinfobox_translate(12).."'''</td><td>" .. t_g.nr_participants_end .. "</td></tr>" end	
	if t_g.speed ~= "" then tab = tab.."<tr><td>'''"..stageinfobox_translate(13).."'''</td><td>" .. t_g.speed .. " "..t_g.speed_unit.."</td></tr>" end		

	for _, value_order in ipairs(t_s.order) do 
		if t_s[value_order].show == true then -- if a section of the stageinfobox should be shown
			tab=tab.."<tr><td colspan='2'>"
			tab=tab.."<table border='0' cellpadding='0' cellspacing='0' style='width:100%'>"
			tab=tab.."<tr><td colspan='3' style='border-bottom:5px solid #fff2cc; background-color:#FFE7A0; text-align:center'>'''"..
				stageinfobox_translate(t_s[value_order].header).."'''</td></tr>"

			for key, value in ipairs(t_s[value_order].order) do
				if t_s[value_order][value][1]~="" then
					a1, jersey_name, jerseyWPID  = jersey(wiki, value, t_g.race, timeofrace)
					if a1~='' then t_s[value_order][value][4] = a1 end
					if t_s[value_order][value][9] ~= '' then 
						t_s[value_order][value][9] = '('.. lang:formatNum(t_s[value_order][value][9])..unit(5)..')'
						if wiki == 'fo' then t_s[value_order][value][9] = string.gsub(t_s[value_order][value][9], "%.", ",") end
					end
					if t_s[value_order][value][8] ~= '' then
						if t_s[value_order][value][8] > 1 then t_s[value_order][value][8] = t_s[value_order][value][8]..unit(7)
						else t_s[value_order][value][8] = t_s[value_order][value][8]..unit(6) end end
					local title, k =mw.ustring.gsub(stageinfobox_translate(t_s[value_order][value][2]), " ", "&nbsp;") 
					if k > 0 then title=mw.ustring.gsub(title, "&nbsp;", "<br>", 1) end --&#32;
					if t_s[value_order][value][5]=="" and t_s[value_order][value][9] =="" then
						tab=tab.."<tr style='vertical-align:top;'><td style='width:1%;background-color:#fff2cc;text-align:right;padding:0 2px 0 2px;white-space:nowrap'>'''"
					else tab=tab.."<tr style='vertical-align:top;'><td rowspan='2' style='width:1%;background-color:#fff2cc;text-align:right;padding:0 2px 0 2px;white-space:nowrap'>'''"
					end
					if value_order~='classement_annex' and t_s[value_order][value][2]~=40 and t_s[value_order][value][2]~=41 then -- Cima Coppi, Cima Pantani with a line break
						if t_s[value_order][value][4] == "" then 
							if (value_order=='results') and (value=='winner_fighting' or value=='winner_fighting2' or value=='cima_coppi' or value=='cima_pantanii') then 
								tab=tab..stageinfobox_translate(t_s[value_order][value][2])
							else tab=tab..number(t_s[value_order][value][3], key, wiki) end
						else 
							if jerseyWPID=='' then tab=tab.."[[File:"..t_s[value_order][value][4].."|20px|"..title.."]]"
							else tab=tab.."[[File:"..t_s[value_order][value][4].."|20px|"..title.."|link="..jerseyWPID.."]]" end
						end
					else 
						if t_s[value_order][value][4] == "" then tab=tab..title
						else 
							if jerseyWPID=='' then 
								if jersey_name ~= '' then tab=tab.."[[File:"..t_s[value_order][value][4].."|20px|"..jersey_name.."]]" .. title
								else tab=tab.."[[File:"..t_s[value_order][value][4].."|20px]]" .. title end
							else tab=tab.."[[File:"..t_s[value_order][value][4].."|20px|link="..jerseyWPID.."]]" .. title end end
					end  
					if t_s[value_order][value][10] == 'deprecated' then 
						tab=tab.."</span>".. "'''</td><td style='padding:0 0.5em 0 0.5em'>".. '<s>' .. transform(t_s[value_order][value][1],'f', timeofrace) .. '</s>'
					else tab=tab.."</span>".. "'''</td><td style='padding:0 0.5em 0 0.5em'>".. transform(t_s[value_order][value][1],'f', timeofrace) end
					tab=tab.."</td><td style='text-align:right;font-size:85%;white-space:nowrap'>" 
					if t_s[value_order][value][6]~='' then tab=tab.. calculate_time(wiki,t_s[value_order][value][6]) end
					if t_s[value_order][value][7]~='' then tab=tab..'+ '.. calculate_time(wiki,t_s[value_order][value][7]) end
					tab= tab.. t_s[value_order][value][8]..  "</td></tr>"
				end

				if t_s[value_order][value][5]~="" and t_s[value_order][value][9] ~="" then -- team row
					tab=tab.."<tr><td colspan='2'>"
					tab = tab.. "<table border='0' cellpadding='0' cellspacing='0' style='width:100%'>"
					tab = tab.. "<tr><td style='width:100%;text-align:right;padding-left:2px'>("..transform(t_s[value_order][value][5],'t', timeofrace)..
						")</td><td style='font-size:85%;vertical-align:top;white-space:nowrap'>"..t_s[value_order][value][9] .. "</td></tr></table>"
				else
					if t_s[value_order][value][5]~="" or t_s[value_order][value][9] ~="" then -- team row
						tab=tab.."<tr><td colspan='2' style='text-align:right;padding-left:2px'>"
						if t_s[value_order][value][5] ~= '' then tab=tab..'('..transform(t_s[value_order][value][5],'t', timeofrace)..')' end
						tab = tab..  '<span style="float:right;font-size:85%;">'.. t_s[value_order][value][9].."</span></td></tr>"
					end					
				end
			end
			tab=tab.. "</table></td></tr>"
		end
	end 

	if t_g.map ~= "" then tab = tab .. "<tr><td colspan='2' style='text-align:center'>".." [[File:".. t_g.map .."|center|300px]]".."</td></tr>" end
	if t_g.sectional_view ~= "" then tab = tab .. "<tr><td colspan='2' style='text-align:center'>".." [[File:".. t_g.sectional_view .."|center|300px]]".."</td></tr>" end	
	tab = tab.."<tr><td>"..t_g.previous.."</td><td style='text-align:right'>" .. t_g.next .. "</td></tr>"
	local s = "d:Wikidata:WikiProject Cycling/Documentation/stageinfobox"
	tab = tab .. "<tr><td colspan='2' style='text-align:right; border-top:3px solid #FFDF80; font-size:75%'>".. --infobox_bottom(1) 
		"[[" .. s .. "|" .. stageinfobox_translate(39).. "]] [[File:Wikidata-logo S.svg|24px|link=d:".. frame.args[1]  .. "]]" 
	tab = tab .. "</span></td></tr>"
	tab= tab.."</table>"
	return tab
end

return p