Modul:Sportperson WD

Fra Wikipedia, den frie encyklopædi
Documentation icon Moduldokumentation[opret]
p = {}

local wd = require( 'Modul:Referencer' )
local sd = require( 'Modul:Sportdata')

p.iRefcounter=0

-- Custom comparison function
local function compareUP(a, b)
    -- Handle nil values
    if a == nil then
        return false
    elseif b == nil then
        return true
    end

    -- Compare the attributes (assuming a and b are strings or numbers)
    return a < b
end

-- Custom comparison function
local function compareDOWN(a, b)
    -- Handle nil values
    if a == nil then
        return true
    elseif b == nil then
        return false
    end

    -- Compare the attributes (assuming a and b are strings or numbers)
    return a < b
end

p.person = function(frame)
    -- Get correct entityid, directly from page or if set by parameter 'id' (usually for testing purposes)
	local entityid
	if (frame.args['id']) then
		entityid=frame.args['id']
		else
		entityid=mw.wikibase.getEntityIdForCurrentPage()
		end
	-- Only show infobox if an entity is set
	if not (entityid==nil) then
		frame['id']=entityid
		local txtTitle=getLabelByEntity( entityid )
	    myArgs={titel=txtTitle,grundstil=txtWidth_px}

		--Image
		img=mw.wikibase.getBestStatements(entityid, 'P18' )
		myArgs=addImage(entityid,'P18',img,myArgs)
	    myArgs[txtHeaderStyle] = txtStyleMain
	    
		--Initiate counter, used for keeping track of the element numbers used to fill the infobox (header1, header2 etc.)
	    iCounter=0
		--Birth date
		entityBirthDate=mw.wikibase.getBestStatements(entityid, 'P569' )
		--Birth date
		entityDeathDate=mw.wikibase.getBestStatements(entityid, 'P570' )
		--Only show if set 
		bHasBirthDate=next(entityBirthDate)
		bDead=next(entityDeathDate)
		
		if bHasBirthDate then
	    	iCounter=iCounter+1
			txtBirthData = read(entityBirthDate[1],'time')
			txtBirthYear = tonumber(string.sub(txtBirthData,2,5))
			txtBirthMonth = tonumber(string.sub(txtBirthData,7,8))
			txtBirthDay = tonumber(string.sub(txtBirthData,10,11))
		    myArgs[txtLabel .. iCounter]=txtBirthDate
		    if (bDead) then 
			    myArgs[txtContent  .. iCounter]=txtDate(txtBirthYear,txtBirthMonth,txtBirthDay) .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,entityBirthDate[1].references,entityid,'P569')) .. txtEditPen(entityid,'P569')
		    	else
		    		if txtBirthDay>0 then -- använd bara mallen "Ålder" om födelsedatum är angivet med precision på dagnivå
			    		myArgs[txtContent  .. iCounter]=frame:expandTemplate{ title = txtAgeTemplate, args = { txtBirthYear, txtBirthMonth, txtBirthDay }} .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,entityBirthDate[1].references,entityid,'P569')) .. txtEditPen(entityid,'P569')
			    	end
			    end
			end
	
		--Birth place
		entityBirthPlace=mw.wikibase.getBestStatements(entityid, 'P19' )
		--Only show if set 
	    if next(entityBirthPlace) then
	    	iCounter=iCounter+1
		    idPlace=read(entityBirthPlace[1],'id')
			wdBirthCountry = readFirstStatement(idPlace,'P17')
			wdBirthPlace = txtWikilink(idPlace)
			if wdBirthCountry then 
				wdBirthPlace = wdBirthPlace .. ', ' .. frame:expandTemplate{ title = txtFlagIconTemplate , args={wdBirthCountry}} .. '[[' .. wdBirthCountry .. ']]' .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,entityBirthPlace[1].references,entityid,'P19')) ..  txtEditPen(entityid,'P19')
		    end
		    myArgs[txtLabel .. iCounter]=txtBirthPlace
	    	myArgs[txtContent  .. iCounter]=wdBirthPlace
	    	end

	    if (bDead) then 
	    	iCounter=iCounter+1
			txtDeathData = read(entityDeathDate[1],'time')
			txtDeathYear = tonumber(string.sub(txtDeathData,2,5))
			txtDeathMonth = tonumber(string.sub(txtDeathData,7,8))
			txtDeathDay = tonumber(string.sub(txtDeathData,10,11))
		    myArgs[txtLabel .. iCounter]=txtDeathDate
		    if (bHasBirthDate) then
		    	if (txtDeathDay>0 and txtBirthDay>0) then  --använd bara mallen "Död datum och ålder" om såväl födelsedatum som dödsdatum har precision på dagnivå
			    	myArgs[txtContent  .. iCounter]=frame:expandTemplate{ title = txtAgeDeadTemplate, args = { txtDeathYear, txtDeathMonth, txtDeathDay,txtBirthYear, txtBirthMonth, txtBirthDay }} .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,entityDeathDate[1].references,entityid,'P570')) .. txtEditPen(entityid,'P570')
		        else
	    			myArgs[txtContent  .. iCounter]=txtDate(txtDeathYear, txtDeathMonth, txtDeathDay) .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,entityDeathDate[1].references,entityid,'P570')) .. txtEditPen(entityid,'P570')
		    	end
			end
	    end

		--Death place
		entityDeathPlace=mw.wikibase.getBestStatements(entityid, 'P20' )
		--Only show if set 
	    if next(entityDeathPlace) then
	    	iCounter=iCounter+1
		    idPlace=read(entityDeathPlace[1],'id')
			wdDeathCountry =readFirstStatement(idPlace,'P17')
			wdDeathPlace = txtWikilink(idPlace) 
			if wdDeathCountry then
				wdDeathPlace = wdDeathPlace .. ', ' .. frame:expandTemplate{ title = txtFlagIconTemplate , args={wdDeathCountry}} .. '[[' .. wdDeathCountry .. ']]' .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,entityDeathPlace[1].references,entityid,'P20')) ..  txtEditPen(entityid,'P20')
			end
		    myArgs[txtLabel .. iCounter]=txtDeathPlace
	    	myArgs[txtContent  .. iCounter]=wdDeathPlace
	    	end

		--Player position
		wdPosition = readFirstStatement(entityid,'P413')
		--Only show if set 
		if not (wdPosition==nil) then
			iCounter=iCounter+1
		    myArgs[txtLabel .. iCounter]=txtPlayerPosition;
		    myArgs[txtContent .. iCounter]=firstToUpper(wdPosition)  ..  txtEditPen(entityid,'P413');
		end

		--Height
		wdHeight=mw.wikibase.getBestStatements(entityid, 'P2048' )
		--Only show if set 
		if next(wdHeight) then
			iCounter=iCounter+1
			fHeight = string.sub(read(wdHeight[1],'amount'),2,-1)
			txtUnitWithInsaneURL= read(wdHeight[1],'unit') 
			indexSlash = txtUnitWithInsaneURL:match'^.*()/' -- Find last slash
			idUnitentity=string.sub(txtUnitWithInsaneURL,indexSlash+1) -- Just read after the last slash
			bUnitok=false
			if (idUnitentity=="Q174728") then
				bUnitok=true
				end
			if (idUnitentity=="Q11573") then				-- Handle if unit is meter rather than centimeter
				fHeight = fHeight * 100
				bUnitok=true
			end
			-- If unit neither centimeter nor meter
			if (not bUnitok) then
				fHeight = txtCannotConvertToCM
				end
		    myArgs[txtLabel .. iCounter]=txtHeight
		    myArgs[txtContent .. iCounter]=fHeight  .. ' cm' .. txtUnpackReference(frame,tblProcessAllRefsForItemWD(frame,wdHeight[1].references,entityid,'P2048')) ..  txtEditPen(entityid,'P2048');
		end

		iCounter=iCounter+1
		bHasResults,txtIndividualresults=individualresults(frame,entityid)
		if (bHasResults) then
		    myArgs[txtHeader .. iCounter]=txtCompetitionResultHeader
		    myArgs[txtHeader .. iCounter .. txtStyle]=txtStyleLevel2
			iCounter=iCounter+1
		    myArgs[txtContent .. iCounter]=txtIndividualresults
			iCounter=iCounter+1
			end

		iCounter=iCounter+1
	    tblClubTeam,tblNationalTeam=tblTeam(frame,entityid,'P54')
	    
	    --show teams information if the person has been a member of a team
	    if next(tblClubTeam) then
		    myArgs[txtHeader .. iCounter]=txtClubTeamHeader
		    for key, value in pairs( tblClubTeam ) do
	    		iCounter=iCounter+1
	    		txtNameClub=value.txtName
	    		if (txtNameClub:sub(1,8)=='(saknas)') then
	    			qId=txtNameClub:sub( 13,-4)
	    			txtNameClub='(saknas)' .. frame:extensionTag("ref", "Etikett och wikilänk saknas, [https://www.wikidata.org/wiki/" .. qId .. " Klicka här] för att lägga till") 
	    			end
		    	myArgs[txtLabel .. iCounter]=txtNameClub
			    myArgs[txtContent .. iCounter]=txtYears(value.yearfrom,value.yearto) .. ' ' .. txtUnpackReference(frame,value.hasref,value.ref).. value.edit
		    	end
			iCounter=iCounter+1
			bHasResults,txtTeamresults=teamresults(frame,entityid)
			if (bHasResults) then
			    myArgs[txtHeader .. iCounter]=txtClubTeamResultHeader
			    myArgs[txtHeader .. iCounter .. txtStyle]=txtStyleLevel2
				iCounter=iCounter+1
			    myArgs[txtContent .. iCounter]=txtTeamresults
				iCounter=iCounter+1
				end
		    end
	    --show national teams information if the person has been a member of a national team
	    if next(tblNationalTeam) then
		    myArgs[txtHeader .. iCounter]=txtNationalTeamHeader
		    local key,value
	    	for key, value in pairs( tblNationalTeam ) do
	    		iCounter=iCounter+1
			    myArgs[txtLabel .. iCounter]=value.nationalteam
			    txtTmp=txtYears(value.yearfrom,value.yearto)
			    if (value.appearances~=nil) then
			    	txtTmp = txtTmp .. ' (' .. value.appearances .. ' ' .. txtAppearances .. ')'  .. ' ' 
			    	end
			    myArgs[txtContent .. iCounter]=txtTmp .. txtUnpackReference(frame,value.hasref,value.ref) .. value.edit
			    end
			iCounter=iCounter+1
			
			bHasResults,txtNationalTeamresults=nationalteamresults(frame,entityid)
			if (bHasResults) then
			    myArgs[txtHeader .. iCounter]=txtNationalTeamResultHeader;
			    myArgs[txtHeader .. iCounter .. txtStyle]=txtStyleLevel2;
				iCounter=iCounter+1
			    myArgs[txtContent .. iCounter]=txtNationalTeamresults
				iCounter=iCounter+1
				end
	    end

	    --show coaching information if the person has been a coach of a team (club or national)
	    tblClubTeamCoach,tblNationalTeamCoach=tblTeam(frame,entityid,'P6087')
	    if next(tblClubTeamCoach) then
		    myArgs[txtHeader .. iCounter]=txtCoachHeader;
		    for key, value in pairs( tblClubTeamCoach ) do
	    		iCounter=iCounter+1
		    	myArgs[txtLabel .. iCounter]=value.txtName
			    myArgs[txtContent .. iCounter]=txtYears(value.yearfrom,value.yearto)   .. ' ' .. txtUnpackReference(frame,value.hasref,value.ref) .. value.edit
			    end
    		iCounter=iCounter+1
    		end
	    if next(tblNationalTeamCoach) then
		    myArgs[txtHeader .. iCounter]=txtNationalTeamCoachHeader;
		    for key, value in pairs( tblNationalTeamCoach ) do
	    		iCounter=iCounter+1
		    	myArgs[txtLabel .. iCounter]=value.nationalteam
			    myArgs[txtContent .. iCounter]=txtYears(value.yearfrom,value.yearto)   .. ' ' .. txtUnpackReference(frame,value.hasref,value.ref) .. value.edit
			    end
			iCounter=iCounter+1
	    	end
		
	    if next(tblClubTeamCoach) or next(tblNationalTeamCoach) then
			bHasResults,txtCoachingresults=coachingresults(frame,entityid)
			if (bHasResults) then
			    myArgs[txtHeader .. iCounter]=txtCoachingResultHeader;
			    myArgs[txtHeader .. iCounter .. txtStyle]=txtStyleLevel2;
				iCounter=iCounter+1
			    myArgs[txtContent .. iCounter]=txtCoachingresults
				iCounter=iCounter+1
				end
			end

	    --show awards if the person has received any
	    tbl=awards(frame,entityid)
	    if next(tbl) then
			iCounter=iCounter+1
		    myArgs[txtHeader .. iCounter]=txtIndividualAwards;
		    txtLastAward=''
	    	for key, value in pairs( tbl) do
	    		iCounter=iCounter+1
	    		if (value.awardedby==nil) then
				    myArgs[txtContent .. iCounter]=value.award  .. txtUnpackReference(frame,value.hasref,value.ref) .. value.edit
	    			else
		    		if (value.award~=txtLastAward) then
					    myArgs[txtHeader .. iCounter]='[[' .. value.award .. ']] '
					    myArgs[txtHeader .. iCounter .. txtStyle]='background:#eaecf0' --txtStyleLevel2
					    txtLastAward=value.award
			    		iCounter=iCounter+1
					    end
				    myArgs[txtContent .. iCounter]=value.awardedby  .. txtUnpackReference(frame,value.hasref,value.ref) .. value.edit
				    end
			    end
			end

		--Finish with link to wikidata entity with information about that you can edit there
	    myArgs['under']='<br>[https://www.wikidata.org/wiki/' .. entityid .. ' '.. txtEditComment .. ']'
	    --Fill the infobox template
		tmp = frame:expandTemplate{ title = txtInfoboxTemplate , args =myArgs}
		return tmp
		else
		return '' --Return nothing if no connected wikidata entity
		end
	end

--Fill tables with information about the teams (club & national) that the person has played with
function tblTeam(frame,entityid,txtRole)
	local outTeam = {}
	local outNationalTeam = {}
    local claimClubs = mw.wikibase.getAllStatements(entityid, txtRole)
    local key, value
    --Loop through all teams (clubs,national,other)
    for key, value in pairs( claimClubs ) do
    	local idTeam = read(value,'id')
		if isClub( idTeam,'P31' ) then
			local tmp=tblTeamInfo(frame,value,entityid,txtRole)
			tmp.txtName=txtTeamname(idTeam,tmp.yearfrom)
			txtGenderSymbol=''
			if (txtRole=='P6087') then					-- Only check for different genders if coaching
				tmp.txtName=tmp.txtName .. txtGender(idTeam)
			end
			table.insert(outTeam, tmp)
		end
		if isNationalTeam( idTeam,'P31') then
			local tmp=tblTeamInfo(frame,value,entityid,txtRole)
			wdCountry = readFirstStatement(idTeam ,'P17')
			txtSitelink=mw.wikibase.getSitelink(idTeam)
			if not (txtSitelink) then
				txtSitelink=getLabelByEntity(idTeam)
				end
			txtGenderSymbol=''
			if (txtRole=='P6087') then					-- Only check for different genders if coaching
				txtGenderSymbol=txtGender(idTeam)
			end
			claimsAge = mw.wikibase.getBestStatements(idTeam, 'P4135' )
			if next(claimsAge) then
				wdAge = string.sub(read(claimsAge[1],'amount'),2,-1)
				tmp.age=wdAge
				tmp.nationalteam=frame:expandTemplate{ title = txtFlagIconTemplate , args={wdCountry}} .. ' [[' ..  txtSitelink .. '|' .. wdCountry .. ' (U' .. wdAge .. ')]]' .. txtGenderSymbol
				else
				tmp.age=''
				tmp.nationalteam=frame:expandTemplate{ title = txtFlagIconTemplate , args={wdCountry}} .. ' [[' ..  txtSitelink .. '|' .. wdCountry .. ']]' .. txtGenderSymbol
				end
			if value['qualifiers'] then
				if value['qualifiers']['P1129'] then
					tmp.appearances=string.sub(readQualifier(value.qualifiers.P1129[1],'amount'),2,-1)
					else
					tmp.appearances=nil
					end
				end
			table.insert(outNationalTeam, tmp)
			end
	    end

    -- Sort after join date
    table.sort(outTeam, function(a, b) return 
    	(compareUP(a.datefrom,b.datefrom)) or  
    	(a.datefrom == b.datefrom and compareUP(a.yearfrom,b.yearfrom)) or  
    	(a.datefrom == b.datefrom and a.yearfrom == b.yearfrom and compareDOWN(a.dateto,b.datorto)) or 
    	(a.datefrom == b.datefrom and a.yearfrom == b.yearfrom and a.dateto == b.datorto and  compareDOWN(a.yearto,b.yearto)) 
    end)
	    table.sort(outNationalTeam, function(a, b) 
	    	return
	    	(a.age < b.age) or  
	    	(a.age == b.age and a.yearfrom < b.yearfrom) or  
	    	(a.age == b.age and a.yearfrom == b.yearfrom and a.yearto < b.yearto) 
	    	end)
	return outTeam,outNationalTeam
end

--Fill table with information about the teams (club & national) that the person has coached
function coach(frame,entityid)
	local out = {}
    local claims = mw.wikibase.getAllStatements(entityid, 'P6087' )
    for key, value in pairs( claims ) do
		local tmp=tblTeamInfo(frame,value,entityid,'P54')
    	local idTeam = read(value,'id')
		tmp.txtName=getLabelByEntity( idTeam )
		table.insert(out, tmp)
    	end
    table.sort(out, function(a, b) return a.yearfrom < b.yearfrom end)
	return out
end

function coachingresults (frame,entityid)
	local txt='<table style="class: biography vcard" width="'.. txtWidth_px .. '"><tr><th width="25px">'.. txtPositionHeader .. '</th><th>' .. txtTeamHeader .. '</th><th>' .. txtSeasonHeader .. '</th></tr>'
	local all=tblTeams(frame,entityid,'both','P6087')
    table.sort(all, function(a, b) return 
		a.iCompetionRank < b.iCompetionRank or 
    	(a.iCompetionRank == b.iCompetionRank and a.txtLeague < b.txtLeague) or 
    	(a.iCompetionRank == b.iCompetionRank and a.txtLeague==b.txtLeague and a.txtDateFrom < b.txtDateFrom)
    end)

	txtCompetitionOld=''
	for i = 1,#all do
		if (all[i].txtLeague~=txtCompetitionOld) then
			txtGenderSymbol=txtGender(all[i].iLeague)

			txt=txt .. '<tr ' .. txtTeamHeaderStyle .. '><td colspan="3"><b>' .. all[i].txtLeague .. '</b>' .. 	txtGenderSymbol .. '</td></tr>'
			txtCompetitionOld=all[i].txtLeague
			end
		if (all[i].bNationalTeam) then
			txtTeam=all[i].txtNationalTeam
			else
			txtTeam=txtWikilink(all[i].iTeam)
			end
		txt=txt .. '<tr>' .. txtPosition(all[i].iPosition) .. '<td>' .. txtTeam  .. '</td>'
		txtTmp=mw.wikibase.getSitelink(all[i].iSeason )
		if not (txtTmp) then
			txtTmp=all[i].txtSeason
			else
			txtTmp='[['..txtTmp .. '|' .. all[i].txtSeason  .. ']] ' 
		end			
		txt=txt .. '<td>' .. txtTmp   .. txtUnpackReference(frame,all[i].hasref,all[i].ref) .. all[i].edit .. '</td>' .. '</tr>'
    end
	txt=txt .. '</table>'
	return #all>0,txt
end

--Fill table with information about awards won by the person
function awards(frame,entityid)
	local out = {}
    local claimAwards = mw.wikibase.getAllStatements(entityid, 'P166' )
    for key, value in pairs( claimAwards ) do
		local tmp={}
    	local idAward = read(value,'id')
		tmp.award=getLabelByEntity( idAward )
		tmp.awardedby ="-"
		if value['qualifiers'] then
			if value['qualifiers']['P585'] then
				tmp.awardedby =string.sub(readQualifier(value['qualifiers']['P585'][1],'time'),2,5)
				else
  				if value['qualifiers']['P1027'] then
					tmp.awardedby =txtWikilink(readQualifier(value['qualifiers']['P1027'][1],'id'))
					end
				end
		end
		tmp.hasref,tmp.ref=tblProcessAllRefsForItemWD(frame,value['references'],entityid,'P166')
		tmp.edit=txtEditPen(entityid,value.id)
		table.insert(out, tmp)
    	end
    table.sort(out, function(a, b) return a.awardedby < b.awardedby end)
	return out
end

function tblTeams(frame,entityid,txtAlternative,txtDetermination)
	local all={}
	--Find all statements for P1344 that has txtDetermination as determination
	local claimsCompetition
	if (txtDetermination=='none') then
	    claimsCompetition = mw.wikibase.getAllStatements(entityid, 'P1344') 
	    else
	    claimsCompetition = mw.wikibase.getAllStatements(entityid, 'P1344',txtDetermination) 
	    end
    local key,value
    for key, value in pairs( claimsCompetition ) do
		--Check if has determination (if it needs to have one, which is the case for team sports)
		if txtDetermination=='none' or (value['qualifiers'] and value['qualifiers'][txtDetermination]) then -- has a determination like P54 "medlem i idrottslag" for players or P6087  "tränare för idrottslag" for coaches
			local tmp={}
			--read id for the season
			tmp.iSeason = read(value,'id')
			if (txtAlternative=='individual') then
				iTeamPlay=getPropertyId(value,'P54')
				local checkInTeam=false
				if (iTeamPlay and (isClub(iTeamPlay,'P31') or isNationalTeam(iTeamPlay,'P31'))) then
					checkInTeam=true
					end
				iTeamManage=getPropertyId(value,'P6087')
				if (iTeamManage and (isClub(iTeamManage,'P31') or isNationalTeam(iTeamManage,'P31'))) then 
					checkInTeam=true
					end
				if (checkInTeam==false) then
					tmp.iPlayer=entityid
					tmp.txtTeam=""
					tmp=tblSeasonData(frame,tmp,value,tmp.iPlayer,'individual')
					if (not (tmp.iStatus==99)) then
						table.insert(all,tmp)
						end
					end
				else
				tmp.iTeam=getPropertyId(value,txtDetermination)
				tmp=tblSeasonData(frame,tmp,value,entityid)
				tmp.txtCountry = readFirstStatement(tmp.iTeam,'P17')
				tmp.txtTeam=txtTeamname(tmp.iTeam,tmp.txtYearFrom)
				--tmp.txtTeam=getProperty(value,txtDetermination)
				if (txtAlternative=='club' and isClub(tmp.iTeam,'P31')) then
					table.insert(all,tmp)
					end
				if ((txtAlternative=='nationalteam') and isNationalTeam(tmp.iTeam,'P31')) then
					tmp.txtNationalTeam='[[' ..  getLabelByEntity( tmp.iTeam ) .. '|' .. tmp.txtCountry .. ']]'
					claimsAge = mw.wikibase.getBestStatements(tmp.iTeam, 'P4135' )
					if next(claimsAge) then
						wdAge = string.sub(read(claimsAge[1],'amount'),2,-1)
						tmp.txtNationalTeam='[[' ..  getLabelByEntity( tmp.iTeam ) .. '|' .. tmp.txtCountry .. ' (U' .. wdAge .. ')]]'
						else
						tmp.txtNationalTeam='[[' ..  getLabelByEntity( tmp.iTeam ) .. '|' .. tmp.txtCountry .. ']]'
						end
					table.insert(all,tmp)
					end
				if (txtAlternative=='both') then
					tmp=tblSeasonData(frame,tmp,value,entityid)
					if isNationalTeam(tmp.iTeam,'P31') then
						claimsAge = mw.wikibase.getBestStatements(tmp.iTeam, 'P4135' )
						if next(claimsAge) then
							wdAge = string.sub(read(claimsAge[1],'amount'),2,-1)
							tmp.txtNationalTeam='[[' ..  getLabelByEntity( tmp.iTeam ) .. '|' .. tmp.txtCountry  .. ' (U' .. wdAge .. ')]]'
							else
							tmp.txtNationalTeam='[[' ..  getLabelByEntity( tmp.iTeam ) .. '|' .. tmp.txtCountry .. ']]'
							end
						tmp.bNationalTeam=true
						else
						tmp.bNationalTeam=false
					end
					if (not (tmp.iStatus==99)) then
						table.insert(all,tmp)
						end
					end
				end
			end
	    end 
	return all
end

function individualresults (frame,entityid)
	-- get all teams the person has played with (according to P54)
	local all=tblTeams(frame,entityid,'individual','none')
	--Sort them based on date
    table.sort(all, function(a, b) return 
    	(a.txtDateTo < b.txtDateTo) or
    	(a.txtDateTo == b.txtDateTo and a.txtDateFrom < b.txtDateFrom)
    end)
	--Start creaing a table
	local txt='<table style="class: biography vcard" width="'..txtWidth_px..'"><tr><th width="20px">' .. txtPositionHeader .. '</th><th>' .. txtCompetitionHeader .. '</th><th width="100px">' .. txtSeasonHeader .. '</th></tr>'
	txtTeamOld=''
	for i = 1,#all do
		if (all[i].txtTeam~=txtTeamOld) then
			local txtTeam=all[i].txtTeam
			if (all[i].txtCountry) then
				txtTeam=frame:expandTemplate{ title = txtFlagIconTemplate , args={all[i].txtCountry,storlek='16px'}} .. ' ' .. txtTeam
				end
			txt=txt .. '<tr ' .. txtTeamHeaderStyle .. '><td colspan="3"><b>' .. txtTeam .. '</b></td></tr>'
			txtTeamOld=all[i].txtTeam
			end
		txt=txt .. "<tr>" .. txtPosition(all[i].iPosition)
		txtTmp=mw.wikibase.getSitelink(all[i].iSeason )
		if not (txtTmp) then
			txtTmp=all[i].txtSeason
			else
			txtTmp='[['..txtTmp .. '|' .. all[i].txtSeason  .. ']] ' 
		end			
		txtSeasonTxt= txtTmp  .. txtUnpackReference(frame,all[i].hasref,all[i].ref) .. all[i].edit
		if (all[i].iStatus==1) then
			txt=txt .. '<td>' .. all[i].txtLeague  .. '</td>'
			txt=txt .. '<td>' .. txtSeasonTxt .. '</td>' 
			end
		if (all[i].iStatus==2) then
			txt=txt .. '<td colspan="2">' .. all[i].txtLeague  ..  txtSeasonTxt .. '</td>' 
			end
		if (all[i].iStatus==3) then
			txt=txt .. '<td colspan="2">' .. txtSeasonTxt .. '</td>' 
			end
		txt=txt .. '</tr>'
    end
	txt=txt .. '</table>'
	return #all>0,txt
end


function teamresults (frame,entityid)
	-- get all teams the person has played with (according to P54)
	local all=tblTeams(frame,entityid,'club','P54')
	--Sort them based on date
    table.sort(all, function(a, b) return 
    	(a.txtDateTo < b.txtDateTo) or
    	(a.txtDateTo == b.txtDateTo and a.txtDateFrom < b.txtDateFrom)
    end)
	--Start creaing a table
	local txt='<table style="class: biography vcard" width="'..txtWidth_px..'"><tr><th width="20px">' .. txtPositionHeader .. '</th><th>' .. txtCompetitionHeader .. '</th><th width="100px">' .. txtSeasonHeader .. '</th></tr>'
	txtTeamOld=''
	for i = 1,#all do
		if (all[i].txtTeam~=txtTeamOld) then
			local txtTeam=all[i].txtTeam
			if (all[i].txtCountry) then
				txtTeam=frame:expandTemplate{ title = txtFlagIconTemplate , args={all[i].txtCountry,storlek='16px'}} .. ' ' .. txtTeam
				end
			txt=txt .. '<tr ' .. txtTeamHeaderStyle .. '><td colspan="3"><b>' .. txtTeam .. '</b></td></tr>'
			txtTeamOld=all[i].txtTeam
			end
		txt=txt .. "<tr>" .. txtPosition(all[i].iPosition)
		txtTmp=mw.wikibase.getSitelink(all[i].iSeason )
		if not (txtTmp) then
			txtTmp=all[i].txtSeason
			else
			txtTmp='[['..txtTmp .. '|' .. all[i].txtSeason  .. ']] ' 
		end			
		txtSeasonTxt= txtTmp  .. txtUnpackReference(frame,all[i].hasref,all[i].ref) .. all[i].edit
		if (all[i].iStatus==1) then
			txt=txt .. '<td>' .. all[i].txtLeague  .. '</td>'
			txt=txt .. '<td>' .. txtSeasonTxt .. '</td>' 
			end
		if (all[i].iStatus==2) then
			txt=txt .. '<td colspan="2">' .. all[i].txtLeague  ..  txtSeasonTxt .. '</td>' 
			end
		if (all[i].iStatus==3) then
			txt=txt .. '<td colspan="2">' .. txtSeasonTxt .. '</td>' 
			end
		txt=txt .. '</tr>'
    end
	txt=txt .. '</table>'
	return #all>0,txt
end

function nationalteamresults (frame,entityid)
	local all=tblTeams(frame,entityid,'nationalteam','P54')
    table.sort(all, function(a, b) return 
		a.iCompetionRank < b.iCompetionRank or 
    	(a.iCompetionRank == b.iCompetionRank and a.txtLeague < b.txtLeague) or 
    	(a.iCompetionRank == b.iCompetionRank and a.txtLeague==b.txtLeague and a.txtDateTo < b.txtDateTo) or 
    	(a.iCompetionRank == b.iCompetionRank and a.txtLeague==b.txtLeague and a.txtDateTo == b.txtDateTo and a.txtDateFrom < b.txtDateFrom) 
    	end)
	local txt='<table style="class: biography vcard" width="100%"><tr><th width="30px">' .. txtPositionHeader .. '</th><th>'..txtNationalTeamHeader..'</th><th>' .. txtSeasonHeader .. '</th></tr>'
	txtCompetitionOld=''
	for i = 1,#all do
		if (all[i].txtLeague~=txtCompetitionOld) then
			txt=txt .. '<tr ' .. txtTeamHeaderStyle .. '><td colspan="3"><b>' .. all[i].txtLeague .. '</b></td></tr>'
			txtCompetitionOld=all[i].txtLeague
			end
		txt=txt .. '<tr>' .. txtPosition(all[i].iPosition) .. '<td>' .. all[i].txtNationalTeam  .. '</td>'
		txtTmp=mw.wikibase.getSitelink(all[i].iSeason )
		if not (txtTmp) then
			txtTmp=all[i].txtSeason
			else
			txtTmp='[['..txtTmp .. '|' .. all[i].txtSeason  .. ']] ' 
		end			
		txt=txt .. '<td>'..txtTmp  .. txtUnpackReference(frame,all[i].hasref,all[i].ref) .. all[i].edit .. '</td>' .. '</tr>'
    end
	txt=txt .. '</table>'
	return #all>0,txt
end

function txtGender(idTeam)
	claimsCompetitionClass = mw.wikibase.getBestStatements(idTeam, 'P2094' )
	if next(claimsCompetitionClass) then
    	local idCompetitionClass = read(claimsCompetitionClass[1],'id')
		claimsGender = mw.wikibase.getBestStatements(idCompetitionClass, 'P279' ) -- Subclass
	    for key, value in pairs( claimsGender ) do
    		idGender = read(value,'id')
			if (idGender=='Q920057') then
				return '[[' .. txtImage .. ':Venus symbol (heavy pink).svg]]'
				end
			if (idGender=='Q35555522') then
				return '[[' .. txtImage .. ':Male symbol (heavy blue).svg]]'
			end
	    end
	end
	return ''		-- If nothing found return empty text
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

-- 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


return p