Modul:PageUtil: Unterschied zwischen den Versionen

2.884 Bytes hinzugefügt ,  22. Juni 2020
K
11 Versionen importiert: Extension Graph
(2017-11-11)
K (11 Versionen importiert: Extension Graph)
 
(4 dazwischenliegende Versionen von 3 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
local PageUtil = { suite  = "PageUtil",
local PageUtil = { suite  = "PageUtil",
                   serial = "2017-11-11" };
                   serial = "2018-10-19",
                  item  = 0 }
--[=[
--[=[
PageUtil
PageUtil
Zeile 14: Zeile 15:
     -- Format message with class="error"
     -- Format message with class="error"
     --    alert  -- string, with message
     --    alert  -- string, with message
     --    frame  -- object
     --    frame  -- object, if known
     -- Returns message with markup
     -- Returns message with markup
     local r = alert
     local scream = alert
     if frame then
     if frame then
         r = string.format( "%s * %s", frame:getTitle(), r )
         scream = string.format( "%s * %s", frame:getTitle(), scream )
     end
     end
     r = string.format( "<span class=\"error\">ERROR * %s</span>", r )
     return tostring( mw.html.create( "span" )
    return r
                            :addClass( "error" )
                            :wikitext( scream ) )
end -- fault()
end -- fault()


Zeile 75: Zeile 77:
             end
             end
         else
         else
             r = string.format( "<div class=\"error\">%s</div>",
             r = tostring( mw.html.create( "div" )
                              source )
                                :addClass( "error" )
                                :wikitext( source ) )
         end
         end
     end
     end
Zeile 105: Zeile 108:
                 seed = source
                 seed = source
             end
             end
            r = frame:expandTemplate( { title = seed } )
             r = string.format( "%s\n%s",
             r = string.format( "<span id='%s'></span>\n%s", segment, r )
                              tostring( mw.html.create( "span" )
                                                :attr( "id", segment ) ),
                              frame:expandTemplate( { title = seed } ) )
             table.insert( assembly,  { source, segment } )
             table.insert( assembly,  { source, segment } )
         else
         else
             r = string.format( "<div class=\"error\">%s</div>",
             r = tostring( mw.html.create( "div" )
                              page.prefixedText )
                                :addClass( "error" )
                                :wikitext( page.prefixedText ) )
         end
         end
     else
     else
         r = string.format( "<div class=\"error\">%s '%s'</div>",
         r = string.format( "%s '%s'", "Unknown page", access )
                          "Unknown page", access )
        r = tostring( mw.html.create( "div" )
                            :addClass( "error" )
                            :wikitext( r ) )
     end
     end
     return r
     return r
Zeile 122: Zeile 130:


PageUtil.failsafe = function ( assert )
PageUtil.failsafe = function ( 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
     local r
     if not assert or  assert <= PageUtil.serial then
    if since == "wikidata" then
         r = PageUtil.serial
        local item = PageUtil.item
        since = false
        if type( item ) == "number"  and  item > 0 then
            local ent = mw.wikibase.getEntity( string.format( "Q%d",
                                                              item ) )
            if type( ent ) == "table" then
                local vsn = ent: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 <= PageUtil.serial then
            r = PageUtil.serial
         else
            r = false
        end
    end
    return r
end -- PageUtil.failsafe()
 
 
 
PageUtil.getProtection = function ( access, action )
    -- Retrieve protection
    --    access  -- string or title or nil, with page, default: current
    --    action  -- string or nil, with action, default: edit
    -- Returns number: One of: 0, 0.5, 0.75, 1
    local t = type( access )
    local r = 0
    local p
    if t == "string" then
        t = mw.title.new( access )
    elseif t == "table" then
        t = access
     else
     else
         r = false
         t = mw.title.getCurrentTitle()
    end
    p = t.protectionLevels
    if type( p ) == "table" then
        local s
        if type( action ) == "string" then
            s = mw.text.trim( action )
            if s == "" then
                s = false
            end
        end
        p = p[ s or "edit" ]
        if type( p ) == "table" then
            for k, v in pairs( p ) do
                if v == "autoconfirmed" then
                    r = 0.5
                elseif v == "editeditorprotected" then
                    r = 0.75
                elseif v == "sysop" then
                    r = 1
                end
            end -- for k, v
        end
     end
     end
     return r
     return r
end -- PageUtil.failsafe()
end -- PageUtil.getProtection()




Zeile 190: Zeile 265:
-- Export
-- Export
local p = { }
local p = { }
p.getProtection = function ( frame )
    local n = PageUtil.getProtection( frame.args[ 1 ], frame.args[ 2 ] )
    local t = { [ 0 ]    = "",
                [ 0.5 ]  = mw.ustring.char( 189 ),
                [ 0.75 ] = mw.ustring.char( 190 ),
                [ 1 ]    = "1" }
    return t[ n ]
end -- p.getProtection


function p.isRedirect()
function p.isRedirect()
     return mw.title.getCurrentTitle().isRedirect and "1"  or  "";
     return mw.title.getCurrentTitle().isRedirect and "1"  or  ""
end -- p.isRedirect
end -- p.isRedirect


Zeile 201: Zeile 285:
     end
     end
     return r
     return r
end
end -- p.merge


p.failsafe = function ( frame )
p.failsafe = function ( frame )