Documentation[créer] [purger]
--functions accessible from other module

local p = {}
flag_table = {}
wiki = string.match(mw.site.server, "%a+")
if wiki == "www" then
	wiki = "fr"
	onwikidata = true
else
	onwikidata = false	
end

function p.fooA (mode,entity,property,var)
	local snak	
	if mode=="value" then snak=entity.claims[property][var].mainsnak.datavalue.value 
	else snak=entity.claims[property][var].mainsnak.datavalue.value[mode]
	end
	return snak, entity.claims[property][var].rank
end

function p.fooB (mode, entity_var, property, var1, var2, ref, var3)
	local snak 
	if mode == 'value' then snak = entity_var.claims[property][var1].references[var2].snaks[ref][var3].datavalue.value 
	else snak = entity_var.claims[property][var1].references[var2].snaks[ref][var3].datavalue.value[mode]
	end
	return snak
end

function p.fooC (mode, entity_var, property, var1, quali, var2)
	local snak
	if mode == 'value' then snak = entity_var.claims[property][var1].references[var2].snaks[ref][var3].datavalue.value 
	else snak = entity_var.claims[property][var1].qualifiers[quali][var2].datavalue.value[mode]
	end
	return snak
end

function p.foo1 (arg)
	local entity = mw.wikibase.getEntity( arg ) 
end

function p.foo2 (entity_var, property, var)
	local snak = entity_var.claims[property][var].mainsnak.datavalue.value.amount 
end

function p.foo3 (entity_var, property, var1, quali, var2) 
	local snak = entity_var.claims[property][var1].qualifiers[quali][var2].datavalue.value.amount 
end

function p.black_list(wiki, Label)
	-- List of Wikipedia articles with the same lemma as the non existing rider article. Those lemmas are printed as text "black" in the tables, 
	-- not "blue" or "red". This way there will be no false wikilinks at the WhatLinksHere entry. List should be updated maybe once a year. 
	local black_list = {}
	if wiki=='de' then black_list={ ["Ryan Anderson"]=true, ["Chris Butler"]=true, ["Josef Černý"]=true, ["Brad Evans"]=true, ["Robert Fontaine"]=true, 
		["Carlos Giménez"]=true, ["George Harper"]=true, ["Mathias Jørgensen"]=true, ["Luis Lemus"]=true, ["David Lozano"]=true, ["Jan Maas"]=true, 
		["James McLaughlin"]=true, ["Nikolaj Michajlow"]=true, ["Antonio Molina"]=true, ["Ben O'Connor"]=true, ["Andrea Peron"]=true, ["Cristian Rodríguez"]=true, 
		["Nick Schultz"]=true, ["Adam Stachowiak"]=true, ["Michel Vermote"]=true, ["Johannes Weber"]=true, ["Martin Weiss"]=true, ["Christopher Williams"]=true, 
		["Samuel Williams"]=true, ["Peter Williams"]=true, ['Stephen Williams']=true, ["Michael Woods"]=true, ["Michael Wright"] = true, ["Edoardo Zardini"]=true, 
		["Georg Zimmermann"]=true } end
	if wiki=='en' then black_list={ ["Tiago da Silva"]=true, ["Jan Maas"]=true, ["Alexander Meier"]=true, ["James McLaughlin"]=true, ["Andrea Nencini"]=true, 
		["Johannes Weber"]=true } end
	if wiki=='fr' then black_list={['Pierre Barbier']=true, ['Jessica Cutler']=true, ['Fernanda da Silva']=true, ['Guillaume Delvaux']=true, ['Willy De Waele']=true, 
		['Jules Dubois']=true, ['Jean Dupont']=true, ['Robert Fontaine']=true, ['René Fournier']=true, ['Pierre Gauthier']=true, ['Marc Goossens']=true, 
		['Claude Guyot']=true, ['Amy Hill']=true, ['Liang Hongyu']=true, ['Marcus Johansson']=true, ['Mathias Jørgensen']=true, ['José Mendoza']=true, 
		['Daniel Müller']=true, ['Henri Parmentier']=true, ['Jean Raynal']=true, ['Pascal Robert']=true, ['Jean-Yves Roy']=true, ['Michael Shermer']=true, 
		['Juris Silovs']=true, ['Jacques Simon']=true, ['Guy Thomas']=true, ['Ryan Thomas']=true, ['Hans Vonk']=true, ['Jan Wijnants']=true } end
	return black_list[Label]
end

function p.winner (wiki, t, winner_id)
	local number = t.number
	local winner_id = winner_id
	local tt = {}
	for num=1,number do table.insert(tt, "") end
	local WDlink_on
	local winner

	-- WDlink_on is used to decide if a Wikidata flag will be shown
	if wiki == "mk" or wiki == "ja" or wiki == "ru" then WDlink_on = true else WDlink_on = false end
	for num=1,number do
		local entity_race = mw.wikibase.getEntityObject(t.race[num][1])
		if pcall(fooA, 'numeric-id', entity_race, 'P1346', 1) == true then		--if exist "winner" (P1346)
		for k, v1 in pairs(entity_race.claims['P1346']) do						
			if pcall(fooA, 'value', entity_race, 'P1346', k, 'P642', 1) == true then	-- Check if "winner" (P1346) is not void due some "of" (P642)
				if pcall(fooC, 'numeric-id', entity_race, 'P1346', k, 'P642', 1) == true 
					and v1.qualifiers.P642[1].datavalue.value['numeric-id'] == winner_id then
		    		local time_var, a, b
	    			winner = "Q"..v1.mainsnak.datavalue.value['numeric-id']
					-- get date of the race, e.g. "+2016-01-01T00:00:00Z"
					-- P585 (point in time for a single day race) or P580 (start time for a stage race)
					a, b = pcall(fooA, 'time', entity_race, 'P585', 1)
					if a == true then time_var = b
					else
						a, b = pcall(fooA, 'time', entity_race, 'P580', 1)
						if a == true then time_var = b end
					end

					-- get nationality of the winner, nationality could have changed, therefore look for qualifiers p580 and p582
					a, b = pcall(fooA, 'numeric-id', mw.wikibase.getEntity( winner ), 'P27', 1) -- standard: only one nationality
					if a == true then
						tt[num] = flag("Q"..b, time_var) .. " " .. WPlinkrider(winner, time_var) .. " "
						if WDlink_on == true then tt[num] = tt[num].. wdLink(winner) end
					else if WDlink_on == true then tt[num] = "  " .. wdLink(winner) else tt[num] = "" end
					end

					-- if person has more than one nationality calculate tt[num] again
					a, b = pcall(fooA, 'numeric-id', mw.wikibase.getEntity( winner ), 'P27', 2)
					if a == true then
						local _, bb = timeStartEnd(winner, "P27", 'numeric-id', time_var)
						tt[num] = flag('Q'..bb, time_var) .. " " .. WPlinkrider(winner, time_var) .. " "
						if WDlink_on == true then tt[num] = tt[num].. wdLink(winner) end
					end
				end -- if v1.
			-- If "winner (P1346)"" is nil, check if still "of" has "numeric-id" and if "has cause" (P828)" for "disqualification"
			else	
				if pcall(fooC, 'numeric-id', entity_race, 'P1346', k, 'P642', 1) == true			-- if exist "of" (P642)
					and v1.qualifiers.P642[1].datavalue.value['numeric-id'] == winner_id			-- if "of" is a winner_id (winner, second or third)
					and pcall(fooC, 'numeric-id', entity_race, 'P1346', k, 'P828', 1) == true		-- if exist "has cause" (P828)
					and v1.qualifiers.P828[1].datavalue.value['numeric-id'] == 1229261 then			-- if cause is "discualification" (Q1229261)
			 		if pcall(fooC, 'numeric-id', entity_race, 'P1346', k, 'P1013', 1) == true then	-- if exist "criterion used" (P1013)
						local criterion = v1.qualifiers.P1013[1].datavalue.value['numeric-id']		-- if criterion is "declared desert" (Q45123627)
			 			if criterion == 45123627 then				-- if criterion is "declared desert" (Q45123627), add others if you want
							tt[num] = WPlink("Q"..criterion)
			 			end
			 		end
				end	
			end
		end -- for k, v1
		end -- if
	end -- for num=
	return tt
end

function p.WPlinkrider(Qnumber, ...) 
	local link=''
	local entity = mw.wikibase.getEntity( Qnumber )
	local Sitelink = entity:getSitelink() -- link to WParticle
	local Sitelink2 = Sitelink
	local Label = entity:getLabel() or ''
	local Label_fr = entity:getLabel( 'fr' ) or ''
	local a, b
	local official_name = ""
	if pcall(foo1, Qnumber) then 
		if Sitelink ~= nil then 
			if arg[1]~=nil then --check for official name
				a, b = pcall(fooA, 'text', entity, 'P1448', 2) -- test if more then two official names
				if a == true then
					a, b = timeStartEnd_entity(entity, 'P1448', 'text', arg[1]) --officialName(entity) 
					if a == true then official_name = b end
				else
					a, b = pcall(fooA, 'text', entity, 'P1448', 1) 
					if a == true then official_name = b end	
				end
				if official_name=='' then
				else
					Sitelink2 = official_name --official_name
				end
			end
			
			link = "[[" .. Sitelink .. "|" .. mw.text.trim(mw.ustring.gsub(Sitelink2, "%b()", "")..' ') .. "]]"	
			if wiki == "de" then -- ru riders get label as alias, to shorten the name
				local a, b = pcall(fooA, 'numeric-id', entity, 'P27', 1) 
				if a == true and (b==159 or b==184 or b==212 or b==232) then link = "[[" .. Sitelink .. "|" .. Label .. "]]" end
			end
		elseif Label ~= '' then 
			if wiki == 'ar' then
				local title = mw.title.new(Label)
				if title.exists 
					then link = Label 
					else link = mw.getCurrentFrame():expandTemplate{ title = 'Ill-WD2', args = {id=Qnumber,en='true'} }--"[[" .. Label.. "]]" 
				end
				
			else
				if black_list(wiki, Label) == true then 
					link = Label -- function black_list: articles that are printed as text, not wikilinks
				else -- detect if there is a different local article with the same name
					local title = mw.title.new(Label)
					if title.exists then link = Label else link = "[[" .. Label.. "]]" end
				end
			end
		else link = Label end 
	else return "unknown ID in function WPlink" end
	if link == '' then 
		link = entity:getLabel('fr') or entity:getLabel('en') or entity:getLabel('de') or ''
		if wiki == "ar" then link = entity:getLabel() or entity:getLabel('en') or entity:getLabel('fr') or entity:getLabel('de') or '' end
		if link ~= '' then link = mw.ustring.gsub(link, "%b()", "")
		else link =  "(label missing)" end  
	end
	return link
end

function p.WPlink(Qnumber, ...) 
	local link=''
	local entity = mw.wikibase.getEntity( Qnumber )
	local Sitelink = entity:getSitelink() -- link to WParticle
	local Sitelink2 = Sitelink
	local Label = entity:getLabel() or ''
	local Label_fr = entity:getLabel( 'fr' ) or ''
	local a, b
	local official_name = ""
	
	if arg[1] == nil then 	
		if pcall(foo1, Qnumber) then 
			if Sitelink ~= nil then 
				if wiki=="en" then --delete string ", ..." from e.g. "Unley, South Australia"
					if Sitelink~=nil then
						local i, _ = mw.ustring.find(Sitelink, ",")
						if i ~= nil then Sitelink2 = mw.ustring.sub(Sitelink, 1, i-1) end
					end
				end
				link = "[[" .. Sitelink .. "|" .. mw.text.trim(mw.ustring.gsub(Sitelink2, "%b()", "")..' ') .. "]]"	
				if wiki == "de" then -- ru riders get label as alias, to shorten the name
					local a, b = pcall(fooA, 'numeric-id', entity, 'P27', 1) 
					if a == true and (b==159 or b==184 or b==212 or b==232) then link = "[[" .. Sitelink .. "|" .. Label .. "]]" end
				end
			elseif Label ~= '' then 
				if wiki == 'ar' then
					local title = mw.title.new(Label)
					if title and title.exists 
						then link = Label 
						else link = mw.getCurrentFrame():expandTemplate{ title = 'Ill-WD2', args = {id=Qnumber,target='en',label=Label} }--"[[" .. Label.. "]]" 
					end
					
				else
					if black_list(wiki, Label) == true then 
						link = Label -- function black_list: articles that are printed as text, not wikilinks
					else -- detect if there is a different local article with the same name
						local title = mw.title.new(Label)
						if title and title.exists then link = Label else link = "[[" .. Label.. "]]" end
					end
				end
			else link = Label end 
		else return "unknown ID in function WPlink" end
		if link == '' then 
			link = entity:getLabel('fr') or entity:getLabel('en') or entity:getLabel('de') or ''
			if wiki == "ar" then link = entity:getLabel() or entity:getLabel('en') or entity:getLabel('fr') or entity:getLabel('de') or '' end
			if link ~= '' then link = mw.ustring.gsub(link, "%b()", "")
			else link =  "(label missing)" end  
		end
	end -- if arg[1] == nil
	
	if arg[1] == 'team' then -- add '(team of the winner)'
		a, b = timeStartEnd(Qnumber, 'P1448', 'text', arg[2]) --officialName(entity) 
		local a1, b1 = pcall(fooA, 'numeric-id', entity, 'P361', 1)
		if a1== true then 
			local a2, b2 = timeStartEnd('Q'..b1, 'P1448', 'text', arg[2]) 
			if a2== true then a = a2 b = b2	end
			entity = mw.wikibase.getEntity( 'Q'..b1 ) 
			Sitelink2 = entity:getSitelink() -- link to WParticle		
		end
		if a == true then official_name = b end
		
		if available_lang_priority == false then
			a, b = pcall(fooA, 'text', entity, 'P1448', 2) -- test if more then two official names
			if a == true then
				a, b = timeStartEnd(Qnumber, 'P1448', 'text', arg[2]) --officialName(entity) 
				if a == true then official_name = b end
			else
				a, b = pcall(fooA, 'text', entity, 'P1448', 1) 
				if a == true then official_name = b end	
			end
		end
				
		if Sitelink ~= nil then 
			if official_name == nil then 
				link = "[[".. Sitelink.. "|".. mw.text.trim(mw.ustring.gsub(Sitelink,"%b()","").. ' ') .. "]]" 
			else
				if wiki=="en" then --delete string ", ..." from e.g. "Unley, South Australia"
					local i, _ = mw.ustring.find(Sitelink, ",")
					if i ~= nil then Sitelink = mw.ustring.sub(Sitelink, 1, i-1) end
				end
				link = link .. '[['.. Sitelink.. "|".. official_name.. ']]'
			end
		end
		if link == '' and Sitelink2 ~= nil then -- code goes from season item to team item
			if official_name == nil or official_name == '' then
				link = "[[".. Sitelink2.. "|".. mw.text.trim(mw.ustring.gsub(Sitelink2,"%b()","").. ' ') .. "]]" 	
			else
				if wiki=="en" then --delete string ", ..." from e.g. "Unley, South Australia"
					local i, _ = mw.ustring.find(Sitelink2, ",")
					if i ~= nil then Sitelink2 = mw.ustring.sub(Sitelink2, 1, i-1) end
				end		
				link = link .. '[['.. Sitelink2.."|".. official_name.. ']]' 
			end		
		end
	
		if Sitelink == nil and Sitelink2 == nil then 
			if official_name ~='' then link = official_name else link = entity:getLabel() end
			if link == nil then link = "(label missing)" end
		end	
	
	-- mw.log('Sitelink=', Sitelink, 'Sitelink2=', Sitelink2, 'official_name=',  official_name) 
	end -- end arg[1]
	if arg[1] == 'pure' then
		if pcall(foo1, Qnumber) then
			if wiki == "en" then --delete string ", ..." from e.g. "Unley, South Australia"
				if Sitelink~=nil then
					local i, _ = mw.ustring.find(Sitelink, ",")
					if i ~= nil then Sitelink2 = mw.ustring.sub(Sitelink, 1, i-1) end
				end
			end		
			if Sitelink ~= nil then link = "[[" .. Sitelink .. "|" .. mw.text.trim(mw.ustring.gsub(Sitelink2, "%b()", "")..' ') .. "]]"	
			elseif wiki == 'ar' then
				local title = mw.title.new(Label)
				if title and title.exists 
					then link = Label 
					else link = mw.getCurrentFrame():expandTemplate{ title = 'Ill-WD2', args = {id=Qnumber,target='en',label=Label} }--"[[" .. Label.. "]]" 
				end
			else link = mw.ustring.gsub(Label, "^(%a)", function (x) return mw.ustring.upper(x) end)		
			end 
		end
	end
	if arg[1] == 'race' then -- for infobox "Competitions"
		local a, b = pcall(fooA, 'numeric-id', entity, 'P31', 1)
		if a == true and b == 1137352 then
			local entity_race = mw.wikibase.getEntity( 'Q' .. b )
			local Label_race = entity_race:getLabel() or ''
			local Sitelink_race = entity_race:getSitelink() or ''
			if Label_race ~= '' and Sitelink ~= nil then link = '[[' .. Sitelink .. '|' .. Label_race .. ']]'
			elseif Sitelink ~= nil then link = '[[' .. Sitelink .. ']]'
			else if Sitelink_race ~= '' then link = '[[' .. Sitelink_race ..'|' .. mw.text.trim(mw.ustring.gsub(Sitelink_race, "%b()", "") .. ' ') .. ']]'
				else link = Label_race or Label end
			end
			if onwikidata == true then link = '[['..entity:getSitelink( 'frwiki')..'|'..entity_race:getLabel('fr')..']]' end
	
		else if Sitelink ~= nil then link = "[[".. Sitelink.. "]]" else link = Label end
		end
	end
	if arg[1] == "Q22348500" then -- add weblink from item "Q22348500" if available
		local Sitelink_claas = mw.wikibase.getEntity( 'Q22348500' ):getSitelink() 	
		if pcall(foo1, Qnumber) then --Sitelink_claas
			if Sitelink == nil then 
				if Sitelink_claas ~= nil then link = "[[" .. Sitelink_claas .. "|" .. Label .. "]]" 
				elseif entity:getLabel( 'ar' )~=nil then link = entity:getLabel( 'ar' )
				elseif entity:getLabel( 'fr' )~=nil then link = entity:getLabel( 'fr' )
				elseif entity:getLabel( 'en' )~=nil then link = entity:getLabel( 'en' )
				elseif entity:getLabel( 'de' )~=nil then link = entity:getLabel( 'de' )				
				else link = Label 
				end 
			end
		end
	end
	return link
end

function p.flag (x, date)
	local year,m,d
	if date==nil then date = '+'..os.date('%Y-%m-%d') end 	
	local flag1 = { -- Q..a current flag, Q..b, Q..c older flag. date: "+2016", "+2016-01-30", ..
	Q29a={"[[File:Flag of Spain.svg|border|20px|ESP]]","+1982",""},
	Q29b={"[[File:Flag of Spain (1977 - 1981).svg|border|20px|ESP]]","+1977","+1981"},
	Q30= "[[File:Flag of the United States (Pantone).svg|border|20px|USA]]",
	Q31= "[[File:Flag of Belgium (civil).svg|border|20px|BEL]]",
	Q38a={"[[File:Flag of Italy.svg|border|20px|ITA]]","+1947",""},
	Q38b={"[[File:Flag of Italy (1861-1946) crowned.svg|border|20px|ITA]]","+1861","+1946"},
	Q39="[[File:Flag of Switzerland.svg|border|15px|SUI]]",
	Q55= "[[File:Flag of the Netherlands.svg|border|20px|NED]]",
	Q142="[[File:Flag of France.svg|border|20px|FRA]]",
	Q145="[[File:Flag of the United Kingdom.svg|border|20px|GBR]]",
	Q183a={"[[File:Flag of Germany.svg|border|20px|GER]]","+1949-05-23",""},
	Q183b={"[[File:Flag of German Reich (1935–1945).svg|border|20px|GER]]","+1935","+1945"},
	Q183c={"[[File:Flag of the NSDAP (1920–1945).svg|border|20px|GER]]","+1933","+1935"},
	Q183d={"[[File:Flag of Germany (3-2 aspect ratio).svg|border|20px|GER]]","+1919","+1933"},
	Q183e={"[[File:Flag of the German Empire.svg|border|20px|GER]]","+1866","+1918"},
	Q218a={"[[File:Flag of Romania.svg|border|20px|ROU]]","+1989-12-27",""},
	Q218b={"[[File:Flag of Romania (1965-1989).svg|border|20px|ROU]]","+1965","+1989-12-26"},
	Q218c={"[[File:Flag of Romania (1952-1965).svg|border|20px|ROU]]","+1952","+1965"},
	Q218d={"[[File:Flag of Romania (1948-1952).svg|border|20px|ROU]]","+1948","+1952"},
	Q218e={"[[File:Flag of Romania.svg|border|20px|ROU]]","+1867-07-01","+1948"},
	Q224a={"[[File:Flag of Croatia.svg|border|20px|CRO]]","+1990-12-21",""},
	Q224b={"[[File:Flag of Croatia (white chequy).svg|border|20px|CRO]]","+1990-06-27","+1990-12-21"}
	}

	if flag1[x..'a']~=nil and date>=flag1[x..'a'][2] and flag1[x..'a'][3]=='' then return flag1[x..'a'][1] end
	if flag1[x..'b']~=nil and date>=flag1[x..'b'][2] and date<=flag1[x..'b'][3] then return flag1[x..'b'][1] end
	if flag1[x..'c']~=nil and date>=flag1[x..'c'][2] and date<=flag1[x..'c'][3] then return flag1[x..'c'][1] end
	if flag1[x..'d']~=nil and date>=flag1[x..'d'][2] and date<=flag1[x..'d'][3] then return flag1[x..'d'][1] end
	if flag1[x..'e']~=nil and date>=flag1[x..'e'][2] and date<=flag1[x..'e'][3] then return flag1[x..'e'][1] end	
	if flag1[x]~=nil then return flag1[x] end	

	local flag2 = { Q16={'Q80110','CAN'}, Q17={'Q43175','JPN'}, Q20={'Q83149','NOR'}, Q27={'Q82205','IRL'}, Q32={'Q133022','LUX'}, Q33={'Q47891','FIN'},
	Q34={'Q81286','SWE'}, Q35={'Q60149','DEN'}, Q36={'Q42436','POL'}, Q37={'Q81477','LTU'}, Q39={'Q124020','SUI'}, Q77={'Q171103','URU'},  
	Q96={'Q122462','MEX'}, Q145={'Q83278','GBR'}, Q159={'Q16611','RUS'}, Q191={'Q81471','EST'}, Q211={'Q130449','LAT'}, Q212={'Q127974','UKR'}, 
	Q213={'Q127155','CZE'}, Q214={'Q160861','SVK'}, Q232={'Q131650','KAZ'}, Q262={'Q83392','DZA'}, Q408={'Q81676','AUS'}, Q414={'Q130774','ARG'}, 
	Q664={'Q160260','NZL'}, Q717={'Q165538','VEN'}, Q739={'Q160425','COL'} }  	

	local xx
	if flag_table[x] ~= nil then return flag_table[x] end

	if flag2[x]~=nil then xx = x x = flag2[x] end -- transform x to new x 
	if type(x)=='table' and pcall(foo1,x[1]) then -- for transformed x
		local entity=mw.wikibase.getEntity(x[1])
		local a,b=pcall(fooA,'value',entity,'P18',1) -- P18 (image)
		if a==true then 
			flag_table[xx] = "[[File:"..b.."|border|20px|"..x[2].."]]" 		
			return "[[File:"..b.."|border|20px|"..x[2].."]]" 
		end 
	end
	if type(x)=='string' and pcall(foo1,x) then -- reading the flag data from untransformed items, e.g. Q29 
		local entity = mw.wikibase.getEntity( x ) 
		local flags = entity.claims["P41"]
		local possible_flag = {}
		local num = 1
		if pcall(fooA, 'numeric-id', entity, 'P41', 1) then
			local v
			for _, v in pairs(flags) do
				if v.qualifiers then
					if (not v.qualifiers["P580"] or v.qualifiers["P580"][1].datavalue.value.time <= date)
						and (not v.qualifiers["P582"] or v.qualifiers["P582"][1].datavalue.value.time >= date)
					then possible_flag[num]=v.mainsnak.datavalue.value num=num+1 end
				else possible_flag[num]=v.mainsnak.datavalue.value num=num+1 end
			end	
			local file
			if possible_flag[1]~=nil then
				file="[[File:"..possible_flag[1].."|border|20px" 
			else
				file="[[File:".."failed".."|border|20px" 
			end	
			local a2,b2=pcall(fooA,'value',entity,'P984',1) -- P984 (country code by the International Olympic Committee)				
			if a2 == true then file=file.."|"..b2.."]]" else file=file.."]]" end 
			flag_table[x] = file		
			return file
		end	
	end 
	return " "
end 

function p.timeStartEnd_entity(entity, property, mode, timeofrace)
	-- mode = 'numeric-id', property = 'P54', Qid = 'Q123'
	--local entity = 	mw.wikibase.getEntity(Qid)
	local a, b = pcall(fooA, mode, entity, property, 1)
	local starttime, endtime
	local available_lang_priority = available_lang_priority
	
	if a == true then
		local timeStartEnd = {}
		local num = 1
		local v
		for _, v in pairs(entity.claims[property]) do
			if pcall(function () local test = v.mainsnak.datavalue.value[mode] end) then -- test if not 'unnown value' 
				if pcall(function () starttime = v.qualifiers["P580"][1].datavalue.value.time end) then
	   				starttime = v.qualifiers["P580"][1].datavalue.value.time
					local _,_,year,m,d = string.find(starttime,"(%d+)-(%d+)-(%d+)")
					if m == '00' and d == '00' then starttime = '+'..year..'-01-01T00:00:00Z' end
				end
				if pcall(function () endtime = v.qualifiers["P582"][1].datavalue.value.time end) then
					endtime = v.qualifiers["P582"][1].datavalue.value.time
					local _,_,year,m,d = string.find(endtime,"(%d+)-(%d+)-(%d+)")
					if m == '00' and d == '00' then endtime = '+'..year..'-12-31T00:00:00Z' end
					if m == '01' and d == '01' then endtime = '+'..year..'-12-31T00:00:00Z' end
				end
	  			if v.qualifiers then
	 				if (not v.qualifiers["P580"] or starttime <= timeofrace)
	 					and
	 					(not v.qualifiers["P582"] or endtime >= timeofrace)
	 				then
	 					if available_lang_priority == true and property == "P1448" then
	 						local test = false
							pcall(function () if type( v.qualifiers["P1448"])=='table' then test=true end end)
							if test == false then
								timeStartEnd[num] = v.mainsnak.datavalue.value[mode]
							else
								local lang_module, lang_WD
								local language= ''
								local zaehler
								for i,j in ipairs(translations.lang_priority) do
										lang_module = j
										for number in pairs(v.qualifiers["P1448"]) do
											lang_WD = v.qualifiers["P1448"][number].datavalue.value.language
											if lang_WD == lang_module then zaehler = number break end
										end -- for number in pairs
									if lang_WD == lang_module then language = lang_WD   break end
									end -- for i,j
								if language~='' then -- a WD translation matching translations.lang_priority exist
										local t = v.qualifiers["P1448"][zaehler].datavalue.value.text
										timeStartEnd[num] = t
		 							else timeStartEnd[num] = v.mainsnak.datavalue.value[mode]--  no matching between lang module and "official name" qualifier
									end -- if language~=''
							end --  test if official name qualifier exist
	 					else timeStartEnd[num] = v.mainsnak.datavalue.value[mode]
						end -- if property == "P1448"
	  					num = num + 1
	 				end
	 			else
	 				timeStartEnd[num] = v.mainsnak.datavalue.value[mode]
	 				num = num + 1
	 			end
			end
	 	end
		return a, timeStartEnd[1]
	else return false, '' end
end

function p.wdLink (id)
	return "[[File:Wikidata-logo S.svg|12px|link=d:" .. id .. "]]"
end

return p