Modul:FootballClub

De la Wikipedia, enciclopedia liberă
Sari la navigare Sari la căutare

Implementează {{ClubFotbal}}


local getArgs = require('Modul:Arguments').getArgs
local illWd = require('Modul:Ill-wd')
local StringUtils = require('Modul:StringUtils')
local wikidata = require('Modul:Wikidata')
local DateUtils = require('Modul:DateUtils')
local clubData = mw.loadData('Modul:FootballClub/data')
local getTimestampedValueFromArray = require('Modul:TimestampedTable').getValueByTimestamp
local p = {}

local prependToString = StringUtils._prependToString

local DEFAULT_FLAG = 'Flag of None.svg'

local function initTeamData(itemId)
	local teamData = {}
	local teamDataRO = {}
	if itemId ~= nil and mw.ustring.match(itemId, 'Q%d+') then
		teamDataRO = clubData.teams[itemId] or {}
	end
	return teamDataRO
end

p.flagFromArgs = function(itemId, ts, teamDataRO)
	itemId = itemId or mw.wikibase.getEntityIdForCurrentPage()
	if not teamDataRO then teamDataRO = initTeamData(itemId) end
	
	if teamDataRO.flag then
		if type(teamDataRO.flag) == 'table' then
			return getTimestampedValueFromArray(teamDataRO.flag, ts)
		else
			return teamDataRO.flag
		end
	end
	return DEFAULT_FLAG
end

p.labelFromArgs = function(itemId, ts, teamDataRO)
	itemId = itemId or mw.wikibase.getEntityIdForCurrentPage()
	if not teamDataRO then teamDataRO = initTeamData(itemId) end

	return teamDataRO.roLabel and (type(teamDataRO.roLabel) == 'table' and getTimestampedValueFromArray(teamDataRO.roLabel, ts) or teamDataRO.roLabel)
end

p.byItemFromArgs = function(itemId, ts)
	local teamDataRO = initTeamData(itemId)

	local teamData = {}
	
	if itemId ~= nil and mw.ustring.match(itemId, 'Q%d+') then
		teamData.linkTo = mw.wikibase.sitelink(itemId) or teamDataRO.linkTo or mw.wikibase.getEntity(itemId):getSitelink('enwiki')
	else
		teamData.linkTo = nil
	end

	teamData.flag = p.flagFromArgs(itemId, ts, teamDataRO)

	teamData.roLabel = p.labelFromArgs(itemId, ts, teamDataRO)
	
	local wikitext = ''
	local teamLink = ''

	if teamData.linkTo then
		if StringUtils._startsWith({teamData.linkTo, '[['}) then
			teamLink = teamData.linkTo
		else
			local teamLinkTitle = mw.title.new(teamData.linkTo, '')
			if teamLinkTitle.exists then
				teamLink = '[[' .. teamData.linkTo .. prependToString({teamData.roLabel, '|'}) .. ']]'
			else
				teamLink = illWd.fromArgs(itemId, teamData.roLabel, teamData.linkTo)
			end
		end
	else
		if itemId ~= nil and mw.ustring.match(itemId, 'Q%d+') then
			teamLink = illWd.fromArgs(itemId, teamData.roLabel, teamData.roLabel)
		else 
			local itemIdPage = mw.title.new(teamData.roLabel or itemId)
			if itemIdPage and itemIdPage.exists then
				teamLink = '[[' .. itemId .. ']]'
			else 
				teamLink = itemId
			end
		end
	end
	
	local flagName = teamData.flag or DEFAULT_FLAG
	wikitext = wikitext .. '[[Fișier:' .. flagName .. '|20px|border]] '
	local nowrapspan = mw.html.create('span'):css('white-space', 'nowrap'):wikitext(teamLink)
	wikitext = wikitext .. tostring(nowrapspan)
	return wikitext
end

p.byKeyFromArgs = function(key, ts)
	local entityId = clubData.entities[key or '']
	return p.byItemFromArgs(entityId or key, ts)
end

p.byKeyFromFrame = function(frame)
	local args = getArgs(frame)
	local key = args[1]
	local ts = args[2]
	return p.byKeyFromArgs(key, ts)
end

p.flagLinkFromArgs = function(entityId, ts)
	local flag = p.flagFromArgs(entityId, ts)
	return '[[Fișier:' .. flag .. '|' .. (size or '20px') .. '|border]] '
end

p.flagFromFrame = function(frame) 
	local args = getArgs(frame)
	local entityId = args[1] or args['q']
	local ts = args[2] or args['ts']
	local size = args[3] or args['size']
	
	return p.flagLinkFromArgs(entityId, ts)
end

return p