Vorlagenprogrammierung Diskussionen Lua 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: 2024-04-22

local GoogleBook = { suite  = "GoogleBook",
                     serial = "2024-04-22",
                     item   = 125516841 }
--[=[
Template:Google Buch
]=]

local Failsafe = GoogleBook



local function factory( args )
    -- Create alerts table from template args
    -- Precondition:
    --     args   -- table, with error message strings
    -- Postcondition:
    --     Returns  table, with error messages
    local r = { }
    local suffixes = "CHNP"
    local s, sign
    for i = 2, #suffixes do
        sign = suffixes:sub( i, i )
        s    = args[ "err" .. sign ]
        if type( s ) == "string" then
            s = mw.text.trim( s )
            if s ~= "" then
                r[ sign:lower() ] = s
            end
        end
    end -- for i
    return r
end -- factory()



GoogleBook.fine = function ( at, alerts, assign, apply, about )
    -- Check identifier
    -- Precondition:
    --     at       -- identifier
    --     alerts   -- table, with error message strings, or not
    --                 .n   -- if length invalid, or not
    --                 .c   -- if ID invalid, or not
    --                 .h   -- if # in parameters, or not
    --                 .p   -- if parameters invalid, or not
    --     assign   -- error category if invalid, or not
    --     apply    -- additional class for error messages, or not
    --     about    -- string mentioning template, or not
    -- Postcondition:
    --     Returns  string  -- error, wikitext
    --              false   -- fine
    --              true    -- if invalid but no signalling
    local errs, r, s
    if type( alerts ) == "table" then
        errs = alerts
    else
        errs = { }
    end
    if type( at ) == "string" then
        local u
        s = mw.text.trim( at )
        if s:find( "&" ) then
            u = mw.text.split( s, "&" )
            s = u[ 1 ]
        end
        if s:len() == 12 then
            if s:match( "^[%w_%-]+$" ) then
                if u then
                    if #u == 2 then
                        s = u[ 2 ]
                        if s:find( "#", 1, true ) then
                            r = errs.h or true
                        elseif not s:match( "^%w+=%S+$" ) then
                            if s == ""  or
                               s == "dq"  or
                               s == "pg"  or
                               s == "q" then
                                   r = errs.p or true
                               end
                        end
                    elseif                                    false then
                        for i = 2, #u do
                             s = u[ i ]
                             if not s:match( "^%S+$" ) then
                                 r = errs.p or true
                                 break -- for i
                             end
                        end -- for i
                    end
                end
            else
                r = errs.c or true
            end
        else
            r = errs.n or true
        end
    else
        r = errs.n or true
    end
    if type( r ) == "string" then
        s = mw.text.trim( r )
        if s == "" then
            r = ""
        else
            local e = mw.html.create( "span" )
                        :addClass( "error" )
            if about then
                e:wikitext( about .. " – " )
            end
            e:wikitext( s )
            if apply then
                s = mw.text.trim( apply )
                if s ~= "" then
                    e:addClass( s )
                     :css( "display", "none" )
                end
            end
            r = tostring( e )
        end
    end
    if r  and  type( assign ) == "string" then
        s = mw.text.trim( assign )
        if s ~= "" then
            if type( r ) ~= "string" then
                r = ""
            end
            r = string.format( "%s[[Category:%s]]", r, s )
        end
    end
    return r or false
end -- GoogleBook.fine()



Failsafe.failsafe = function ( atleast )
    -- Retrieve versioning and check for compliance
    -- Precondition:
    --     atleast  -- string, with required version
    --                         or wikidata|item|~|@ or false
    -- Postcondition:
    --     Returns  string  -- with queried version/item, also if problem
    --              false   -- if appropriate
    -- 2024-03-01
    local since  = atleast
    local last   = ( since == "~" )
    local linked = ( since == "@" )
    local link   = ( since == "item" )
    local r
    if last  or  link  or  linked  or  since == "wikidata" then
        local item = Failsafe.item
        since = false
        if type( item ) == "number"  and  item > 0 then
            local suited = string.format( "Q%d", item )
            if link then
                r = suited
            else
                local entity = mw.wikibase.getEntity( suited )
                if type( entity ) == "table" then
                    local seek = Failsafe.serialProperty or "P348"
                    local vsn  = entity:formatPropertyValues( seek )
                    if type( vsn ) == "table"  and
                       type( vsn.value ) == "string"  and
                       vsn.value ~= "" then
                        if last  and  vsn.value == Failsafe.serial then
                            r = false
                        elseif linked then
                            if mw.title.getCurrentTitle().prefixedText
                               ==  mw.wikibase.getSitelink( suited ) then
                                r = false
                            else
                                r = suited
                            end
                        else
                            r = vsn.value
                        end
                    end
                end
            end
        elseif link then
            r = false
        end
    end
    if type( r ) == "nil" then
        if not since  or  since <= Failsafe.serial then
            r = Failsafe.serial
        else
            r = false
        end
    end
    return r
end -- Failsafe.failsafe()



-- Export
local p = { }



p.fine = function ( frame )
    -- Check identifier
    -- #invoke
    --     id        -- identifier
    --     errN      -- error message if length invalid, or not
    --     errC      -- error message if ID invalid, or not
    --     errH      -- error message if # in parameters, or not
    --     errP      -- error message if parameters invalid, or not
    --     cat       -- error category if invalid, or not
    --     class     -- additional class for error messages, or not
    --     template  -- mentioning template, or not
    local r = GoogleBook.fine( frame.args.id,
                               factory( frame.args ),
                               frame.args.cat,
                               frame.args.class,
                               frame.args.template )
    if type( r ) ~= "string" then
        r = ""
    end
    return r
end -- p.fine



p.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.failsafe( since )  or  ""
end -- p.failsafe()



setmetatable( p,  { __call = function ( func, ... )
                                 setmetatable( p, nil )
                                 return Failsafe
                             end } )

return p