Die Dokumentation für dieses Modul kann unter Modul:MaireParINSEE/Doku erstellt werden

--[=[ 2014-12-26
RetrieveByCode::MaireParINSEE
]=]



local Database = "MaireParINSEE"
local Config



local function fault( alert )
    -- Format message with class="error"
    --     alert  -- string, with message
    -- Returns message with markup
    return string.format( "<span class=\"error\">ERROR * %s</span>",
                          alert )
end -- fault()



local function fetch( assigned )
    -- Retrieve configuration issue
    --     assigned  -- string or number; with key
    -- Returns nil, or configuration value
    local r
    if Config == nil then
        r      = string.format( "Module:%s/config", Database )
        Config = mw.loadData( r )
    end
    if type( Config ) == "table" then
        r = Config[ assigned ]
    else
        Config = false
    end
    return r
end -- fetch()



local function find( area, access, ask )
    -- Retrieve info by code
    --     area    -- string, with module subdivision, or "data"
    --     access  -- string or number; with record key
    --     ask     -- nil, string or number; with field key
    --                if nil, return entire record
    -- Returns nil, record table or field value
    local s = string.format( "Module:%s/%s", Database, area )
    local r = mw.loadData( s )
    if type( r ) == "table" then
        r = r[ access ]
        if r and ask then
            r = r[ ask ]
        end
    end
    return r
end -- find()



local function flip( about, another )
    -- Format wikilink
    --     about    -- nil or string; link title, maybe target
    --     another  -- nil or string; link target
    -- Returns nil or formatted string
    local r
    if another then
        if another == about then
            r = string.format( "[[%s]]", about )
        else
            r = string.format( "[[%s|%s]]", another, about )
        end
    else
        r = about
    end
    return r
end -- flip()



local function format( at )
    -- Format date according to local customs
    --     at  -- nil, string or number; year, "YYYY-MM", "YYYY-MM-DD"
    -- Returns nil, if unknown; or formatted string
    local s = type( at )
    local r = at
    if s == "string" then
        local n = #at
        if n == 7 then
            s = fetch( "YYYY-MM" )
            if s then
                r = at .. "-01"
            end
        elseif n == 10 then
            s = fetch( "YYYY-MM-DD" )
        else
            s = false
        end
        if s then
            local cl = mw.language.getContentLanguage()
            r = cl:formatDate( s, r )
        end
    elseif s == "number" then
        r = string.format( "%d", at )
    end
    return r
end -- format()



-- Export
local p = { }

function p.query( access, ask )
    -- Retrieve info by code
    --     access  -- string or number, with INSEE code
    --     ask     -- string or number, one of 1 "d" "p"
    -- Returns nil, if unknown; or string with data or error message
    local s = type( access )
    local n, r
    if s == "string" then
        s = mw.text.trim( access )
        if s:match( "^%d?%d%d%d%d$" ) then
            n = tonumber( access )
        else
            r = fault( "Invalid INSEE=" .. access )
        end
    elseif s == "number" then
        n = access
        s = string.format( "%05d", access )
    end
    if n then
        if n < 1000 then
            r = fault( "INSEE < 1000:" .. access )
        elseif n > 98999 then
            r = fault( "INSEE > 98999:" .. access )
        else
            local sub = s:sub( 1, 2 )
            if ask == 1 then
                local e = find( sub, n )
                if e then
                    local q = e.q
                    r = e[ 1 ]
                    if r and q then
                        q = string.format( "Q%d", q )
                        r = flip( r,  mw.wikibase.sitelink( q ) )
                    end
                end
            elseif ask == "d" then
                r = format( find( sub, n, "d" ) )
            elseif ask == "p" then
                r = find( sub, n, "p" )
                if r then
                    r = flip( r,  fetch( r ) )
                end
            else
                r = fault( ".query(ask) INVALID" )
            end
        end
    end
    return r
end -- .query()



function p.maire( frame )
    return p.query( frame.args[ 1 ], 1 )  or  ""
end
function p.depuis( frame )
    return p.query( frame.args[ 1 ], "d" )  or  ""
end
function p.parti( frame )
    return p.query( frame.args[ 1 ], "p" )  or  ""
end

return p