Saltar para o conteúdo

Módulo:Lista de aparições de equipes

Origem: Wikipédia, a enciclopédia livre.

-- This module implements [[Template:Lista de aparições de equipes]].

local p = {}

local data_competitions
local data_old_names
local function load_data(frame)
	-- Load data module (or its sandbox) and set variables from its exported data.
	if not data_competitions then
		frame = frame or mw.getCurrentFrame()
		local sandbox = frame:getTitle():find('sandbox', 1, true) and '/sandbox' or ''
		local datamod = mw.loadData('Módulo:Lista de aparições de equipes/dados' .. sandbox)
		data_competitions = datamod.competitions
		data_old_names = datamod.old_names
	end
end

local function strip_to_nil(text)
	-- If text is a string, return its trimmed content, or nil if empty.
	-- Otherwise return text (which may, for example, be nil).
	if type(text) == 'string' then
		text = text:match('(%S.-)%s*$')
	end
	return text
end

local function make_options(args)
	-- Return table of options from validated args or throw error.
	local options = {}
	local function valid_integer(name, min, max, is_optional)
		local arg = args[name]
		if arg == nil or arg == '' then
			if is_optional then
				return nil
			end
			error('Parâmetro ' .. name .. ' está ausente')
		end
		arg = tonumber(arg)
		if type(arg) ~= 'number' then
			error('Parâmetro ' .. name .. ' não é um número')
		end
		if math.floor(arg) ~= arg then
			error('Parâmetro ' .. name .. ' não é um número inteiro')
		end
		if not (min <= arg and arg <= max) then
			error('Parâmetro ' .. name .. ' não é válido')
		end
		return arg
	end
	local function valid_text(name)
		local arg = args[name]
		if arg == nil or arg == '' then
			error('Parâmetro ' .. name .. ' está ausente')
		end
		if type(arg) ~= 'string' then
			error('Parâmetro ' .. name .. ' não é um string')
		end
		return arg
	end
	options.competition = valid_text('competição')
	options.team = valid_text('equipe')
	-- Check ROC/TPE
	if options.team=='República da China' then
		local pageYear = tonumber(mw.ustring.match(mw.title.getCurrentTitle().text, '[%d]+')) -- mw.title.getCurrentTitle().text:match('^%d+')
		if pageYear and pageYear > 1950 and pageYear < 1980 then
			options.team = 'Taipé Chinês'
		end
	end
	-- end of ROC/TPE check
	options.competitions = data_competitions[options.competition] or data_competitions[data_old_names[options.competition]]
	local begin_optional
	if options.competitions then
		begin_optional = true
	else
		options.interval = valid_integer('intervalo', 1, 30)
	end
	options.ano_inicio = valid_integer('ano_inicio', 1800, 2100, begin_optional)
	options.ano_fim = valid_integer('ano_fim', 1800, 2100, true)
	if options.ano_inicio and options.ano_fim then
		if options.ano_inicio > options.ano_fim then
			error('Parâmetro ano_fim não deve ser anterior a ano_inicio')
		end
	end
	options.ano_desqualificado = valid_integer('ano_desqualificado', 1800, 2100, true)
	return options
end

local function extract_range(text)
	-- Return first (if text is a single year), or first, last if a range.
	-- The returned values are numbers.
	-- Return nothing if text is invalid.
	local year = text:match('^(%d+)$')
	if year then
		if #year == 4 then
			return tonumber(year)
		end
		return
	end
	local first, dash, last = text:match('^(%d+)(%D+)(%d+)$')
	if not (first and #first == 4) then
		return
	end
	dash = strip_to_nil(dash)
	if not (dash == '-' or dash == '–') then
		return
	end
	if #last ~= 4 then
		if #last == 2 then
			last = first:sub(1, 2) .. last
		else
			return
		end
	end
	first = tonumber(first)
	last = tonumber(last)
	if first < last then
		return first, last
	elseif first == last then
		return first
	end
end

local function competition_absences(data)
	-- Return two tables with absent years and absent year ranges.
	-- Parameter data is an array of strings from template parameters, or
	-- numbers or strings from built-in data.
	-- Parameters that are blank or not numbers or strings are ignored.
	local absent_years, absent_ranges = {}, {}
	for _, item in ipairs(data) do
		if type(item) == 'number' then
			absent_years[item] = true
		else
			item = strip_to_nil(item)
			if type(item) == 'string' then
				local first, last = extract_range(item)
				if not first then
					error('Ano ' .. item .. ' não é válido')
				end
				if last then
					table.insert(absent_ranges, {first, last})
				else
					absent_years[first] = true
				end
			end
		end
	end
	return absent_years, absent_ranges
end

local function competition_information(args)
	-- Return four tables with competition and team information:
	-- * List of competition years that the team attended or could have attended.
	-- * Table of disqualified years (the team was absent, but there is an
	--   article regarding the absent year).
	-- * Table of absent years (when the team did not attend).
	-- * List of pairs of years (absent for each year in range, inclusive).
	local options = make_options(args)
	local absences
	local comp_years = {}
	local ano_inicio = options.ano_inicio
	local ano_fim = options.ano_fim
	local competitions = options.competitions
	if competitions then
		absences = competitions[options.team] or competitions[data_old_names[options.team]]
		ano_inicio = ano_inicio or (absences and absences.ano_inicio) or 0
		ano_fim = ano_fim or (absences and absences.ano_fim) or 9999
		for _, y in ipairs(competitions) do
			if y > ano_fim then
				break
			elseif y >= ano_inicio then
				table.insert(comp_years, y)
			end
		end
	else
		ano_fim = ano_fim or (os.date('!*t').year + options.interval)
		for y = ano_inicio, ano_fim, options.interval do
			table.insert(comp_years, y)
		end
	end
	local anos_desqualificado = {}
	if options.ano_desqualificado then
		-- Input currently only allows entry of a single disqualified year.
		-- However processing works for any number of such years.
		anos_desqualificado[options.ano_desqualificado] = true
	end
	return comp_years, anos_desqualificado, competition_absences(absences or args)
end

local function gameName(year, inputName)
	-- Modifies output of display being sent back to the list
	--  for games that have had a name change but are still considered
	--  the same competition.
	if inputName=="World Athletics Championships" or inputName=="World Championships in Athletics" then
		if year <= 2017 then
			return "World Championships in Athletics"
		else
			return "World Athletics Championships"
		end
	elseif (inputName=="Jogos do Império Britânico"
		or inputName=="Jogos do Império Britânico e da Commonwealth"
		or inputName=="Jogos da Comunidade Britânica"
		or inputName=="Jogos da Commonwealth") then
			if year <= 1950 then
				return "Jogos do Império Britânico"
			elseif year <= 1966 then
				return "Jogos do Império Britânico e da Commonwealth"
			elseif year <= 1974 then
				return "Jogos da Comunidade Britânica"
			else
				return "Jogos da Commonwealth"
			end
	elseif inputName=="Jogos Peninsulares do Sudeste Asiático"
		or inputName=="Jogos do Sudeste Asiático"
		or inputName=="SEAP Games"
		or inputName=="SEA Games" then
			if year <= 1975 then
				return "Jogos Peninsulares do Sudeste Asiático"
			else
				return "Jogos do Sudeste Asiático"
			end
	elseif inputName=="Jogos Asiáticos em Recinto Coberto" or inputName=="Asian Indoor and Martial Arts Games" then
		if year <= 2009 then
			return "Jogos Asiáticos em Recinto Coberto"
		else
			return "Jogos Asiáticos de Artes Marciais e Recinto Coberto"
		end
	elseif inputName=="Jogos do Cruzeiro do Sul" or inputName=="Jogos Sul-Americanos" then
		if year <= 1982 then
			return "Jogos do Cruzeiro do Sul"
		else
			return "Jogos Sul-Americanos"
		end
	elseif inputName=="All-Africa Games" or inputName=="African Games" then
		if year <= 2011 then
			return "All-Africa Games"
		else
			return "African Games"
		end
	else
		return inputName
	end
end

local function teamName(year, inputName, comp)
	-- Modifies output of display being sent back to the list
	--  for games that have had a name change but are still considered
	--  the same competition.
	if inputName=="Essuatíni" or inputName=="Suazilândia" then
		if year < 2018 or year == 2018 and comp == 'Jogos da Commonwealth' then
			return "Suazilândia"
		else
			return "Essuatíni"
		end
	elseif inputName=="Rodésia do Sul" or inputName=="Rodésia" or inputName=="Zimbabwe" then
			if year < 1980 then
				if (comp=="Jogos do Império Britânico"
					or comp=="Jogos do Império Britânico e da Commonwealth"
					or comp=="Jogos da Comunidade Britânica"
					or comp=="Jogos da Commonwealth") then
						return "Rodésia do Sul"
				elseif comp=="Jogos Olímpicos de Verão" or comp=="Jogos Paralímpicos de Verão" then
						return "Rodésia"
				end
			else
				return "Zimbabwe"
			end
	elseif (inputName=="República da China"
			or inputName=="Formosa"
			or inputName=="Taiwan"
			or inputName=="Taipé Chinês") then
				if year <= 1956 or year == 1972 or year == 1976 then
					return "República da China"
				elseif year==1960 then
					return "República da China (Formosa)"
				elseif year==1964 or year==1968 then
					return "Taiwan"
				elseif year > 1976 then
					return "Taipé Chinês"
				end
	elseif inputName=="Rodésia do Norte" or inputName=="Zâmbia" then
			if year <= 1964 then
				return "Rodésia do Norte"
			else
				return "Zâmbia"
			end
	elseif inputName=="Aden" or inputName=="Arábia do Sul" or inputName=="Federação da Arábia do Sul" then
			if year < 1966 then
				return "Aden"
			else
				return "Arábia do Sul"
			end
	elseif inputName=="Guiana Britânica" or inputName=="Guiana" then
			if year < 1966 then
				return "Guiana Britânica"
			else
				return "Guiana"
			end
	elseif inputName=="Tanzânia" or inputName=="Tanganica" then
			if year < 1966 then
				return "Tanganica"
			else
				return "Tanzânia"
			end
	elseif inputName=="Ceylon" or inputName=="Sri Lanka" then
			if year <= 1972 then
				return "Ceylon"
			else
				return "Sri Lanka"
			end
	elseif inputName=="Honduras Britânicas" or inputName=="Belize" then
			if year <= 1973 then
				return "Honduras Britânicas"
			else
				return "Belize"
			end
	elseif inputName=="Daomé" or inputName=="Benim" then
			if year <= 1975 then
				return "Daomé"
			else
				return "Benim"
			end
	elseif inputName=="Alto Volta" or inputName=="Burquina Fasso" then
			if year <= 1983 then
				return "Alto Volta"
			else
				return "Burquina Fasso"
			end
	elseif inputName=="Burma" or inputName=="Mianmar" then
			if year < 1990 then
				return "Burma"
			else
				return "Mianmar"
			end
	elseif inputName=="Alemanha Ocidental" or inputName=="Alemanha Ocidental" then
		if comp == "Jogos Paralímpicos de Verão" or comp == "Jogos Paralímpicos de Inverno" then
			if year < 1992 then
				return "Alemanha Ocidental"
			else
				return "Alemanha Ocidental"
			end
		end
	elseif inputName=="República Democrática do Congo" or inputName=="Zaire" or inputName=="Congo-Kinshasa" then
		if year < 1971 then
			return "Congo-Kinshasa"
		elseif year >= 1971 and year <=1996 then
			return "Zaire"
		else
			return "República Democrática do Congo"
		end
	elseif (inputName=="Atletas Olímpicos Individuais" 
		or inputName=="Atletas Olímpicos Independentes" 
		or inputName=="Participantes Olímpicos Independentes"
		or inputName=="Atletas Olímpicos da Rússia"
		or inputName=="ROC") then
		if year == 1992 or year==2014 then
			return "Participantes Olímpicos Independentes"
		elseif year == 2000 then
			return "Atletas Olímpicos Individuais"
		elseif year == 2012 or year==2016 then
			return "Atletas Olímpicos Independentes"
		elseif year == 2018 then
			return "Atletas Olímpicos da Rússia"
		elseif year == 2020 then
			return "ROC"
		else
			return inputName
		end
	elseif inputName=="Sérvia e Montenegro" or inputName=="Iugoslávia" then
		if year < 2004 then
			if comp == "Jogos do Mediterrâneo" then
				return "Iugoslávia"
			else
				return "Iugoslávia"
			end
		else
			return "Sérvia e Montenegro"
		end
	elseif (inputName=="Equipe Olímpica de Refugiados" 
		or inputName=="Equipe Olímpica de Refugiados") then
		if year == 2016 then
			return "Equipe Olímpica de Refugiados"
		elseif year == 2020 then
			return "Equipe Olímpica de Refugiados"
		else
			return inputName
		end
	elseif (inputName=="Participantes Paralímpicos Independentes" 
		or inputName=="Atletas Paralímpicos Individuais" 
		or inputName=="Atletas Paralímpicos Independentes"
		or inputName=="RPC"
		or inputName=="Neutral Paralympic Athletes") then
		if year == 1992 then
			return "Participantes Paralímpicos Independentes"
		elseif year == 2000 then
			return "Atletas Paralímpicos Individuais"
		elseif year==2016 then
			return "Atletas Paralímpicos Independentes"
		elseif year==2018 then
			return "Atletas Paralímpicos Neutros"
		elseif year == 2020 or year==2022 then
			return "RPC"
		else
			return inputName
		end
	elseif inputName=="Macedônia do Norte" or inputName=="Macedônia" then
		if year < 2019 then
			return "Macedônia"
		else
			return "Macedônia do Norte"
		end
	elseif inputName=="Malásia" or inputName=="Malaia" then
		if year < 1963 then
			return "Malaia"
		else
			return "Malásia"
		end
	elseif inputName=="Gana" or inputName=="Costa Dourada" then
		if year < 1957 then
			return "Costa Dourada"
		else
			return "Gana"
		end
	elseif inputName=="Atletas Independentes da FINA"
		or inputName=="Equipe de Refugiados da FINA"
		or inputName=="Atletas da FINA" then
		if year==2017 or year==2019 then
			return "Atletas Independentes da FINA"
		elseif year==2022 then
			return "Equipe de Refugiados da FINA"
		else
			return "Atletas da FINA"
		end
	end
	
	return inputName
end

function p._main(args)
	load_data()  -- in case this function is called by another module
	local list = require('Módulo:Lista').horizontal
	local competitions, anos_desqualificado, absent_years, absent_ranges = competition_information(args)
	local current_year = os.date('!*t').year
	local function is_absent(y)
		if absent_years[y] then
			return true
		end
		for _, range in ipairs(absent_ranges) do
			if range[1] <= y and y <= range[2] then
				return true
			end
		end
		return false
	end
	local appearances = {}
	local absent_first, absent_last
	for i = 1, #competitions + 1 do  -- +1 to handle any trailing absences
		local y = competitions[i]
		if y and is_absent(y) then
			if absent_first then
				absent_last = y
			else
				absent_first = y
			end
		else
			if absent_first then
				table.insert(appearances,
					'<span style="color:gray">' ..
					(absent_last and (absent_first .. '–' .. absent_last) or absent_first) ..
					'</span>')
				absent_first, absent_last = nil, nil
			end
			if y then
				local display = tostring(y)
				if y > current_year then
					display = '<i>' .. display .. '</i>'
				end
				if anos_desqualificado[y] then
					display = '<del>' .. display .. '</del>'
				end
				local compName = gameName(y, args['competição'])
				local teamOut = teamName(y, args.equipe, args['competição'])
				if compName == 'Campeonato Mundial de Esqui Alpino' then
					table.insert(appearances, string.format(
					'[[%s no %s de %d|%s]]',
					teamOut, compName, y, display
					))
				else
					table.insert(appearances, string.format(
						'[[%s nos %s de %d|%s]]',
						teamOut, compName, y, display
					))
				end
			end
		end
	end
	return list(appearances)
end
				
function p.main(frame)
	load_data(frame)
	return p._main(frame.args['equipe'] and frame.args or frame:getParent().args)
end

return p