Modul:Medaljörer WD

Från Wikipedia

Dokumentation [visa] [redigera] [historik] [rensa sidcachen]


Detta är en modul för att information om medaljörer vid tävlingsarrangemang. Modulen hämtar data uppgifter från Wikidata och lägger in det i Mall:Faktamall. Modulen anropas genom att lägga in antingen {{#Invoke:Medaljörer | resulttable}} eller {{#Invoke:Medaljörer | medaltable}} i artikeln.

Funktioner och argument[redigera wikitext]

Modulen har två funktioner:

  • resulttable visar resultaten upplaga för upplaga
  • medaltable visar en medaljliga för samtliga upplagor

De tar för närvarande följande argument (resulttable tar alla argument, medaltable bara first och topteams):

  • first: Q-ID för den första upplagan (t.ex. Q48432 för Världsmästerskapet i fotboll 1930 (Q48432)). Obligatoriskt
  • host: Om värden för turneringarna ska visas (antingen true eller false). Antas vara sant om inget värde är satt
  • topteams: Hur många placeringar som ska visas (heltal). Antas vara 3 (guld/silver/brons) om inget värde är satt. Värden över 4 tolkas som 4.
  • shownoteams: Om antalet lag som deltog (angivet med antal deltagare (P1132)) ska visas. Antas vara falskt om inget värde är satt.
  • alternatecolour: Om radfärgen ska alternera mellan vanlig bakgrundsfärg och ljusgrå. Antas vara sant om inget värde är satt.

Beskrivning[redigera wikitext]

Modulen är för närvarande under utveckling och finns bara inlagd på ett begränsat antal artikelsidor. Undvik att lägga in den i artiklar i någon större omfattning tills vidare, tills utformningen blivit mer stabil. Bägge funktionerna fungerar så att de starta med den första upplagan och sedan använder följs av (P156) för att gå vidare till nästa upplaga så länge det finns en nästa upplaga.

Exempel[redigera wikitext]

p= {}
local wd = require( 'Modul:Referenshantering' )
local sd = require( 'Modul:Sportdata' )

p.iRefcounter=0

p.resulttable = function(frame)
	local txtError=''
	if (frame.args['first']) then
		entityid=frame.args['first']
		else
		entityid=mw.wikibase.getEntityIdForCurrentPage()
	end
	frame['id']=entityid
	if (frame.args['host']=="false") then
		showhost=false
		else
		showhost=true
	end
	if (frame.args['topteams']) then
		topteams=tonumber(frame.args['topteams'])
		else
		topteams=3
	end
	if (frame.args['shownoteams']) then
		shownoteams=frame.args['shownoteams']
		else
		shownoteams=false
	end
	if (frame.args['alternatecolour']) then
		alternatecolour=frame.args['alternatecolour']
		else
		alternatecolour=true
	end
	if (frame.args['separator']) then
		txtSeparator=frame.args['separator']
		else
		txtSeparator="–"
	end
	if (frame.args['prefix']) then
		txtPrefix=frame.args['prefix'] 
		else
		tblCompetitionType=mw.wikibase.getBestStatements(entityid, 'P3450')
		if (tblCompetitionType) then
			iCompetition=read(tblCompetitionType[1],'id') 
			txtPrefix=mw.wikibase.getLabel(iCompetition) 
			else
			txtPrefix=''
			end
	end
	if (frame.args['suffix']) then
		txtSuffix=frame.args['suffix']
		else
		txtSuffix='';
	end
	if (frame.args['showredlinks']=='true') then
		bshowredlinks=true
		else
		bshowredlinks=false
	end

	--Non-season columns
	iColumns=topteams+1
	if (showhost=='true') or (showhost==true) then
		iColumns=iColumns+1
		end
	iC=1
	arraySeasons={};
	arraySeasons[entityid]=true;
	actOn={};
	actOnAction={};
	actOnText={};
	while (frame.args['special' .. iC]) do
		table.insert(actOn,frame.args['special' .. iC])
		table.insert(actOnAction,frame.args['special' .. iC .. 'action'])
		table.insert(actOnText,frame.args['special' .. iC .. 'text'])
		iC=iC+1
	end
	local txt="<table class=\'wikitable\' style='class:wikitable;text-align:center;width:100%;'><tr>"
	if (showhost==true) then
		txt=txt.."<th colspan='2'>Säsong</th>"
		width="width:" .. ((100-32)/topteams) .. "%;"
	else
		txt=txt.."<th rowspan='2' style='width:16%'>"..txtEditionLabel.."</th>"
		width="width:" .. ((100-16)/topteams) .. "%;"
		end
	txt=txt.."<th colspan='" .. topteams .. "' style='text-align:center;>"..txtPodium.."</th>"
		if (shownoteams) then
		txt=txt..'<th rowspan="2">'..txtTeamHeader..'</th>'
		end
	txt=txt..'</tr><tr>'
	if (showhost==true) then
		txt=txt..'<th style="width:16%">'..txtSeasonHeader..'<ref>'..txtSeasonClarification..'</ref></th><th style="width:16%">'..txtHostCityLabel..'</th>'
	end
	txt=txt.."<th style='background:gold;text-align:center;"..width.."'>"..txtGoldHeader.."</th>"
	if (topteams>1) then
		txt=txt.."<th style='background:silver;text-align:center;"..width.."'>"..txtSilverHeader.."</th>"
		if (topteams>2) then
			txt=txt.."<th style='background:#cc9966;text-align:center;"..width.."'>"..txtBronzeHeader.."</th>"
			if (topteams>3) then
				txt=txt.."<th style='text-align:center;"..width.."'>"..txtFourthHeader.."</th>"
				end
			end
		end
	hasNext=true
	iMax=1
	iC=0
	iColourVersion=1
	while(hasNext) do
	if (alternatecolour) then
		if (iColourVersion==1) then txt=txt..'<tr>' end
		if (iColourVersion==2) then txt=txt..'<tr style="background:#E8E8F0;">' end
		else
		txt=txt..'<tr>'
	end

	hasanydate,txtDateFrom,txtDateTo,txtYearFrom,txtSeason=processDates(entityid,txtSeparator)
	if not (hasanydate) then
		--If no start year check for era	
		p2348=mw.wikibase.getBestStatements(entityid, 'P2348' ) -- Om tidsperiod
		if (next(p2348)) then
			hasanydate,txtDateFrom,txtDateTo,txtYearFrom,txtSeason=processDates(read(p2348[1],'id'),txtSeparator)
		end
	end
	if not hasanydate then
		p585=mw.wikibase.getBestStatements(entityid, 'P585' ) -- Om tidsperiod
		if (next(p585)) then
			txtSeason=string.sub(read(p585[1],'time'),2,5)
			else
			-- If neither startyear nor era, do not try do display more detailed information, just display information about the season of the league	
			txtSeason="Period okänd"
			end
		end
	txtSitelink=mw.wikibase.getSitelink(entityid)
	txtActAction=''
	txtActText=''
	posActOn=findPosition(actOn, txtSeason)
	if (not(posActOn==nil)) then
		txtActAction=actOnAction[posActOn]
		txtActText=actOnText[posActOn]
		end
	if not(isempty(txtSitelink)) then
		txtSeason=txtSeason..'<br>[['..txtSitelink..'|' .. txtMoreInfo .. ']]'
		else
		if (bshowredlinks) then
			txtSitelink=txtPrefix .. ' ' .. txtSeason .. ' ' .. txtSuffix
			txtSeason=txtSeason..'<br>[['..txtSitelink..'|' .. txtMoreInfo .. ']]'
			end
		end
	txtHead='<td>'..txtSeason
	if (txtActAction=='comment') then
		txtHead=txtHead.. '<ref>' .. txtActText .. '</ref>'
		end
	txtHead=txtHead..'</td>'
	if (txtActAction=='addbefore') then
		txt=txt..'<tr style="background:#FFFFBB;"><td colspan="'..iColumns..'">'.. txtActText .. '</td></tr>'
		end
	txt=txt..txtHead
	if (showhost=='true') or (showhost==true) then
		iHostCities,txtHostCities=getHost(frame,entityid,'P276')
    		txtHost='<td>'
    	if (iHostCities==0) then
    		iHostCities,txtHostCities=getHost(frame,entityid,'P17')
    		end
	    if (iHostCities>0) then
	    	txtHost=txtHost..txtHostCities
	    end
    		txtHost=txtHost..'</td>'
    		txt=txt..txtHost
	end
	
	if (txtActAction=='replace') then
		txt=txt..'<td colspan="'..topteams..'" style="font-style:italic;">'.. txtActText .. '</td>'
		else
	claimsTeams=mw.wikibase.getBestStatements(entityid,'P1923' )
	first={}
	second={}
	third={}
	fourth={}
	refs={}
	for k, value in pairs(claimsTeams) do
		iTeam=read(value,'id')
		iPos=0
		if (value.qualifiers) then
			iPos=string.sub(readQualifier(value.qualifiers.P1352[1],'amount'),2,-1)
			end
		if (iPos=="1") then
			table.insert(first,iTeam)
			refs[iTeam]=txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,value.references,entityid,'P1352')) ..  txtEditPen(entityid,value.id)
			end
		if (iPos=="2") then
			table.insert(second,iTeam)
			refs[iTeam]=txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,value.references,entityid,'P1352')) ..  txtEditPen(entityid,value.id)
			end
		if (iPos=="3") then
			table.insert(third,iTeam)
			refs[iTeam]=txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,value.references,entityid,'P1352')) ..  txtEditPen(entityid,value.id);
			end
		if (iPos=="4") then
			table.insert(fourth,iTeam)
			refs[iTeam]=txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,value.references,entityid,'P1352')) ..  txtEditPen(entityid,value.id);
			end
	end
	bSomeoneplaced=false
	if not(next(first) == nil) then		
	    pos1='<td>'
	    bFirstElement=true
		for key, value in pairs(first) do
			if bFirstElement then
				bFirstElement=false
				else
				pos1=pos1..txtAnd
				end
			pos1=pos1..txtShowTeam(frame,value,txtYearFrom) .. refs[value]
		end
		pos1=pos1..'</td>'
		else
		pos1='<td></td>'
		end
	txt=txt..pos1
	if (topteams>1) then
		if not(next(second) == nil) then		
		    pos2='<td>'
		    bFirstElement=true
			for key, value in pairs(second) do
				if bFirstElement then
					bFirstElement=false
					else
					pos2=pos2..txtAnd
					end
				pos2=pos2..txtShowTeam(frame,value,txtYearFrom) .. refs[value]
				end
			pos2=pos2..'</td>'
			else
			pos2='<td></td>'
		end
		txt=txt..pos2
		if (topteams>2) then
			if not(next(third) == nil) then		
			    pos3='<td>'
			    bFirstElement=true
				for key, value in pairs(third) do
					if bFirstElement then
						bFirstElement=false
						else
						pos3=pos3..txtAnd
						end
					pos3=pos3..txtShowTeam(frame,value,txtYearFrom) .. refs[value]
					end
				pos3=pos3..'</td>'
				else
				pos3='<td></td>'
				end
			txt=txt..pos3
			if (topteams>3) then
				if not(next(fourth) == nil) then		
			    pos4='<td>'
				for key, value in pairs(fourth) do
					pos4=pos4..txtShowTeam(frame,value,txtYearFrom) .. refs[value]
					end
				pos4=pos4..'</td>'
				else
				pos4='<td></td>'
				end
			txt=txt..pos4
			end
			if (shownoteams) then
				p1132=mw.wikibase.getBestStatements(entityid, 'P1132' ) -- Om tidsperiod
				if (next(p1132)) then
					iNoTeams=string.sub(read(p1132[1],'amount'),2,-1)
					else
					iNoTeams=''
					end
				txt=txt..'<td>'..iNoTeams..'</td>'
			end
		end

		end
	end
	
	claimNext=mw.wikibase.getBestStatements(entityid,'P156')
	if next(claimNext) then
			entityid=read(claimNext[1],'id')
		else
		claimLeague=mw.wikibase.getAllStatements(entityid, 'P3450' )
	    entityid=getPropertyId(claimLeague[1],'P156')
		if isempty(entityid) then
			hasNext=false
			end
	end
		if arraySeasons[entityid] then
		txtError=txtError.."<div style='color:red;text-align:center;'>" .. txtWikidataObject .. " " .. entityid .. " " .. txtAlreadyUsed .. "</div>[[" .. txtCategory .. ":" .. txtLinkedListError .. "]]"
		hasNext=false
		else
		if (hasNext) then
			arraySeasons[entityid]=true;
			end
		end
	iC=iC+1
	if (iC>100) then
		hasNext=false
		end
	iColourVersion=3-iColourVersion

	end --end while loop
	txt=txt..'</tr>'
	return frame:preprocess(txt..'</table><div style="text-align:center;font-style:italic;">'..txtDataFromWikipedia..'</div>'..txtError)
	end


p.medaltable = function(frame)
	if (frame.args['first']) then
		entityid=frame.args['first']
		else
		entityid=mw.wikibase.getEntityIdForCurrentPage()
	end
	if (frame.args['topteams']) then
		topteams=tonumber(frame.args['topteams'])
		else
		topteams=3
	end
	width="width:" .. (80/topteams) .. "%;"
	local txt="<table class=\'wikitable sortable\' style='class:wikitable;text-align:center;width:100%;'><tr>"
	txt=txt..'<th>'..txtPositionHeader..'</th><th>'..txtTeamHeader..'</th>'
	txt=txt.."<th style='background:gold;text-align:center;"..width.."'>".. txtGoldHeader .. "</th>"
	if (topteams>1) then
		txt=txt.."<th style='background:silver;text-align:center;"..width.."'>".. txtSilverHeader .. "</th>"
		if (topteams>2) then
			txt=txt.."<th style='background:#cc9966;text-align:center;"..width.."'>".. txtBronzeHeader .. "</th>"
			if (topteams>3) then
				txt=txt.."<th style='text-align:center;"..width.."'>".. txtFourthHeader .. "</th>"
				end
			end
		end
	hasNext=true
	iMax=1
	iC=0
	
	local teams={}
	
	while(hasNext) do
	txt=txt..'<tr>'
	
	claimsTeams=mw.wikibase.getBestStatements(entityid, 'P1923' )
	for k, value in pairs(claimsTeams) do
		iTeam=read(value,'id')
		if (teams[iTeam]==nil) then
			txtName=txtShowTeam(frame,iTeam)
			txtSortName=getLabelByEntity(iTeam)
			if (txtSortName==nil) then
				txtSortName=''
				end
			newteam={name=txtName,sortname=txtSortName,pos1=0,pos2=0,pos3=0,pos4=0}
			teams[iTeam]=newteam
		end
		iPos=0
		if (value.qualifiers) then
			iPos=string.sub(readQualifier(value.qualifiers.P1352[1],'amount'),2,-1)
			end
		if (iPos=="1") then
			teams[iTeam].pos1=teams[iTeam].pos1+1
			end
		if (iPos=="2") then
			teams[iTeam].pos2=teams[iTeam].pos2+1
			end
		if (iPos=="3") then
			teams[iTeam].pos3=teams[iTeam].pos3+1
			end
		if (iPos=="4") then
			teams[iTeam].pos4=teams[iTeam].pos4+1
			end
	end

	oldentityid=entityid
	entityid=readId(entityid,'P156')
	if isempty(entityid) then
		claimLeague=mw.wikibase.getBestStatements(oldentityid, 'P3450' )[1]
	    entityid=getPropertyId(claimLeague,'P156')
		if isempty(entityid) then
			hasNext=false
			end
		end
	iC=iC+1
	if (iC>100) then
		hasNext=false
		end
	end

	teams_nokey={}
	for k, value in pairs(teams) do
		table.insert(teams_nokey,value)
	end

	if (topteams==2) then
		table.sort(teams_nokey, function(a, b)
	    	return 
				a.pos1 > b.pos1 or
				(a.pos1 == b.pos1 and a.pos2 > b.pos2) or 
	    		(a.pos1 == b.pos1 and a.pos2 == b.pos2 and a.sortname<b.sortname)
			    end)
		end
	
	if (topteams==3) then
		table.sort(teams_nokey, function(a, b)
	    	return 
				a.pos1 > b.pos1 or
				(a.pos1 == b.pos1 and a.pos2 > b.pos2) or 
				(a.pos1 == b.pos1 and a.pos2 == b.pos2 and a.pos3 > b.pos3) or 
	    		(a.pos1 == b.pos1 and a.pos2 == b.pos2 and a.pos3 == b.pos3 and a.sortname<b.sortname)
			    end)
		end

	if (topteams==4) then
		table.sort(teams_nokey, function(a, b)
	    	return 
				a.pos1 > b.pos1 or
				(a.pos1 == b.pos1 and a.pos2 > b.pos2) or 
				(a.pos1 == b.pos1 and a.pos2 == b.pos2 and a.pos3 > b.pos3) or 
	    		(a.pos1 == b.pos1 and a.pos2 == b.pos2 and a.pos3 == b.pos3 and a.pos4 > b.pos4) or
	    		(a.pos1 == b.pos1 and a.pos2 == b.pos2 and a.pos3 == b.pos3 and a.pos4 == b.pos4 and a.sortname<b.sortname)
			    end)
		end

	iPos=1
	pos1last=0
	pos2last=0
	pos3last=0
	pos4last=0
	for k, value in pairs(teams_nokey) do
		tablerow='<tr>'
		empty=false
		if (topteams==2 and value.pos1==0 and value.pos2==0) then
			empty=true
			end
		if (topteams==3 and value.pos1==0 and value.pos2==0 and value.pos3==0) then
			empty=true
		end
		if (topteams==4 and value.pos1==0 and value.pos2==0 and value.pos3==0 and value.pos4==0) then
			empty=true
		end
		if empty then break end
		same=false
		if (topteams==2 and value.pos1==pos1last and value.pos2==pos2last) then
			same=true
			end
		if (topteams==3 and value.pos1==pos1last and value.pos2==pos2last and value.pos3==pos3last) then
			same=true
			end
		if (topteams==4 and value.pos1==pos1last and value.pos2==pos2last and value.pos3==pos3last and value.pos4==pos4last) then
			same=true
			end

		if not(same) then
			tablerow=tablerow..'<td>'..iPos .. '</td>' else
			tablerow=tablerow..'<td></td>' end
				
		tablerow=tablerow..'<td style="text-align:left;">'.. value.name ..'</td>'
		tablerow=tablerow..'<td>'..lineifzero(value.pos1)..'</td>'
		if (topteams>1) then
			tablerow=tablerow..'<td>'..lineifzero(value.pos2)..'</td>'
			if (topteams>2) then
				tablerow=tablerow..'<td>'..lineifzero(value.pos3)..'</td>'
				if (topteams>3) then
					tablerow=tablerow..'<td>'..lineifzero(value.pos4)..'</td>'
					end
				end
			end
		tablerow=tablerow ..'</tr>'
		txt=txt..tablerow
		pos1last=value.pos1
		pos2last=value.pos2
		pos3last=value.pos3
		pos4last=value.pos4
		iPos=iPos+1
		end
	return frame:preprocess(txt..'</table><div style="text-align:center;font-style:italic;">'..txtWikidataExtract..'</ref></div>')
	end

-- Funktion från https://stackoverflow.com/questions/41942289/display-contents-of-tables-in-lua
function tprint (tbl, indent)
  if not indent then indent = 0 end
  local toprint = string.rep(" ", indent) .. "{\r\n"
  indent = indent + 2 
  for k, v in pairs(tbl) do
    toprint = toprint .. string.rep(" ", indent)
    if (type(k) == "number") then
      toprint = toprint .. "[" .. k .. "] = "
    elseif (type(k) == "string") then
      toprint = toprint  .. k ..  "= "   
    end
    if (type(v) == "number") then
      toprint = toprint .. v .. ",\r\n"
    elseif (type(v) == "string") then
      toprint = toprint .. "\"" .. v .. "\",\r\n"
    elseif (type(v) == "table") then
      toprint = toprint .. tprint(v, indent + 2) .. ",\r\n"
    else
      toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
    end
  end
  toprint = toprint .. string.rep(" ", indent-2) .. "}"
  return toprint
end

-- from: https://stackoverflow.com/questions/2421695/first-character-uppercase-lua
function firstToUpper(str)
	if (str) then
	    return (str:gsub("^%l", string.upper))
	    else
	    return ''
	    end
end

function txtShowTeam(frame,iTeam,iYear)
if (isNationalTeam(iTeam,'P31')) then
	wdTeam=mw.wikibase.getEntity(iTeam)
	wdCountry = getOneValue(wdTeam ,'P1532')
	if not wdCountry then
		wdCountry = getOneValue(wdTeam ,'P17')
		end
	txtSitelink=mw.wikibase.getSitelink(iTeam)
	if not (txtSitelink) then
		txtSitelink=getLabelByEntity(iTeam)
		end
	return frame:expandTemplate{ title = txtFlagIconTemplate , args={wdCountry}} .. ' [[' ..  txtSitelink .. '|' .. wdCountry .. ']]'
 else
 	return txtWikilink(iTeam,iYear)
 	end
end

-- Function to find the position of a value in a table
function findPosition(tbl, value)
    for i, v in ipairs(tbl) do
        if v == value then
            return i -- Return the index if the value is found
        end
    end
    return nil -- Return nil if the value is not found in the table
end

function getHost(frame,entityid,property)
		claimsHostCities=mw.wikibase.getBestStatements(entityid,property)
		txtHostCities=''
		iHostCityNo=1
		iHostCities=#(claimsHostCities)
		txtHostCitiesRefs=''
		bFoundHostCity=false
		for k, value in pairs(claimsHostCities) do
			bFoundHostCity=true
			iHostCity=read(value,'id')
	    	if not (iHostCityNo==1) then
	    		if not (iHostCityNo==iHostCities) then
			    	txtHostCities =  txtHostCities .. ', '
			    	else
			    	txtHostCities =  txtHostCities .. txtAnd
			    	end
		    	end
		    txtHostCities=txtHostCities .. txtWikilink(iHostCity) .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,value.references,entityid,'P17'))
		    iHostCityNo=iHostCityNo+1
		end
		return iHostCities,txtHostCities
end

function lineifzero(text)
	if (text==0) then
		return "-"
	else
		return text
	end
end

function readId(entityid,txtProperty)
	statements=mw.wikibase.getBestStatements(entityid,txtProperty)
	if next(statements)==nil then
		return nil
		else
		return read(statements[1],'id')
		end
	end


return p