Module:ISO 3166: Difference between revisions

PS (talk | contribs)
m 1 revision imported
PS (talk | contribs)
m 1 revision imported
 
(One intermediate revision by one other user not shown)
Line 8: Line 8:
local getArgs = require("Module:Arguments").getArgs
local getArgs = require("Module:Arguments").getArgs
local data = mw.loadData("Module:ISO 3166/data/National")
local data = mw.loadData("Module:ISO 3166/data/National")
--[[----------C A T S----------]]-- -- Assign error categories
local function cats(cat, args)
if not (args.nocat and args.nocat == 'true') and mw.title.getCurrentTitle().namespace == 0 then
if cat == 'nocountry' then
return '[[Category:Wikipedia articles with obscure country]]'
elseif cat == 'nosubdivision' then
return '[[Category:Wikipedia articles with obscure subdivision]]'
end
end
return ''
end


--[[----------F I N D N A M E----------]]-- -- Finds the name in the database
--[[----------F I N D N A M E----------]]-- -- Finds the name in the database
Line 56: Line 68:
if not text then return nil end
if not text then return nil end
local accents = {["À"]="A",["Á"]="A",["Â"]="A",["Ã"]="A", -- accent list
local accents = {["À"]="A",["Á"]="A",["Â"]="A",["Ã"]="A",
["Ä"]="A",["Å"]="A",["Ç"]="C",["È"]="E",["É"]="E",
["Ä"]="A",["Å"]="A",["Æ"]="AE",["Ç"]="C",["È"]="E",
["Ê"]="E",["Ë"]="E",["Ì"]="I",["Í"]="I",["Î"]="I",
["É"]="E",["Ê"]="E",["Ë"]="E",["Ì"]="I",["Í"]="I",
["Ï"]="I",["Ñ"]="N",["Ò"]="O",["Ó"]="O",["Ô"]="O",
["Î"]="I",["Ï"]="I",["Ð"]="D",["Ñ"]="N",["Ò"]="O",
["Õ"]="O",["Ö"]="O",["Ø"]="O",["Ù"]="U",["Ú"]="U",
["Ó"]="O",["Ô"]="O",["Õ"]="O",["Ö"]="O",["Ø"]="O",
["Û"]="U",["Ü"]="U",["Ý"]="Y"
["Ù"]="U",["Ú"]="U",["Û"]="U",["Ü"]="U",["Ý"]="Y",
["Þ"]="TH",
["Ā"]="A",["Ă"]="A",["Ą"]="A",["Ć"]="C",["Č"]="C",
["Ĉ"]="C",["Ċ"]="C",["Ď"]="D",["Đ"]="D",["Ĕ"]="E",
["Ě"]="E",["Ė"]="E",["Ē"]="E",["Ę"]="E",["Ŋ"]="N",
["Ğ"]="G",["Ģ"]="G",["Ĝ"]="G",["Ġ"]="G",["Ĥ"]="H",
["Ħ"]="H",["Ĭ"]="I",["İ"]="I",["Ī"]="I",["Į"]="I",
["Ĩ"]="I",["IJ"]="IJ",["Ĵ"]="J",["Ķ"]="K",["Ĺ"]="L",
["Ľ"]="L",["Ļ"]="L",["Ŀ"]="L",["Ł"]="L",["Ń"]="N",
["Ň"]="N",["Ņ"]="N",["Ŏ"]="O",["Ő"]="O",["Ō"]="O",
["Œ"]="OE",["Ŕ"]="R",["Ř"]="R",["Ŗ"]="R",["Ś"]="S",
["Š"]="S",["Ş"]="S",["Ŝ"]="S",["Ť"]="T",["Ţ"]="T",
["Ŧ"]="T",["Ŭ"]="U",["Ű"]="U",["Ū"]="U",["Ų"]="U",
["Ů"]="U",["Ũ"]="U",["Ŵ"]="W",["Ŷ"]="Y",["Ÿ"]="Y",
["Ź"]="Z",["Ž"]="Z",["Ż"]="Z",
}
}
local remove = {"NATION OF","COUNTRY OF","TERRITORY OF", -- text to be removed list
local remove = {"NATION OF","COUNTRY OF","TERRITORY OF", -- text to be removed list
"FLAG OF","FLAG","KINGDOM OF","STATE OF"," STATE ",
"FLAG OF","FLAG","KINGDOM OF","STATE OF"," STATE ",
"PROVINCE OF","PROVINCE","TERRITORY"
"PROVINCE OF","PROVINCE","TERRITORY","COUNTY","GOVERNORATE"
}
local patterns = {"(%a)%.(%a)%.",".+:","|.+","%(.-%)", -- patterns to follow (order may matter)
"%.%S.*","^THE ","%_","%-","%d%d?%d?PX"
}
}
local patterns = {[".+:"]="",["|.+"]="",["%(.-%)"]="", -- patterns to follow (order may matter)
local replacements = {["(%a)%.(%a)%."]="%1%2", -- Replacements for patterns above (order does not matter)
["%..*"]="",["^THE "]="",["%_"]=" ",["%-"]=" ",
[".+:"]="",["|.+"]="",["%(.-%)"]="",
["%.%S.*"]="",["^THE "]="",["%_"]=" ",["%-"]=" ",
["%d%d?%d?PX"]="",
["%d%d?%d?PX"]="",
}
}
Line 75: Line 105:
text = mw.text.unstripNoWiki(text) -- Remove <nowiki> tags
text = mw.text.unstripNoWiki(text) -- Remove <nowiki> tags
text = mw.ustring.upper(text) -- Case insensitivity
text = mw.ustring.upper(text) -- Case insensitivity
text = mw.ustring.gsub(text,"[À-Ý]",accents) -- Deaccent
text = mw.ustring.gsub(text,"[À-Ż]",accents) -- Deaccent
for pattern,value in pairs(patterns) do -- Follow patterns
for _,pattern in ipairs(patterns) do -- Follow patterns
text = mw.ustring.gsub(text,pattern,value)  
local value = replacements[pattern]
text = mw.ustring.gsub(text,pattern,value)  
end
end
Line 104: Line 135:


function p.luacode(args)
function p.luacode(args)
 
args[1], args[2] = args[1] or '', args[2] or ''
local code1 = p.strip(args[1])
local code1 = p.strip(args[1])
local code2 = p.strip(args[2])
local code2 = p.strip(args[2])
Line 112: Line 143:
local eot = args.error or ""
local eot = args.error or ""
local catnocountry = (args.nocat and args.nocat == 'true') and ''  
if code1 == "" then
or '[[Category:Wikipedia page with obscure country]]'
mw.addWarning((args[1] ~= "" and ('Invalid parameter "' .. args[1] .. '"') or 'No parameter') ..
local catnosubdivision = (args.nocat and args.nocat == 'true') and ''  
' for the country given ([[Module:ISO 3166]]).')
or '[[Category:Wikipedia page with obscure subdivision]]'
return cats('nocountry', args), '<span style="font-size:100%" class="error">"No parameter for the country given"</span>'
if (code1 or "") == "" then
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end
return catnocountry, '<span style="font-size:100%" class="error">"No parameter for the country given"</span>'
end
end
local notRecognized ='%s ("%s") is not a recognized %s in [[ISO 3166-%s]] ([[Module:ISO 3166]]).'
if (code2 or "") == "" then --3166-1 code
if code2 == "" then --3166-1 code
for alpha2,cdata in pairs(data) do
for alpha2,cdata in pairs(data) do
if findname(alpha2,cdata,code1) then
if findname(alpha2,cdata,code1) then
Line 132: Line 161:
end
end
end
end
if string.find(args[1] or '',"%-") then
if string.find(args[1],"%-") then
args[1], args[2] = string.match(args[1] or '',"^([^%-]*)%-(.*)$")
args[1], args[2] = string.match(args[1],"^([^%-]*)%-(.*)$")
if args[2] then return p.luacode(args) end
if args[2] then return p.luacode(args) end
end
end
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end
mw.addWarning(string.format(notRecognized, code1, args[1], 'country', '1'))
return catnocountry
return cats('nocountry', args)
else --3166-2 code
else --3166-2 code
for alpha2,cdata in pairs(data) do                                       
for alpha2,cdata in pairs(data) do                                       
Line 159: Line 188:
return p.luacode(args)
return p.luacode(args)
end
end
if mw.title.getCurrentTitle().namespace ~= 0 then catnosubdivision = '' end
mw.addWarning(string.format(notRecognized, code2, args[2], 'subdivision', '2:' .. alpha2))
return catnosubdivision
return cats('nosubdivision', args)
end
end
end
end
if string.find(args[1] or '',"%-") then
if string.find(args[1],"%-") then
args[1] = string.match(args[1] or '',"^([^%-]*)%-(.*)$")
args[1] = string.match(args[1],"^([^%-]*)%-(.*)$")
return p.luacode(args)
return p.luacode(args)
end
end
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end
mw.addWarning(string.format(notRecognized, code1, args[1], 'country', '1'))
return catnocountry
return cats('nocountry', args)
end
end
 
mw.addWarning(string.format(notRecognized, code1, args[1], 'country', '1'))
return cats('nocountry', args)
end
end


Line 199: Line 229:
local code1 = p.luacode(args)
local code1 = p.luacode(args)
local code2 = ''
local code2 = ''
if string.find(code1,"%-") then
if string.find(code1,"%-") then
code1, code2 = string.match(code1,"^([^%-]*)%-(.*)$")
code1, code2 = string.match(code1,"^([^%-]*)%-(.*)$")
Line 210: Line 240:
or (data[code1]["displayname"] or data[code1]["name"])
or (data[code1]["displayname"] or data[code1]["name"])
else
else
return '[[Category:Wikipedia page with obscure country]]'
return cats('nocountry', args)
end
end
else --3166-2 code
else --3166-2 code
Line 217: Line 247:
sdata = mw.loadData("Module:ISO 3166/data/"..code1)
sdata = mw.loadData("Module:ISO 3166/data/"..code1)
else
else
return '[[Category:Wikipedia page with obscure country]]'
return cats('nocountry', args)
end
end
if sdata[code2] then
if sdata[code2] then
Line 223: Line 253:
or (sdata[code2]["displayname"] or sdata[code2]["name"])
or (sdata[code2]["displayname"] or sdata[code2]["name"])
else
else
return '[[Category:Wikipedia page with obscure country]]'
return cats('nocountry', args)
end
end
end
end
Line 246: Line 276:
local args = frame.args
local args = frame.args
local subdivisionqueried = false
local subdivisionqueried = false
local catnocountry = (args.nocat and args.nocat == 'true') and ''
or '[[Category:Wikipedia page with obscure country]]'
local catnosubdivision = (args.nocat and args.nocat == 'true') and ''
or '[[Category:Wikipedia page with obscure subdivision]]' or ''
local tracking = ''
local tracking = ''
local targs = {}
local targs = {}
Line 269: Line 295:
if region == '' then
if region == '' then
region = p.luacode({country, nocat = 'true'})
region = p.luacode({country, nocat = 'true'})
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry, catnosubdivision = '', '' end
if region == '' then
if region == '' then
tracking = tracking .. catnocountry
tracking = tracking .. cats('nocountry', args)
elseif subdivisionqueried == true then
elseif subdivisionqueried == true then
tracking = tracking .. catnosubdivision
tracking = tracking .. cats('nosubdivision', args)
end
end
end
end