User:Psemdel

From Wikidata
Jump to navigation Jump to search
function UCIcalendar_translate(wiki, x) 
	local l10nDef = { }	

	l10nDef["fr"] = {"Calendrier UCI", "Date", "Course", "Pays", "Classe", "Vainqueur", "Deuxième", "Troisième"}
	l10nDef["en"] = {"UCI Calendar", "Date", "Race", "Country", "Class", "Winner",  "Second", "Third"}
	l10nDef["de"] = {"UCI Kalender", "Datum", "Rennen", "Staat", "Klasse", "Sieger", "Zweiter", "Dritter"}
	l10nDef["da"] = {"","Dato", "Løb", "Land", "Klasse", "Vinder","Toer", "Treer"}
	l10nDef["es"] = {"", "Fecha", "Carrera", "País", "Clase", "Ganador","Segundo", "Tercero"}
	l10nDef["ja"] = {"", "開催日", "レース", "国", "国", "勝者","準優勝", "3位"}
	l10nDef["lv"] = {"",  "Datums", "Sacensības", "Valsts", "Kategorija", "Uzvarētājs","Otrais", "Trešais"}
	l10nDef["mk"] = {"",  "Датум", "Трка", "Земја", "Класа", "Победник", "Второпласиран", "Третопласиран"}
	l10nDef["no"] = {"",  "Dato", "Ritt", "Land", "Klasse", "Vinner","Andreplass", "Tredjeplass"}
	l10nDef["ru"] = {"",  "Дата", "Гонка", "Страна", "Класс", "Победитель","Второй", "Третий"}
	
	local l10n = l10nDef[wiki]
	if not l10n then l10n = l10nDef["fr"] end  -- default
	return l10n[x]	
end 

function p.womenucicalendar(frame)
	----- function to display UCI women calendar of one year ----
	----- based on victories function -----
	----- author: psemdel -----
	local onlywinner, year, headertemp, startofseason

	if frame.args[1] ~= nil then 
		year=	string.gsub(frame.args[1], "%c", "") 
	else
		year = "2018"
	end
	if  year == "2018" then calendarID="Q47005682"
	elseif  year == "2017" then calendarID="Q27765666" 
	elseif  year == "2016" then calendarID="Q22696468"
	elseif  year == "2015" then calendarID="Q18348936" 
	elseif  year == "2014" then calendarID="Q15831496" 	
	elseif  year == "2013" then calendarID="Q6425932"
	elseif  year == "2012" then calendarID="Q2466796" 
	elseif  year == "2011" then calendarID="Q2466792" 
	elseif  year == "2010" then calendarID="Q2933831" 
	elseif  year == "2009" then calendarID="Q2933830" 
	elseif  year == "2008" then calendarID="Q2933828" 
	elseif  year == "2007" then calendarID="Q3650627" 	
	elseif  year == "2006" then calendarID="Q16154659" 	
	end	--else nil

	if frame.args[2] ~= nil then
		onlywinner =tonumber(frame.args[2])
	else
		onlywinner=1
	end

	if onlywinner==1 then
		headertemp={2, 3, 4, 5, 6}
	else
		headertemp={2, 3, 4, 5, 6, 7, 8}
	end

	local s = {
		header_function = UCIcalendar_translate, -- translations are in function victories_translate
		header_1 = 1, -- translation 1 in function victories_translate is printed in the upper part of the table header
		
		header_2 = headertemp,-- translations 2, 3, 4, 5, 6 in function victories_translate are printed in this order 
										-- in the lower part of the table header. The second value 3 in {4, 3} tells where the icon will go.
		data_type = {'date', 'race', 'country', 'class', 'rider'},
		country_column = 3,
		flag_column = 2, 
		data_sort_type = {'', 'unsortable', '', '', '','',''}, -- see https://meta.wikimedia.org/wiki/Help:Sorting
		item = calendarID,
		property = 'P527',
		no_country = {},
		only_winner = onlywinner, --display only the winner or 1st,2nd and 3rd
		error_message = 0
	}
	return tableA(s, womenucicalendar_main(s))
end

function womenucicalendar_main(s)--Display the UCI women calendar of one year, function similar to listofwinners
	local calendarID=s.item
	local h = {}
	local country = true
	local sortkey, sortkey_class
	local tBody = ''
	local tableBody, sortindex = {}, {}
	local a1, b1, c1
	local timeofrace
    
    ------- Declaration of the classes ------
    
	-- 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" } 
	local class_2x = {"Q22231107", "Q23005603", "Q22231115", "Q22231109", "Q22231112", "Q22231113", "Q22231117"} --multi stage races
	
	-- 1.UWT, 2.UWT, 1.HC, 2.HC, 1.1, 1.2, 2.1, 2.2, 1.Ncup, 2.Ncup, 1.2U, 2.2U, CC, CN, CDM, 1.WWT, 2.WWT
	-- CC, CDM, 
	-- 1.UWT, 2.UWT, 1.WWT, 2.WWT, 2.Ncup, 1.Ncup, 2.HC, 1.HC, 2.1, 1.1, 2.2, 2.2U, 1.2, 1.2U, CN
	-- https://fr.wikipedia.org/wiki/Projet:Cyclisme/Usages#Victoires
	local class_sort={["Q22231106"]='01', ["Q22231107"]='01', ["Q23005601"]='01', ["Q23005603"]='01', ["Q22231115"]='01', -- 1.UWT, 2.UWT, 1.WWT, 2.WWT, 2.Ncup
		["Q22231114"]='02', ["Q22231109"]='03', ["Q22231108"]='04', ["Q22231112"]='05', ["Q22231110"]='06', -- 1.Ncup, 2.HC, 1.HC, 2.1, 1.1
		["Q22231113"]='07', ["Q22231117"]='07', ["Q22231111"]='08', ["Q22231116"]='08', ["Q22231119"]='09', --2.2, 2.2U, 1.2, 1.2U, CN
		
		["Q22231118"]='13', ["Q23015458"]='15' } 
	
	local stages = {'Q18131152', 'Q20646667', 'Q20646670', 'Q20680270', 'Q20646668', 'Q20679712', 'Q2348250', 'Q2266066', 'Q485321'}
	
	-- UCI Europe Tour,..., UCI ProTour, UCI World Calendar, UCI WorldTour, UCI Women’s WorldTour, UCI Women's Road World Rankings
	local UCI_Circuits = {'Q1194340', 'Q1063423', 'Q1063430', 'Q268357', 'Q1039648', 'Q1329578', 'Q12270097', 'Q635366', 'Q21075974', 'Q1693153'}

	-- Displayed classes for UCI women calendar
	local displayed_class = {"Q22231109","Q22231110","Q22231111","Q22231112","Q22231113";"Q22231118"}

	----- Test if everything is fine -----
	_, _, s.item = string.find(s.item, "(%w+)")
	local entity = mw.wikibase.getEntityObject( s.item )
	
	pcall(function() test = entity.claims[s.property][1].mainsnak.snaktype end)
	if test == 'somevalue' or test == 'novalue' then s.error_message = 2 return '' end	
	
	if pcall(function() test = entity.claims[s.property][1].mainsnak.datavalue.value['numeric-id'] end)
	then else s.error_message = 1  return ''
	end
	
	for _, value in pairs(s.no_country) do -- get data if country should be printed in this wiki
	   	if value == wiki then country = false end
	end
	
	--------- Definition local functions ------
	function fn_date(h,entity_race)
		local i
		sortkey=''
		for _,property in ipairs({"P585","P582", "P580"}) do 
			-- give time dates, when property 'point in time', 'start time' or 'end time' is used
			a1, b1, c1 = pcall(fooA, 'time', entity_race, property, 1)
			if a1 == true and h['time'] == '' then i = 1 
				while a1 == true and c1 == 'deprecated' do
					i = i + 1
					a1, b1, c1 = pcall(fooA, 'time', entity_race, property, i)
				end
				local _, _, y, m, d = string.find(b1, "(%d+)%p(%d+)%p(%d+)")
				if m ~= '00' and d =='00' then sortkey = y..m..'32' h['time'] = mw.language.getContentLanguage():formatDate("M", '+'..y.."-"..m.."-31")
				elseif m == '00' and d =='00' then sortkey = y..'12'..'32' h['time'] = ''
				else sortkey = y..m..d  h['time'] = func_date (b1, 'small')
				end
				timeofrace = b1
				break
			end
	  	end
	return '<td data-sort-value="'..sortkey..  '" style="text-align:right;padding:0 0.5em">'.. h['time'].. '</td>'
	end
	
	local function fn2_country()
	a,b = timeStartEnd(h['raceID'], "P17", 'numeric-id', timeofrace)
	h['countryID'] = 'Q'..b 
		if available_list == true and country == true then
			if type( translations.list ) == "function" then h['countryName'] = translations.list('Q'..b) h['countryID'] = 'Q'..b end
			if h['countryName'] == '' then
				local country = mw.wikibase.getEntityObject( 'Q'..b )
				h['countryName'] = country:getLabel() or country:getLabel('en') .. ' (en)'
			end
		end
	--no stage in UCI calendar
	end

	local function fn_country(h)
		local str
		if available_list==true and country == true then
			fn2_country()
			if h['countryID'] ~= '' and h['countryName'] ~= '' then str = '<td style="text-align:' .. textalign .. ';padding:0 0.5em">'..flag(h['countryID'], timeofrace).. ' '.. h['countryName'].. '</td>'
			elseif h['countryID'] == '' and h['countryName'] ~= '' then str = '<td style="text-align:' .. textalign .. ';padding:0 0.5em">'.. '   '.. h['countryName'].. '</td>'
			elseif h['countryID'] ~= '' and h['countryName'] == '' then str = '<td style="text-align:' .. textalign .. ';padding:0 0.5em">'..flag(h['countryID'], timeofrace).. '</td>'
			elseif h['countryID'] ~= '' and h['countryName'] ~= '' then str = '<td style="text-align:' .. textalign .. ';padding:0 0.5em">'.. '   '.. '</td>'		
			end
		else str = ''
		end
		return str
	end

	local function fn_race(h)
		local str, a, b, a1, b1, a2, b2, a3, b3
		local label_x = ''
		local entity_x1
		local Sitelink_x, Sitelink_x0, Sitelink_x1, Sitelink_x2 = '', '', ''
		local entity_race = mw.wikibase.getEntityObject( h['raceID'] )
		local entity_type, entity_num = '', 0 
		local level = -1
		local link
		local multi_stage = false
		a, b = pcall(fooA, 'numeric-id', entity_race, 'P31', 1)
		if a == true then
			-- test for stage item
			for i=1,#stages do
				if "Q"..b == stages[i] then entity_type = 'stage item' end -- stage article
			end

			-- test for 'no stage item' - item
			a1, b1 = pcall(fooA, 'numeric-id', entity_race, 'P31', 1) -- first value for P31
			a2, b2 = pcall(fooA, 'numeric-id', entity_race, 'P31', 2) -- second value for P31
			if a1 == true and a2 == true then -- for two values
				if "Q"..b1 == "Q22231119" or "Q"..b1 == "Q23015458" then entity_num = 2 entity_type = 'championship' end
				if "Q"..b2 == "Q22231119" or "Q"..b2 == "Q23015458" then entity_num = 1 entity_type = 'championship' end					
				if entity_type == '' then
					for i=1,#class do -- #class = number of elements in class
						if "Q"..b1 == class[i] then 
							for ii=1,#class_2x do
   								if "Q"..b1 == class_2x[ii] then multi_stage = "Q"..b1 break end 
   							end
   							entity_num = 2 entity_type = 'no stage item' break
   						end
						if "Q"..b2 == class[i] then
							for ii=1,#class_2x do
   								if "Q"..b2 == class_2x[ii] then multi_stage = "Q"..b2 break end
   							end
   							entity_num = 1 entity_type = 'no stage item' break
   						end
					end
				end
			end

			-- else A
			if entity_type == '' and a1 == true and a2 == false then -- for one value
				entity_type = 'else A' 
			end
			-- else B

		end

		if entity_type == 'else A' then
			local entity_l
			a1, b1 = pcall(fooA, 'text', entity_race, 'P1448', 1) -- show the "official name"
			if a1 == true then	
				local lang_WD, text_WD
				for _,v in pairs(entity_race.claims["P1448"]) do
					lang_WD = v.mainsnak.datavalue.value.language
					text_WD = v.mainsnak.datavalue.value.text
					if lang_WD == wiki or lang_WD == 'en' or lang_WD == 'fr' then label_x =text_WD break end
				end 			
			end		

			if label_x == '' then 
				a1, b1 = pcall(fooA, 'numeric-id', entity_race, 'P31', 1)
				if a1 == true then 
					entity_l = mw.wikibase.getEntityObject( 'Q'..b1 )
					label_x = entity_l:getLabel( ) or
					entity_l:getLabel( 'en' ) or
					entity_l:getLabel( 'de' ) or
					entity_l:getLabel( 'fr' ) or ''	
				
				end
			end
			if entity_l~= nil then Sitelink_x1 = entity_l:getSitelink(wiki..'wiki')	end		
			if entity_race~= nil then Sitelink_x0 = entity_race:getSitelink(wiki..'wiki') end
			if Sitelink_x0 ~= nil then Sitelink_x = Sitelink_x0	level = 0				
			elseif Sitelink_x1 ~= nil then Sitelink_x = Sitelink_x1 level = 1
			end
		end

		if entity_type == 'no stage item' then 
			a1, b1 = pcall(fooA, 'numeric-id', entity_race, 'P31', entity_num)
			if a1 == true then 
				entity_l = mw.wikibase.getEntityObject( 'Q'..b1 )
				label_x = entity_l:getLabel( ) or
					entity_l:getLabel( 'en' ) or
					entity_l:getLabel( 'de' ) or
					entity_l:getLabel( 'fr' ) or ''	
			end
			if entity_l~= nil then Sitelink_x1 = entity_l:getSitelink(wiki..'wiki')	end		
			if entity_race~= nil then Sitelink_x0 = entity_race:getSitelink(wiki..'wiki') end
			if Sitelink_x0 ~= nil then Sitelink_x = Sitelink_x0	level = 0				
			elseif Sitelink_x1 ~= nil then Sitelink_x = Sitelink_x1 level = 1
			end
		end

		if entity_type == 'championship' then 
			a1, b1 = pcall(fooA, 'numeric-id', entity_race, 'P31', entity_num)
			if a1 == true then
				entity_l = mw.wikibase.getEntityObject( 'Q'..b1 )
				a2, b2 = pcall(fooA, 'text', entity_l, 'P2561', 1) -- show the "name"
				if a2 == true then
					local lang_WD, text_WD
					for _,v in pairs(entity_l.claims["P2561"]) do
						lang_WD = v.mainsnak.datavalue.value.language
						text_WD = v.mainsnak.datavalue.value.text
						if wiki == lang_WD then label_x =text_WD break end
					end
				end
				if label_x == '' then						
					label_x = entity_l:getLabel( ) or
					entity_l:getLabel( 'en' ) or
					entity_l:getLabel( 'de' ) or
					entity_l:getLabel( 'fr' ) or ''	
				end
			end

			a1, b1 = pcall(fooA, 'numeric-id', entity_l, 'P361', 1)
			if a1 == true then 
				entity_x = mw.wikibase.getEntityObject( 'Q'..b1 )
				if entity_x ~= nil then 
					Sitelink_x = entity_x:getSitelink(wiki..'wiki')
					if Sitelink_x~=nil then link = '[['..Sitelink_x..'|'.. label_x.. ']]' else link = label_x end						
					if wiki == 'ar' then 
						Sitelink_x = entity_x:getSitelink('arwiki')
						if Sitelink_x~=nil then link = '[['..Sitelink_x..'|'.. label_x.. ']]' 
						else link = mw.getCurrentFrame():expandTemplate{ title = 'Ill-WD2', args = {id='Q'..b1,en='true'} }
						end 
					end				
				end
			end
		end

		if entity_type == 'stage item' then
			local entity_l 
			a1, b1 = pcall(fooA, 'numeric-id', entity_race, 'P361', 1)
			if a1 == true then 
				entity_x1 = mw.wikibase.getEntityObject( 'Q'..b1 )
				a2, b2 = pcall(fooA, 'numeric-id', entity_x1, 'P31', 1)
					if a2 == true then
						local is_class = false
						for i=1,#class do 
   							if "Q"..b2 == class[i] then is_class = true break end
   						end
						if is_class == false then entity_l = mw.wikibase.getEntityObject( 'Q'..b2 ) 
						else
							a3, b3 = pcall(fooA, 'numeric-id', "Q"..b2, 'P31', 2)
							if a3 == true then entity_l = mw.wikibase.getEntityObject( 'Q'..b3 ) end
						end 
					end	
					label_x = entity_l:getLabel( ) or
						entity_l:getLabel( 'en' ) or
						entity_l:getLabel( 'de' ) or
						entity_l:getLabel( 'fr' ) or ''
			end
			entity_stage = mw.wikibase.getEntityObject( 'Q18131152' )
			entity_prolog = mw.wikibase.getEntityObject( 'Q485321' )			

			local comma = ", "			
			if wiki == 'fr' then 
				for _,v in ipairs({"^Trois", "^Quatre", "^Boucles"}) do  -- ^ match only at the beginning
					local i, _ = mw.ustring.find(label_x , v)
					if i ~= nil then comma = " des " end
				end
				for _,v in ipairs({"^Triptyque", "^Tour", "^Grand Prix", "^Circuit", "^Mémorial", "^Trophée"}) do
					local i, _ = mw.ustring.find(label_x , v)
					if i ~= nil then comma = " du " end
				end
				for _,v in ipairs({"^Ronde", "^Semaine", "^Classica", "^Flèche", "^Course", "^Classique"}) do
					local i, _ = mw.ustring.find(label_x , v)
					if i ~= nil then comma = " de la " end
				end
				for _,v in ipairs({"^Étoile"}) do
					local i, _ = mw.ustring.find(label_x , v)
					if i ~= nil then comma = " de l'" end
				end
			end
		
			a, b = pcall(fooA, 'value', entity_race, 'P1545', 1)		
			if a == false then b = '?' end
			if b ~= '0' then
				local l10nDef = {["fr"]="étape", ["en"]="stage", ["ar"]="مرحلة", ["br"]="Tennad", ["ca"]="etapa", ["cs"]="etapa", ["de"]="Etappe", ["da"]="etape", ["eo"]="Etapo",
				["es"]="etapa", ["eu"]="Etapa", ["fi"]="Etappi", ["fo"]="teinur", ["hu"]="Szakasz", ["it"]="Tappa", ["ja"]="ステージ", ["la"]="Statio", ["lb"]="etapp",
				["lv"]="Posms", ["mk"]="Етапа", ["nl"]="Etappe", ["no"]="etappe", ["pl"]="Etap", ["pt"]="Etapa", ["ro"]="Etapa", ["ru"]="Этап", ["sk"]="Etapa",
				["sv"]="Etapp" }
				local stage_label = l10nDef[wiki]
				if stage_label == nil 
					then 
						stage_label = entity_stage:getLabel(wiki) or entity_stage:getLabel('fr') 
						if wiki == "ar" then stage_label = entity_stage:getLabel(wiki) or entity_stage:getLabel('en')  end
				end
				local substage = ' '
				local i,j = string.find(b, "%a+")
				local k,l = string.find(b, "%d+")
				local value1, value2
				value2 = string.sub(b, k, l)
				if i ~= nil then value1 = string.sub(b, i, j) end
				if value1 ~= nil then substage = value1..' ' b = value2 end

				if entity_l~= nil then Sitelink_x2 = entity_l:getSitelink(wiki..'wiki') end
				if entity_x1~=nil then Sitelink_x1 = entity_x1:getSitelink(wiki..'wiki') end
				if entity_race~=nil then Sitelink_x0 = entity_race:getSitelink(wiki..'wiki') end
				if Sitelink_x0 ~= nil then Sitelink_x = Sitelink_x0	level = 0
				elseif Sitelink_x1 ~= nil then Sitelink_x = Sitelink_x1 level = 1
				elseif Sitelink_x2 ~= nil then Sitelink_x = Sitelink_x2 level = 2
				end

				if wiki=="de" or wiki=="da" or wiki=="fo" or wiki=="no" then --label_x = label_x.. ', '.. b.. '. '.. stage_label
					if level == 1 or level == 2 then link = '[['..Sitelink_x..'|'.. label_x.. ']]' ..', '.. b.. '. '.. substage.. stage_label end
					if level == 0 then link = '[['..Sitelink_x.. '|'.. label_x.. ', '.. b.. '. '.. substage.. stage_label.. ']]' end
					if level == -1 then link = label_x.. ', '.. b.. '. '.. substage.. stage_label end					
				elseif wiki=='en' or wiki=='br' then label_x = label_x.. ', '.. stage_label.. ' '.. b
				
				elseif wiki=='ar' then 
					label_x = label_x  .. '، ' .. stage_label .. ' '.. b
					if entity_l~= nil then Sitelink_x2 = (entity_l:getSitelink('arwiki') or entity_l:getSitelink('enwiki')) end
					if entity_x1~=nil then Sitelink_x1 = (entity_x1:getSitelink('arwiki') or entity_x1:getSitelink('enwiki')) end
					if entity_race~=nil then Sitelink_x0 = (entity_race:getSitelink('arwiki') or entity_race:getSitelink('enwiki'))  end

				elseif wiki == 'fr' then
					if level == 1 or level == 2 then
						if b == '1' then link = b.. '<sup>re</sup>'.. substage.. stage_label.. comma.. '[[:fr:'..Sitelink_x..'#1re.C2.A0.C3.A9tape'..'|'.. label_x.. ']]' 
						else link = b.. '<sup>e</sup>'.. substage.. stage_label.. comma.. '[[:fr:'..Sitelink_x..'#'.. b.. 'e.C2.A0.C3.A9tape'..'|'.. label_x..']]' 
						end
					end
					if level == 0 then 
						if b == '1' then link = '[[:fr:'..Sitelink_x.. '|'.. b.. '<sup>re</sup>'.. substage.. stage_label.. comma.. label_x.. ']]' 
						else link = '[[:fr:'..Sitelink_x.. '|'.. b.. '<sup>e</sup>'.. substage.. stage_label.. comma.. label_x..']]' 
						end
					end
					if level == -1 then
						if b == '1' then link = b.. '<sup>re</sup>'.. substage.. stage_label.. comma.. label_x
						else link = b.. '<sup>e</sup>'.. substage.. stage_label.. comma.. label_x
						end						
					end
					
				elseif wiki == "lb" then
					if level == 1 or level == 2 then
						if b == '1' then link = b.. '<sup>re</sup>'.. substage.. stage_label.. comma.. '[[:'..wiki..':'..Sitelink_x..'#1re.C2.A0.C3.A9tape'..'|'.. label_x.. ']]' 
						else link = b.. '<sup>e</sup>'.. substage.. stage_label.. comma.. '[[:'..wiki..':'..Sitelink_x..'#'.. b.. 'e.C2.A0.C3.A9tape'..'|'.. label_x..']]' 
						end
					end
					if level == 0 then 
						if b == '1' then link = '[[:'..wiki..':'..Sitelink_x.. '|'.. b.. '<sup>re</sup>'.. substage.. stage_label.. comma.. label_x.. ']]' 
						else link = '[[:'..wiki..':'..Sitelink_x.. '|'.. b.. '<sup>e</sup>'.. substage.. stage_label.. comma.. label_x..']]' 
						end
					end
					if level == -1 then
						if b == '1' then link = b.. '<sup>re</sup>'.. substage.. stage_label.. comma.. label_x
						else link = b.. '<sup>e</sup>'.. substage.. stage_label.. comma.. label_x
						end
					end
				elseif wiki=="ca" then label_x = b.. 'a '.. stage_label.. ' del '.. label_x
				elseif wiki=="es" then label_x = b.. "<sup>a</sup> ".. stage_label.. ', '.. label_x
				else label_x = stage_label.. ' '.. b..', '.. label_x 
				end
			else -- Prolog
				if entity_l~= nil then Sitelink_x2 = entity_l:getSitelink(wiki..'wiki') end
				if entity_x1~=nil then Sitelink_x1 = entity_x1:getSitelink(wiki..'wiki') end
				if entity_race~=nil then Sitelink_x0 = entity_race:getSitelink(wiki..'wiki') end
				if Sitelink_x0 ~= nil then Sitelink_x = Sitelink_x0	level = 0
				elseif Sitelink_x1 ~= nil then Sitelink_x = Sitelink_x1 level = 1
				elseif Sitelink_x2 ~= nil then Sitelink_x = Sitelink_x2 level = 2
				end
				
				local l10nDef = { ["fr"]="Prologue", ["ar"]="المقدمة", ["en"]="Prologue", ["br"]="Prolog", ["ca"]="Pròleg", ["cs"]="Prolog", ["de"]="Prolog", ["da"]="Prolog", ["eo"]="Prologo",
				["es"]="Prólogo", ["eu"]="Aitzinetapa", ["fi"]="Prologi", ["fo"]="Forteinur", ["hu"]="Prolog", ["it"]="Prologo", ["ja"]="プロローグ", ["la"]="Incipit prologus", ["lb"]="Prolog", ["lv"]="Prologs",
				["mk"]="Пролог",  ["nl"]="Proloog", ["no"]="Prolog", ["pl"]="Prologu", ["pt"]="Prólogo", ["ro"]="Prolog", ["ru"]="Пролог", ["sk"]="Prológ", ["sv"]="Prolog" }
				local prolog_label 
				prolog_label = l10nDef[wiki]
				if prolog_label == nil then
					prolog_label = entity_prolog:getLabel() or entity_prolog:getLabel('en') or entity_prolog:getLabel('fr') or
					entity_prolog:getLabel('de')
					prolog_label = mw.ustring.gsub(prolog_label, "^(%a)",function (x) return mw.ustring.upper(x) end)
				end

				if wiki=="de" or wiki=="da" or wiki=="fo" or wiki=="no" then 
					-- label_x = label_x.. ', '.. prolog_label
					if level >= 0 then link = '[['..Sitelink_x.. '|'.. label_x.. ']]'.. ', '.. prolog_label end
				elseif wiki == 'en' then label_x = label_x.. ', '.. prolog_label
				elseif wiki == 'ar' then label_x = label_x.. '، '.. prolog_label
				elseif wiki == 'fr' then
					if level > 0 then link = prolog_label.. comma.. '[[:fr:'..Sitelink_x..'#'..prolog_label.. '|'.. label_x.. ']]' end
					if level == 0 then link = '[[:fr:'..Sitelink_x..'#'..prolog_label.. '|'.. prolog_label.. comma.. label_x.. ']]' end
					if level == -1 then link = prolog_label.. comma.. label_x end
				elseif wiki == 'lb' then
					if level > 0 then link = prolog_label.. comma.. '[[:'..wiki..':'..Sitelink_x..'#'..prolog_label.. '|'.. label_x.. ']]' end
					if level == 0 then link = '[[:'..wiki..':'..Sitelink_x..'#'..prolog_label.. '|'.. prolog_label.. comma.. label_x.. ']]' end
					if level == -1 then link = prolog_label.. comma.. label_x end
				else label_x = prolog_label.. ', '.. label_x 
				end
			end
		end

		if level == -1 and link == nil then
			local Sitelink = ''
			local entity_k = mw.wikibase.getEntityObject( h['raceID'] )
			Sitelink = entity_k:getSitelink(wiki..'wiki')
			if Sitelink ~= '' and Sitelink ~= nil then				
				link = '[['..Sitelink..'|'..label_x..']]'
			end
		end
	mw.log(multi_stage, entity_type)
		if level >= 0 and link == nil and Sitelink_x ~= '' then
			--change for calendar, no need for "general classification,..."
			if onwikidata == true then link = '[[:fr:'..Sitelink_x..'|'..label_x..']]'
			else 
				link = '[['..Sitelink_x..'|'..label_x..']]'
			end
		end
		if link == nil then link = label_x end
	
		if available_list==true and country == true then str =  '<td style="text-align:'.. textalign ..';padding:0 0.5em">'..link.. '</td>'
		else
			fn2_country()
			if h['countryID'] ~= '' then str = '<td style="text-align:'.. textalign ..';padding:0 0.5em">'.. flag(h['countryID'], timeofrace).. ' '.. link.. '</td>'
			else str = '<td style="text-align:'.. textalign ..';padding:0 2.3em">'..link.. '</td>'
			end
		end		
	return str
	end	--end fn
	
	local function fn_classtester(h)
		local entity_race = mw.wikibase.getEntityObject( h['raceID'] )
		local display=false
		local a, b, c, d

		a, b = pcall(fooA, 'numeric-id', entity_race, 'P31', 1) -- first value for P31
		c, d = pcall(fooA, 'numeric-id', entity_race, '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 == displayed_class[i] then display=true break end
				if "Q"..d == displayed_class[i] then display=true break end
			end
		elseif  a == true then
			for i=1,#class do -- #class = number of elements in class
				if "Q"..b == displayed_class[i] then display=true break end
			end
		elseif c==true then
			for i=1,#class do -- #class = number of elements in class
				if "Q"..d == displayed_class[i] then display=true break end
			end	
		end
		return display
	end

	local function fn_class(h)
		local entity_race = mw.wikibase.getEntityObject( h['raceID'] )
		local entity_type, entity_num = '', 0 
		local class_text = ''
		local link = ''
		local Sitelink
		sortkey_class = ''
		
		a, b = pcall(fooA, 'numeric-id', entity_race, 'P31', 1)
		if a == true then
			-- test for stage item
			for i=1,#stages do
				if "Q"..b == stages[i] then entity_type = 'stage item' end
			end
		end		

		if entity_type == 'stage item' then 
			local a1, b1
			a1, b1 = pcall(fooA, 'numeric-id', entity_race, 'P361', 1)
			if a1 == true then
				local a, b, c, d
				local entity = mw.wikibase.getEntityObject( 'Q'..b1 )
				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"..d == class[i] then class_text = mw.wikibase.label('Q'..d) sortkey_class = class_sort['Q'..d] break end
	   				end -- for i
				end -- if a
			end
			link = class_text
			if link ~= '' then 
				a, b = pcall(fooA, 'numeric-id', entity_race, 'P361', 1) -- first value for P361
				if a == true then 
					entity_II = mw.wikibase.getEntityObject('Q'..b)
					c, d = pcall(fooA, 'numeric-id', entity_II, 'P361', 1) -- first value for P361
					if c == true then entity_circuit = mw.wikibase.getEntityObject('Q'..d) end
				end
			end
		else -- not a stage item
			local a, b, c, d, e, f
			a, b = pcall(fooA, 'numeric-id', entity_race, 'P31', 1) -- first value for P31
			c, d = pcall(fooA, 'numeric-id', entity_race, '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"..d == class[i] then class_text = mw.wikibase.label('Q'..d) sortkey_class = class_sort['Q'..d] break end
				end
			end
		
			link = class_text
			if link ~= '' then 
				a, b = pcall(fooA, 'numeric-id', entity_race, 'P361', 1) -- first value for P361
				if a == true then entity_circuit = mw.wikibase.getEntityObject('Q'..b) end
			end
		end
	
		if link ~= '' then 
			e, f = pcall(fooA, 'numeric-id', entity_circuit, 'P31', 1)
			if e == true then 
				for i=1,#UCI_Circuits do -- abcd
					if "Q"..f == UCI_Circuits[i] then
						if onwikidata == true then 
							Sitelink = entity_circuit:getSitelink('frwiki')
							if Sitelink~= nil then link = '[[:fr:'..Sitelink..'|'..link..']]' end
						else
							Sitelink = entity_circuit:getSitelink(wiki..'wiki')
							if Sitelink~=nil then link = '[['..Sitelink..'|'..link..']]' end
						end
						break 
					end
				end
			end
		end
		return '<td data-sort-value="'..sortkey_class.. '" style="text-align:center;padding:0 0.5em">'..link.. '</td>'
	end--end fn class
	
	local function fn_rider(h,entity_race,order,displayteam)
		local qual 
		local winner= ''
		local winner_flag=''
		
		local checkstage=false
		local propertyID
		
		if order==1 or order==nil then
			--checkstage=true
			propertyID=20882667
		elseif order==2 then
			propertyID=20882668
		else
			propertyID=20882669
		end

		a, b = pcall(fooC, 'numeric-id', entity_race, 'P1346', 1, 'P642', 1)
		if a == true then 
			winner = '' winner_flag = ''
			for k, v in ipairs(entity_race.claims["P1346"]) do
				for kk, vv in ipairs(v.qualifiers['P642']) do 
					if vv.datavalue.value['numeric-id'] == propertyID then -- overall winner general classification
						winner = 'Q'.. v.mainsnak.datavalue.value['numeric-id']
					end
					if checkstage==true then
						if vv.datavalue.value['numeric-id'] == 20882747 then  -- stagewinner
							if winner == '' then winner = 'Q'.. v.mainsnak.datavalue.value['numeric-id'] end
						end
					end
				end
			end
			winner_flag = ''
			local a, b = timeStartEnd(winner, "P27", 'numeric-id', timeofrace)
		   	if a == true then 
		   		winner_flag = flag('Q'..b, timeofrace).. ' '		
			end
		end
	
		if winner ~= '' then
			local winner_team = false
			local entity_winner = mw.wikibase.getEntityObject( winner )
			a, b = pcall(fooA, 'numeric-id', entity_winner, 'P31', 1) -- looking if winner is a person
			if a == true and b ~= 5 then winner_team = true end			
			
			if winner_team == false then 
				if displayteam==false or displayteam==nil then
					return '<td style="text-align:'.. textalign ..';padding:0 0.5em">'..winner_flag.. WPlink(winner).. '</td>' 
				else
					local team
					a, b = timeStartEnd(winner, 'P54', 'numeric-id', timeofrace)  --winner  'Q4505812'
					if a == true then
						--teamOftheWinner = mw.wikibase.getEntity( 'Q'..b )
				--		g.team[rank] = national_team:getLabel() or g.country[rank].. s
						team = WPlink('Q'..b, 'team', timeofrace)
						return '<td style="text-align:'.. textalign ..';padding:0 0.5em">'..winner_flag.. WPlink(winner).." ("..team..")".. '</td>' 
					else	
						return '<td style="text-align:'.. textalign ..';padding:0 0.5em">'..winner_flag.. WPlink(winner).. '</td>' 
					end
				end
			else 
				winner_flag = ''
				local a, b = timeStartEnd(winner, "P17", 'numeric-id', timeofrace) -- flag of team
			   	if a == true then 
			   		winner_flag = flag('Q'..b, timeofrace).. ' '		
				end			
				return '<td style="text-align:'.. textalign ..';padding:0 0.5em">'..winner_flag ..WPlink(winner, 'team', timeofrace).. '</td>' 
			end
		else 
			return '<td style="text-align:'.. textalign ..';padding:0 0.5em">'..''.. '</td>'
		end
	end
	
	----- Begin of the main part of the code
	if calendarID ~= nil then
		local entity = mw.wikibase.getEntity(calendarID)
		local WDlink_on
		local WPcontent = {}
		
		local num = 0
		if pcall(fooA, 'numeric-id', entity, 'P527', 1) == true then 
			for k, v1 in pairs(entity.claims['P527']) do 
				--For all races in the UCI calendar--
				h['time'] = '' 	h['countryID'] = '' h['countryName'] = ''
				h['raceID'] = 'Q'.. v1.mainsnak.datavalue.value['numeric-id'] 
				h['item1ID'] = h['raceID']	h['item2ID'] = '' h['item3ID'] = ''
				local entity_race = mw.wikibase.getEntityObject( h['raceID'] )
				
				---- Create a row ----
				tBody = '<tr style= "line-height: 1.8em; padding: 5px;">'
				if fn_classtester(h)==true then
					if s.only_winner==0 then
						for k,v in ipairs({fn_date(h,entity_race),fn_race(h), fn_country(h), fn_class(h), fn_rider(h,entity_race,1,true), fn_rider(h,entity_race,2,true), fn_rider(h,entity_race,3,true)}) do
							tBody = tBody..v
						end
					else
						for k,v in ipairs({fn_date(h,entity_race),fn_race(h), fn_country(h), fn_class(h), fn_rider(h,entity_race,1,true)}) do
							tBody = tBody..v
						end
					end
					tBody = tBody.. '</tr>'
					---- Add the row to the table
					table.insert(sortindex, sortkey)
					table.insert(tableBody, tBody)
				end
			end
		end -- for k, v1
	return table.concat (tableBody) 
	end -- no calendarUCI
end