Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Page: Difference between revisions

From Neodyland Wiki
m fix
mNo edit summary
 
(One intermediate revision by one other user not shown)
Line 23: Line 23:


-- subpages = require('Module:Page').subpages
-- subpages = require('Module:Page').subpages
-- for page in subpages('Page') do ... end
-- for page in subpages('Page', [{options}]) do ... end
-- Extended: redirects now reported and if namespace has subpages disabled, return zero results


-- Debug strings
function _M.subpages(title, options )
-- for pg in p.subpages( "Template:Translation_possible", true ) do print(pg) end -->  >1 results
    local title, options, pattern = _M.clean(title), options or {};
-- for pg in p.subpages( "Template:Translation_possible" ) do print(pg) end -->  0 results
   
-- for pg in p.subpages( "Commons:Picture of the Year/2010", true, true, function() print('more found') end, true ) do print(pg) end --> "more found"
    pattern, title = pcall(mw.title.new, title, 0);
    if not pattern or not title then
        return ipairs({});
    elseif not options.ignoreNS and not mw.site.namespaces[title.namespace].hasSubpages then
        return ipairs({});
    end
   
    options.ignoreNS = nil;
    pattern = table.concat{ string.rep( '.', string.len( title.text ) ), '/', '([^\r\n]+)\r?\n' };
    title = title.prefixedText;
    local frame, expand, decode = mw:getCurrentFrame(), mw.text.unstrip, mw.text.decode;
    local function get(name, options, pattern )
        local params = {};
        for k,v in pairs(options) do
            table.insert( params, table.concat{ k, '=', v ~= nil and tostring(v) or '' } );
        end
        params = table.concat( params, '|' );
        if params ~= "" then params = table.concat{ '|', params }; end
 
        --[[
Due to a change in Scribunto for Lua, expand() will no longer work on special tags,
including <gallery>, <ref> or other costly extension tags (except "#tag:") or special parser
functions which are converted to UNIQ markers starting and ending by an ASCII DEL character ('\127').
 
mw.text.unstrip() now ONLY expands and unstrips "<nowiki>" tags, and completely kills ALL others
markers without expanding them to their generated HTML!
 
The actual full expansion of stripped tags can occur only in MediaWiki after return from Lua.
@see https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#mw.text.unstrip
 
For this reason we only get an empty list of subpages, and we can no longer bypass the costly
expansion limits of "#ifexist:" or costly calls to mw.title.new() by using the Special:prefixindex
parser function...


function _M.subpages(title, redirects, ignoreSubpagesNotEnabled, onMoreSubpages, abortOnMoreSubpages)
It is also not possible to use frame:callParserFunction() to process "Special:*" parser functions,
    local title, redirects, prefix = _M.clean(title), redirects and '[ %l="%-]-' or '';
even if they are converted to "#tag:Special*" in Mediawiki, they are stripped in MediaWiki,
    local returnZeroResults = function()
but they not found by Lua's frame:callParserFunction().
         prefix = nil
 
         title = function() return nil end
As well frame:extensionTag() cannot be used and generate the same error in Lua (tag "special" not found).
     end
        --]]
     prefix, title = pcall(mw.title.new, title, 0);
        params = expand( frame:preprocess( string.format('{{special:prefixindex/%s/%s}}', name, params) ) );
     if prefix and title then
        params = string.gsub( params, '%b<>', function(tag) return tag == '</a>' and '\n' or ''; end );
         local ns =  mw.site.namespaces[title.namespace]
         options = {}
         if ns.hasSubpages or ignoreSubpagesNotEnabled then
         for k in string.gmatch( params, pattern ) do table.insert( options, decode(k) ); end
             local string, frame, expand = mw.ustring, mw:getCurrentFrame(), mw.text.unstrip;
        return options;
             local path = '{{Special:PrefixIndex/%s/}}';
     end;
             local processedText = expand( frame:preprocess( string.format(path, title.prefixedText ) ) )
     local subpages = get( title, options, pattern );
              
      
            if onMoreSubpages then
    return function(title, last)
                local cbType  = type(onMoreSubpages)
         local page;
                local cbCompat= cbType == 'function' or ( cbType == 'table' and getmetatable(onMoreSubpages).__call )
         while true do
                if cbCompat then
             page = table.remove( subpages, 1 );
                    -- Check whether there are more than 344 subpages (which is the maximum returned by Special:PrefixIndex)
            if page ~= nil then return page, last; end
                    local res, count = processedText:gsub( '<a ', '<a ' )
             options.from = table.concat{ title, '/', last };
                    if count > 344 then
             subpages = get( title, options, pattern );
                        onMoreSubpages()
             table.remove( subpages, 1 );
                        if abortOnMoreSubpages then processedText = '' end
            if #subpages == 0 then
                    end
                 return nil;
                 end
             end
             end
           
            prefix = string.len( title.text ) + 2;
            title = string.gmatch( processedText, '<a href="[^"]*" title="[^"]*"' .. redirects .. '>(.-)</a>' );
        else
            returnZeroResults()
         end
         end
    else
     end, title;
        returnZeroResults()
     end
    return function()
        local pg = title();
        return pg and mw.ustring.sub(pg, prefix) or nil;
    end
end
end


return _M
return _M
Cookies help us deliver our services. By using our services, you agree to our use of cookies.