r13914 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r13913‎ | r13914 | r13915 >
Date:01:32, 29 April 2006
Author:robchurch
Status:old
Tags:
Comment:
Add a <subpages /> tag which produces a linked list of all subpages of the current page
Modified paths:
  • /trunk/extensions/SubpageList.php (added) (history)

Diff [purge]

Index: trunk/extensions/SubpageList.php
@@ -0,0 +1,67 @@
 2+<?php
 3+
 4+/**
 5+ * Add a <subpages /> tag which produces a linked list of all subpages of the current page
 6+ *
 7+ * @package MediaWiki
 8+ * @subpackage Extensions
 9+ * @author Rob Church <robchur@gmail.com>
 10+ * @copyright © 2006 Rob Church
 11+ * @licence GNU General Public Licence 2.0 or later
 12+ */
 13+
 14+if( defined( 'MEDIAWIKI' ) ) {
 15+
 16+ $wgExtensionFunctions[] = 'efSubpageListSetup';
 17+ $wgExtensionCredits['parser'][] = array( 'name' => 'Subpage List', 'author' => 'Rob Church' );
 18+
 19+ function efSubpageListSetup() {
 20+ global $wgParser;
 21+ $wgParser->setHook( 'subpages', 'efSubpageList' );
 22+ }
 23+
 24+ function efSubpageList( $input, $args, &$parser ) {
 25+ $dbr =& wfGetDB( DB_SLAVE );
 26+
 27+ # Use the skin and title provided by the parser
 28+ $skin = $parser->mOptions->getSkin();
 29+ $self = $parser->mTitle;
 30+
 31+ # Prepare to run the SQL queries
 32+ $page = $dbr->tableName( 'page' );
 33+ $ns = (int)$self->getNamespace();
 34+ $like = $dbr->addQuotes( $self->getDBkey() . '/%' );
 35+
 36+ # Execute the SQL and retrieve a list of pages
 37+ $sql = "SELECT page_title FROM $page WHERE page_namespace = {$ns} AND page_title LIKE {$like}";
 38+ $res = $dbr->query( $sql, 'efSubpageList' );
 39+
 40+ # Prepare a bunch of links to the pages
 41+ while( $row = $dbr->fetchObject( $res ) ) {
 42+ $title = Title::makeTitleSafe( $ns, $row->page_title );
 43+ if( is_object( $title ) )
 44+ $links[] = '<li>' . $skin->makeKnownLinkObj( $title, efSubpageListGetText( $title ) ) . '</li>';
 45+ }
 46+ $dbr->freeResult( $res );
 47+
 48+ # Dump out the HTML
 49+ return count( $links ) ? "<ul>" . implode( "\n", $links ) . "</ul>" : '';
 50+ }
 51+
 52+ /**
 53+ * Given a title, e.g. Foo/Bar, return the rightmost segment
 54+ * Foo/Bar => Bar
 55+ * Dog/Cat/Mouse => Cat/Mouse
 56+ */
 57+ function efSubpageListGetText( &$title ) {
 58+ $parts = explode( '/', $title->getText() );
 59+ array_shift( $parts );
 60+ return implode( '/', $parts );
 61+ }
 62+
 63+} else {
 64+ echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
 65+ die( -1 );
 66+}
 67+
 68+?>
\ No newline at end of file
Property changes on: trunk/extensions/SubpageList.php
___________________________________________________________________
Added: svn:eol-style
169 + native

Status & tagging log