Modul:Multilingual/maintain

aus Wikipedia, der freien Enzyklopädie
--[=[ 2019-11-01
Multilingual/maintain -- data source maintainenance issues
]=]
local MLM = { }



local fill = function ( assembly, access, assign, adjust )
    -- Attach submodule
    -- Precondition:
    --     assembly  -- table
    --     access    -- submodule title
    --     assign    -- component name
    --     adjust    -- language code, or false
    -- Postcondition:
    --     Returns  table with additional components
    --     Throws error, if not available
    local lucky, got = pcall( require,
                              "Module:Multilingual/" .. access )
    local r = assembly
    local elem, part
    if type( got ) == "table" then
        for series, collection in pairs( got ) do
            if adjust  and  series ~= adjust then
            elseif type( series ) == "string" then
                if type( collection ) == "table" then
                    series = series:lower()
                    part = r[ series ]
                    if type( part ) ~= "table" then
                         r[ series ] = { }
                         part        = r[ series ]
                    end
                    for k, v in pairs( collection ) do
                        k = k:lower()
                        elem = part[ k ]
                        if type( elem ) ~= "table" then
                            part[ k ] = { }
                            elem      = part[ k ]
                        end
                        elem[ assign ] = v
                    end -- for k, v
                elseif series:sub( 1, 1 ) ~= "#" then
                    got = string.format( "Multilingual/%s invalid at %s",
                                         access, series )
                    error( got, 0 )
                end
            else
                got = string.format( "Multilingual/%s bad key type %s",
                                     access, type( series ) )
                error( got, 0 )
            end
        end -- for in, collection
    end
    return r
end -- fill()



local first = function ( a1, a2 )
    -- Sort obeying hyphen
    -- Precondition:
    --     a1  -- first string
    --     a2  -- second string
    -- Postcondition:
    --     Returns true when a1 is less than a2
    local n1 = a1:find( "-", 1, true )
    local n2 = a2:find( "-", 1, true )
    local r
    if n1 then
        if n2 then
            local s1 = a1:sub( 1, n1 )
            local s2 = a2:sub( 1, n2 )
            if s1 == s2 then
                r = ( a1:sub( n1 )  <  a2:sub( n2 ) )
            else
                r = ( s1 < s2 )
            end
        else
            r = ( a1:sub( 1, n1 )  <  a2 )
        end
    elseif n2 then
        r = ( a1  <  a2:sub( 1, n2 ) )
    else
        r = ( a1 < a2 )
    end
    return  r
end -- first()



MLM.fetchLanguageNames = function ( ask, amount )
    -- Make wikitable of language names in a certain language
    -- Precondition:
    --     ask     -- code of requested language
    --     amount  -- kind of extension
    -- Postcondition:
    --     Returns wikitext string
    local codes      = { }
    local scope      = amount or "mw"
    local slang      = ask or "native"
    local collection = mw.language.fetchLanguageNames( slang, scope )
    local mwdef, n, r, s
    for k, v in pairs( collection ) do
        table.insert( codes, k )
    end -- for k, v
    n = #codes
    table.sort( codes, first )
    if amount == "all" then
        local mwfile = mw.language.fetchLanguageNames( slang, "mwfile" )
        mwdef = mw.language.fetchLanguageNames( slang, "mw" )
        for k, v in pairs( mwdef ) do
            if mwfile[ k ] then
                s = "mw file"
            else
                s = "mw"
            end
            mwdef[ k ] = s
        end -- for k, v
    end
    r = "{| class='wikitable sortable'\n" ..
        string.format( "|+ '''<code>%s</code>''' (%s)\n",
                       slang, scope ) ..
        "|-\n" ..
        "! Code !! Name\n"
        if mwdef then
            r = r .. "! Scope\n"
        end
    for i = 1, n do
        s = codes[ i ]
        r = string.format( "%s|-\n|<code>%s</code>||%s\n",
                           r, s, collection[ s ] )
        if mwdef then
            s = mwdef[ s ]
            if s then
                r = string.format( "%s| %s\n", r, s )
            end
        end
    end -- for i
    r = string.format( "%s|}\n%d items\n\n",
                       r, n )
    return r
end -- MLM.fetchLanguageNames()



MLM.forward = function ( args )
    -- Make wikitable of maintainenance issues
    -- Precondition:
    --    args   -- table of options
    --              lack    -- show missing CLDR definitions
    --              leap    -- show open issues only
    --              learnt  -- show superfluous local entries only
    --              slang   -- limit to particular language
    -- Postcondition:
    --     Returns wikitext string
    local codes  = { }
    local pars   = args or { }
    local leap   = ( pars.leap    and  pars.leap   ~= "0" )
    local learnt = ( pars.learnt  and  pars.learnt ~= "0" )
    local slang  = ( pars.slang   and  pars.slang ~= "" )
    local slice  = "%s|-\n" ..
                   "|<code>%s</code>||<code>%s</code>||%s||%s||%s||%s\n"
    local r      = "{| class='wikitable sortable'\n" ..
                   "|-\n" ..
                   "! In !! For !! Suggested !! @MW !! @CLDR !! ToDo\n"
    local data = fill( { }, "names", "shift", slang )
    local elem, series, server, shift, single, state, super, tupel
    data = fill( data, "cldr", "super", slang )
    for series, collection in pairs( data ) do
        part = data[ series ]
        for k, v in pairs( collection ) do
            table.insert( codes,
                          string.format( "%s %s", series, k ) )
            v.server = mw.language.fetchLanguageName( k, series )
        end -- for k, v
    end -- for in, collection
    table.sort( codes, first )
    for i = 1, #codes do
        tupel  = mw.text.split( codes[ i ], " ", true )
        series = tupel[ 1 ]
        single = tupel[ 2 ]
        elem   = data[ series ][ single ]
        shift  = elem.shift  or ""
        server = elem.server or ""
        super  = elem.super  or ""
        if not elem.super then
            if shift == server then
                state = "Upstream"
            else
                state = "Learn"
            end
        elseif server ~= super then
            if shift == super then
                state = "CLDR"
            elseif elem.shift then
                state = "Proposal"
            elseif series == "en" then
                state = "Conflict"
            else
                state = mw.language.fetchLanguageName( single, "en" )
                if server == state then
                    state = false
                else
                    state = "Conflict"
                end
            end
        elseif elem.shift then
            if shift == server then
                state = "Discard"
            else
                state = "Proposal"
            end
        else
            state = false
        end
        if state then
            r = string.format( slice,
                               r,
                               series, single, shift, server, super,
                               state )
        end
    end -- for i
    r = r .. "|}\n\n"
    return r
end -- MLM.forward()



-- Export
local p = { }

p.fetchLanguageNames = function ( frame )
    -- Make wikitable of language names in a certain language
    --     1  -- code of requested language
    --     2  -- kind of extension
    return MLM.fetchLanguageNames( frame.args[ 1 ], frame.args[ 2 ] )
--forward class="wikitable sortable"
end -- p.fetchLanguageNames

p.forward = function ( frame )
    -- Make wikitable of maintainenance issues
    --    leap    -- show open issues only
    --    learnt  -- show superfluous local entries only
    --    slang   -- limit to particular language
    return MLM.forward( frame.args )
end -- p.forward

p.MLM = function ()
    return MLM
end -- p.MLM

return p