Hoppa till innehållet

Modul:Sportserie WD

Från Wikipedia

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

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

p.iRefcounter=0

p.infobox=function(frame)
	p.frame=frame
	p=p.data()
    myArgs=p.tblInfobox_canvas()
    p.iCounter=0
	if p.image then
		showImage(p.image)
		else
		if p.logo and (not(p.frame.args['hidelogo'])) then
			showImage(p.logo)
			end
		end
	p.showpartininfobox(localisation.txtCompetitionLabel,'competition',true)
	p.showpartininfobox("  – " .. localisation.txtSportLabel,'sport',true)
	if (p.competitionclass) then
		local txtCompetitionClass
		if (p.bmaxage) then
			txtCompetitionClass=p.competitionclass.data .. ' (' .. localisation.txtMaxLabel .. ' ' .. string.sub(p.maxage,2) .. ' ' .. localisation.txtYearLabel .. ')'
			else
			txtCompetitionClass=p.competitionclass.data
			end			
		txtCompetitionClass=txtCompetitionClass .. txtUnpackReference_model(p.frame,p.competitionclass.ref,p.competitionclass.qid,p.competitionclass.pid)
		p.addContent("  – " .. localisation.txtClassLabel,txtCompetitionClass)
		end
	p.loopitems({"  – " .. localisation.txtAreaLabel,"  – " .. localisation.txtAreaLabel_plural},'area',nil,{'data'},', ',localisation.txtAnd)
	p.loopitems({"  – " .. localisation.txtOrganiserLabel,"  – " .. localisation.txtOrganiserLabel_plural},'organiser',nil,{'data'},', ',localisation.txtAnd)
	p.showpartininfobox(localisation.txtEditionLabel,'edition',true)

	if (p.start) then
		if (p.finish) then
			if ((p.start.precision==9) and (p.finish.precision==9)) then
				if (p.start.year==p.finish.year) then
					p.addContent(localisation.txtPeriodLabel,p.start.year)
					else
					p.addContent(localisation.txtPeriodLabel,p.start.year ..  ' – '  .. p.finish.year)
					end
				end
			if ((p.start.precision==11) and (p.finish.precision==11)) then
				if (p.start.year==p.finish.year) then
					if (p.start.raw==p.finish.raw) then
						p.addContent(localisation.txtPeriodLabel,p.start.formatted)
						else
						iMonthFrom=string.sub(p.start.formatted,7,8)
						iMonthTo=string.sub(p.finish.formatted,7,8)
						txtStart=string.sub(p.start.formatted,1,-13)
						if (iMonthFrom==iMonthTo) then
							local iSpaceChar=string.find(txtStart," ")
							txtStart=string.sub(txtStart,1,iSpaceChar-1)
							end
						p.addContent(localisation.txtPeriodLabel,txtStart ..  ' – '  .. p.finish.formatted)
						end
					else
					p.addContent(localisation.txtPeriodLabel,p.start.formatted ..  ' – '  .. p.finish.formatted)
					end
				end
			else
			p.addContent(localisation.txtPeriodLabel,p.start.formatted)
			end
		else
		if (p.finish) then
			p.addContent(localisation.txtPeriodLabel,p.finish.formatted)
			end
		if (p.at) then
			p.addContent(localisation.txtPeriodLabel,p.at.formatted)
			end
		end

	if p.area and p.countries and not (p.area.id==p.countries.id) then
		p.loopitems({localisation.txtHostLabel,localisation.txtHostLabel},'countries',showCountry,{'id','data'},', ',localisation.txtAnd,true)
		end
	p.loopitems({localisation.txtHostCityLabel,localisation.txtHostCitiesLabel},'locations',nil,{'data'},', ',localisation.txtAnd,true)
	local competitors
	if (p.teams) then
		competitors=p.teams
		p.typeofcompetitors='teams'
		else
		competitors=p.people
		p.typeofcompetitors='people'
		end
	
	p.addHeader(localisation.txtResult)
	if(competitors) then -- Just write out results if any competitors have been listed
		competitionclasses=listalluniques(competitors,"P2094")
		if (#competitionclasses>0) then
			for i=1,#competitionclasses do
				p.addHeader(firstToUpper( mw.wikibase.getLabel(competitionclasses[i], txtLanguageCode )),'background:#ececec')
--	style="background:#ececec"
				showResults(p.filter_quick(competitors,"P2094",competitionclasses[i]))
			end
			else
			showResults(competitors)
			end
		p.loopitems(localisation.txtRelegated,'relegated',txtShowTeam_wrapped,{'id'},', ',localisation.txtAnd)
		else
		local bFinished=false
		if (p.finish) then
			bFinished=rm.now()>p.finish.raw
			else
			if (p.at) then
				bFinished=rm.now()>p.at.raw
				else
				if (p.start) then
					bFinished=rm.now()>p.start.raw
					end
				end
			end
		if (bFinished) then
			p.addContent('',localisation.txtNoWinner)
	    	else
			p.addContent('',localisation.txtNoWinnerYet)
	    	end
		end
    -- Under --
    local iPrev,iNext
    if (p.previous) then
    	iPrev=p.previous.id
    	else
    	iPrev=p.competition.P155
    	end
    if (p.next) then
    	iNext=p.next.id
    	else
    	iNext=p.competition.P156
    	end
    local txtPrev=getLink(iPrev,'previous')
    local txtNext=getLink(iNext,'next')

	myArgs[localisation.txtContent  .. '240']=frame:expandTemplate{ title = 'align' , args ={'left',txtPrev}} .. frame:expandTemplate{ title = 'align' , args ={'right',txtNext}}
	myArgs['under']='[https://www.wikidata.org/wiki/' .. p.objectMainId .. ' '.. txtEditComment .. ']'
--	p.lastchecked()	
	p.dochecks()

	return frame:expandTemplate{ title = localisation.txtTemplate .. ':' .. localisation.txtInfoboxTemplate , args =myArgs}

end

p.data = function()
	if (p.frame.args['id']) then
		p.objectMainId=p.frame.args['id'] else
		p.objectMainId=mw.wikibase.getEntityIdForCurrentPage()
	end
	-- get main id (if set in call use that, otherwise use connected object)
	if (p.objectMainId) then
		tmpBasicData=getGeneralInformation(p.objectMainId,true)
		p=merge(p,tmpBasicData)
		p.objectIds={}
		end
	return p
end

function getGeneralInformation(qid,ismain)
	local ret={}
	ret.image=getImage(qid,'P18')
	ret.label=getLabelByEntity(qid)
	ret.competition=useStatement(qid,'P3450',nil,{'P155','P156'},nil,true)
	ret.previous=useStatement(qid,'P155')
	ret.next=useStatement(qid,'P156')
	ret.start=useStatement(qid,'P580','time')
	ret.finish=useStatement(qid,'P582','time')
	ret.at=useStatement(qid,'P585','time')
	ret.sport=useStatement(ret.competition.id, 'P641' )
	ret.competitionclass=useStatement(ret.competition.id, 'P2094' )
	if (ret.start) then
		ret.bmaxage,ret.maxage=readStatementAtDate(ret.competition.id, 'P4135','amount',ret.start.raw)
		else
		ret.bmaxage,ret.maxage=readStatementAtDate(ret.competition.id, 'P4135','amount')
		end
	ret.area=sd.competitionarea(ret.competition.id)
	ret.organiser=useStatement(ret.competition.id, 'P664' )
	ret.edition=useStatement(qid, 'P393' )
	ret.noteams=useStatement(qid, 'P1132' )
	ret.nogames=useStatement(qid, 'P1350' )
	ret.locations=useStatement(qid, 'P276' )
	ret.countries=useStatement(qid, 'P17' )
	ret.teams=useStatement(qid, 'P1923', nil,{'P1352','P2094'})
	ret.people=useStatement(qid, 'P710', nil,{'P1352','P1706','P2094'})
	ret.relegated=useStatement(qid, 'P2882' )

--[=====[ 
	ret.results=getResults(qid)
	ret.awards=useStatement(qid, 'P166','function',{'P585','P1027'},p.fillAward)
--]=====]
	return ret
end

p.dochecks=function()
	if (rm.iSourcesMissing>=settings.iMaxSourcesMissingWithoutWarning) then
		myArgs['under']=myArgs['under'] .. '[[' .. localisation.txtCategory .. ':' .. localisation.txtManyMissingSourcesCategory .. ']]'
		end
	if (not p.competition) then
		myArgs['under']=myArgs['under']..'[['..localisation.txtCategory .. ':' .. localisation.txtMissingCompetition .. ']]'
		end

	if (not (p.start or p.at or p.finish)) then
		myArgs['under']=myArgs['under']..'[['..localisation.txtCategory .. ':' .. localisation.txtNoDateInformation .. ']]'
		end

	if (not (p.area)) then
		myArgs['under']=myArgs['under']..'[['..localisation.txtCategory .. ':' .. localisation.txtMissingArea .. ']]'
		end

	if (not (p.sport)) then
		myArgs['under']=myArgs['under']..'[['..localisation.txtCategory .. ':' .. localisation.txtMissingSport_Competition .. ']]'
		end
	
--	if (not p.arraySports) then 
--		myArgs['under']=myArgs['under']..'[['..txtCategory .. ':' .. txtMissingSport_Club .. ']]'
--		end
end

function getLink(id,direction)
	local txtRet
	if (id) then
    	txtRet=mw.wikibase.getSitelink(id)
		if not (txtRet) then
			local txtLinkedObjectlabel =mw.wikibase.getLabelByLang(id,txtLanguageCode )
			if (txtLinkedObjectlabel) then
				local txtThisArticlename = mw.wikibase.getSitelink(p.objectMainId)
				local txtLinkedArticlename=txtThisArticlename
				
				-- Step 1: Extract up to two years from str1 using string.gmatch
				local years_in_label = {}
				for year in string.gmatch(txtLinkedObjectlabel, "%d%d%d%d") do
				    table.insert(years_in_label, year)
					end

				local years_in_article = {}
				for year in string.gmatch(txtLinkedArticlename, "%d%d%d%d") do
				    table.insert(years_in_article, year)
					end

				if (direction=='next') then 
					if (#years_in_label >= 2) and (#years_in_article >= 2) then
					    txtLinkedArticlename = string.gsub(txtLinkedArticlename, years_in_article[2], years_in_label[2], 1)
					end
				end
	
				if (#years_in_label >= 1) and (#years_in_article >= 1) then
				    txtLinkedArticlename = string.gsub(txtLinkedArticlename, years_in_article[1], years_in_label[1], 1)
				end
	
				if (direction=='previous') then 
					if (#years_in_label >= 2) and (#years_in_article >= 2) then
					    txtLinkedArticlename = string.gsub(txtLinkedArticlename, years_in_article[2], years_in_label[2], 1)
					end
				end
	
				txtRet = txtLinkedArticlename
				end
			end
    	end

	if (txtRet) then
		if (direction=='previous') then 
	    	txtRet='← [[' .. txtRet .. '|' .. localisation.txtPreviousLabel .. ']]'
	    	end
		if (direction=='next') then 
	    	txtRet='→ [[' .. txtRet .. '|' .. localisation.txtNextLabel .. ']]'
	    	end
    	else
    	txtRet=''
		end
	return txtRet
end

function txtShowTeam_wrapped(obj)
	return txtShowTeam(p.frame,obj.id)
end

function showResults(tbl)
--	p.addContent("debug",tprint(tbl))
	if (#tbl==0) then  -- Need this for some reason as otherwise lua will not treat table with one element as being tables
		tbl[1]=tbl
		end
--	p.addContent("debug",#tbl)
	showPosition(tbl,1)
	showPosition(tbl,2)
	showPosition(tbl,3)
end

function showPosition(tbl,iPos)
	competitorsatposition=p.filter_quick(tbl,"P1352","+" .. iPos)
--	p.addContent("debug",tprint(competitorsatposition))
	local txtRet=''
	for i=1,#competitorsatposition do
		if (p.typeofcompetitors=='teams') then
			txtRet=txtRet .. txtShowTeam(p.frame,competitorsatposition[i].id)
			else
			txtRet=txtRet .. competitorsatposition[i].data
			end				
		txtRet=txtRet .. txtUnpackReference_model(p.frame,competitorsatposition[i].ref,competitorsatposition[i].qid,competitorsatposition[i].pid)
		if (i<#competitorsatposition) then
			txtRet=txtRet .. '<br>'
			end
		end
	local txtLabel
	if (iPos==1) then
		txtLabel=txtWinner
		end
	if (iPos==2) then
		txtLabel=txtSecond
		end
	if (iPos==3) then
		txtLabel=txtThird
	end
	p.addContent(txtLabel,txtRet)
end

function showCountry(obj)
	return p.frame:expandTemplate{ title = localisation.txtFlagIconTemplate , args={getLabelByEntity(obj.id)}} .. ' ' .. obj.data

end

p.filter_quick=function(tbl,txtVariable,selectedvalue)
	local iItems=#tbl										-- Count number of items
	local tblOut={}													-- Empty table that will be filled by the results of the filter query
	for i=1,iItems do												-- Loop through all items
		if (tbl[i][txtVariable]==selectedvalue) then		-- If the data value for the chosen variable of this item is the same as the selected value (for the filter) ...
			table.insert(tblOut,tbl[i])						-- into the out table
		end
	end
	return tblOut
	end

function txtShowTeam(frame,iTeam)
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 = localisation.txtFlagIconTemplate , args={wdCountry}} .. ' [[' ..  txtSitelink .. '|' .. wdCountry .. ']]'
 else
 	local tblTeamname,bIsOk=rm.tblWikilink(iTeam)
	if (tblTeamname.txtSitelink) then
		return '[[' .. tblTeamname.txtSitelink .. '|' .. tblTeamname.txtLabel .. ']]'
		else
		return tblTeamname.txtLabel
		end
 	end
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