יחידה:תבנית מידע/אישיות כדורגל

מתוך ויקיפדיה, האנציקלופדיה החופשית

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:תבנית מידע/אישיות כדורגל/תיעוד

local WIKIDATA_CLUBS = 'P54'
local NATIONAL_FOOTBALL_TEAM = 6979593
local WOMEN_NATIONAL_FOOTBALL_TEAM = 23905105

--local NATIONAL_FOOTBALL_TEAM_UNDER_17 = Q23901123
--UNDER 18 =Q23904671
--UNDER 19 =Q23901137
--UNDER 20 =Q23895910
--local NATIONAL_FOOTBALL_TEAM_UNDER_21 = 6979740

local FOOTBALL_NATIOANL_TEAM_TYPE = { NATIONAL_FOOTBALL_TEAM, WOMEN_NATIONAL_FOOTBALL_TEAM }
local CLUBS_TYPE = {476028, 28140340, 51481377}

local PropertyLink = require('Module:PropertyLink')

local function extractYear(v)
	local yearstr = mw.text.trim(v[1].datavalue.value.time, "+")
	yearstr = mw.ustring.match(yearstr, "^\-?%d+")
	return tonumber(yearstr)
end

local function formatNationalFootball(propValue)
	local label, lang = mw.wikibase.getLabelWithLang( "Q" .. propValue.value['numeric-id'] ) 
	if not label or lang~='he' then
		return  '[[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית: ' .. mw.wikibase.label( 'P54' ) .. ']][[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית]]', true
	end
	local sitelink=  mw.wikibase.sitelink("Q" .. propValue.value['numeric-id'])
	label = mw.ustring.gsub(label, 'נבחרת (%a+) בכדורגל נשים', '%1')
	label = mw.ustring.gsub(label, 'נבחרת (%a+) בכדורגל', '%1')
	label = mw.ustring.gsub(label, 'הנבחרת השנייה של (%a+) בכדורגל', '%1-2')
	if sitelink then
		return mw.ustring.format('[[%s|%s]]', sitelink, label), false
	else
		return mw.ustring.format('%s', label), false
	end
end

local function wikidataClubs(entityId, filterTypes)
	local clubs = {}
	local warnings = ''
	local clubsClaims = mw.wikibase.getBestStatements(entityId, WIKIDATA_CLUBS)
	if (not clubsClaims) or (#clubsClaims==0) then return clubs, warnings end
	for i, property in ipairs(clubsClaims) do
		local propValue = property.mainsnak and property.mainsnak.datavalue
		local targetEntity =  "Q" .. propValue.value['numeric-id'] 
		local targetIs =  mw.wikibase.getBestStatements(targetEntity, 'P31')
		local isFootballClub = false
		for j, targetProp in pairs(targetIs) do
			if filterTypes[targetProp.mainsnak.datavalue.value['numeric-id']] then
				targetIs=targetProp.mainsnak.datavalue.value['numeric-id']
				isFootballClub = true
				break
			end
		end
		
		if isFootballClub then
			local formattedValue, missingTranslation
			if targetIs == NATIONAL_FOOTBALL_TEAM or targetIs == WOMEN_NATIONAL_FOOTBALL_TEAM or targetIs==NATIONAL_FOOTBALL_TEAM_UNDER_21 then
				formattedValue, missingTranslation  = formatNationalFootball(propValue)
			end

			if not formattedValue then
				formattedValue, missingTranslation = PropertyLink.formatEntity("Q" .. propValue.value['numeric-id'])
			end
	        if missingTranslation then
				warnings =  '[[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית: ' .. mw.wikibase.label( 'P54' ) .. ']][[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית]]'
	    	end
			local club = {
				name=formattedValue
			}
			local isValidClub = true
			if  property.qualifiers then
				if property.qualifiers['P580'] and property.qualifiers['P580'][1] and property.qualifiers['P580'][1].datavalue and property.qualifiers['P580'][1].datavalue.value  then 
					club['startyear'] = extractYear(property.qualifiers['P580'])
				else
					isValidClub = false -- no start year data, skip
				end
	    		if property.qualifiers['P582']  and property.qualifiers['P582'][1] and property.qualifiers['P582'][1].datavalue and property.qualifiers['P582'][1].datavalue.value then 
	    			club['endyear'] =  extractYear(property.qualifiers['P582'])
	    		end
	    		if property.qualifiers['P1350'] then
	    			if property.qualifiers['P1350'][1] and property.qualifiers['P1350'][1].datavalue then
	    				club['games'] =  tonumber(property.qualifiers['P1350'][1].datavalue.value.amount)
	    			else
	    				--unhandeld case with games (examples may be unknown value)
	    				mw.logObject(property.qualifiers['P1350'])
	    			end
	    		else
	    			club['games'] = nil
	    		end
	    		if property.qualifiers['P1351'] then
					club['goals'] =  tonumber(property.qualifiers['P1351'][1].datavalue.value.amount)
				end
				if property.qualifiers['P1642'] then
					local transactionSitelink = mw.wikibase.sitelink(property.qualifiers['P1642'][1].datavalue.value.id)
					if  transactionSitelink=='השאלה (ספורט)' then
						club.name = '[['..transactionSitelink..'|←]] ' .. club.name
					end
				end
			else
				isValidClub = false -- no details, skip
			end
			
			if isValidClub then
				table.insert(clubs, club)
			end
		end
	end
	
	return clubs, warnings
end

function formatClubRow(root, clubArgs) 
	local yearsRange = clubArgs.startyear
	if clubArgs.startyear ~= clubArgs.endyear then
		if clubArgs.endyear then
			yearsRange = clubArgs.startyear .. '–'.. clubArgs.endyear
		else
			yearsRange = clubArgs.startyear
		end
	end
	
	local clubTag = root:tag('tr')
            :tag('td')
                :wikitext(yearsRange)
                :done()
			:tag('td')
                :wikitext(clubArgs.name)
                :done()
    if clubArgs.games~=nil then            
    	if clubArgs.goals==nil then
    		-- omit the goals, may consider difference handling with more cases
			clubTag:tag('td')
				:tag('span'):wikitext(clubArgs.games):attr('title','משחקים'):done()
		        :done()    		
    	else
			clubTag:tag('td')
				:tag('span'):wikitext(clubArgs.games):attr('title','משחקים'):done()
				:tag('span'):wikitext(' ('.. clubArgs.goals ..')'):attr('title', 'שערים'):done()
		        :done()
	    end
    end
end

local function sortByStart(x, y)
	if x.startyear and y.startyear then
		return x.startyear<y.startyear
	end
	return true
end

function formatClubs(frame)
	local clubsType = frame.args['סוג']
	local width = frame.args['רוחב'] or '100%'
	local entityId = frame.args['יישות'] or mw.wikibase.getEntityIdForCurrentPage() 
	if clubsType == 'מועדונים' then
		clubsType = CLUBS_TYPE
	elseif clubsType == 'נבחרות' then
		clubsType = FOOTBALL_NATIOANL_TEAM_TYPE
	end
	local clubTypeFilter = {}
	for i,k in ipairs(clubsType) do
		clubTypeFilter[k] = 1
	end
	local clubs, warnings = wikidataClubs(entityId, clubTypeFilter)
	if (warnings and #warnings >0) or #clubs==0 then
		return warnings
	end
	
	table.sort(clubs, sortByStart)
	local root = mw.html.create('table'):css('width', width)
	local sumGames = 0
	local sumGoals = 0
	local hasGames = true
	for k,v in pairs(clubs) do
		-- avoid nil arithmetic on the below
		if v.games == nil or v.goals==nil  then
			hasGames = false
		else
			sumGames = sumGames+v.games
			sumGoals = sumGoals+v.goals
		end
		formatClubRow(root, v)
	end
	
	-- sum row
	if #clubs > 1 and hasGames then
		root:tag('tr')
	        :tag('td')
	            :wikitext('סה"כ'):css('font-weight','bold')
	            :done()
			:tag('td')
	            :wikitext('')
	            :done()
			:tag('td'):css('font-weight','bold')
				:tag('span'):wikitext(sumGames):attr('title','משחקים'):done()
				:tag('span'):wikitext(' ('.. sumGoals ..')'):attr('title', 'שערים'):done()
	            :done()
    end
    -- edit in wikidata
	root:tag('tr')
            :tag('td')
            :attr('colspan',3):css('text-align','left')
            :wikitext(' [[File:Blue pencil RTL.svg|15px|link=https://www.wikidata.org/wiki/'..entityId.. '?uselang=he#'..WIKIDATA_CLUBS..'|עריכת הנתון בוויקינתונים]]')
            :done()
	
	return tostring(root)..warnings
end

return {
	['מועדונים']=formatClubs
}