Extension:SubPageList3/SPLKCode

From MediaWiki.org
Jump to navigation Jump to search

The User:Karora rewrite of the SubPageList extensions; kept here for archival and reference purposes.

<?php
/**
* 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();
}