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
iterate over all subpages
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
function _M.subpages(title, redirects, ignoreSubpagesNotEnabled, onMoreSubpages, abortOnMoreSubpages)
         return ipairs({});
     local title, redirects, prefix = _M.clean(title), redirects and '[ %l="%-]-' or '';
    elseif not options.ignoreNS and not mw.site.namespaces[title.namespace].hasSubpages then
     local returnZeroResults = function()
         return ipairs({});
         prefix = nil
         title = function() return nil end
     end
     end
     prefix, title = pcall(mw.title.new, title, 0);
      
     if prefix and title then
    options.ignoreNS = nil;
        local ns = mw.site.namespaces[title.namespace]
    pattern = table.concat{ string.rep( '.', string.len( title.text ) ), '/', '([^\r\n]+)\r?\n' };
        if ns.hasSubpages or ignoreSubpagesNotEnabled then
     title = title.prefixedText;
            local string, frame, expand = mw.ustring, mw:getCurrentFrame(), mw.text.unstrip;
    local frame, expand, decode = mw:getCurrentFrame(), mw.text.unstrip, mw.text.decode;
            local path = '{{Special:PrefixIndex/%s/}}';
    local function get(name, options, pattern )
            local processedText = expand( frame:preprocess( string.format(path, title.prefixedText ) ) )
        local params = {};
           
        for k,v in pairs(options) do
            if onMoreSubpages then
            table.insert( params, table.concat{ k, '=', v ~= nil and tostring(v) or '' } );
                local cbType  = type(onMoreSubpages)
        end
                local cbCompat= cbType == 'function' or ( cbType == 'table' and getmetatable(onMoreSubpages).__call )
        params = table.concat( params, '|' );
                if cbCompat then
        if params ~= "" then params = table.concat{ '|', params }; end
                    -- Check whether there are more than 344 subpages (which is the maximum returned by Special:PrefixIndex)
        params = expand( frame:preprocess( string.format('{{special:prefixindex/%s/%s}}', name, params) ) );
                    local res, count = processedText:gsub( '<a ', '<a ' )
        params = string.gsub( params, '%b<>', function(tag) return tag == '</a>' and '\n' or ''; end );
                    if count > 344 then
        options = {}
                        onMoreSubpages()
        for k in string.gmatch( params, pattern ) do table.insert( options, decode(k) ); end
                        if abortOnMoreSubpages then processedText = '' end
        return options;
                    end
    end;
                 end
    local subpages = get( title, options, pattern );
   
    return function(title, last)
        local page;
        while true do
            page = table.remove( subpages, 1 );
            if page ~= nil then return page, last; end
            options.from = table.concat{ title, '/', last };
            subpages = get( title, options, pattern );
            table.remove( subpages, 1 );
            if #subpages == 0 then
                 return nil;
             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

Revision as of 18:21, 18 May 2013

Module documentation[ create · purge ]
This documentation is transcluded from Module:Page/doc.
local _M = {};

function _M.clean(title)
    local string = mw.ustring;
    
    title = string.gsub( title, '^%s*%[*%[%[(.-)%]%]%]*%s*$', function(l)
        return string.gsub( l, '^([^|]*)|.*$', '%1', 1 )
    end, 1 )
        :gsub( '[%s_]+', ' ' )
        :gsub( '/+', '/' )
        :gsub( '^%s', '', 1 )
        :gsub( '%s$', '', 1 )
        :gsub( '/$', '', 1 );
    
    if title == '' then
        return tostring( mw.title.getCurrentTitle() );
    elseif string.sub( title, 1, 1) == '/' then
        return table.concat{ tostring( mw.title.getCurrentTitle() ), title };
    else
        return title;
    end
end

-- subpages = require('Module:Page').subpages
-- for page in subpages('Page', [{options}]) do ... end

function _M.subpages(title, options )
    local title, options, pattern = _M.clean(title), options or {};
    
    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
        params = expand( frame:preprocess( string.format('{{special:prefixindex/%s/%s}}', name, params) ) );
        params = string.gsub( params, '%b<>', function(tag) return tag == '</a>' and '\n' or ''; end );
        options = {}
        for k in string.gmatch( params, pattern ) do table.insert( options, decode(k) ); end
        return options;
    end;
    local subpages = get( title, options, pattern );
    
    return function(title, last)
        local page;
        while true do
            page = table.remove( subpages, 1 );
            if page ~= nil then return page, last; end
            options.from = table.concat{ title, '/', last };
            subpages = get( title, options, pattern );
            table.remove( subpages, 1 );
            if #subpages == 0 then
                return nil;
            end
        end
    end, title;
end

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