Modul:ISO3166/loadData
Vorlagenprogrammierung | Diskussionen | Lua | Test | Unterseiten | |||
Modul | Deutsch | English
|
Modul: | Dokumentation |
Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus
Weiterleitung der Diskussionsseite fehlt
local ISO3166 = { suite = "ISO3166",
sub = "loadData",
serial = "2019-10-13",
join = 70862569,
main = 69799761,
codes = { } }
--[=[
Create ISO 3166 mw.loadData() table
]=]
local fetch = function ( achieve, alert )
-- Retrieve data from Commons .tab JSON
-- Precondition:
-- achieve -- string, with name of Commons tab
-- alert -- true, for throwing error on data problem
-- Returns table, with mapping string->string
local storage = string.format( "ISO3166/%s.tab", achieve )
local lucky, data = pcall( mw.ext.data.get, storage )
local r = { }
local s
if type( data ) == "table" and
type( data.data ) == "table" then
local entry, sign, set
data = data.data
for i = 1, #data do
entry = data[ i ]
if type( entry ) == "table" then
sign = entry[ 1 ]
if type( sign ) == "string" then
if r[ sign ] then
s = string.format( "Duplicated: %s (%d) @%s",
sign, i, achieve )
else
set = entry[ 2 ]
if type( set ) == "string" then
r[ sign ] = set
if alert then
if mw.text.trim( sign ) ~= sign or
sign:upper() ~= sign or
sign == "" then
s = string.format( "%s '%s' @%s",
"Bad key:",
sign, achieve )
end
if mw.text.trim( set ) ~= set or
set:upper() ~= set or
set == "" then
s = string.format( "%s %s:'%s' @%s",
"Bad value:",
sign, set,
achieve )
end
end
else
s = string.format( "Invalid value: %s @%s",
sign, achieve )
end
end
else
s = string.format( "Bad entry (%d) @%s", i, achieve )
end
else
s = string.format( "Invalid entry (%d) @%s", i, achieve )
end
end -- for i
else
s = "Missing or invalid page: commons:" .. storage
end
if s and alert then
error( s, 0 )
end
return r
end -- fetch()
ISO3166.codes = fetch( "codes" )
ISO3166.reverse = fetch( "reverse" )
ISO3166.failsafe = ISO3166.serial
return ISO3166