Jump to content

Module:ISO 3166/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BrandonXLF (talk | contribs) at 19:23, 12 February 2019. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}
local data = mw.loadData("Module:ISO 3166/data/National")
local catnocountry = '[[Category:Wikipedia page with obscure country]]'
local catnosubdivision = '[[Category:Wikipedia page with obscure subdivision]]'
--[[----------S T R I P----------]]--										
local function strip(text)
	local accents = {
		["À"]="A", ["Á"]="A", ["Â"]="A", ["Ã"]="A", ["Ä"]="A", ["Å"]="A",
		["Ç"]="C",
		["È"]="E", ["É"]="E", ["Ê"]="E", ["Ë"]="E",
		["Ì"]="I", ["Í"]="I", ["Î"]="I", ["Ï"]="I",
		["Ñ"]="N",
		["Ò"]="O", ["Ó"]="O", ["Ô"]="O", ["Õ"]="O", ["Ö"]="O", ["Ø"]="O",
		["Ù"]="U", ["Ú"]="U", ["Û"]="U", ["Ü"]="U",
		["Ý"]="Y"
	}
	local remove = {		
		" STATE ",
		"%d+PX",
		"<.*>",
		".+:",
		"|.+",
		"%(.-%)",
		"%..*",
		"^THE",
		"UNIQ.*QINU",
		"%W",
		"NATIONOF",
		"COUNTRYOF",
		"TERRITORYOF",
		"FLAGOF",
		"FLAG",
		"KINGDOMOF",
		"STATEOF",
		"PROVINCEOF",
		"PROVINCE",
		"TERRITORY",
		"REPUBLICOF",
		"PEOPLESREPUBLICOF"
	}
	local patterns = {															
		["%_"]=" ",
		["%-"]=" "
	}
	text = mw.ustring.upper(text)												
	-- Case insensitivity
	text = mw.ustring.gsub(text,"[À-Ý]",accents)								
	-- Deaccent
	text = mw.text.unstrip(text)												
	-- Remove markers
	for find, replace in pairs(patterns) do										
	-- Follow gsub patterns
		text = mw.ustring.gsub(text,find,replace) 
	end
	for _, find in ipairs(remove) do											
	-- Remove unneeded text
		text = mw.ustring.gsub(text,find,"") 
	end
	return text
end
--[[----------F I N D N A M E----------]]--										
-- Checks if the input is in the current table entry (key and value)
local function findname(code,cdata,qry)
	local sqry = strip(qry)
	if sqry == code
		or sqry == cdata["alpha3"]
		or sqry == cdata["numeric"]
		or sqry == strip(cdata["isoname"] or '')
		or sqry == strip(cdata["name"] or '')
	then
		return true
	end
	for _,tname in pairs(cdata["isonames"] or {}) do
		if sqry == strip(tname) then
			return true
		end
	end
	for _,tname in pairs(cdata["altnames"] or {}) do
		if sqry == strip(tname) then
			return true
		end
	end
	return false
end
--[[----------I S O N A M E----------]]--										
-- Find the ISO name of a country/region
local function isoname(data,ldata,lang)
	lang = lang or ldata["defaultlang"] or data["defaultlang"] or "en"
	return ldata["isonames"] and ldata["isonames"][lang] or ldata["isoname"] or ''
end
--[[----------G E T C O D E---------]]--									
-- Makes the ISO code of a country
local function getcode(args)
	catnocountry = args.nocat == 'true' and '' or catnocountry
	catnosubdivision = args.nocat == 'true' and '' or catnosubdivision
	if not args[1] then return catnocountry end
	if string.find(args[1], "^%s*(%w%w?%w?)%-(%w%w?%w?)") then
		args[1], args[2] = string.match(args[1],"^%s*(%w%w?%w?)%-(%w%w?%w?)")
	end
	if args[1] then args[1] = strip(args[1]) end
	if args[2] then args[2] = strip(args[2]) end
	if args["codetype"] == "3" then args["codetype"] = "alpha3" end
	--3166-1 code
	if not args[2] then															
		for alpha2,cdata in pairs(data) do
			if findname(alpha2,cdata,args[1]) then
				if args.type == "numeric" or args.type == "alpha3" then
					return cdata[args.type]
				elseif args.type == "name" then
					if args.isoname or args.lang then
						return isoname(data,cdata,args.lang)
					end
					return cdata["name"]
				end
				return alpha2
			end
		end
		return catnocountry
	--3166-2 code
	else																		
		for alpha2,cdata in pairs(data) do                                       
			if findname(alpha2,cdata,args[1]) then
				-- For England, Wales etc.
				if mw.ustring.match(alpha2,"GB") then							
					alpha2 = "GB"
				end
				local scdata = mw.loadData("Module:ISO 3166/data/"..alpha2)
				for scode,sdata in pairs(scdata) do
					if type(sdata)=="table" then
						if findname(scode,sdata,args[2]) then
							if args.type == "name" then
								if args.isoname or args.lang then
									return isoname(scdata,sdata,args.lang)
								end
								return cdata["name"]
							else
								return alpha2.."-"..scode
							end
						end
					end
				end
				return catnosubdivision
			end
		end
		return catnocountry
	end
end
--[[----------M A K E F U N C---------]]--	
local function makefunc(params)
	params = params or {}
	return function(frame)
		local args = {}
		if type(frame.args) == 'table' then
			args = frame.args
		elseif type(frame) == 'table' then
			args = frame
		end
		if params.type then
			args.type = params.type
		end
		if params.func == 'strip' then
			return strip(args[1] or args.text)
		else
			return getcode(args)
		end
	end
end
--[[----------E X P O R T E D   F U N C T I O N S---------]]--
p.strip = makefunc({func='string'})
p.code = makefunc()
p['alpha2'] = makefunc()
p['alpha3'] = makefunc({type='alpha3'})
p.numeric = makefunc({type='numeric'})
p.name = makefunc({type='name'})
--[[----------P . G E O C O O R D I N S E R T---------]]--						
-- Wrapper for Module:Coordinates.coordinsert
function p.geocoordinsert(frame)
	-- {{#invoke:ISO 3166|geocoordinsert|{{coord|...}}
	-- |country=..|subdivision1=...|subdivision2=...
	-- |type=...|scale=...|dim=...|source=...|globe=...
	-- }}
	local args = frame.args
	local subdivisionqueried = false
	catnocountry = args.nocat == 'true' and '' or catnocountry
	catnosubdivision = args.nocat == 'true' and '' or catnosubdivision
	local tracking = ''
	local targs = {}
	targs[1] = args[1] or ''
	for i, v in pairs(args) do
		if i == 'country' and not mw.ustring.find(targs[1], 'region:') then
			local country = v
			local k, region = 1, ''
			-- look for a valid subdivision
			while region == '' and k < 3 do
				local subdivision = args['subdivision' .. k] or ''
				if subdivision ~= '' then
					region = p.luacode({country, subdivision, nocat = 'true'})
					subdivisionqueried = true
				end
				k = k + 1
			end
			-- subdivision lookup failed or never attempted, try country only
			if region == '' then
				region = p.luacode({country, nocat = 'true'})
				if region == '' then
					tracking = tracking .. catnocountry
				elseif subdivisionqueried == true then
					tracking = tracking .. catnosubdivision
				end
			end
			-- something worked, add it to the targs
			if region ~= '' then
				targs[#targs + 1] = 'region:' .. region
			end
		elseif i == 'type' or i == 'scale' or i == 'dim' 
				or i == 'source' or i == 'globe' then
			targs[#targs + 1] = i .. ':' .. v
		end
	end
	-- call Module:Coordinates.coordinsert if there is something to insert
	if #targs > 1 then
		local coordinsert = require('Module:Coordinates').coordinsert
		return coordinsert({args = targs}) .. tracking
	end
	-- otherwise, just return the coordinates
	return targs[1] .. tracking
end
return p