User:Karora/ListSubPages
From MediaWiki.org
|
ListSubPages Release status: unknown |
|
|---|---|
| Implementation | Tag |
| Description | ListSubPages is a simple extension which creates a bulletted list of the subpages of a page. Either just the next layer in the hierarchy (by default) or all pages below this one. |
| Last Version | 1.0 (2008-01-22) |
| MediaWiki | 1.6.x or above |
| License | No license specified |
| Download | In page |
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:
- * [[Page/Subpage|Subpage]]
- * [[Page/Subpage/2Subpage|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:
* <ListSubPages/> to list the next level of hierarchy
* <ListSubPages allpages="yes"/> 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.
*/
if ( !defined( 'MEDIAWIKI' ) ) {
echo "This file is part of MediaWiki, it is not a valid entry point.\n";
exit( 1 );
}
$wgExtensionFunctions[] = "wfListSubPages";
/**
* Initialise the hooks to add the functions
*/
function wfListSubPages () {
global $wgParser ;
$wgParser->setHook ( "ListSubPages" , 'ListSubPages_render' );
}
/**
* Actually render the sub-page list
*/
function ListSubPages_render ( $text, $args, &$parser ) {
global $wgTitle;
global $wgContLang;
global $wgDBprefix;
$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 = <<<EOSQL
SELECT page_title,
substring( page_title from char_length('${title}') + 1) AS sub_page_title
FROM ${wgDBprefix}page
WHERE substring( page_title from 1 for char_length('${title}') ) = '${title}'
AND page_is_redirect != '1'
AND page_namespace = ${ns}
EOSQL;
if ( ! $allpages ) {
$sql .= " AND substring( page_title from char_length('${title}') + 1) NOT LIKE '%/%'";
}
$sql .= " ORDER BY lower(page_title)";
$db =& wfGetDB( DB_MASTER );
$result = $db->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|%s]]%s', $nsprefix, $row[0], $row[1], "\n");
}
}
$output = $parser->parse( $output, $parser->mTitle, $parser->mOptions, true, false );
return $output->getText();
}
Warning: Default sort key "ListSubPages" overrides earlier default sort key "LISTSUBPAGES".

