Sari la conținut

Modul:FootballClub

De la Wikipedia, enciclopedia liberă

Implementează {{ClubFotbal}} și {{ClubFotbal2}}


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, inv)
	local teamDataRO = initTeamData(itemId)

	local teamData = {}
	
	if itemId ~= nil and mw.ustring.match(itemId, 'Q%d+') then
		teamData.linkTo = mw.wikibase.getSitelink(itemId) or teamDataRO.linkTo or mw.wikibase.getSitelink(itemId, '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
	local nowrapspan = mw.html.create('span'):css('white-space', 'nowrap'):wikitext(teamLink)
	if inv then
		wikitext = wikitext .. tostring(nowrapspan) .. ' [[Fișier:' .. flagName .. '|20px|border]]'
	else
		wikitext = wikitext .. '[[Fișier:' .. flagName .. '|20px|border]] ' .. tostring(nowrapspan)
	end
	local category = '' 
	if clubData.teams[itemId] == nil and teamData.linkTo == nil then
		 category = category..'[[Categorie:ClubFotbal fără legătură Wikidata]]'
	end
	return wikitext..category
end

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

p.byKeyFromFrame = function(frame)
	local args = getArgs(frame)
	local key = args[1]
	local ts = args[2]
	args = frame.args
	local inv = args['inv']
	return p.byKeyFromArgs(key, ts, inv)
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