Modul:ISO3166/loadData

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

local ISO3166 = { suite  = "ISO3166",
                  sub    = "loadData",
                  serial = "2019-12-01",
                  item   = 70862569,
                  main   = 69799761 }
--[=[
Create ISO 3166 mw.loadData() table
]=]



if mw.site.server:match( "%.beta%.wmflabs%.org$" ) then
    require( "Module:No Globals" )
    ISO3166.item = 540272
    ISO3166.main = 541481
    ISO3166.serialProperty = "P253085"
end



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 = { }
    if type( data ) == "table"  and
       type( data.data ) == "table" then
        local entry, s, 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
    end
    return r
end -- fetch()



ISO3166.codes    = fetch( "codes", true )
ISO3166.reverse  = fetch( "reverse", true )
ISO3166.failsafe = ISO3166.serial



return ISO3166