Modul:Vorlage:LuaModuleDoc: Unterschied zwischen den Versionen

(2018-02-18)
(2018-08-01)
Zeile 1: Zeile 1:
local LuaModuleDoc = { suite  = "LuaModuleDoc",
local LuaModuleDoc = { suite  = "LuaModuleDoc",
                       serial = "2018-02-18",
                       serial = "2018-08-01",
                       item  = 12981914 }
                       item  = 12981914,
                      frame  = false,
                      ns    = -9999,
                                -- current namespace number
                      nsDocs = -99999,
                                -- central documentation namespace number
                      title  = false,
                                -- current page Title object
                      transl = false }
--[=[
--[=[
Support {{LuaModuleDoc}}
Support {{LuaModuleDoc}}
Zeile 7: Zeile 15:
* failsafe()
* failsafe()
]=]
]=]
-- Module globals
local CurrentTitle
local Frame




Zeile 21: Zeile 23:
     --    arg    -- string or number; argument identifier
     --    arg    -- string or number; argument identifier
     --    assign  -- any, optional; default value
     --    assign  -- any, optional; default value
     local r = Frame.args[ arg ]
    -- Uses:
    --    >  LuaModuleDoc.frame
     local r = LuaModuleDoc.frame.args[ arg ]
     if type( r ) ~= "string" then
     if type( r ) ~= "string" then
         if assign == nil then
         if assign == nil then
Zeile 65: Zeile 69:
     --    string
     --    string
     -- Uses:
     -- Uses:
    --    >  LuaModuleDoc.frame
     --    fetch()
     --    fetch()
     -- MediaWiki:Move-leave-redirect  Weiterleitung erstellen
     -- MediaWiki:Move-leave-redirect  Weiterleitung erstellen
Zeile 101: Zeile 106:
         end
         end
         path  = { sub,  mw.uri.buildQueryString( create ) }
         path  = { sub,  mw.uri.buildQueryString( create ) }
         r      = Frame:callParserFunction( "fullurl", path )
         r      = LuaModuleDoc.frame:callParserFunction( "fullurl", path )
         show  = mw.message.new( show ):plain()
         show  = mw.message.new( show ):plain()
         button = mw.title.makeTitle( mw.site.namespaces.Template.id,
         button = mw.title.makeTitle( mw.site.namespaces.Template.id,
Zeile 111: Zeile 116:
                       Link    = r,
                       Link    = r,
                       Text    = show }
                       Text    = show }
             r      = Frame:expandTemplate{ title = button.template,
             r      = LuaModuleDoc.frame:expandTemplate{
                                          args  = button }
                                                title = button.template,
                                                args  = button }
         else
         else
             r = string.format( "<br>[%s '''%s''']", r, show )
             r = string.format( "<br>[%s '''%s''']", r, show )
Zeile 126: Zeile 132:
     --    script  -- string; module name (main)
     --    script  -- string; module name (main)
     -- Uses:
     -- Uses:
    --    >  LuaModuleDoc.title
    --    >  LuaModuleDoc.frame
     --    fetch()
     --    fetch()
     --    >< CurrentTitle
     --    mw.wikibase.getEntity()
    --    mw.title.makeTitle()
     -- Return:
     -- Return:
     --    string
     --    string
    -- Uses:
    --    mw.wikibase.getEntity()
    --    mw.title.makeTitle()
     local entity = mw.wikibase.getEntity()
     local entity = mw.wikibase.getEntity()
     local r
     local r
Zeile 150: Zeile 156:
                     t = mw.title.makeTitle( space, s )
                     t = mw.title.makeTitle( space, s )
                     s = "//" .. repo.host
                     s = "//" .. repo.host
                     if mw.title.equals( CurrentTitle, t )  and
                     if mw.title.equals( LuaModuleDoc.title, t )  and
                       mw.site.server:find( s, 1, true ) then
                       mw.site.server:find( s, 1, true ) then
                         mode = 2
                         mode = 2
Zeile 160: Zeile 166:
             s = fetch( "templateGlobal", "" )
             s = fetch( "templateGlobal", "" )
             if #s > 0 then
             if #s > 0 then
                 local lucky, bib = pcall( require,
                 local l, bib = pcall( require,
                                          CurrentTitle.prefixedText )
                                      LuaModuleDoc.title.prefixedText )
                 local vsn = entity:formatPropertyValues( "P348" )
                 local vsn = entity:formatPropertyValues( "P348" )
                 params = { }
                 params = { }
Zeile 195: Zeile 201:
                     params.update = "1"
                     params.update = "1"
                 end
                 end
                 r = Frame:expandTemplate{ title=s,
                 r = LuaModuleDoc.frame:expandTemplate{ title=s,
                                          args=params }
                                                      args=params }
             end
             end
         end
         end
Zeile 232: Zeile 238:




local function navDevelop( nsDocs, start, script )
local function navDevelop( start, script )
     -- Most interesting test and development page
     -- Most interesting test and development page
     -- Precondition:
     -- Precondition:
    --    nsDocs  -- number; central documentation namespace number
     --    start  -- string; central documentation root
     --    start  -- string; central documentation root
     --    script  -- string; basic module name
     --    script  -- string; basic module name
Zeile 241: Zeile 246:
     --    string or false:  full page name, if any
     --    string or false:  full page name, if any
     -- Uses:
     -- Uses:
    --    >  LuaModuleDoc.nsDocs
     --    mw.title.makeTitle()
     --    mw.title.makeTitle()
     local n  = nsDocs
     local n  = LuaModuleDoc.nsDocs
     local r  = false
     local r  = false
     local sub = "/" .. fetch( "subTest", "Test" )
     local sub = "/" .. fetch( "subTest", "Test" )
Zeile 280: Zeile 286:
     --    specific  -- string, optional; additional information
     --    specific  -- string, optional; additional information
     -- Uses:
     -- Uses:
    --    >  LuaModuleDoc.frame
     --    fetch()
     --    fetch()
     local show = fetch( "pageErr" )
     local show = fetch( "pageErr" )
Zeile 288: Zeile 295:
             table.insert( pars, specific )
             table.insert( pars, specific )
         end
         end
         r = Frame:expandTemplate{ title = show, args = pars }
         r = LuaModuleDoc.frame:expandTemplate{ title = show,
                                              args = pars }
     else
     else
         local e = mw.html.create( "span" )
         local e = mw.html.create( "span" )
Zeile 334: Zeile 342:




local function navLangs( nsDocs, start, script )
local function navLangs( start, script )
     -- Analyze languages
     -- Analyze languages
     -- Precondition:
     -- Precondition:
     --    current page is supposed to transclude LuaModuleDoc
     --    current page is supposed to transclude LuaModuleDoc
    --    nsDocs  -- number; central documentation namespace number
     --    start  -- string; central documentation root
     --    start  -- string; central documentation root
     --    script  -- string; basic module name
     --    script  -- string; basic module name
     -- Return:
     -- Postcondition:
     --    table: args for navigation template
     --    LuaModuleDoc.transl is set up as table:
     --             [1] basic module name
    --                        args for navigation template
     --             [2] number of language codes  +  1
     --                         [1] basic module name
     --             [3] first language code
     --                         [2] number of language codes  +  1
     --             [4] second language code
     --                         [3] first language code
     --             ... list of further language codes
     --                         [4] second language code
     --                         ... list of further language codes
     -- Uses:
     -- Uses:
    --    >  LuaModuleDoc.nsDocs
    --      < LuaModuleDoc.transl
     --    fetch()
     --    fetch()
     --    navLang()
     --    navLang()
     --    mw.title.makeTitle()
     --    mw.title.makeTitle()
     local e, i, s, t
     local e, i, s, t
    local r = { }
     local specified = fetch( "langsRequest", false )
     local specified = fetch( "langsRequest", false )
     local super    = start .. "/" .. script .. "/"
     local super    = start .. "/" .. script .. "/"
    LuaModuleDoc.transl = { }
     if type( specified ) == "string" then
     if type( specified ) == "string" then
         if specified:match( "^%s*$" ) then
         if specified:match( "^%s*$" ) then
Zeile 361: Zeile 371:
         end
         end
     end
     end
     navLang( fetch( "langsDefault" ),  r,  specified )
     navLang( fetch( "langsDefault" ),  LuaModuleDoc.transl,  specified )
     navLang( fetch( "langsMore" ),  r,  true  )
     navLang( fetch( "langsMore" ),  LuaModuleDoc.transl,  true  )
     navLang( specified, r, false )
     navLang( specified, LuaModuleDoc.transl, false )
     if #r < 1 then
     if #LuaModuleDoc.transl < 1 then
         r = { { "en", false } }
         LuaModuleDoc.transl = { { "en", false } }
     end
     end
     for i = #r, 1, -1 do
     for i = #LuaModuleDoc.transl, 1, -1 do
         e = r[ i ]
         e = LuaModuleDoc.transl[ i ]
         s = e[ 1 ]
         s = e[ 1 ]
         if e[ 2 ] then
         if e[ 2 ] then
             t = mw.title.makeTitle( nsDocs,  super .. s )
             t = mw.title.makeTitle( LuaModuleDoc.nsDocs,  super .. s )
             if not t.exists then
             if not t.exists then
                 s = false
                 s = false
Zeile 377: Zeile 387:
         end
         end
         if s then
         if s then
             r[ i ] = s
             LuaModuleDoc.transl[ i ] = s
         else
         else
             table.remove( r, i )
             table.remove( LuaModuleDoc.transl, i )
         end
         end
     end -- for i -1
     end -- for i -1
     table.insert( r, 1, script )
     table.insert( LuaModuleDoc.transl,
     table.insert( r, 2, tostring( #r ) )
                  1,
    return r
                  script )
     table.insert( LuaModuleDoc.transl,
                  2,
                  tostring( #LuaModuleDoc.transl ) )
end -- navLangs()
end -- navLangs()






local function navMerge( start, swift, ns, nsDocs, script, sub, lead )
local function navMerge( start, swift, script, sub, lead )
     -- Include various external content into page
     -- Include various external content into page
     -- Precondition:
     -- Precondition:
     --    start  -- string; defined pageDocRoot argument
     --    start  -- string; defined pageDocRoot argument
     --    swift  -- string; defined pageNav argument
     --    swift  -- string; defined pageNav argument
    --    ns      -- number; current namespace number
    --    nsDocs  -- number; central documentation namespace number
     --    script  -- string; module name (main)
     --    script  -- string; module name (main)
     --    sub    -- string or false; possible sub-module name
     --    sub    -- string or false; possible sub-module name
     --    lead    -- true: Module: namespace;  false: text namespace
     --    lead    -- true: Module: namespace;  false: text namespace
     -- Uses:
     -- Uses:
     --    >< CurrentTitle
     --    > LuaModuleDoc.transl
     --    navLangs()
     --    >  LuaModuleDoc.ns
    --    >  LuaModuleDoc.nsDocs
    --    >  LuaModuleDoc.title
    --    >  LuaModuleDoc.frame
     --    navError()
     --    navError()
     --    mw.title.makeTitle()
     --    mw.title.makeTitle()
     --    createPage()
     --    createPage()
     --    fetch()
     --    fetch()
     local server = mw.site.server
     local server = mw.site.server
     local super   = false
     local super = false
    local collect = navLangs( nsDocs, start, script )
     local t     = navDevelop( start, script )
     local t       = navDevelop( nsDocs, start, script )
     local collect, low, r, s
     local low, r, s
    collect = LuaModuleDoc.transl
     if t then
     if t then
         collect.Test = t
         collect.Test = t
     end
     end
     if ns == nsDocs then
     if LuaModuleDoc.ns == LuaModuleDoc.nsDocs then
         s = string.format( "%s/%s", start, script )
         s = string.format( "%s/%s", start, script )
         if CurrentTitle.text == s then
         if LuaModuleDoc.title.text == s then
             super = CurrentTitle.text .. "/"
             super = LuaModuleDoc.title.text .. "/"
         elseif sub then
         elseif sub then
             collect.subDoc = sub
             collect.subDoc = sub
             collect.subModule = sub
             collect.subModule = sub
             s = string.format( "%s/%s/%s", start, script, sub )
             s = string.format( "%s/%s/%s", start, script, sub )
             if CurrentTitle.text == s then
             if LuaModuleDoc.title.text == s then
                 s = string.format( "%s/%s", s, collect[ 3 ] )
                 s = string.format( "%s/%s", s, collect[ 3 ] )
                 t = mw.title.makeTitle( nsDocs, s )
                 t = mw.title.makeTitle( LuaModuleDoc.nsDocs, s )
                 if t.exists then
                 if t.exists then
                     super = CurrentTitle.text .. "/"
                     super = LuaModuleDoc.title.text .. "/"
                 end
                 end
             end
             end
Zeile 432: Zeile 446:
     elseif lead and sub then
     elseif lead and sub then
         s = string.format( "%s/%s/%s", start, script, sub )
         s = string.format( "%s/%s/%s", start, script, sub )
         t = mw.title.makeTitle( nsDocs, s )
         t = mw.title.makeTitle( LuaModuleDoc.nsDocs, s )
         if t.exists then
         if t.exists then
             collect.subDoc = sub
             collect.subDoc = sub
Zeile 446: Zeile 460:
         end
         end
     end
     end
     r = Frame:expandTemplate{ title = swift, args = collect }
     r = LuaModuleDoc.frame:expandTemplate{ title = swift,
                                          args = collect }
     if not low then
     if not low then
         t = CurrentTitle.talkPageTitle
         t = LuaModuleDoc.title.talkPageTitle
         if ns == mw.site.namespaces.Module.id then
         if LuaModuleDoc.ns == mw.site.namespaces.Module.id then
             local doc
             local doc
             s  = string.format( "%s/%s", start, script )
             s  = string.format( "%s/%s", start, script )
             doc = mw.title.makeTitle( nsDocs, s )
             doc = mw.title.makeTitle( LuaModuleDoc.nsDocs, s )
             if not doc.exists then
             if not doc.exists then
                 r = r .. createPage( "NoPageCentral",
                 r = r .. createPage( "NoPageCentral",
                                     doc.prefixedText,
                                     doc.prefixedText,
                                     start,
                                     start,
                                     nsDocs,
                                     LuaModuleDoc.nsDocs,
                                     script )
                                     script )
                 t = false
                 t = false
Zeile 465: Zeile 480:
             if super then
             if super then
                 s = "NoTalkCentral"
                 s = "NoTalkCentral"
             elseif ns == nsDocs + 1 then
             elseif LuaModuleDoc.ns == LuaModuleDoc.nsDocs + 1 then
                 s = false
                 s = false
             else
             else
Zeile 477: Zeile 492:
                                               t.prefixedText,
                                               t.prefixedText,
                                               start,
                                               start,
                                               nsDocs + 1,
                                               LuaModuleDoc.nsDocs + 1,
                                               script ) )
                                               script ) )
             end
             end
Zeile 488: Zeile 503:
         local space, sub, support
         local space, sub, support
         for i = 3, #collect do
         for i = 3, #collect do
             t = mw.title.makeTitle( nsDocs, super .. collect[ i ] )
             t = mw.title.makeTitle( LuaModuleDoc.nsDocs,
                                    super .. collect[ i ] )
             s = t:getContent()
             s = t:getContent()
             if s then
             if s then
Zeile 499: Zeile 515:
                 else
                 else
                     r = r ..
                     r = r ..
                         Frame:expandTemplate{ title = t.prefixedText }
                         LuaModuleDoc.frame:expandTemplate{
                                                title = t.prefixedText }
                     break    -- for i
                     break    -- for i
                 end
                 end
Zeile 513: Zeile 530:
                     swift = string.format( "%s/%s", swift, sub )
                     swift = string.format( "%s/%s", swift, sub )
                 end
                 end
                 r = r .. Frame:expandTemplate{ title = s,
                 r = r ..
                                              args = { swift } }
                    LuaModuleDoc.frame:expandTemplate{ title = s,
                                                      args = { swift } }
             end
             end
         end
         end
Zeile 535: Zeile 553:




local function navPage( lead, ns, nsDocs )
local function navPage( lead )
     -- Return navigation text; analyze page location
     -- Return navigation text; analyze page location
     -- Precondition:
     -- Precondition:
     --    current namespace will support LuaModuleDoc
     --    current namespace will support LuaModuleDoc
     --    lead    -- true: Module: namespace;  false: text namespace
     --    lead    -- true: Module: namespace;  false: text namespace
    --    ns      -- number; current namespace
    --    nsDocs  -- number; namespace for doc
     -- Uses:
     -- Uses:
     --    >< CurrentTitle
     --    > LuaModuleDoc.title
    --    >  LuaModuleDoc.transl
     --    fetch()
     --    fetch()
    --    navLangs()
    --    navMerge()
     --    navError()
     --    navError()
    --    navMerge()
     local r
     local r
     local start = fetch( "pageDocRoot" )
     local start = fetch( "pageDocRoot" )
Zeile 556: Zeile 574:
         end
         end
         s = s .. "([^/]+)/(.*/?)$"
         s = s .. "([^/]+)/(.*/?)$"
         script, s = string.match( CurrentTitle.text .. "/",  s )
         script, s = string.match( LuaModuleDoc.title.text .. "/",  s )
         if type( script ) == "string" then
         if type( script ) == "string" then
             local sub  = false
             local sub  = false
             local swift = fetch( "pageNav" )
             local swift = fetch( "pageNav" )
            navLangs( start, script )
             if type( s ) == "string" then
             if type( s ) == "string" then
                 if #s > 1 then
                 if #s > 1 then
Zeile 568: Zeile 587:
                     if q.exists and q.contentModel == "Scribunto" then
                     if q.exists and q.contentModel == "Scribunto" then
                         sub = s
                         sub = s
                        for i = 1, #LuaModuleDoc.transl do
                            if LuaModuleDoc.transl[ i ] == s then
                                sub = false
                                break    -- for i
                            end
                        end -- for i
                     end
                     end
                 end
                 end
             end
             end
             if type( swift ) == "string" then
             if type( swift ) == "string" then
                 r = navMerge( start, swift, ns, nsDocs, script, sub, lead )
                 r = navMerge( start,
                              swift,
                              script,
                              sub,
                              lead )
             else
             else
                 r = navError( "configMissing", "pageNav" )
                 r = navError( "configMissing", "pageNav" )
             end
             end
         else
         else
             r = navError( "BadPage" ) .. CurrentTitle.text
             r = navError( "BadPage" ) .. LuaModuleDoc.title.text
         end
         end
     else
     else
Zeile 590: Zeile 619:
     -- Start execution; return navigation text; analyze namespace
     -- Start execution; return navigation text; analyze namespace
     -- Uses:
     -- Uses:
     --    >< CurrentTitle
     --     < LuaModuleDoc.title
    --      < LuaModuleDoc.ns
     --      < LuaModuleDoc.nsDocs
     --    fetch()
     --    fetch()
     --    mw.title.getCurrentTitle()
     --    mw.title.getCurrentTitle()
Zeile 598: Zeile 629:
     local nsDocs = fetch( "nsDocs" )
     local nsDocs = fetch( "nsDocs" )
     if nsDocs then
     if nsDocs then
         CurrentTitle = mw.title.getCurrentTitle()
         local lead
         local ns   = CurrentTitle.namespace
        LuaModuleDoc.title  = mw.title.getCurrentTitle()
         local lead = ( ns == mw.site.namespaces.Module.id )
         LuaModuleDoc.ns     = LuaModuleDoc.title.namespace
         nsDocs = tonumber( nsDocs )
         lead               = ( LuaModuleDoc.ns ==
         if lead or ns == nsDocs  or ns == nsDocs + 1 then
                                mw.site.namespaces.Module.id )
             r = navPage( lead, ns, nsDocs )
         LuaModuleDoc.nsDocs = tonumber( nsDocs )
         if lead or
          LuaModuleDoc.ns == LuaModuleDoc.nsDocs  or
          LuaModuleDoc.ns == LuaModuleDoc.nsDocs + 1 then
             r = navPage( lead )
         else
         else
             r = navError( "BadNamespace" )
             r = navError( "BadNamespace" )
Zeile 612: Zeile 647:
     return  r
     return  r
end -- navigation()
end -- navigation()
LuaModuleDoc.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 r
    if since == "wikidata" then
        local item = LuaModuleDoc.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 <= LuaModuleDoc.serial then
            r = LuaModuleDoc.serial
        else
            r = false
        end
    end
    return r
end -- LuaModuleDoc.failsafe()






-- Provide template access
-- Provide template access
local p = {}
local p = {}


function p.nav( frame )
function p.nav( frame )
     -- Uses:
     -- Uses:
    --      < LuaModuleDoc.frame
     --    navigation()
     --    navigation()
     local lucky, r
     local lucky, r
     Frame = frame
     LuaModuleDoc.frame = frame
     lucky, r = pcall( navigation )
     lucky, r = pcall( navigation )
     if not lucky then
     if not lucky then
Zeile 634: Zeile 705:
end
end


function p.failsafe()
 
     return LuaModuleDoc.serial
 
p.failsafe = function ( frame )
    -- Check or retrieve version information
    -- Precondition:
    --    frame  -- object; #invoke environment
    -- Postcondition:
    --    Return string with error message or ""
    -- Uses:
    --    LuaModuleDoc.failsafe()
    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 LuaModuleDoc.failsafe( since )  or  ""
end -- p.failsafe()
end -- p.failsafe()


return p
return p