Merge "Remove unused $wgDebugDBTransactions"
[lhc/web/wiklou.git] / includes / specials / SpecialPrefixindex.php
index 49dc6c7..fca1748 100644 (file)
 <?php
 /**
+ * Implements Special:Prefixindex
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup SpecialPage
  */
 
 /**
- * Entry point : initialise variables and call subfunctions.
- * @param $par String: becomes "FOO" when called like Special:Prefixindex/FOO (default NULL)
- * @param $specialPage SpecialPage object.
- */
-function wfSpecialPrefixIndex( $par=NULL, $specialPage ) {
-       global $wgRequest, $wgOut, $wgContLang;
-
-       # GET values
-       $from = $wgRequest->getVal( 'from' );
-       $prefix = $wgRequest->getVal( 'prefix' );
-       $namespace = $wgRequest->getInt( 'namespace' );
-       $namespaces = $wgContLang->getNamespaces();
-
-       $indexPage = new SpecialPrefixIndex();
-
-       $wgOut->setPagetitle( ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) )
-               ? wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
-               : wfMsg( 'allarticles' )
-       );
-
-       if ( isset($par) ) {
-               $indexPage->showPrefixChunk( $namespace, $par, $specialPage->including(), $from );
-       } elseif ( isset($prefix) ) {
-               $indexPage->showPrefixChunk( $namespace, $prefix, $specialPage->including(), $from );
-       } elseif ( isset($from) ) {
-               $indexPage->showPrefixChunk( $namespace, $from, $specialPage->including(), $from );
-       } else {
-               $wgOut->addHtml( $indexPage->namespacePrefixForm( $namespace, null ) );
-       }
-}
-
-/**
- * implements Special:Prefixindex
+ * Implements Special:Prefixindex
+ *
  * @ingroup SpecialPage
  */
 class SpecialPrefixindex extends SpecialAllpages {
        // Inherit $maxPerPage
 
-       // Define other properties
-       protected $name = 'Prefixindex';
-       protected $nsfromMsg = 'allpagesprefix';
-       
+       function __construct(){
+               parent::__construct( 'Prefixindex' );
+       }
+
        /**
-       * HTML for the top form
-       * @param integer $namespace A namespace constant (default NS_MAIN).
-       * @param string $from dbKey we are starting listing at.
-       */
-       function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ) {
+        * Entry point : initialise variables and call subfunctions.
+        * @param $par String: becomes "FOO" when called like Special:Prefixindex/FOO (default null)
+        */
+       function execute( $par ) {
+               global $wgContLang;
+
+               $this->setHeaders();
+               $this->outputHeader();
+
+               $out = $this->getOutput();
+               $out->addModuleStyles( 'mediawiki.special' );
+
+               # GET values
+               $request = $this->getRequest();
+               $from = $request->getVal( 'from', '' );
+               $prefix = $request->getVal( 'prefix', '' );
+               $ns = $request->getIntOrNull( 'namespace' );
+               $namespace = (int)$ns; // if no namespace given, use 0 (NS_MAIN).
+               $hideredirects = $request->getBool( 'hideredirects', false );
+
+               $namespaces = $wgContLang->getNamespaces();
+               $out->setPageTitle(
+                       ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) )
+                               ? $this->msg( 'prefixindex-namespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
+                               : $this->msg( 'prefixindex' )
+               );
+
+               $showme = '';
+               if( isset( $par ) ) {
+                       $showme = $par;
+               } elseif( $prefix != '' ) {
+                       $showme = $prefix;
+               } elseif( $from != '' && $ns === null ) {
+                       // For back-compat with Special:Allpages
+                       // Don't do this if namespace is passed, so paging works when doing NS views.
+                       $showme = $from;
+               }
+
+               // Bug 27864: if transcluded, show all pages instead of the form.
+               if ( $this->including() || $showme != '' || $ns !== null ) {
+                       $this->showPrefixChunk( $namespace, $showme, $from, $hideredirects );
+               } else {
+                       $out->addHTML( $this->namespacePrefixForm( $namespace, null, $hideredirects ) );
+               }
+       }
+
+       /**
+        * HTML for the top form
+        * @param $namespace Integer: a namespace constant (default NS_MAIN).
+        * @param $from String: dbKey we are starting listing at.
+        * @param $hideredirects Bool: hide redirects (default FALSE)
+        * @return string
+        */
+       function namespacePrefixForm( $namespace = NS_MAIN, $from = '', $hideredirects = false ) {
                global $wgScript;
-               $t = SpecialPage::getTitleFor( $this->name );
 
                $out  = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
                $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
-               $out .= Xml::hidden( 'title', $t->getPrefixedText() );
+               $out .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
                $out .= Xml::openElement( 'fieldset' );
                $out .= Xml::element( 'legend', null, wfMsg( 'allpages' ) );
                $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
                $out .= "<tr>
                                <td class='mw-label'>" .
-                               Xml::label( wfMsg( 'allpagesfrom' ), 'nsfrom' ) .
+                               Xml::label( wfMsg( 'allpagesprefix' ), 'nsfrom' ) .
                                "</td>
                                <td class='mw-input'>" .
-                                       Xml::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
+                                       Xml::input( 'prefix', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
                                "</td>
                        </tr>
                        <tr>
@@ -75,10 +109,22 @@ class SpecialPrefixindex extends SpecialAllpages {
                                        Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
                                "</td>
                                <td class='mw-input'>" .
-                                       Xml::namespaceSelector( $namespace, null ) . ' ' .
-                                       Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
+                               Html::namespaceSelector( array(
+                                               'selected' => $namespace,
+                                       ), array(
+                                               'name'  => 'namespace',
+                                               'id'    => 'namespace',
+                                               'class' => 'namespaceselector',
+                               ) ) .
+                               Xml::checkLabel(
+                                       wfMsg( 'allpages-hide-redirects' ),
+                                       'hideredirects',
+                                       'hideredirects',
+                                       $hideredirects
+                               ) . ' ' .
+                               Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
                                "</td>
-                               </tr>";
+                       </tr>";
                $out .= Xml::closeElement( 'table' );
                $out .= Xml::closeElement( 'fieldset' );
                $out .= Xml::closeElement( 'form' );
@@ -87,45 +133,50 @@ class SpecialPrefixindex extends SpecialAllpages {
        }
 
        /**
-        * @param integer $namespace (Default NS_MAIN)
-        * @param string $from list all pages from this name (default FALSE)
+        * @param $namespace Integer, default NS_MAIN
+        * @param $prefix String
+        * @param $from String: list all pages from this name (default FALSE)
+        * @param $hideredirects Bool: hide redirects (default FALSE)
         */
-       function showPrefixChunk( $namespace = NS_MAIN, $prefix, $including = false, $from = null ) {
-               global $wgOut, $wgUser, $wgContLang;
-
-               $fname = 'indexShowChunk';
+       function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null, $hideredirects = false ) {
+               global $wgContLang;
 
-               $sk = $wgUser->getSkin();
-
-               if (!isset($from)) $from = $prefix;
+               if ( $from === null ) {
+                       $from = $prefix;
+               }
 
                $fromList = $this->getNamespaceKeyAndText($namespace, $from);
                $prefixList = $this->getNamespaceKeyAndText($namespace, $prefix);
                $namespaces = $wgContLang->getNamespaces();
-               $align = $wgContLang->isRtl() ? 'left' : 'right';
 
                if ( !$prefixList || !$fromList ) {
-                       $out = wfMsgWikiHtml( 'allpagesbadtitle' );
+                       $out = wfMsgExt( 'allpagesbadtitle', 'parse' );
                } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
                        // Show errormessage and reset to NS_MAIN
                        $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
                        $namespace = NS_MAIN;
                } else {
                        list( $namespace, $prefixKey, $prefix ) = $prefixList;
-                       list( /* $fromNs */, $fromKey, $from ) = $fromList;
+                       list( /* $fromNS */, $fromKey, ) = $fromList;
 
-                       ### FIXME: should complain if $fromNs != $namespace
+                       ### @todo FIXME: Should complain if $fromNs != $namespace
 
                        $dbr = wfGetDB( DB_SLAVE );
 
+                       $conds = array(
+                               'page_namespace' => $namespace,
+                               'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
+                               'page_title >= ' . $dbr->addQuotes( $fromKey ),
+                       );
+
+                       if ( $hideredirects ) {
+                               $conds['page_is_redirect'] = 0;
+                       }
+
                        $res = $dbr->select( 'page',
                                array( 'page_namespace', 'page_title', 'page_is_redirect' ),
-                               array(
-                                       'page_namespace' => $namespace,
-                                       'page_title LIKE \'' . $dbr->escapeLike( $prefixKey ) .'%\'',
-                                       'page_title >= ' . $dbr->addQuotes( $fromKey ),
-                               ),
-                               $fname,
+                               $conds,
+                               __METHOD__,
                                array(
                                        'ORDER BY'  => 'page_title',
                                        'LIMIT'     => $this->maxPerPage + 1,
@@ -133,17 +184,20 @@ class SpecialPrefixindex extends SpecialAllpages {
                                )
                        );
 
-                       ### FIXME: side link to previous
+                       ### @todo FIXME: Side link to previous
 
                        $n = 0;
                        if( $res->numRows() > 0 ) {
-                               $out = '<table style="background: inherit;" border="0" width="100%">';
-       
-                               while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
+                               $out = Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-prefixindex-list-table' ) );
+
+                               while( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
                                        $t = Title::makeTitle( $s->page_namespace, $s->page_title );
                                        if( $t ) {
                                                $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
-                                                       $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
+                                                       Linker::linkKnown(
+                                                               $t,
+                                                               htmlspecialchars( $t->getText() )
+                                                       ) .
                                                        ($s->page_is_redirect ? '</div>' : '' );
                                        } else {
                                                $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
@@ -160,32 +214,52 @@ class SpecialPrefixindex extends SpecialAllpages {
                                if( ($n % 3) != 0 ) {
                                        $out .= '</tr>';
                                }
-                               $out .= '</table>';
+                               $out .= Xml::closeElement( 'table' );
                        } else {
                                $out = '';
                        }
                }
 
-               if ( $including ) {
+               $footer = '';
+               if ( $this->including() ) {
                        $out2 = '';
                } else {
-                       $nsForm = $this->namespacePrefixForm( $namespace, $prefix );
-                       $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
-                       $out2 .= '<tr valign="top"><td>' . $nsForm;
-                       $out2 .= '</td><td align="' . $align . '" style="font-size: smaller; margin-bottom: 1em;">' .
-                                       $sk->makeKnownLink( $wgContLang->specialPage( $this->name ),
-                                               wfMsg ( 'allpages' ) );
-                       if ( isset($dbr) && $dbr && ($n == $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
-                               $namespaceparam = $namespace ? "&namespace=$namespace" : "";
-                               $out2 .= " | " . $sk->makeKnownLink(
-                                       $wgContLang->specialPage( $this->name ),
-                                       wfMsgHtml( 'nextpage', htmlspecialchars( $s->page_title ) ),
-                                       "from=" . wfUrlEncode ( $s->page_title ) .
-                                       "&prefix=" . wfUrlEncode ( $prefix ) . $namespaceparam );
+                       $nsForm = $this->namespacePrefixForm( $namespace, $prefix, $hideredirects );
+                       $self = $this->getTitle();
+                       $out2 = Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-prefixindex-nav-table' ) )  .
+                               '<tr>
+                                       <td>' .
+                                               $nsForm .
+                                       '</td>
+                                       <td id="mw-prefixindex-nav-form" class="mw-prefixindex-nav">';
+
+                       if( isset( $res ) && $res && ( $n == $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
+                               $query = array(
+                                       'from' => $s->page_title,
+                                       'prefix' => $prefix,
+                                       'hideredirects' => $hideredirects,
+                               );
+
+                               if( $namespace || ($prefix == '')) {
+                                       // Keep the namespace even if it's 0 for empty prefixes.
+                                       // This tells us we're not just a holdover from old links.
+                                       $query['namespace'] = $namespace;
+                               }
+                               $nextLink = Linker::linkKnown(
+                                               $self,
+                                               wfMsgHtml( 'nextpage', str_replace( '_',' ', htmlspecialchars( $s->page_title ) ) ),
+                                               array(),
+                                               $query
+                                       );
+                               $out2 .= $nextLink;
+
+                               $footer = "\n" . Html::element( "hr" )
+                                       . Html::rawElement( "div", array( "class" => "mw-prefixindex-nav" ), $nextLink );
                        }
-                       $out2 .= "</td></tr></table><hr />";
+                       $out2 .= "</td></tr>" .
+                               Xml::closeElement( 'table' );
                }
 
-               $wgOut->addHtml( $out2 . $out );
+               $this->getOutput()->addHTML( $out2 . $out . $footer );
        }
 }