Modul:Vorlage:OxfordDNB

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
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-03-08

local ODNB = { suite  = "OxfordDNB",
               serial = "2024-03-08",
               maxID  = 199999,
               item   = 124723080 }
--[=[
Template:OxfordDNB
]=]


local Failsafe = ODNB



ODNB.www = function ( at, alert, assign )
    -- Build www link
    -- Precondition:
    --     at      -- identifier
    --     alert   -- error message if invalid, or not
    --     assign  -- error category if invalid, or not
    -- Postcondition:
    --     Returns  string  -- link or error
    --              false   -- if invalid but no signalling
    local s = type( at )
    local n, r
    if s == "string" then
        s = mw.text.trim( at )
        if s:find( "/", 2, true ) then
            local sT1, sT2, sD
            sT1, sT2, sD = s:match( "^(%d%d?)/1010(%d%d)(%d%d%d)$" )
            if sT1  and
               tonumber( sT1 ) == tonumber( sT2 ) then
                n = tonumber( sT1 .. sD )
            end
        elseif s:match( "^1010%d%d%d%d%d$" ) then
            n = tonumber( s:sub( 5 ) )
            if n == 0 then
                n = false
            end
        elseif s:match( "^[1-9]%d?%d?%d?%d?%d?$" ) then
            n = tonumber( s )
        elseif s:match( "^%d%d?%d?%.[1-9]%d?%d?%d?%d?%d?$" ) then
            r = string.format( "[[doi:10.1093/odnb/9780198614128.%s]]", s )
        end
    elseif s == "number"  and  at >= 1  and  at <= ODNB.maxID then
        n = math.floor( at )
    end
    if n then
        r = string.format( "[[doi:10.1093/ref:odnb/%d]]", n )
    elseif not r then
        if alert then
            local e = mw.html.create( "span" )
                        :addClass( "error" )
                        :css( { ["margin-left"]  = "1em",
                                ["margin-right"] = "1em" } )
            s = type( alert )
            if s == "string" then
                s = mw.text.trim( alert )
                if s == "" then
                    e = false
                else
                    e:wikitext( s )
                end
            elseif s == "table" then
                e:tag( alert )
            else
                e = false
            end
            if e then
                r = " " .. tostring( e )
            end
        end
        if type( assign ) == "string" then
            s = mw.text.trim( assign )
            if s ~= "" then
                r = string.format( "%s[[Category:%s]]",
                                   r or "",
                                   s )
            end
        end
    end
    return r or false
end -- ODNB.www



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.www = function ( frame )
    -- www link
    -- #invoke
    --     id   -- identifier
    --     err  -- error message if invalid
    --     cat  -- error category if invalid
    return ODNB.www( frame.args.id, frame.args.err, frame.args.cat )
           or ""
end -- p.www



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