Aller au contenu

Module:Cycling race/subfunctions

Cette page est semi-protégée.
Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 8 avril 2018 à 12:15 et modifiée en dernier par Psemdel (discuter | contributions). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.

 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

return p