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.
Revision as of 20:41, 16 May 2013 by commons>Rillke (+onMoreSubpages +abortOnMoreSubpages options)
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') do ... end
-- Extended: redirects now reported and if namespace has subpages disabled, return zero results

-- Debug strings
-- for pg in p.subpages( "Template:Translation_possible", true ) do print(pg) end -->  >1 results
-- for pg in p.subpages( "Template:Translation_possible" ) do print(pg) end -->   0 results

function _M.subpages(title, redirects, ignoreSubpagesNotEnabled, onMoreSubpages, abortOnMoreSubpages)
    local title, redirects, prefix = _M.clean(title), redirects and '[ %l="%-]-' or '';
    local returnZeroResults = function()
        prefix = nil
        title = function() return nil end
    end
    prefix, title = pcall(mw.title.new, title, 0);
    if prefix and title then
        local ns =  mw.site.namespaces[title.namespace]
        if ns.hasSubpages or ignoreSubpagesNotEnabled then
            local string, frame, expand = mw.ustring, mw:getCurrentFrame(), mw.text.unstrip;
            local path = '{{Special:PrefixIndex/%s/}}';
            local processedText = expand( frame:preprocess( string.format(path, title.prefixedText ) ) )
            
            if onMoreSubpages then
                local cbType  = type(onMoreSubpages)
                local cbCompat= cbType == 'function' or ( cbType == 'table' and getmetatable(onMoreSubpages).__call )
                if cbCompat then
                    -- Check whether there are more than 344 subpages (which is the maximum returned by Special:PrefixIndex)
                    local res, count = processedText:gsub( '<a>', '<b>' )
                    if count > 344 then
                        onMoreSubpages()
                        if abortOnMoreSubpages then processedText = '' end
                    end
                end
            end
            
            prefix = string.len( title.text ) + 2;
            title = string.gmatch( processedText, '<a href="[^"]*" title="[^"]*"' .. redirects .. '>(.-)</a>' );
        else
            returnZeroResults()
        end
    else
        returnZeroResults()
    end
    return function()
        local pg = title();
        return pg and mw.ustring.sub(pg, prefix) or nil;
    end
end

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