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 15:04, 15 May 2013 by commons>Rillke (extending M.subpages)
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

function _M.subpages(title, redirects)
    local title, redirects, prefix = _M.clean(title), redirects and ' class="mw%-redirect"' 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 then
            local string, frame, expand = mw.ustring, mw:getCurrentFrame(), mw.text.unstrip;
            local path = '{{Special:PrefixIndex/%s/}}';
            prefix = string.len( title.text ) + 2;
            title = string.gmatch(
                expand( frame:preprocess( string.format(path, title.prefixedText ) ) ),
                '<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.