Modul:Sportserie WD

Från Wikipedia
Hoppa till navigering Hoppa till sök

Dokumentationen för denna modul kan skapas på Modul:Sportserie WD/dok

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

p.iRefcounter=0

txtCompetition="Tävling"
txtSport="Sport"
txtClass="Tävlingsklass"
txtArea="Område"
txtOrganiser="Arrangör"
txtEdition="Upplaga"
txtResult="Resultat"
txtPeriod='Spelades'
txtWinner="[[Fil:Gold medal icon.svg|16px]] Guld"
txtSecond="[[Fil:Silver medal icon.svg|16px]] Silver"
txtThird="[[Fil:Bronze medal icon.svg|16px]] Brons"
txtInfoboxTemplate='Faktamall'
txtRelegated="Nerflyttade"
txtNoWinner="<i>Ingen vinnare utsedd</i>"
txtNoWinnerYet="<i>Ingen vinnare utsedd än</i>"
txtFlagIconTemplate='Flaggbild'
txtHostLabel='Värd'
txtHostsLabel='Värdar'
txtHostCityLabel='Spelort'
txtHostCitiesLabel='Spelorter'
txtNoTeamsLabel='Antal lag'
txtNoGamesLabel='Antal matcher'
txtYearLabel='år'

--Parameternames for the infobox tempate
txtLabel='etikett'
txtContent='innehåll'
txtHeader='rubrik'
txtHeaderStyle='rubrikstil'
txtStyle='stil'
txtAnd=' och '

--Help text
txtEditComment='Redigera uppgifter i faktarutan'

--Layout
txtStyleMain='background:orange'

p.map = function(frame)
	if (frame.args['id']) then
		entityid=frame.args['id']
		else
		entityid=mw.wikibase.getEntityIdForCurrentPage()
		end
	if not (entityid==nil) then
		missing={}
		missingcoordinate={}
		txtArea=frame.args['area'];
	if (frame.args['id']) then
		intWidth_px=frame.args['intWidth_px']
		else
		intWidth_px=370
		end
		leagueseason=mw.wikibase.getEntity(entityid) 	
	    local claims = leagueseason:getBestStatements( 'P1923' )
	    myArgs={txtArea,width=intWidthpx}
	--    myArgs['width']=intWidth_px
	--    txt=frame:expandTemplate{ title='Kartposition start', myArgs}
	    txt='{{Kartposition start |' .. txtArea .. ' |width=' .. intWidth_px .. '}}'
	
	    for key, value in pairs( claims ) do
	    	idTeam = read(value,'id')
	    	if (idTeam) then
				txtTeam=txtWikilink( idTeam )
				team=mw.wikibase.getEntity(idTeam) 	
		    	claims2 = team:getBestStatements( 'P115' )[1]
		    	if (claims2) then
		    		idArena=read(claims2,'id')
					arena=mw.wikibase.getEntity(idArena) 	
					txtArena=txtWikilink( idArena )
			    	claims3 = arena:getBestStatements( 'P625' )[1]
			    	if (claims3) then
	--					txt=txt..tmp .. tmp2 .. read(claims3,'longitude') .. ':' .. read(claims3,'latitude') 
					    myArgs={ txtArea, lat=read(claims3,'latitude')}
					    myArgs['long']=read(claims3,'longitude')
					    myArgs['plats']='<span style="position:relative;top:0px;">' .. txtTeam .. '</span>'
	
	--				    txt=txt..frame:expandTemplate{title = "Kartposition markör", myArgs}
	
						txt=txt .. '{{Kartposition markör|' .. txtArea .. ' |lat=' .. read(claims3,'latitude') .. ' |long=' .. read(claims3,'longitude')  .. ' |markör= |plats=<span style="position:relative;top:0px;">' .. txtTeam .. '</span>}}'
	--					txt=txt..tmp .. tmp2 .. tprint (claims3)
						else
						table.insert(missingcoordinate,txtArena)
						end
					else
					table.insert(missing,txtTeam)
					end					
				else
	--			txt=txt..'A'
			end
	    end
	
	    myArgs={txtArea,undertext='Placering av deltagande lag, elitserien 2020/2021'}
	--    myArgs['undertext']="Placering av deltagande lag, elitserien 2020/2021"
		txt=txt .. '{{Kartposition slut|Sverige|undertext=Placering av deltagande lag i ' .. getLabelByEntity(entityid) ..  '.'
		iCounter=0
	    for key, value in pairs( missing ) do
			if (iCounter==0) then
				txt = txt .. 'Data om hemmarena saknas för: ' .. value
				else
				txt = txt .. ', ' .. value
			end
			iCounter=iCounter+1
		end
		iCounter=0
	    for key, value in pairs( missingcoordinate ) do
			if (iCounter==0) then
				txt = txt .. 'Data om arenans koordinater saknas för: ' .. value
				else
				txt = txt .. ', ' .. value
			end
		end
		txt = txt  .. '}}'   
	
	
	
	--    txt=txt..frame:expandTemplate{ title="Kartposition slut",myArgs}
	
	--	tmp = frame:expandTemplate{ title = "Faktamall" , args =myArgs}
	--	return '<nowiki>' .. txt .. '</nowiki>'
		txt=frame:preprocess(txt)
		else
		txt=''
		end
		return txt
end

p.infobox=function(frame)
	local bFinished=false
	if (frame.args['id']) then
		entityid=frame.args['id']
		else
		entityid=mw.wikibase.getEntityIdForCurrentPage()
		end
	if not (entityid==nil) then
		iCounter=1
		entity=mw.wikibase.getEntity(entityid) 
		myArgs={}
		wdTitle=getLabelByEntity( entityid )
		
    	myArgs={titel=wdTitle,grundstil='width:320px'}
	    idPrev=getOneEntityId(entity,'P155')
	    idNext=getOneEntityId(entity,'P156')
 		claimInstanceOf=entity:getBestStatements( 'P31' )
 		bMultisport=false
		for k, value in pairs(claimInstanceOf) do
			iInstanceOf=read(value,'id')
			if (iInstanceOf=='Q167170') then bMultisport=true end
    	end
	    
 		claimLeague=entity:getBestStatements( 'P3450' )[1]
	    if claimLeague then
		    idLeague=read(claimLeague,'id')
		    entityLeague=mw.wikibase.getEntity(idLeague)
		    if not(idPrev) then
			    idPrev=getPropertyId(claimLeague,'P155')
			    end
		    if not(idNext) then
			    idNext=getPropertyId(claimLeague,'P156')
			    end
		    if idPrev then
	     		txtPrev=txtWikilink(idPrev)
	     		else
	     		txtPrev=''
	     		end
		    if idNext then
	     		txtNext=txtWikilink(idNext)
	     		else
	     		txtNext=''
	     		end
			myArgs[txtHeaderStyle] = txtStyleMain
			myArgs[txtLabel .. iCounter]=txtCompetition
	    	myArgs[txtContent  .. iCounter]=firstToUpper(txtWikilink(idLeague))  ..  txtEditPen(entityid,'P3450')
			iCounter=iCounter+1
			entitySport=entityLeague:getBestStatements( 'P641' )
		    if next(entitySport) then
			    idSport=read(entitySport[1],'id')
			    myArgs[txtLabel .. iCounter]="&nbsp;&nbsp;–&nbsp;" .. txtSport
		    	myArgs[txtContent  .. iCounter]='[[' .. firstToUpper(mw.wikibase.getLabel(idSport)) .. ']]'
				iCounter=iCounter+1
				end
			entityClass=entityLeague:getBestStatements( 'P2094' )
		    if next(entityClass) then
			    idClass=read(entityClass[1],'id')
			    myArgs[txtLabel .. iCounter]="&nbsp;&nbsp;–&nbsp;" .. txtClass
		    	myArgs[txtContent  .. iCounter]=firstToUpper(mw.wikibase.getLabel(idClass))
		    	-- Check if it is for any age class, and if so add information about maximum age
				entityAgeClass=entityLeague:getBestStatements( 'P4135' )
			    if next(entityAgeClass) then
			    	fAge=read(entityAgeClass[1],'amount')
				    myArgs[txtContent  .. iCounter]=myArgs[txtContent  .. iCounter] .. ' (max ' .. string.sub(fAge,2) .. ' ' .. txtYearLabel .. ')'
				    end
				iCounter=iCounter+1
				end
			entityArea=entityLeague:getBestStatements( 'P17' )
		    if not(next(entityArea)) then
				entityArea=entityLeague:getBestStatements( 'P30' )
				end
		    if next(entityArea) then
			    idArea=read(entityArea[1],'id')
			    myArgs[txtLabel .. iCounter]="&nbsp;&nbsp;–&nbsp;" .. txtArea
		    	myArgs[txtContent  .. iCounter]=firstToUpper(txtWikilink(idArea))
				iCounter=iCounter+1
			    end
			entityOrganiser=entityLeague:getBestStatements( 'P664' )
		    if next(entityOrganiser) then
			    idOrganiser=read(entityOrganiser[1],'id')
			    myArgs[txtLabel .. iCounter]="&nbsp;&nbsp;–&nbsp;" .. txtOrganiser
		    	myArgs[txtContent  .. iCounter]='[[' .. firstToUpper(mw.wikibase.getLabel(idOrganiser)) .. ']]'
				iCounter=iCounter+1
				end
	    	else
	    	txtPrev=''
	    	txtNext=''
			end
		txtEditionCount=entity:formatPropertyValues( 'P393' )
	    if txtEditionCount then
		    myArgs[txtLabel .. iCounter]=txtEdition
	    	myArgs[txtContent  .. iCounter]=txtEditionCount.value
			iCounter=iCounter+1
		    end
		entityStart=entity:getBestStatements( 'P580' )
	    if next(entityStart) then
			formattedStart=entity:formatPropertyValues( 'P580' )
			txtDateFrom=read(entityStart[1],'time')
			txtYearFrom=string.sub(txtDateFrom,2,5)
		    myArgs[txtLabel .. iCounter]=txtPeriod
	    	myArgs[txtContent  .. iCounter]=formattedStart['value'] .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,entityStart[1].references,entityid,'P580')) ..  txtEditPen(entityid,'P580') ..  ' - '
			entityEnd=entity:getBestStatements( 'P582' )
		    if next(entityEnd) then
		    	bFinished=true
				txtDateTo=read(entityEnd[1],'time')
				txtYearTo=string.sub(txtDateTo,2,5)
				formattedEnd=entity:formatPropertyValues( 'P582' )
				if (txtDateFrom==txtDateTo) then
			    	myArgs[txtContent  .. iCounter]=formattedEnd['value']  .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,entityEnd[1].references,entityid,'P582')) ..  txtEditPen(entityid,'P582')
					else
					if (txtYearFrom==txtYearTo) then
						iMonthFrom=string.sub(txtDateFrom,7,8)
						iMonthTo=string.sub(txtDateTo,7,8)
						formattedStart['value'] =string.sub(formattedStart['value'] ,1,-6)
						if (iMonthFrom==iMonthTo) then
							iSpaceChar=string.find(formattedStart['value']," ")
							formattedStart['value']=string.sub(formattedStart['value'],1,iSpaceChar-1)
							end
					end
			    	myArgs[txtContent  .. iCounter]=formattedStart['value']  .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,entityStart[1].references,entityid,'P580')) ..  txtEditPen(entityid,'P580') ..  ' - ' .. formattedEnd['value']  .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,entityEnd[1].references,entityid,'P582')) ..  txtEditPen(entityid,'P582')
			    	end
				end
			iCounter=iCounter+1
	    	end

		txtNoTeams=entity:formatPropertyValues( 'P1132' )
	    if txtNoTeams then
		    myArgs[txtLabel .. iCounter]=txtNoTeamsLabel
	    	myArgs[txtContent  .. iCounter]=txtNoTeams.value
			iCounter=iCounter+1
		    end

		txtNoGames=entity:formatPropertyValues( 'P1350' )
	    if txtNoGames then
		    myArgs[txtLabel .. iCounter]=txtNoGamesLabel
	    	myArgs[txtContent  .. iCounter]=txtNoGames.value
			iCounter=iCounter+1
		    end

		claimsHosts=entity:getBestStatements( 'P17' )
		txtHosts=''
		iHostNo=1
		iHosts=#(claimsHosts)
		txtHostRefs=''
		for k, value in pairs(claimsHosts) do
			iHost=read(value,'id')
	    	if not (iHostNo==1) then
	    		if not (iHostNo==iHosts) then
			    	txtHosts =  txtHosts .. ', '
			    	else
			    	txtHosts =  txtHosts .. txtAnd
			    	end
		    	end
    		txtCountry=getLabelByEntity(iHost);
		    txtHosts=txtHosts .. frame:expandTemplate{ title = txtFlagIconTemplate , args={txtCountry}} .. ' [[' ..  txtCountry .. ']]'
--		    txtHostRefs = txtHostRefs .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,value.references,entityid,'P17'))
		    iHostNo=iHostNo+1
			end
    	-- Only show if there is one or several hosts, and that host is not the same as area of the competition 
    	-- i.e. don't show if it is a national competition and the country is repeated as the host
	    if (iHosts>0 and not (iHosts==1 and iHost==idArea)) then
		    if (iHosts>1) then
		    	txtTmpLabel=txtHostsLabel
			    else
			    txtTmpLabel=txtHostLabel
			    end
		    myArgs[txtLabel .. iCounter]= txtTmpLabel ..  txtEditPen(entityid,'P17');
			myArgs[txtContent  .. iCounter]=txtHosts .. txtHostRefs
			iCounter=iCounter+1
		    end

		claimsHostCities=entity:getBestStatements( 'P276' )
		txtHostCities=''
		iHostCityNo=1
		iHostCities=#(claimsHostCities)
		txtHostCitiesRefs=''
		for k, value in pairs(claimsHostCities) do
			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)
--		    txtHostRefs = txtHostRefs .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,value.references,entityid,'P17'))
		    iHostCityNo=iHostCityNo+1
	    	end
	    if (iHostCities>0) then
		    if (iHostCities>1) then
		    	txtTmpLabel=txtHostCityLabel
			    else
			    txtTmpLabel=txtHostCitiesLabel
			    end
		    myArgs[txtLabel .. iCounter]= txtTmpLabel ..  txtEditPen(entityid,'P17');
			myArgs[txtContent  .. iCounter]=txtHostCities .. txtHostCitiesRefs
			iCounter=iCounter+1
			end

		if (not bMultisport) then	-- Only show results if not a multisport event
			claimsTeams=entity:getBestStatements( 'P1923' )
			first={}
			second={}
			third={}
			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
			end
			bSomeoneplaced=false
			if not(next(first) == nil) then		
		    	myArgs[txtHeader  .. iCounter]=txtResult 
				iCounter=iCounter+1
			    myArgs[txtLabel .. iCounter]=txtWinner
			    local txtShow=''
				for key, value in pairs(first) do
					txtShow=txtShow..txtShowTeam(frame,value,txtYearFrom) .. refs[value]
					end
		    	myArgs[txtContent  .. iCounter]=txtShow
				iCounter=iCounter+1
				bSomeoneplaced=true
				end
			if not(next(second) == nil) then		
			    myArgs[txtLabel .. iCounter]=txtSecond
			    local txtShow=''
				for key, value in pairs(second) do
					txtShow=txtShow..txtShowTeam(frame,value,txtYearFrom) .. refs[value]
					end
		    	myArgs[txtContent  .. iCounter]=txtShow
				iCounter=iCounter+1
				bSomeoneplaced=true
				end
			if not(next(third) == nil) then		
			    myArgs[txtLabel .. iCounter]=txtThird
			    local txtShow=''
				for key, value in pairs(third) do
					txtShow=txtShow..txtShowTeam(frame,value,txtYearFrom) .. refs[value]
					end
		    	myArgs[txtContent  .. iCounter]=txtShow
				iCounter=iCounter+1
				bSomeoneplaced=true
			end
			if (not bSomeoneplaced) then
				if (bFinished) then
			    	myArgs[txtContent  .. iCounter]=txtNoWinner
			    	else
			    	myArgs[txtContent  .. iCounter]=txtNoWinnerYet
			    	end
				iCounter=iCounter+1
			end
			claimsRelegatedTeams=entity:getBestStatements( 'P2882' )
			txtRelegatedTeams=''
			iRelegatedTeams=0
			for k, value in pairs(claimsRelegatedTeams) do
				iTeam=read(value,'id')
		    	if (iRelegatedTeams==0) then
				    iRelegatedTeams=1
				    else
			    	txtRelegatedTeams =  txtRelegatedTeams .. '<br>'
		    		end
			    txtRelegatedTeams=txtRelegatedTeams .. txtShowTeam(frame,iTeam) ..  txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,value.references,entityid,'P2882')) ..  txtEditPen(entityid,value.id);
		    	end
		    if (iRelegatedTeams>0) then
			    myArgs[txtLabel .. iCounter]=txtRelegated
				myArgs[txtContent  .. iCounter]=txtRelegatedTeams
				iCounter=iCounter+1
		    end
			end
		myArgs[txtContent  .. '240']=frame:expandTemplate{ title = 'align' , args ={'left','&larr; ' .. txtPrev}} .. frame:expandTemplate{ title = 'align' , args ={'right',txtNext .. ' &rarr;'}}
		myArgs['under']= '[https://www.wikidata.org/wiki/' .. entityid .. ' '.. txtEditComment .. ']'
		tmp = frame:expandTemplate{ title = txtInfoboxTemplate , args =myArgs}
		return tmp
		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

-- 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 p.Annat()
	return mw.wikibase.isValidEntityId('Q1537948')
end
return p