User:Joshurtree/SpecialListSubPages

From Meta

Jump to: navigation, search
 
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.

Contents

[edit] Messages Needed

  • ListSubPagesSubTitle
  • nosubpages

[edit] Usage

Put the code in extensions/SpecialListSubPages.php of your wiki then add the following to LocalSettings.php.

require_once('extensions/SpecialListSubPages.php');

It can either be called independently using a link e.g. [[Special:ListSubPages/User:Joshurtree]] or including directly in your page using {{:Special:ListSubPages}}.

[edit] To Do

  • Adapt template version to use a parameter rather than the current page as the parent page.
  • Have it added to the toolbox

[edit] Changes

  • Sanitized page title with addQuotes - Mthornton
  • Changed to work with MediaWiki 1.10.1 -- w:User:Yakatz

[edit] Code

<?php
$wgExtensionFunctions[] = "loadSpecialListSubpages";
require_once("includes/SpecialPage.php");

class SpecialListSubPages extends SpecialPage {

        function SpecialListSubPages() {
                SpecialPage::SpecialPage('ListSubPages', '', false);
                $this->includable(true);
        }

        function execute( $parent = null ) {
                global $wgRequest, $wgOut, $wgUser;
                $fname = 'listsubpages';
                
        
                // Handle Errors
                $target = $this->including() ? $wgRequest->getVal( 'title' ) : 
                                (isset( $parent ) ? $parent : $wgRequest->getVal( 'target' ));
                
                if (is_null($target)) {
                        $wgOut->errorpage( 'notargettitle', 'notargettext' );
                        return;
                }
        
                $target = Title::newFromURL( $target );
                if( !$target ) {
                        $wgOut->errorpage( 'notargettitle', 'notargettext' );
                        return;
                }
                
                $skin = $wgUser->getSkin();
                if (!$this->including()) {
                        // Start Page
                        $wgOut->setPagetitle( $target->getPrefixedText() );
                        $wgOut->setSubtitle( wfMsg( 'subpagetitle' ) );
                
                
                        $wgOut->addHTML('< ' . $skin->makeKnownLinkObj($target, '', '' ) . "<br />\n");
                }
                
                // Do query
                list($limit, $offset) = $wgRequest->getLimitOffset();        
                $dbr =& wfGetDB( DB_READ );
                
                $result = $dbr->select( 'page' ,
                                array( 'page_namespace', 'page_title' ),
                                array(  'page_namespace' => $target->getNamespace(),
                                        "page_title LIKE " . $dbr->addQuotes($target->getDbKey() . '/%') ),
                                $fname,
                                array( 'LIMIT' => "$limit", 'OFFSET' => "$offset") );
                
                // Output results
                if ( 0 == $dbr->numRows( $result ) ) {
                        $wgOut->addWikiText(wfMsg('nosubpages'));
                        return;
                }
                
                $prevnext = wfViewPrevNext( $offset, $limit, $specialTitle,
                                           'target=' . urlencode( $target->getPrefixedDbKey() ),
                                           ( $dbr->numRows( $res ) <= $limit ) );
                $wgOut->addHTML( $prevnext );
                
                $rows = array();
                $wgOut->addHTML('<ul>');
                while ($row = $dbr->fetchObject($result)) {
                        $wgOut->addHTML('<li> ');
                        $link = Title::makeTitle( $row->page_namespace, $row->page_title);
                        $wgOut->addHTML($skin->makeKnownLinkObj($link));
                        $wgOut->addHTML('</li>'); 
                }
                
                $dbr->freeResult($result);
                $wgOut->addHTML('</ul>');
                $wgOut->addHTML($prevnext);
        }
}
        

function loadSpecialListSubpages()
{
        SpecialPage::addPage(new SpecialListSubpages);
}

?>
Personal tools