Zum Inhalt springen

Modul:TemplateData/global

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 2. April 2018 um 14:31 Uhr durch PerfektesChaos (Diskussion | Beiträge) (2018-03-30). Sie kann sich erheblich von der aktuellen Version unterscheiden.
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Vorlagenprogrammierung Diskussionen Lua Test Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus

Dies ist die (produktive) Mutterversion eines global benutzten Lua-Moduls.
Wenn die serial-Information nicht übereinstimmt, müsste eine Kopie hiervon in das lokale Wiki geschrieben werden.
Versionsbezeichnung auf WikiData: 2020-08-01

Updating notwendig

(lokal: 2018-03-30)

local Export = { suite    = "TemplateDataGlobal",
                 serial   = "2018-03-30",
                 item     = -1,
                 subpages = "TemplateData",
                 suffix   = "tab" }
--[=[
Retrieve TemplateData from Commons:Data (or other global source)
require()
Inspired by [[User:Yurik]].
]=]



local function failsafe( assert )
    -- Retrieve versioning and check for compliance
    -- Precondition:
    --     assert  -- string, with required version or "wikidata",
    --                or false
    -- Postcondition:
    --     Returns  string with appropriate version, or false
    local since = assert
    local r
    if since == "wikidata" then
        local item = Export.item
        since = false
        if type( item ) == "number"  and  item > 0 then
            local entity = mw.wikibase.getEntity( string.format( "Q%d",
                                                                 item ) )
            if type( entity ) == "table" then
                local vsn = entity:formatPropertyValues( "P348" )
                if type( vsn ) == "table"  and
                   type( vsn.value) == "string" and
                   vsn.value ~= "" then
                    r = vsn.value
                end
            end
        end
    end
    if not r then
        if not since  or  since <= Export.serial then
            r = Export.serial
        else
            r = false
        end
    end
    return r
end -- failsafe()



local function find( access )
    -- Fetch data from page
    -- Parameter:
    --    access  -- string, with core page name
    -- Returns
    --    1. string, with prefixed page name
    --    2. table with JSON data, or error message
    local storage = access
    local lucky, r
    if Export.suffix  and  not storage:find( ".", 2, true ) then
        local k = -1 - #Export.suffix
        if storage:sub( k ) ~= "." .. Export.suffix then
            storage = string.format( "%s.%s", storage, Export.suffix )
        end
    end
    if Export.subpages  and  not storage:find( "/" ) then
        storage = string.format( "%s/%s", Export.subpages, storage )
    end
    lucky, r = pcall( mw.ext.data.get, storage, "_" )
    storage = "Data:" .. storage
    if mw.site.siteName ~= "Wikimedia Commons" then
        storage = "commons:" .. storage
    end
    if type( r ) ~= "table"  and  type( r ) ~= "string" then
        r = "INVALID"
    end
    return storage, r
end -- find()



local function flat( apply )
    -- Convert tabular data into TemplateData
    -- Parameter:
    --     apply -- table, with tabular data
    -- Returns string, with error message, or table, with TemplateData
    local r, scream
    local function failed( at, alert )
              if scream then
                  scream = string.format( "%s * #%d: %s",
                                          scream, at, alert )
              else
                  scream = add
              end
          end -- failed()
    if type( apply.schema ) == "table"  and
       type( apply.schema.fields ) == "table"  and
       type( apply.data ) == "table" then
        local order = { }
        local entry, got, params, parOrder, s, sign, td, v
        for k, v in pairs( apply.schema.fields ) do
            if type( v ) == "table" then
                table.insert( order, v.name )
            end
        end    -- for k, v
        for i = 1, #apply.data do
            entry = apply.data[ i ]
            if type( entry ) == "table" then
                got  = { }
                sign = false
                for j = 1, #entry do
                    s = order[ j ]
                    v = entry[ j ]
                    if type( v ) == "string" then
                        v = mw.text.trim( v )
                        if v == "" then
                            v = false
                        end
                    end
                    if v then
                        if s == "name" then
                            sign = v
                        else
                            got[ s ] = v
                        end
                    end
                end    -- for j
                if sign == "|" then
                    if td then
                        failed( i, "root repeated" )
                    else
                        td = { description = got.description }
                        if type( got.type ) == "string" then
                            td.format = got.type:gsub( "N", "\n" )
                        end
                    end
                elseif sign then
                    if params then
                        if params[ sign ] then
                            failed( i, "name repeated: " .. sign )
                        end
                    else
                        params   = { }
                        parOrder = { }
                    end
                    params[ sign ] = got
                    table.insert( parOrder, sign )
                else
                    failed( i, "missing name" )
                end
            else
                failed( i, "invalid component" )
            end
        end    -- for i
        r = td or { }
        r.params     = params
        r.paramOrder = parOrder
    else
        r = "bad tabular structure"
    end
    return scream or r or "EMPTY"
end -- flat()



local function furnish( apply, at )
    -- Convert external data into TemplateData
    -- Parameter:
    --     apply -- table, with external data
    --     at    -- string, with page name
    -- Returns string, with error message, or table, with TemplateData
    local r
    if at:sub( -4 ) == ".tab" then
        r = flat( apply )
    else
        r = "Unknown page format"
    end
    return r
end -- furnish()



Export.failsafe = function ( frame )
    -- Versioning interface
    local s = type( frame )
    local since
    if s == "table" then
        since = frame.args[ 1 ]
    elseif s == "string" then
        since = frame
    end
    if since then
        since = mw.text.trim( since )
        if since == "" then
            since = false
        end
    end
    return failsafe( since )  or  ""
end -- Export.failsafe()



Export.fetch = function ( access )
    -- Fetch data from site
    -- Parameter:
    --    access  -- string, with page specification
    -- Returns
    --    1. string, with error message or prefixed page name
    --    2. table with TemplateData, or not
    local storage, t = find( access )
    local s
    if type( t ) == "table" then
        t = furnish( t, storage )
        if type( t ) ~= "table" then
            s = t
        end
    else
        s = t
    end
    if type( t ) ~= "table" then
        storage = string.format( "[[%s]]", storage )
        if s then
            storage = string.format( "%s * %s", s )
        end
        t = false
    end
    return storage, t
end -- Export.fetch()



return Export