Documentation icon Moduldokumentation[opret]
local wd = require( 'Modul:Sportdata localisation' )
local wd = require( 'Modul:Referencer' )

	CompetitionDb={}
	
	iClubRating=100000		-- Added to club competitions to make them appear after national team compeitions

	--National team competitions (women)--
	CompetitionDb['Q2468836']=1; --World Championship
	CompetitionDb['Q3892502']=2; -- OS 
	CompetitionDb['Q42344325']=3; -- Volleyball Nations League
	CompetitionDb['Q300180']=3; -- FIVB World Grand Prix
	CompetitionDb['Q1218218']=4; -- FIVB Volleyball World Grand Champions Cup
	CompetitionDb['Q15088720']=4; -- FIVB Volleyball World Cup
	CompetitionDb['Q54949960']=5; -- FIVB Challenger Cup
	
	CompetitionDb['Q2426029']=10; --  European Championships
	CompetitionDb['Q282128']=11; --  European Volleyball League
	CompetitionDb['Q109638556']=12; --  European Volleyball League
	CompetitionDb['Q21206633']=13; --  European Games
	
	CompetitionDb['Q15067587']=10; -- Asian Championships
	CompetitionDb['Q3562833']=11; -- Asian games
	CompetitionDb['Q4583126']=12; -- AVC Cup
	
	CompetitionDb['Q14830021']=10; -- African Championships
	CompetitionDb['Q15215179']=11; -- African games

	CompetitionDb['Q2556446']=20; --  Montreux Volley Masters
	CompetitionDb['Q15141274']=21; --  Mediterranean Games
	CompetitionDb['Q3892505']=22; --  Panarabic Games
	CompetitionDb['Q3540464']=23; --  Trofeo Valle d'Aosta
	CompetitionDb['Q3902807']=24; --  Piemonte Women Cup

	CompetitionDb['Q117229451']=100; --  NM
	
	--Junior national team competitions--
	CompetitionDb['Q113135013']=1000; --  U21-EM
	CompetitionDb['Q169956']=1001; --  U19-EM
	CompetitionDb['Q2229672']=1002; --  U19-EM
	CompetitionDb['Q48833861']=1003; --  U19-EM
	
	CompetitionDb['Q19689266']=1000; --U23 African Championship
	CompetitionDb['Q1750573']=1001; --U20 African Championship
	CompetitionDb['Q1751126']=1001; --U18 African Championship

	-- Club competitions
	CompetitionDb['Q1753033']=iClubRating+1; --  World Championship

	CompetitionDb['Q2366460']=iClubRating+10; --  CEV Champions League
	CompetitionDb['Q2642651']=iClubRating+11; --  CEV Cup
	CompetitionDb['Q3060065']=iClubRating+12; --  CEV Challenge Cup
	
	CompetitionDb['Q497386']=iClubRating+1000; --  National competition (top division), Turkey
	CompetitionDb['Q497376']=iClubRating+1001; --  National cup, Turkey

	CompetitionDb['Q10481723']=iClubRating+1000; --  National competition (top division), Sweden
	CompetitionDb['Q118088860']=iClubRating+1001; --  National cup, Sweden
	CompetitionDb['Q108461871']=iClubRating+1002; --  Division 1 (2nd division), Sweden
	CompetitionDb['Q111976490']=iClubRating+1003; --  Division 2 (3nd division), Sweden
	CompetitionDb['Q122832182']=iClubRating+1004; --  Division 3 (4 division), Sweden
	
	--National team competitions (men)--
	CompetitionDb['Q22669035']=1; --World Championship
	CompetitionDb['Q3892574']=2; -- Olympic Games

	CompetitionDb['Q6834']=10; --  European Championships
	CompetitionDb['Q1467808']=11; --  European Volleyball League

	--Club competitions (men)--
	CompetitionDb['Q300098']=10+iClubRating; --  CEV Champions League
	CompetitionDb['Q300114']=11+iClubRating; --  CEV Cup
	CompetitionDb['Q300130']=12+iClubRating; --  CEV Challenge Cup

function competitionData(tbl,claimsCompetition)
	--Get the id of the reoccuring sports event
    tbl.iLeague=read(claimsCompetition[1],'id')
    --Get name of the reoccuring sports event
    tbl.txtLeague=txtWikilinkWOsitelink(tbl.iLeague)
    --Get its rank (lower number = more status)
    tbl.iCompetionRank=CompetitionDb[tbl.iLeague]
    --If the reoccuring sports event has no stored rank number give it a very high one (= low status)
	if (tbl.iCompetionRank==nil) then
		tbl.iCompetionRank=999999
	end

	--Get competition class (i.e. women's football, men's basketball etc.)
    local claimsType = mw.wikibase.getAllStatements(tbl.iLeague,'P2094' )
    if (next(claimsType)) then
		tbl.iCompetitionType=read(claimsType[1],'id')
    end
	return tbl
end


function iCompetitionrank(iCompetion)
--sasongsinfo_local
end

function tblSeasonData(frame,tbl,value,entityid,txtAlternative)
	tbl.iStatus=1														-- Everything ok	
    --Default values if nothing found
    tbl.iCompetitionType="Q0"
    tbl.txtDateFrom=''
    tbl.txtDateTo=''
    
    --Check wether the object is an instance of a reoccuring sports event, a qualification event or not at all sports related
    local claimsMainorQualifier = mw.wikibase.getAllStatements(tbl.iSeason, 'P31' )

	tbl.bMainOrQualifier='-' -- Defaults to not being a competition
	bFoundQualification=false   -- Check if found qualification (will take priority of instance of both Q27020041 and Q2122052/Q51036091 )
    --Loop through instances statements
    for key, value in pairs( claimsMainorQualifier ) do
		iInstanceOf=read(value,'id')
		
		--If a main competition
		if (not(bFoundQualification) and ((iInstanceOf=='Q27020041') or (iInstanceOf=='Q18536594') or (iInstanceOf=='Q51031626'))) then
			tbl.bMainOrQualifier='M' -- Main competition
--			break
			end
		--If a qualifier
		if ((iInstanceOf=='Q2122052') or (iInstanceOf=='Q51036091')) then
			--Set as qualifier in table
			tbl.bMainOrQualifier='Q'
			--Get references
			tbl.hasref,tbl.ref=tblProcessAllRefsForItemWD(frame,value['references'],entityid,'P1344')
			--Look for statement for what it qualifies for
			claimsMainCompetition=mw.wikibase.getAllStatements(tbl.iSeason, 'P3085' )
			iMainCompetition=read(claimsMainCompetition[1],'id')
			if not isempty(iMainCompetition) then
				tbl.iSeason=iMainCompetition
				end
			bFoundQualification=true
--			break
			end
		tbl.instanceof=read(claimsMainorQualifier[1],'id')
    end
	
	--Get the claim for the reoccuring sports event
    local claimsCompetition = mw.wikibase.getAllStatements(tbl.iSeason, 'P3450' )
    if (claimsCompetition[1]) then											-- Has a link to leauge (from the individual season) 
    	if (not (tbl.bMainOrQualifier=='Q')) then
    		tbl.bMainOrQualifier='M'
    		end
    	--Fill tbl with data about the reoccuring sports event (rather than the individual season)
    	tbl=competitionData(tbl,claimsCompetition)
		else
		--If nothing found try for a season that it might be part of instead (for example if there is a joint entity for all competitions independent of gender)
	    bFoundCompetition=false			--Start with setting that nothing has been found
	    local claimsCompetition2
	    local claimsCompetitionSeasonPartOf = mw.wikibase.getAllStatements(tbl.iSeason, 'P361' )
	    --If part of another reoccuring sports event
	    if (claimsCompetitionSeasonPartOf[1]) then
			iCompetitionSeasonPartOf=read(claimsCompetitionSeasonPartOf[1],'id')
		    claimsCompetition2 = mw.wikibase.getAllStatements(iCompetitionSeasonPartOf, 'P3450' )
		    bFoundCompetition=true
	    end
	    if (bFoundCompetition and claimsCompetition2[1]) then											-- Part of a reoccuring sports event (from the individual season) 
	    	tbl=competitionData(tbl,claimsCompetition2)
			else		
			--If neithet instance of Q27020041 nor has a statement for P3450 -> assume not a sports competiton at all
			if (tbl.bMainOrQualifier=='-') then
				tbl.iStatus=99					-- Return with a status that makes sure it is not considered a sports result
				tbl.txtSeason=''
				tbl.txtLeague=''
				tbl.edit=''
				tbl.hasref=false
				tbl.ref=''
				tbl.iCompetionRank=999999
				return tbl
				else
				tbl.txtLeague=txtClickToAddCompetitionType .. ' [https://www.wikidata.org/wiki/' .. tbl.iSeason .. ' ' .. txtHere .. '].' .. txtaddWikiData(txtProperty3450,'tävlingstyp',txtExplanationProperty3450)
				tbl.txtLeague=txtCompetitionNotSet.. frame:extensionTag("ref", tbl.txtLeague) .. '[[' .. txtCategory .. ':' .. txtGeneralError .. ']]'
			    tbl.iCompetitionType="Q1"
				tbl.iStatus=2
				end
			end
	    end
	bFounddates,tbl.txtDateFrom,tbl.txtDateTo,tbl.txtYearFrom,tbl.txtSeason=processDates(tbl.iSeason)
	if not bFounddates then
		--If no start year check for era	
		p2348=mw.wikibase.getAllStatements(tbl.iSeason, 'P2348' ) -- Om tidsperiod
		if (next(p2348)) then
			bFounddates,tbl.txtDateFrom,tbl.txtDateTo,tbl.txtYearFrom,tbl.txtSeason=processDates(read(p2348[1],'id') )
		end
	end
	if not bFounddates then
		p585=mw.wikibase.getAllStatements(tbl.iSeason,'P585' ) -- Om tidsperiod
		if (next(p585)) then
			tbl.txtDateFrom=read(p585[1],'time')
			tbl.txtDateTo=tbl.txtDateFrom
			tbl.txtYearFrom=string.sub(tbl.txtDateTo,2,5)
			tbl.txtSeason=tbl.txtYearFrom
			else
			-- If neither startyear nor era, do not try do display more detailed information, just display information about the season of the league	
			tbl.txtSeason=txtWikilink(tbl.iSeason)
			if not tbl.iStatus==2 then
				tbl.iStatus=3
				else
				tbl.txtSeason=txtDateMissing .. '[[' .. txtCategory .. ':' .. txtGeneralError .. ']]'
				end
			end
		end
	iPositionDirect=getProperty(value,'P1352')
	iPartner=getPropertyId(value,'P1706')
	if (iPartner==nil) then
		iPartner=getPropertyId(value,'P1327')
	end
	
	--If a main competition the position was not set directly in the statement using P1352
	if ((tbl.bMainOrQualifier=='M') and iPositionDirect==nil or (txtAlternative and iPartner==nil)) then
		
		--Depending on if it is a competition for individuals or teams different properties should be used (P710/P1923) to list the participants
		if (txtAlternative) then
			txtPropertyForParticipation='P710' --individuals
--			claimParticipants=mw.wikibase.getAllStatements(tbl.iSeason, txtPropertyForParticipation ) --individuals
			iThisParticipant=tbl.iPlayer
			else
			txtPropertyForParticipation='P1923' --teams
			iThisParticipant=tbl.iTeam
		end
		-- events
		claimParticipants=mw.wikibase.getAllStatements(tbl.iSeason,txtPropertyForParticipation)
		txtPos=''
		tbl.hasref=false
		tbl.ref=''
		--Loop through participants of the event until find the particpant for which this function was called
	    for key, value in pairs(claimParticipants ) do
	    	--Read participant id
			iParticipantLoop=read(value,'id')
			--Check if it is the same one as for which the function was called
			if (iParticipantLoop==iThisParticipant) then
				if (value.qualifiers) then
					txtPos=string.sub(readQualifier(value.qualifiers.P1352[1],'amount'),2,-1)
					--If individual look for any people that they played with (for example when people play in pairs)
					if (txtAlternative and value.qualifiers.P1327) then
					    iPartner=readQualifier(value.qualifiers.P1327[1],'id')
					    else
					    	if (txtAlternative and value.qualifiers.P1706) then
						    iPartner=readQualifier(value.qualifiers.P1706[1],'id')
						    end
					    end
					end
					tbl.hasref,tbl.ref=tblProcessAllRefsForItemWD(frame,value['references'],tbl.iSeason,txtPropertyForParticipation)
				break
			    end
	    	end
	    tbl.iPosition=txtPos
	    else
	    tbl.iPosition=iPositionDirect
		tbl.hasref,tbl.ref=tblProcessAllRefsForItemWD(frame,value['references'],entityid,'P1344')
	end

    -- If qualifier	
	if (tbl.bMainOrQualifier=='Q') then
		tbl.iPosition=999998
		end
    if (txtAlternative and iPartner) then
	    tbl.txtTeam=txtTogetherWith .. txtWikilink(iPartner,'id')
		end
	tbl.edit=txtEditPen(entityid,value.id)
	return tbl
end

function txtPosition(iPosition,txtIsQualifier)
	local bMedal=0
	if (iPosition==999999) then
		iPosition=nil
		end
	if (iPosition==999998) then
		iPosition=txtQualifier
		end
	if not (iPosition==nil) then
		if ((iPosition==1) or (iPosition=="1")) then
			txtPosColour='gold'
			bMedal=1
			end
		if ((iPosition==2) or (iPosition=="2")) then
			txtPosColour='silver'
			bMedal=1
			end
		if ((iPosition==3) or (iPosition=="3")) then
			txtPosColour='#CC9966'
			bMedal=1
			end
		if (bMedal==1) then
			return '<td align="center" bgcolor="' .. txtPosColour .. '">' .. iPosition  .. '</td>'
			else
			return '<td>' .. iPosition  .. '</td>'
			end
		else
		return '<td></td>'
		end
end

function txtYears(startyear,endyear,txtSeparator)
if (not (txtSeparator)) then
	txtSeparator='–'
	end
if (startyear=='' or startyear==nil) then
	if (endyear=='' or endyear==nil) then
		if (endyear=='' or endyear==nil) then
			return ''
			else
			return txtSeparator
			end
		else
		return txtSeparator .. endyear
		end
	else
	if (endyear==startyear) then
		return startyear
		else
		return startyear .. txtSeparator .. endyear
		end
	end
end

function processDates(entityid,txtSeparator)
	txtDateFrom=''
	txtDateTo=''
	local p580=mw.wikibase.getAllStatements(entityid, 'P580' )
	local hasstart=next(p580)
	p582=mw.wikibase.getAllStatements(entityid, 'P582' )
	local hasend=next(p582)
	if hasstart then
		txtDateFrom=read(p580[1],'time')
		txtYearFrom=string.sub(txtDateFrom,2,5)
		end
	if hasend then
		txtDateTo=read(p582[1],'time')
		txtYearTo=string.sub(txtDateTo,2,5)
		end
	--For sorting purposes set date from and to date to the same if one of them is lacking
	if hasend and not hasstart then
		txtDateFrom=txtDateTo
		txtYearFrom=txtYearTo
		end
	if hasstart and not hasend then
		txtDateTo=txtDateFrom
		txtYearTo=txtYearFrom
		end
	hasanydate=hasstart or hasend
	if (hasanydate) then
		txtSeason=txtYears(txtYearFrom,txtYearTo,txtSeparator)
		end
	return hasanydate,txtDateFrom,txtDateTo,txtYearFrom,txtSeason
end

--Fill table with information about the player's time at a club
function tblTeamInfo(frame,value,entityid,txtProperty)
	local tmp={}
	tmp.yearfrom,tmp.yearto,tmp.datefrom,tmp.dateto = years(value)  
	tmp.hasref,tmp.ref=tblProcessAllRefsForItemWD(frame,value['references'],entityid,txtProperty)
	tmp.edit=txtEditPen(entityid,value.id)
	return tmp
end

function years(value)
	if value['qualifiers'] then
		if value['qualifiers']['P580'] then
			startdate=readQualifier(value.qualifiers.P580[1],'time')  or ''    -- readQualifier returns nil when P580 is "somevalue"
			startyear=string.sub(startdate,2,5)
			else
			startdate=''
			startyear=''
			end
		if value['qualifiers']['P582'] then
			enddate=readQualifier(value.qualifiers.P582[1],'time') or ''     -- readQualifier returns nil when P582 is "somevalue"
			endyear=string.sub(enddate,2,5)
			else
			enddate=''
			endyear=''
			end
		return startyear,endyear,startdate,enddate
		else
			return '','','',''
		end
end

function txtDate (iYear,iMonth,iDay)
	bHasMonth=not isempty(iMonth)
	bHasDay=not isempty(iDay)
	if (bHasMonth and bHasDay) then
		return mw.getCurrentFrame():preprocess('{{#time:[[j F]] [[Y]]|'..iYear..'-'..iMonth..'-'..iDay..'}}')
		else
		if (bHasMonth) then
			return mw.getCurrentFrame():preprocess('{{#time:F [[Y]]|'..iYear..'-'..iMonth..'-01}}')
			else
			return '[[' .. iYear .. ']]'
			end
		end
end

function isempty(foo)
	return foo==nil or foo=='' or foo==0
end

--Returns name of the team a given year
function txtTeamname(idTeam,year)
	return txtWikilink(idTeam,year)
end

function isClub( id,txtProperty,iLevel )
	if not (iLevel) then
		iLevel=1
		end
	local instanceofs=mw.wikibase.getBestStatements(id, txtProperty )
	local key, value
	--Start by checking it is not a national team
    for key, value in pairs( instanceofs ) do
		local txtId=read(value,'id')
		if (txtId == 'Q1194951') then return false end
		end
    for key, value in pairs( instanceofs ) do
		local txtId=read(value,'id')
		if (txtId == 'Q847017' or txtId == 'Q12973014' or txtId=='Q108395614'  or txtId=='Q13580678' or txtId=='Q115898316' or txtId=='Q15944511' or txtId=='Q476028') then return true end
		if (iLevel<4) then
			if (isClub(txtId,'P279',iLevel+1)) then return true end
			end
		end
		return false
end

function isNationalTeam( id,txtProperty,iLevel)
	if not (iLevel) then
		iLevel=1
		end
	local instanceofs=mw.wikibase.getBestStatements(id, txtProperty );
    for key, value in pairs( instanceofs ) do
		local txtId=read(value,'id')
		if txtId=='Q1194951' then return true end
		if (iLevel<4) then
			if (isNationalTeam(txtId,'P279',iLevel+1)) then return true end
			end
	end
	return false
end

function showresults(myframe,all,txtShowResultsFor)
	local txt=''
	iAllCompetitionTypesTheSame=true
    iLastType=nil
    for key, value in pairs( all ) do
    	if (iAllCompetitionTypesTheSame) then	--Only loop till a different value has been found
	    	if (iLastType==nil) then					--If first item set value for iLastType, otherwise compare if same as last one
	    		iLastType=value.iCompetitionType
	    		else
	    		iAllCompetitionTypesTheSame=iLastType==value.iCompetitionType
		    	end
	    	end
    	end
	if (myframe.args['oldestfirst']) then
	    table.sort(all, function(a, b)
	    	return 
				a.iCompetitionType > b.iCompetitionType or
				(a.iCompetitionType == b.iCompetitionType and a.iCompetionRank < b.iCompetionRank) or 
				(a.iCompetitionType == b.iCompetitionType and a.iCompetionRank == b.iCompetionRank and a.txtLeague > b.txtLeague) or 
	    		(a.iCompetitionType == b.iCompetitionType and a.iCompetionRank == b.iCompetionRank and a.txtLeague==b.txtLeague and a.txtDateFrom < b.txtDateFrom) or
	    		(a.iCompetitionType == b.iCompetitionType and a.iCompetionRank == b.iCompetionRank and a.txtLeague==b.txtLeague and a.txtDateFrom == b.txtDateFrom and a.txtDateTo < b.txtDateTo) 
			    end)
		else
	    table.sort(all, function(a, b)
	    	return 
				a.iCompetitionType > b.iCompetitionType or
				(a.iCompetitionType == b.iCompetitionType and a.iCompetionRank < b.iCompetionRank) or 
				(a.iCompetitionType == b.iCompetitionType and a.iCompetionRank == b.iCompetionRank and a.txtLeague > b.txtLeague) or 
	    		(a.iCompetitionType == b.iCompetitionType and a.iCompetionRank == b.iCompetionRank and a.txtLeague==b.txtLeague and a.txtDateFrom > b.txtDateFrom) or
	    		(a.iCompetitionType == b.iCompetitionType and a.iCompetionRank == b.iCompetionRank and a.txtLeague==b.txtLeague and a.txtDateFrom == b.txtDateFrom and a.txtDateTo > b.txtDateTo) 
			    end)
		end
	if (bCondensed) then
	    table.sort(all, function(a, b)
	    	return 
				a.iCompetitionType > b.iCompetitionType or
				(a.iCompetitionType == b.iCompetitionType and a.iCompetionRank < b.iCompetionRank) or 
				(a.iCompetitionType == b.iCompetitionType and a.iCompetionRank == b.iCompetionRank and a.txtLeague > b.txtLeague) or 
	    		(a.iCompetitionType == b.iCompetitionType and a.iCompetionRank == b.iCompetionRank and a.txtLeague==b.txtLeague and a.iPosition < b.iPosition) or 
	    		(a.iCompetitionType == b.iCompetitionType and a.iCompetionRank == b.iCompetionRank and a.txtLeague==b.txtLeague and a.iPosition == b.iPosition and a.txtDateFrom < b.txtDateFrom) or
	    		(a.iCompetitionType == b.iCompetitionType and a.iCompetionRank == b.iCompetionRank and a.txtLeague==b.txtLeague and a.iPosition == b.iPosition and a.txtDateFrom == b.txtDateFrom and a.txtDateTo < b.txtDateTo) 
			    end)
		end
	txtCompetitionOld=''
	iCompetitionTypeOld="Q0"
	iOldPosition=-1
	local txtPositionRow='<tr><th width="30px">' .. txtPositionHeader .. '</th><th>' .. txtSeasonHeader .. '</th></tr>'
	if (not bHideable) then
		txt=txt .. '<table style="class: biography vcard" width="100%">' .. txtPositionRow
		end
	for i = 1,#all do
		if (all[i].iStatus==1) then
			txtSeasonLink=mw.wikibase.getSitelink(all[i].iSeason)
			end
		if (all[i].iStatus==2 or all[i].iStatus==3) then
			txtSeasonShow= getLabelByEntity(all[i].iSeason) 
			else
			txtSeasonShow=firstToUpper(all[i].txtSeason)
			end
		if not(iAllCompetitionTypesTheSame) then			-- Only show competition type if there is more than one
			if (all[i].iCompetitionType~=iCompetitionTypeOld) then
				if (all[i].iCompetitionType=="Q0") then
					txtCompetitionType=txtClickToAddCompetitionType .. ' [https://www.wikidata.org/wiki/' .. all[i].iLeague .. ' här].' .. txtaddWikiData(txtClass,txtClass,'('..txtClassHelp..')')
					txtCompetitionType=txtCompetitionTypeNotSet.. myframe:extensionTag("ref", txtCompetitionType) .. '[[' .. txtCategory .. ':' .. txtGeneralError .. ']]'
					else
					if (all[i].iCompetitionType=="Q1") then
						txtCompetitionType=txtClickToAddCompetition .. ' ' .. txtaddWikiData(txtProperty3450,txtPropertyP3450Help)
						txtCompetitionType=txtCompetitionTypeNotSet.. myframe:extensionTag("ref", txtCompetitionType) .. '[[' .. txtCategory .. ':' .. txtGeneralError .. ']]'
						else
						txtCompetitionType = firstToUpper(getLabelByEntity(all[i].iCompetitionType))
						end
					end
--				txt=txt .. '<table style="class: biography vcard" width="100%">'..txtPositionRow .. '</table>'
				if (bHideable) then
					if (i>1) then
						txt=txt .. '</table>'
					end
					txt=txt..'<table class="biography vcard" width="100%"><tr style="' .. txtStyleLevel2 .. '"><td colspan="2">' .. txtCompetitionType .. '</td><tr>'
					else
					txt=txt..'<tr style="' .. txtStyleLevel2 .. '"><td colspan="2">' .. txtCompetitionType .. '</td><tr>'
					end
				iCompetitionTypeOld=all[i].iCompetitionType
				end
			end
		if (all[i].txtLeague~=txtCompetitionOld) then
			if (bHideable) then
				if (i>1) then
					txt=txt .. '</table>'
					end
				txt=txt .. '<table class="mw-collapsible mw-collapsed biography vcard" width="100%" style="border-collapse: collapse; border: 1px solid darkgray;margin: 2px 0;"><tr ' .. txtTeamHeaderStyle .. '><td colspan="2"><b>' .. firstToUpper(all[i].txtLeague) .. '</b></td></tr>'..txtPositionRow
				else
				txt=txt .. '<tr ' .. txtTeamHeaderStyle .. '><td colspan="2"><b>' .. firstToUpper(all[i].txtLeague) .. '</b></td></tr>'
				end
			txtCompetitionOld=all[i].txtLeague
		end
		if not ((bCondensed) and (i>1) and (all[i].iPosition==iOldPosition) and (all[i].txtLeague==all[i-1].txtLeague) and (all[i].iCompetitionType==all[i-1].iCompetitionType)) then
			txt=txt..'<tr width="30px">' .. txtPosition(all[i].iPosition) --..'<td>'
			if (myframe.args['condensed']) then
				txt = txt ..'<td align="left" style="padding-left:10px">'
				else
				txt = txt ..'<td>'
				end
			else
		    txt=txt.. ', '
			end
		if not (txtSeasonLink==nil) then
			txt=txt ..'[[' .. txtSeasonLink .. '|' .. txtSeasonShow  .. ']]'
			else
			txt=txt .. txtSeasonShow
		end
		txt=txt..txtUnpackReference(myframe,all[i].hasref,all[i].ref) .. all[i].edit
		if not ((bCondensed) and (i<#all) and (all[i].iPosition==all[i+1].iPosition) and (all[i].txtLeague==all[i+1].txtLeague) and (all[i].iCompetitionType==all[i+1].iCompetitionType)) then
			txt=txt.. '</td></tr>'
			end
		iOldPosition=all[i].iPosition
	end
	txt=txt .. '</table>'
	return all,txt
end

-- Function to add an Image to an infobox
--
-- Arguments:
--   entityid - the entity whose image we want to show
--   txtProperty - property used to get the image (usually P18, but can also be P154)
--   img - result of a call to entity:getBestStatements( 'P18' ) or equivalent
--   myArgs - array used to fill an infobox

function addImage(entityid,txtProperty,img,myArgs)
	bDescription=false -- variable to check if a description has been found
	if next(img) then
		wdImage = img[1]['mainsnak']['datavalue']['value']
	    if img[1].qualifiers and img[1].qualifiers.P2096 then
	    	-- Loop through all descriptions
		    for key, value in pairs(img[1].qualifiers.P2096) do
			    -- Only add if has the same language code as the local wikipedia
			    language=readQualifier(value,'language')
			    if (language==txtLanguageCode) then
			    	bDescription=true                                                                    -- description found!
				    myArgs[txtCaption]=readQualifier(value,'text') ..  txtEditPen(entityid,txtProperty)  -- add description (+ pen to make it easier to edit)
				    end
			    end
	    end
	    -- add image to the infobox array
	    myArgs[txtImage]='[[File:' .. wdImage .. '|' .. txtWidth_px .. ']]'
	    if (bDescription==false) then
	    	myArgs[txtImage]=myArgs[txtImage]..txtEditPen(entityid,txtProperty)
	    	end
	end
	return myArgs
end