Jump to content

Module:Country alias

ពីវិគីភីឌា
កំណែ​នៅ ម៉ោង០៥:២៥ ថ្ងៃពុធ ទី១៥ ខែកក្កដា ឆ្នាំ២០២០ ដោយ RalvahKaset (ការពិភាក្សា | ការរួមចំណែក) (បានបង្កើតទំព័រដែលផ្ដើមដោយ -- This module returns the country name or the flag name for a country, -- based on the three-letter IOC/CGA/FINA alias...)

Documentation for this module may be created at Module:Country alias/doc

-- 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
    ក្រង់ប្រឺតាញ (និង N.I.)         GBR, GBR_WCA (latter added to add text in parens)
    ហុងកុថ                        HKG, HKG_CGF (latter added to keep colonial flag)
    ឯកត្តអត្តពលិកម្មអូឡាំពិក      IOA, IOA_2000 (IOA changed to Independent Olympic Athletes in 2012)
    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
    អង់គ្វីឡា                         AIA, ANG_CGF
    អង់ទីកា និងបាប៊ូដា              ANT, ATG
    បារ៉ែន                          BHN, BHR, BRN
    គូរ៉ាសាវ                          CUR, CUW
    ទីម័រខាងកើត					 TLS, TMP
    ប្រជុំកោះហ្វេរ៉ូ                    FAR, FRO
    គែនស៊ី                         GGY, GUE
    អ៊ីរ៉ង់                             IRI, IRN
    អៀរឡង់                          IRE, IRL - IRE is *only* for CGF apps
    ជែស៊ី                           JER, JEY
    លីបង់                          LBN, LIB
    ម៉ុងសេរ៉ាត់                       MNT, MSR
    នីការ៉ាក្វា                        NCA, NIC
    កោះន័រហ្វូក                   NFI, NFK
    អូម៉ង់                            OMA, OMN
    ក្រុមអត្តពលិកជនភៀសខ្លួនអូឡាំពិក             ROA, ROT
    រូម៉ានី                          ROM, ROU
    សាំងអេលែន                     SHE, SHN
    សាំងវ៉ាំងសង់ និងក្រេណាឌីន SVG, VIN
    សារ៉ាវ៉ាក់                          SAR, SWK
    សិង្ហបុរី                        SGP, SIN
    អាហ្វ្រិកខាងត្បូង                     RSA, SAF
    តុងកា                            TGA, TON
    ទ្រីនីតេ និងតូបាគោ              TRI, TTO
    ប្រជុំកោះតួក និងកៃកូស         TCA, TCI, TKS

Oddity that needs to be revisited
    ប៉ូលីណេស៊ីបារាំង                 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 fullName then
		year = tonumber(fullName:match('^%d+'))  -- ignore args.year
	end
	if alias == 'ANG' then
		if games == 'កីឡាសហធនរដ្ឋ' then
			alias = 'ANG_CGF'
		end
	elseif alias == 'ART' then
		if games == 'កីឡាក្នុងសាល និងក្បាច់គុនអាស៊ី' then
			alias = 'ART_AIMAG'
		end
	elseif alias == 'GBR' then
		if games == 'អត្តពលិកជើងឯកពិភពលោក' or games == 'អត្តពលកម្មជើងឯកអឺរ៉ុប' then
			alias = 'GBR_WCA'
		elseif games == 'ជើងឯកអឺរ៉ុប' then
			if year == 2018 then
				alias = 'GBR_WCA'
			end
		end
	elseif alias == 'HKG' then
		if 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
	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('Module: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('នាមក្លែងប្រទេសមិនត្រូវត្រូវ: ' .. tostring(alias))
	end
	if yes(args.flag) then
		return getFlag(args, country) or quit('មិនមានទង់កំណត់សម្រាប់' .. alias)
	else
		return country.name or quit('មិនមានឈ្មោះកំណត់សម្រាប់' .. alias)
	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 1 គួរតែជាក្រមប្រទេស')
	local games = stripToNil(args[2])
	local athletes = stripToNil(args[3])
	games = games and (games .. ' អូឡាំពិក') or 'អូឡាំពិក'
	local parms = {
		alias = code,
		fullName = games,
		year = games:match('^%d+'),
		games = games:gsub('^%d+ ?', ''),
	}
	local fullName = countryAlias(parms)
	parms.flag = true
	return (('[[File:{flag}|22x20px|border|alt=|link=]]&nbsp;[[{name} at the {games}|{name}]]{athletes}')
		:gsub('{(%w+)}', {
			athletes = athletes and
				('&nbsp;<span style="font-size:90%;">(' .. athletes .. ')</span>') or
				'',
			flag = countryAlias(parms),
			games = games,
			name = fullName,
		}))
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=''
	if dispType == 'name' or dispType == 'team' then
		code = stripToNil(args[1]) or error('Parameter 1 should be a country code')
		games = stripToNil(args[2]) or error('Parameter 2 should be a competition name')
		athletes = stripToNil(args[3])
	elseif dispType == 'athlete' or dispType == 'medalist' then
		athletes = stripToNil(args[1]) or error('Parameter 1 should be the name(s) of the athlete(s)')
		code = stripToNil(args[2]) or error('Parameter 2 should be a country code')
		games = stripToNil(args[3]) or error('Parameter 3 should be a competition name')
	end
	local dispName = stripToNil(args.name)
	
	local parms = {
		alias = code,
		fullName = games,
		year = games:match('^%d+'),
		games = games:gsub('^%d+ ?', ''),
	}
	local fullName = countryAlias(parms)
	parms.flag = true
	
	if dispType == 'name' then
		return (('[[File:{flag}|22x20px|border|alt=|link=]]&nbsp;[[{name} at the {games}|{dispName}]]{athletes}')
			:gsub('{(%w+)}', {
				athletes = athletes and
					('&nbsp;<span style="font-size:90%;">(' .. athletes .. ')</span>') or
					'',
				flag = countryAlias(parms),
				games = games,
				name = fullName,
				dispName = dispName or fullName,
			}))
	elseif dispType == 'team' then
		return (('[[File:{flag}|22x20px|border|alt=|link=]]&nbsp;[[{name} at the {games}|{dispName}]]{alias}')
			:gsub('{(%w+)}', {
				alias = ('&nbsp;<span style="font-size:90%;">(' .. code .. ')</span>'),
				flag = countryAlias(parms),
				games = games,
				name = fullName,
				dispName = dispName or fullName,
			}))
	elseif dispType == 'athlete' then
		return (('[[File:{flag}|22x20px|border|alt=|link=]]&nbsp;{athletes}&nbsp;<span style="font-size:90%;">([[{name} at the {games}|{dispName}]])</span>')
			:gsub('{(%w+)}', {
				athletes = athletes,
				flag = countryAlias(parms),
				games = games,
				name = fullName,
				dispName = code,
			}))
	elseif dispType == 'medalist' then
		return (('{athletes}<br>[[File:{flag}|23x15px|border|alt=|link=]]&nbsp;[[{name} at the {games}|{dispName}]]')
			:gsub('{(%w+)}', {
				athletes = athletes,
				flag = countryAlias(parms),
				games = games,
				name = fullName,
				dispName = dispName or fullName,
			}))
	end
end
local function main(frame)
	return countryAlias(frame.args)
end

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