Country alias[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: létrehozás]

-- This module returns the country name or the flag name for a country,
-- based on the three-letter IOC/CGA/FINA alias.

--[[
The following country code is used for multiple countries:
    ANG (workaround: added ANG_CGF for use with Commonwealth Games)

The following names have different names/flags based on sport/year
    Great Britain (and N.I.)         GBR, GBR_WCA (latter added to add text in parens)
    Hong Kong                        HKG, HKG_CGF (latter added to keep colonial flag)
    Individual Olympic Athletes      IOA, IOA_2000 (IOA changed to Independent Olympic Athletes in 2012)
    Russian Athletes				 OAR (2018), ROC_2020 (2020 Summer, 2022 Winter)
    SWZ                              Swaziland became Eswatini after the 2018 Commonwealth Games
    MKD								 Macedonia became North Macedonia in 2019
    ART								 No "Athlete" before Refugee Team @ 2017 AIMAG

The following countries have multiple aliases due to CGF/IOC/FINA/IAAF/etc differences, or deprecated uses
    Anguilla                         AIA, ANG_CGF
    Antigua and Barbuda              ANT, ATG
    Bahrain                          BHN, BHR, BRN
    Curaçao                          CUR, CUW
    East Timor						 TLS, TMP
    Faroe Islands                    FAR, FRO
    Guernsey                         GGY, GUE
    Iran                             IRI, IRN
    Ireland                          IRE, IRL - IRE is *only* for CGF apps
    Jersey                           JER, JEY
    Lebanon                          LBN, LIB
    Montserrat                       MNT, MSR
    Nicaragua                        NCA, NIC
    Norfolk Island                   NFI, NFK
    Oman                             OMA, OMN
    Refugee Olympic Team             ROA, ROT, EOR
    Romania                          ROM, ROU
    Saint Helena                     SHE, SHN
    Saint Vincent and the Grenadines SVG, VIN
    Sarawak                          SAR, SWK
    Singapore                        SGP, SIN
    South Africa                     RSA, SAF
    Tonga                            TGA, TON
    Trinidad and Tobago              TRI, TTO
    Turks and Caicos Islands         TCA, TCI, TKS

Oddity that needs to be revisited
    French Polynesia                 PYF, TAH - TAH has been converted to Tahiti per SILENCE
]]

local function stripToNil(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 yes(parameter)
	-- Return true if parameter should be interpreted as "yes".
	return ({ y = true, yes = true, on = true, [true] = true })[parameter]
end

local function getAlias(args)
	-- Return alias parameter, possibly modified for exceptional cases.
	local alias = stripToNil(args.alias)
	local games = stripToNil(args.games)
	local year = tonumber(args.year)
	local fullName = stripToNil(args.fullName)
	if alias == 'ANG' then
		if games == 'Commonwealth Games' then
			alias = 'ANG_CGF'
		end
	elseif alias == 'ART' then
		if games == 'Asian Indoor and Martial Arts Games' then
			alias = 'ART_AIMAG'
		end
	elseif alias == 'GBR' then
		if games == 'World Championships in Athletics' or games == 'World Athletics Championships' or games == 'European Athletics Championships' then
			alias = 'GBR_WCA'
		elseif games == 'European Championships' then
			if year == 2018 then
				alias = 'GBR_WCA'
			end
		end
	elseif alias == 'HKG' then
		if games == 'Commonwealth Games' then
			alias = 'HKG_CGF'
		end
	elseif alias == 'IOA' then
		if year == 2000 then
			alias = 'IOA_2000'
		end
	elseif alias == 'MAL' then
		if year and year > 1963 then
			alias = 'MAS'
		end
	elseif alias == 'SWZ' then
		if fullName then
			if year and year >= 2018 and fullName ~= '2018 Commonwealth Games' then
				alias = 'SWZ_YO2018'
			end
		elseif year and year >= 2018 and games ~= 'Commonwealth Games' then
			alias = 'SWZ_YO2018'
		end
	elseif alias == 'MKD' then
		if year and year >= 2019 then
			alias = 'MKD_2019'
		end
	elseif alias == 'VNM' then
		if year and year <= 1954 then
			alias = 'VIE'
		end
	elseif alias == 'ROC' or alias == 'RUS' then
		if year and year==2020 and games=='nyári olimpiai játékok' then
			alias = 'ROC_2020'
		elseif year and year==2022 and games=='téli olimpiai játékok' then
			alias = 'ROC_2020'
		elseif year and year==2020 and games=='nyári olimpiai játékok' then
			alias = 'RPC'
		end
	end
	return alias
end

local function getFlag(args, country)
	-- Return name of flag selected from country data (nil if none defined).
	local year = tonumber(args.year)
	local games = stripToNil(args.games)
	if games then
		local gdata = country[games]
		if gdata then
			if type(gdata) == 'string' then
				return gdata
			end
			if gdata[year] then
				return gdata[year]
			end
		end
	end
	for _, item in ipairs(country) do
		if type(item) == 'string' then
			return item
		end
		if year and year <= item[1] then
			return item[2]
		end
	end
end

local data = mw.loadData('Modul:Country alias/data')
local function countryAlias(args)
	local alias = getAlias(args)
	local country = data.countries[alias] or data.countries[data.countryAliases[alias]]
	local function quit(message)
		return args.error or error(message)
	end
	if not country then
		return quit('Helytelen kód: ' .. tostring(alias))
	end
	if yes(args.flag) then
		return getFlag(args, country) or quit('A(z) ' .. alias.. ' kódhoz nincs zászló definiálva')
	else
	return country.name or quit('A(z) ' .. alias .. ' kódhoz nincs országnév definiálva')
	end
end

local function countryNevelo(args)
	local alias = getAlias(args)
	local country = data.countries[alias] or data.countries[data.countryAliases[alias]]
	local function quit(message)
		return args.error or error(message)
	end
	if not country then
		return quit('Helytelen kód: ' .. tostring(alias))
	end
	if country.orszagnevelo == nil then
		return ''
	else
		return country.orszagnevelo .. ' '
	end
end

local function countryMelleknev(args)
	local alias = getAlias(args)
	local country = data.countries[alias] or data.countries[data.countryAliases[alias]]
	local function quit(message)
		return args.error or error(message)
	end
	if not country then
		return quit('Helytelen kód: ' .. tostring(alias))
	end
	if country.melleknev == nil then
		return quit('A(z) ' .. alias.. ' kódhoz nincs melléknév definiálva')
	else
		return country.melleknev
	end
end

local function countryDispName(args)
	local alias = getAlias(args)
	local country = data.countries[alias] or data.countries[data.countryAliases[alias]]
	local function quit(message)
		return args.error or error(message)
	end
	if not country then
		return quit('Helytelen kód: ' .. tostring(alias))
	end
	if country.dispname == nil then
		return country.name
	else
		return country.dispname
	end
end

 --ÉVSZÁM NÉVELŐJE
local function evnevelo(args, gamesyear)
 	local gamesyear = gamesyear
	gamesyear = mw.text.trim( gamesyear )
first = mw.ustring.sub(gamesyear, 1, 1) -- évszám első karaktere
length = mw.ustring.len(gamesyear) -- évszám hossza (karakterek száma)
	if  first == '5' or (length>=4 and first == '1') then
    evnevelo = 'az'
    	else
    evnevelo = 'a'
	end
end

local function CallIfexist(args) -- Lista, amikor nem kell ifexist
 	local NOBkod = stripToNil(args.code)
 	local ev = tonumber(args.gamesyear)
 	local tipuskod = stripToNil(args.games)
 	--ev = 1960
 	--tipuskod = 'nyári'
--KIMENETELEK
    --ifexist kivételek
    --AMI BIZTOSAN LÉTEZIK ÉS ÉVCIKKRE LINKELHETŐ
    --ÉV SZERINT
if
        (ev >=1964 and ev <=2016 and tipuskod == 'nyári') -- nyári 1972 és 2016 között
        or (ev == 1896 and tipuskod == 'nyári') -- nyári 1896
        --típusonként
        or (ev <= 2014 and tipuskod == 'téli') -- téli 1924 és 2014 között
    --EGYEDIEK
        --Oroszország 2020 nyári
        or (ev == 2020 and NOBkod == 'ROC') -- ROC (Orosz Olimpiai Bizottság) 2020 nyári
        --Oroszország 2018 téli
        or (ev == 2018 and NOBkod == 'OAR') -- OAR (Oroszország) 2018 téli
        --Korea 2018 téli
        or (ev == 2018 and NOBkod == 'COR') -- COR (Korea) 2018 téli
    --NEMZETEK SZERINT
        or (NOBkod == 'HUN' and ev> 0 ) -- Magyarország
        --kivételek 1900 és 1936 között
        or (NOBkod == 'LUX' and (ev>=1924 and ev<=1936)) -- Luxemburg 1924 és 1936 között
        --kivételek 1908 és 1920 között
        or (NOBkod == 'ARG' and (ev>=1908 and ev<=1920)) -- Argentína 1908 és 1920 között
        then CallIfexist = 'evcikk'
    --AMI BIZTOSAN NEM LÉTEZIK ÉS FŐCIKKRE LINKELHETŐ
    elseif
        (ev == 0) -- ha NOB, vagy NOBcsapatnál nincs megadva évszám
     or ((tipuskod == 'nyári') and -- nyáriak
           (ev >=1900 and ev <= 1948 and NOBkod  == 'GRE')
        or (ev >=1956 and ev <= 1960 and NOBkod  == 'GRE')
        or (ev >=1900 and ev <= 1948 and NOBkod  == 'USA')
        or (ev >=1956 and ev <= 1960 and NOBkod  == 'USA')
        or (ev >=1952 and ev <= 1960 and NOBkod  == 'URS')
        or (ev >=1900 and ev <= 1936)) -- mindegyik 1900 és 1936 között
        then CallIfexist = 'focikk'
    else
    	CallIfexist = 'igen'
end
end

local function flagIOC(frame)
	-- Implement {{flagIOC}} which previously called this module three times.
	-- Returns <flag> <country link> <athletes>, with the third value optional
	local args = frame:getParent().args
	local code = stripToNil(args[1]) or error('flagIOC parameter 1 should be a country code') -- kód
	local gamesyear = stripToNil(args[2]) -- évszám
	local games = stripToNil(args[3]) -- típus
	local athletes = stripToNil(args[4]) -- versenyzők száma
	local menathletes = stripToNil(args[5])
	local womenathletes = stripToNil(args[6])
	games2 = games and (games .. ' olimpiai játékok') or 'olimpiai játékok' -- típus kibővítve az "olimpiai játékok" szöveggel
	gamesyear = gamesyear or '' -- évszám egyenlő az évszám vagy semmi (ha nincs megadva)
		local szocikk = ''
		local szocikk2 = '{name} az olimpiai játékokon' -- főcikk
	if gamesyear == '' then -- ha nincs év és típus, akkor "az" szó (linkelés "...az olimpiai játékokon" cikkekre)
		szocikk = szocikk2
	else -- ha van év és típus, akkor évnévelő és évszámmal linkelés a konkrét cikkre
		evnevelo(args, gamesyear) --évszám névelőjánek behívása
		local parmscall = { --parmscall paraméterek
		code = code,
		gamesyear = gamesyear,
		games = games,
	}
		CallIfexist(parmscall) --IFexist adatok behívása a parmscall paraméterekkel
		local szocikk1 = '{name} ' .. evnevelo .. ' {year}. évi {games}on' -- évcikk
		if CallIfexist == 'igen' then
		szocikk = frame:preprocess( '{{#ifexist: ' .. szocikk1 .. '|' .. szocikk1 .. '|' .. szocikk2 .. '}}' )
		elseif CallIfexist == 'evcikk' then
		szocikk = szocikk1
		elseif CallIfexist == 'focikk' then
		szocikk = szocikk2
		end
	end
	local parms = { -- parms paraméterbe:
		alias = code, -- alias egyenlő code
		fullName = gamesyear .. ' ' .. games2, -- fullName egyenlő "évszám típus"
		year = gamesyear:match('^%d+'), -- year egyenlő évszám
		games = games2, -- games egyenlő típus kibővítve az "olimpiai játékok" szöveggel
	}
	local fullName = countryAlias(parms) -- a fullName paraméterbe az ország nevének behívása
	local orszagnevelo = countryNevelo(parms) -- az orszagnevelo paraméterbe az ország névelőjének behívása
	parms.flag = true -- átváltás az ország zászlójának behívására
	local dispName = countryDispName(parms) -- a dispName paraméterbe az ország megjelenítési nevének behívása, ha van
	return (('[[File:{flag}|22x20px|border|alt=|link=]]&nbsp;[[' .. szocikk .. '|{dispname}]]{athletes}') -- alapszöveg, a {} közötti részeket lentebb kicseréli
		:gsub('{(%w+)}', {
			athletes = athletes and
				('&nbsp;<span style="font-size:90%;">(' .. athletes .. ')</span>') or
				'',
			flag = countryAlias(parms),  -- zászló behívása és a 'flag' cseréje a behívott zászlóra
			games = games2, --'games' cseréje a behívott típusra
			year = gamesyear, --'year' cseréje a megadott évszámra
			name = orszagnevelo .. fullName, --'name' cseréje a behívott országnévre
			dispname = dispName, --'dispname' cseréje a behívott megjelenítési névre
		}))
end

local function flagXYZ(frame)
	-- Implement {{flagIOC2}} and its variants which previously called this module three times.
	-- Returns one of four possible outputs:
	--	from flagIOC2:			<flag> <country link> <athletes>, with the third value optional
	--	from flagIOC2team:		<flag> <country link> <country alias>
	--	from flagIOC2athlete:	<flag> <athlete(s)> <country alias/link>
	--	from flagIOC2medalist:	<athlete(s)><br><flag> <country link>
	local args = frame:getParent().args
	local dispType = stripToNil(frame.args['type'])
	local code=''
	local games=''
	local athletes=''
	local team=''
	if dispType == 'name' or dispType == 'csapat' then
		code = stripToNil(args[1]) or error('Az 1. paraméternek kódnak kell lennie')
		gamesyear = stripToNil(args[2]) or error('A 2. paraméternek évszámnak kell lennie')
		games = stripToNil(args[3]) or error('A 3. paraméternek típusnak kell lennie')
		athletes = stripToNil(args[4])
	elseif dispType == 'sportoló' or dispType == 'érmes' then
		athletes = stripToNil(args[1]) or error('Az 1. paraméternek névnek kell lennie')
		code = stripToNil(args[2]) or error('A 2. paraméternek kódnak kell lennie')
		gamesyear = stripToNil(args[3]) or error('A 3. paraméternek évszámnak kell lennie')
		games = stripToNil(args[4]) or error('A 4. paraméternek típusnak kell lennie')
	elseif dispType == 'vál' or dispType == 'válj' then
		team = stripToNil(args[1]) or error('Az 1. paraméternek válogatottnévnek kell lennie')
		code = stripToNil(args[2]) or error('A 2. paraméternek kódnak kell lennie')
		gamesyear = stripToNil(args[3]) or error('A 3. paraméternek évszámnak kell lennie')
		games = stripToNil(args[4]) or error('A 4. paraméternek típusnak kell lennie')
	end
	evnevelo(args, gamesyear) --évszám névelőjánek behívása
		local parmscall = { --parmscall paraméterek
		code = code,
		gamesyear = gamesyear,
		games = games,
	}
	CallIfexist(parmscall) --IFexist adatok behívása
	games = games and (games .. ' olimpiai játékok') or 'olimpiai játékok'
	if team ~= nil then
        	--Kikeressük a 'válogatott' elé kerülő sportágat jelölő szócskat, es meghatározzuk a szótagszámát
        	--Ugyanis 6 szótagszám alatt mindenképpen egybe kell írni a 'válogatott'-tal. (pl. rögbiválogatott)
        	sportSzavak = mw.text.split(team, " ", true)
        	prefixValogatott = sportSzavak[table.maxn(sportSzavak)]
        	_, szotagszam=mw.ustring.gsub(prefixValogatott, "[AÁEÉIÍOÓÖŐUÚÜŰaáeéiíoóöőuúüű]", "")
        	
        	-- A 'válogatott' szó 4 szótagú tehát ha a prefix 2 szótagnál nem több, akkor egybeírjuk
        	if szotagszam <= 2 then
				team = team .. 'válogatott'
			else
				team = team .. '-válogatott'
        	end
        else
        end
	local parms = {
		alias = code,
		fullName = gamesyear .. ' ' .. games,
		year = gamesyear:match('^%d+'),
		games = games,
	}
	local dispName = countryDispName(parms) -- a dispName paraméterbe az ország megjelenítési nevének behívása, ha van
--	games = (games .. ' olimpiai játékok')
	local fullName = countryAlias(parms)  -- a fullName paraméterbe az ország nevének behívása
	local orszagnevelo = countryNevelo(parms) -- az orszagnevelo paraméterbe az ország névelőjének behívása
	local melleknev = countryMelleknev(parms) -- a melleknev paraméterbe az ország melléknevének behívása
	local szocikk = ''
	local szocikk1 = '{name} ' .. evnevelo .. ' {year}. évi {games}on' -- évcikk
	local szocikk2 = '{name} az olimpiai játékokon' -- főcikk
	parms.flag = true
	if CallIfexist == 'igen' then
		szocikk = frame:preprocess( '{{#ifexist: ' .. szocikk1 .. '|' .. szocikk1 .. '|' .. szocikk2 .. '}}' )
		elseif CallIfexist == 'evcikk' then
		szocikk = szocikk1
		elseif CallIfexist == 'focikk' then
		szocikk = szocikk2
	end
	if dispType == 'name' then
		szocikk = (('[[File:{flag}|22x20px|border|alt=|link=]]&nbsp;[[{name} ' .. evnevelo .. ' {year}. évi {games}on|{dispName}]]{athletes}')
			:gsub('{(%w+)}', {
				athletes = athletes and
					('&nbsp;<span style="font-size:90%;">(' .. athletes .. ')</span>') or
					'',
				flag = countryAlias(parms),
				games = games,
				year = gamesyear,
				name = orszagnevelo .. fullName,
				dispName = dispName,
			}))
		return szocikk
	elseif dispType == 'csapat' then
		return (('[[File:{flag}|22x20px|border|alt=|link=]]&nbsp;[[' .. szocikk .. '|{dispName}]]{alias}')
			:gsub('{(%w+)}', {
				alias = ('&nbsp;<span style="font-size:90%;">(' .. code .. ')</span>'),
				flag = countryAlias(parms),
				year = gamesyear,
				games = games,
				name = orszagnevelo .. fullName,
				dispName = dispName,
			}))
	elseif dispType == 'sportoló' then
		return (('[[File:{flag}|22x20px|border|alt=|link=]]&nbsp;{athletes}&nbsp;<span style="font-size:90%;">([[' .. szocikk .. '|{dispName}]])</span>')
			:gsub('{(%w+)}', {
				athletes = athletes,
				flag = countryAlias(parms),
				year = gamesyear,
				games = games,
				name = orszagnevelo .. fullName,
				dispName = code,
			}))
	elseif dispType == 'érmes' then
		return (('{athletes}<br>[[File:{flag}|23x15px|border|alt=|link=]]&nbsp;[[' .. szocikk .. '|{dispName}]]')
			:gsub('{(%w+)}', {
				athletes = athletes,
				flag = countryAlias(parms),
				year = gamesyear,
				games = games,
				name = orszagnevelo .. fullName,
				dispName = dispName,
			}))
	elseif dispType == 'vál' then
		return (('[[File:{flag}|23x15px|border|alt=|link=]]&nbsp;[[{valogatott}|{dispName}]] <span style="font-size:90%;">([[' .. szocikk .. '|{code}]])</span>')
			:gsub('{(%w+)}', {
				flag = countryAlias(parms),
				valogatott = melleknev .. ' ' .. team,
				year = gamesyear,
				games = games,
				name = orszagnevelo .. fullName,
				dispName = dispName,
				code = code,
			}))
	elseif dispType == 'válj' then
		return (('[[{valogatott}|{dispName}]] <span style="font-size:90%;">([[' .. szocikk .. '|{code}]])</span>&nbsp;[[File:{flag}|23x15px|border|alt=|link=]]')
			:gsub('{(%w+)}', {
				flag = countryAlias(parms),
				valogatott = melleknev .. ' ' .. team,
				year = gamesyear,
				games = games,
				name = orszagnevelo .. fullName,
				dispName = dispName,
				code = code,
			}))
	end
end
local function main(frame)
	return countryAlias(frame.args)
end

return {
	flagIOC = flagIOC,
	flagXYZ = flagXYZ,
	main = main,
}