Modul:Sandlådan/GunnarLarsson/test1

Från Wikipedia

Dokumentationen för denna modul kan skapas på Modul:Sandlådan/GunnarLarsson/test1/dok

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

p.iRefcounter=0

txtSerie="Serie"
txtSport="sport"
txtEdition="Upplaga"
txtResult="Resultat"
txtPeriod='Spelades'
txtWinner="Vinnare"
txtSecond="Tvåa"
txtThird="Trea"
txtInfoboxTemplate='Faktamall'
txtAnd="och"
txtMoreInfo="mer information"

--Parameternames for the infobox tempate
txtLabel='etikett'
txtContent='innehåll'
txtHeader='rubrik'

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

p.resulttable = function(frame)
	if (frame.args['first']) then
		entityid=frame.args['first']
		else
		entityid=mw.wikibase.getEntityIdForCurrentPage()
	end
	if (frame.args['host']) then
		showhost=frame.args['host']
		else
		showhost=true
	end
	if (frame.args['topteams']) then
		topteams=tonumber(frame.args['topteams'])
		else
		topteams=3
	end
	width="width:25%;"
	local txt="<table class=\'wikitable\' style='class:wikitable;text-align:center;width:100%;'><tr>"
	if (showhost) then
		txt=txt.."<th colspan='2'>Säsong</th>"
	else
		txt=txt.."<th rowspan='2'>Säsong</th>"
		end
	txt=txt.."<th colspan='" .. topteams .. "' style='text-align:center;>Podium</th></tr><tr>"
	txt=txt..'<th>År<ref>Då tävlingen genomfördes – kan skilja sig från namnet på tävlingen</ref></th>'
	if (showhost=='true') or (showhost==true) then
		txt=txt..'<th>Spelplats</th>'
	end
	txt=txt.."<th style='background:gold;text-align:center;"..width.."'>Guld</th>"
	if (topteams>1) then
		txt=txt.."<th style='background:silver;text-align:center;"..width.."'>Silver</th>"
		if (topteams>2) then
			txt=txt.."<th style='background:#cc9966;text-align:center;"..width.."'>Brons</th>"
			if (topteams>3) then
				txt=txt.."<th style='text-align:center;"..width.."'>Fyra</th>"
				end
			end
		end
	hasNext=true
	entity=mw.wikibase.getEntity(entityid) 	
	iMax=1
	iC=0
	while(hasNext) do
	txt=txt..'<tr>'

--	txt=txt..'<td>'..txtWikiLink(entityid)..'</td>'
	hasanydate,txtDateFrom,txtDateTo,txtYearFrom,txtSeason=processDates(entity)
	if not (hasanydate) then
			--If no start year check for era	
		p2348=entity:getAllStatements( 'P2348' ) -- Om tidsperiod
		if (next(p2348)) then
			hasanydate,txtDateFrom,txtDateTo,txtYearFrom,txtSeason=processDates(mw.wikibase.getEntity(read(p2348[1],'id')) )
		end
	end
	if not hasanydate then
		p585=entity:getAllStatements( '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)
	if not(isempty(txtSitelink)) then
		txtSeason=txtSeason..'<br>[['..txtSitelink..'|' .. txtMoreInfo .. ']]'
		end
	txtHead='<td>'..txtSeason..'</td>'

	txt=txt..txtHead
	if (showhost=='true') or (showhost==true) then
		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) .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,value.references,entityid,'P17'))
--		    txtHostRefs = txtHostRefs .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,value.references,entityid,'P17'))
		    iHostCityNo=iHostCityNo+1
		end
    		txtHost='<td>'
	    if (iHostCities>0) then
	    	txtHost=txtHost..txtHostCities
	    end
    		txtHost=txtHost..'</td>'
    		txt=txt..txtHost


		end
	claimsTeams=entity:getBestStatements( '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>'
		for key, value in pairs(first) do
			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>'
			for key, value in pairs(second) do
				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>'
				for key, value in pairs(third) do
					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
			end
		end
	if (not bSomeoneplaced) then
		--
	end
	
	entityid=getOneEntityId(entity,'P156')
	if isempty(entityid) then
		hasNext=false
		else
		entity=mw.wikibase.getEntity(entityid) 	
		iC=iC+1
		if (iC>100) then
			hasNext=false
			end
		end

	end --end while loop
	txt=txt..'</tr>'
	return frame:preprocess(txt..'</table><div style="text-align:center;font-style:italic;">Uppgifterna i listan kommer från Wikidata. Klicka på [[File:Arbcom_ru_editing.svg||8px|baseline|class=noviewer]]-symbolen för att ändra en uppgift</div>')
	end

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)
	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}
 		claimLeague=entity:getBestStatements( 'P3450' )[1]
	    if claimLeague then
		    idLeague=read(claimLeague,'id')
		    entityLeague=mw.wikibase.getEntity(idLeague)
		    idPrev=getPropertyId(claimLeague,'P155')
		    if idPrev then
	     		txtPrev=txtWikilink(idPrev)
	     		else
	     		txtPrev=''
	     		end
		    idNext=getPropertyId(claimLeague,'P156')
		    if idNext then
	     		txtNext=txtWikilink(idNext)
	     		else
	     		txtNext=''
	     		end
			entitySport=entity:getBestStatements( 'P641' )[1]
		    if next(entitySport) then
			    idSport=read(entitySport,'id')
			    myArgs[txtLabel .. iCounter]=txtSerie .. " (" .. txtSport .. ")"
		    	myArgs[txtContent  .. iCounter]=firstToUpper(txtWikilink(idLeague)) .. " (" .. firstToUpper(txtWikilink(idSport)) .. ")"
				else
			    myArgs[txtLabel .. iCounter]=txtSerie
		    	myArgs[txtContent  .. iCounter]=firstToUpper(txtWikilink(idLeague))
				end
			iCounter=iCounter+1
	    	else
	    	txtPrev=''
	    	txtNext=''
			end

		txtEditionCount=entity:formatPropertyValues( 'P393' )
	    if txtEditionCount then
		    myArgs[txtLabel .. iCounter]=txtEdition
	    	myArgs[txtContent  .. iCounter]=txtEditionCount.value
			iCounter=iCounter+1
			end
		formattedStart=entity:formatPropertyValues( 'P580' )
	    if formattedStart then
		    myArgs[txtLabel .. iCounter]=txtPeriod
	    	myArgs[txtContent  .. iCounter]=formattedStart['value'] .. ' - '
			formattedEnd=entity:formatPropertyValues( 'P582' )
		    if formattedEnd then
		    	myArgs[txtContent  .. iCounter]=myArgs[txtContent  .. iCounter] .. formattedEnd['value']
				end
			iCounter=iCounter+1
			end
		claimsTeams=entity:getBestStatements( 'P1923' )
		first=0
		second=0
		third=0
		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
				first=iTeam
				end
			if (iPos=="2") then
				second=iTeam
				end
			if (iPos=="3") then
				third=iTeam
				end
			end
		if (first>"0") then		
	    	myArgs[txtHeader  .. iCounter]=txtResult 
			iCounter=iCounter+1
		    myArgs[txtLabel .. iCounter]=txtWinner
	    	myArgs[txtContent  .. iCounter]=txtWikilink(first) 
			iCounter=iCounter+1
			if (second>"0") then		
			    myArgs[txtLabel .. iCounter]=txtSecond
		    	myArgs[txtContent  .. iCounter]=txtWikilink(second) 
				iCounter=iCounter+1
				if (third>"0") then		
				    myArgs[txtLabel .. iCounter]=txtThird
			    	myArgs[txtContent  .. iCounter]=txtWikilink(third) 
					iCounter=iCounter+1
				end
			end
		end
		myArgs['under']='[https://www.wikidata.org/wiki/' .. entityid .. ' '.. txtEditComment .. ']<br>'.. frame:expandTemplate{ title = 'align' , args ={'left','&larr; ' .. txtPrev}} .. frame:expandTemplate{ title = 'align' , args ={'right',txtNext .. ' &rarr;'}}
		tmp = frame:expandTemplate{ title = txtInfoboxTemplate , args =myArgs}
		return tmp
		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 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 p.Annat()
	return mw.wikibase.isValidEntityId('Q1537948')
end
return p