Extension:ListSubPages

Here's a simple extension I wrote for listing subpages. I looked at SubPageList2 but it just seemed overly complicated. This should work with MediaWiki 1.6 and onwards, and it should work with PostgreSQL or MySQL database backends. I'm using it with 1.6.10, PHP4 & MySQL 4.1 in one place and with 1.12alpha, PHP5 and PostgreSQL 8.1 in another, so it should cover a fairly wide gamut.

The sub page titles are displayed as a list of wiki links, as:
 * * Subpage
 * * Subpage/2Subpage

<? /**
 * This code is copyright (C) by Andrew McMillan (2008) and is licensed under the
 * terms of the GNU General Public License, version 2
 * (see http://www.fsf.org/licenses/gpl.html).
 * Usage:
 *  to list the next level of hierarchy
 *  to list all sub pages.
 * Known bugs:
 * None that I know of :-)
 * Could be improved to control sub-page order, perhaps. An option
 * to set the $title somewhere other than the current page is also
 * potentially useful. Neither is very hard.
 * Could be improved to control sub-page order, perhaps. An option
 * to set the $title somewhere other than the current page is also
 * potentially useful. Neither is very hard.

if ( !defined( 'MEDIAWIKI' ) ) { echo "This file is part of MediaWiki, it is not a valid entry point.\n"; exit( 1 ); }

$wgExtensionFunctions[] = "wfListSubPages";

/** function wfListSubPages { global $wgParser ; $wgParser->setHook ( "ListSubPages", 'ListSubPages_render' ); }
 * Initialise the hooks to add the functions

/** function ListSubPages_render ( $text, $args, &$parser ) { global $wgTitle; global $wgContLang; global $wgDBprefix;
 * Actually render the sub-page list

$allpages = (isset($args['allpages']) && $args["allpages"] == "yes");

$title = $wgTitle->getDBkey. '/'; // The pagename (without namespace) $ns = $wgTitle->getNamespace;     // The namespace as a number

/** * Originally developed on PostgreSQL 8.1 but also tested on MySQL 4.1 */ $sql = <<query( $sql); if ( ! $result || $db->numRows($result) == 0 ) { $output = "* No sub pages found."; } else {

$nsprefix = $wgContLang->getNsText($ns); $nsprefix .= ($nsprefix ==  ?  : ":"); $output = ""; while ( $row = $db->fetchRow( $result ) ) { $output .= sprintf( '* %s%s', $nsprefix, $row[0], $row[1], "\n"); } }

$output = $parser->parse( $output, $parser->mTitle, $parser->mOptions, true, false );

return $output->getText; }