User:Juxo/Extensions/ListSubPages

From Meta, a Wikimedia project coordination wiki

Jump to: navigation, search
Blue Glass Arrow.svg MediaWiki logo.png
This page should be moved to MediaWiki.org.
Please do not move the page by hand. It will be imported by a MediaWiki.org administrator with the full edit history. In the meantime, you may continue to edit the page as normal.

REQUIRES: Mediawiki 1.5 or higher

ListSubPages lists all sub-pages for any given page. If an article doesn't have any sub-pages it returns "This page doesn't have any sub-pages"

Internationalisation of the default "no result"-return string might be done some day if someone wants to include this in the Mediawiki tree.

Currently it simply discards any variables you give to it ie.

<ListSubPages>This text never gets processed</ListSubPages>

I am planning to enable passing parameters that allow alternate ordering on the result and even further to show only some of the sub-pages that some page has.

There is unfortunatelly one hard coded variable that you must change for this extension to work: $wikipath


[edit] The code

<?
# This code is copyright (C) by Juho Ville Heikkurinen (2005) and is licensed under the
# terms of the GNU General Public License, version 2 
# (see http://www.fsf.org/licenses/gpl.html). Derivative works and later versions of
# the code will also be considered free software licensed under the same terms.

# version 0.1.2 Fixed a bug that caused the program to always say that there are no subpages
# version 0.1.1 outputs "This page doesn't have any sub-pages" if there are none
# version 0.1 Lists all sub-pages if there are any. If there are none it outputs nothing.

# NOTE: you must change $wikipath to reflect your installation.
# for example for Wikipedia you would use
# $wikipath = "../wiki/"

# Known bugs:
# 'order by page_title' gives capital B before lowercase A

# TODO (or actually just ideas):
# 1) Enable passing parameters to alter the ordering of the results e.g. "order by edit time" 
# 2) Enable the use of regular expressions to limit subpages to be viewed
# 3) Enable limiting the depth of subpages to show

$wgExtensionFunctions[] = "wfListSubPages";
 
function wfListSubPages ()
        {
                global $wgParser ;
                global $wgTitle;
                $wgParser->setHook ( "ListSubPages" , parse_subs );
        }
        
#First extension written from scratch
function parse_subs ($input)
        {
                global $wgTitle;
                global $wgContLang;
                global $wgDBprefix; #Added to work with sites using DB prefixes (Edited: Lee Burton)

                $wikipath = "../alpha/"; ######## CHANGE THIS TO REFLECT YOUR INSTALLATION
                $dbr =& wfGetDB( DB_MASTER );  #Get a REFERENCE to a database connection (I suppose)
                $output =""; #initialise output variable
                $title = $wgTitle->getDBkey(); #get the pagename (without namespace)
                $ns = $wgTitle->getNamespace(); # get the namespace as a number


                # $wgDBprefix added to work for sites with DB prefixes (Edited: Lee Burton)
                $sql = "SELECT page_title FROM " . $wgDBprefix . "page where page_namespace='". $ns ."' and page_title LIKE '". $title."/%' ORDER BY page_title";
        $res = $dbr->query( $sql);
        $rowCount = $dbr->numRows($res);
        if ($rowCount == 0) {
        		return "This page doesn't have any sub-pages";
        }
        else {

                $output = "<b>This page has ". $rowCount . " sub-pages:</b>\n";
                $output .= "<ul>";
                # Make sure $ns is definitely a number and $title is encoded with wfStrencode() 
                # (above comment by TimStarling) I suppose that $wgTitle->getDBkey(); gives the title encoded.
                while ( $myrow = $dbr->fetchRow( $res ) ) {
                        $output .= "<li /><a href=\"" . $wikipath . $wgContLang->getNsText($ns) .":". $myrow[0] . "\">" . $wgContLang->getNsText($ns) .":" . $myrow[0] . "</a>\n";
                }
                $output .= "</ul>";     
        }
                return $output;
        }

?>

You can find here a modified version of this extension. This version has some more feature, some proposed by Juxo.