From Wikibooks, open books for an open world
Jump to navigation Jump to search
Module documentation[view] [edit] [history] [purge]

Note: this module is not working at the moment. See Commons:Module:Page for details.

Iterates over page names under a prefix, using Special:PrefixIndex. The prefix is not stripped from the subpage names.

Usage[edit source]

subpages = require('Module:Subpages').subpages
for page in subpages('Page') do
    -- etc.

See also[edit source]

Based on m:Module:Subpages.

local _M = {};

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

function _M.subpages(title, options)
    local title, options, query = _M.clean(title), options or {};
    query, title = pcall(, title, 0);
    if not query or not title then
        return ipairs({});
    elseif not options.ignoreNS and not[title.namespace].hasSubpages then
        return ipairs({});
    title = title.prefixedText;
    -- merge options
    query = {};
    options.ignoreNS = nil;
    options.stripprefix = 1;
    for k,v in pairs(options) do
        if v ~= nil then
            table.insert( query, table.concat{ k, '=', tostring(v) } );
    query = table.concat{ '{{special:prefixindex/', title, '|', table.concat( query, '|' ) };
    -- the pages
    local frame, expand = mw:getCurrentFrame(), mw.text.unstrip;
    local function get(name)
        return string.gmatch( expand( frame:preprocess( name ) ), '<a[^>]*>(.-)</a>' );
    local pages = get(table.concat{ query, '}}' });
    local count = 0;
    return function(title, last)
        local query, get, page, decode = query, get, pages(), mw.text.decode;
        count = count+1;
        if page ~= nil then return decode(page), count; end
        if last == nil then return nil, count-1; end
        pages = get( table.concat{ query, '|from=', title, last, '}}' } );
        page = pages();
        return (page and decode(page)), count
    end, title;

return _M