Vorlagenprogrammierung Diskussionen Lua Unterseiten
Modul Deutsch English

Modul: Dokumentation

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


--[=[ 2015-02-07
Vorlage:UpstreamUpdate
]=]



local Support = "UpstreamUpdate"



local function fair( access, all, frame )
    -- Return template transclusion
    --     access  -- string, with page name
    --     all     -- table, with entries
    --     frame   -- object
    local r = all[ access ]
    if r then
        r = frame:expandTemplate{  title = Support,  args= { r }  }
    end
    return r
end -- fair()



local function feed( area, all )
    -- Return list of members
    --     area  -- string, with namespace number
    --     all   -- table, with entries
    local space = mw.site.namespaces[ area ].name
    local room  = { }
    local r
    for k, v in pairs( all ) do
       table.insert( room, k )
    end    -- for k, v
    table.sort( room )
    for k, v in pairs( room ) do
        if r then
            r = r .. "\n"
        else
            r = ""
        end
        r = string.format( "%s* [[%s:%s|%s]] %s %s",
                           r, space, v, v, mw.ustring.char( 8211 ),
                           all[ v ] )
    end    -- for k, v
    return r
end -- feed()



local function fiat( arglist )
    -- Perform task
    --     arglist  -- table, with parameters
    --                        .frame    -- object, or nil
    --                        .ns       -- string/number, for "space"
    --                        .service  -- string: "single", "space"
    local mode, ns, page, r
    if arglist.service == "single" then
        page = mw.title.getCurrentTitle()
        if page.contentModel ~= "wikitext" then
            mode = 1
            ns   = page.namespace
        end
    elseif arglist.service == "space" then
        mode = 2
        ns   = tonumber( arglist.ns )
    end
    if mode and ns then
        local config, frame, lucky
        if arglist.frame then
            frame = arglist.frame
        else
            frame = mw.getCurrentFrame()
        end
        config = mw.loadData( frame:getTitle() .. "/config" )
        if type( config ) == "table" then
            local room = config[ ns ]
            if type( room ) == "table" then
                if mode == 1 then
                    r = fair( page.text, room, frame )
                else
                    r = feed( ns, room )
                end
            end
        end
    end
    return r
end -- fiat()



-- Export
local p = { }

p.fire = function ( arglist )
    -- Perform task
    --     arglist  -- table, with parameters
    local lucky, r = pcall( fiat, arglist )
    if lucky then
        if not r  and  arglist.frame then
            r = ""
        end
    else
        local frame
        if arglist.frame then
            frame = arglist.frame
        else
            frame = mw.getCurrentFrame()
        end
        r = string.format( "<span class=\"error\">%s</span>", r )
    end
    return r
end -- p.fire()

p.space = function ( frame )
    return p.fire( { frame   = frame,
                     ns      = frame.args.ns,
                     service = "space" } )
end -- p.space()

p.single = function ( frame )
    return p.fire( { frame   = frame,
                     service = "single" } )
end -- p.single()

return p