MediaWiki  REL1_24
SpecialPrefixindex.php
Go to the documentation of this file.
00001 <?php
00029 class SpecialPrefixindex extends SpecialAllPages {
00030 
00035     protected $stripPrefix = false;
00036 
00037     protected $hideRedirects = false;
00038 
00039     // number of columns in output table
00040     protected $columns = 3;
00041 
00042     // Inherit $maxPerPage
00043 
00044     function __construct() {
00045         parent::__construct( 'Prefixindex' );
00046     }
00047 
00052     function execute( $par ) {
00053         global $wgContLang;
00054 
00055         $this->setHeaders();
00056         $this->outputHeader();
00057 
00058         $out = $this->getOutput();
00059         $out->addModuleStyles( 'mediawiki.special' );
00060 
00061         # GET values
00062         $request = $this->getRequest();
00063         $from = $request->getVal( 'from', '' );
00064         $prefix = $request->getVal( 'prefix', '' );
00065         $ns = $request->getIntOrNull( 'namespace' );
00066         $namespace = (int)$ns; // if no namespace given, use 0 (NS_MAIN).
00067         $this->hideRedirects = $request->getBool( 'hideredirects', $this->hideRedirects );
00068         $this->stripPrefix = $request->getBool( 'stripprefix', $this->stripPrefix );
00069         $this->columns = $request->getInt( 'columns', $this->columns );
00070 
00071         $namespaces = $wgContLang->getNamespaces();
00072         $out->setPageTitle(
00073             ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) )
00074                 ? $this->msg( 'prefixindex-namespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
00075                 : $this->msg( 'prefixindex' )
00076         );
00077 
00078         $showme = '';
00079         if ( $par !== null ) {
00080             $showme = $par;
00081         } elseif ( $prefix != '' ) {
00082             $showme = $prefix;
00083         } elseif ( $from != '' && $ns === null ) {
00084             // For back-compat with Special:Allpages
00085             // Don't do this if namespace is passed, so paging works when doing NS views.
00086             $showme = $from;
00087         }
00088 
00089         // Bug 27864: if transcluded, show all pages instead of the form.
00090         if ( $this->including() || $showme != '' || $ns !== null ) {
00091             $this->showPrefixChunk( $namespace, $showme, $from );
00092         } else {
00093             $out->addHTML( $this->namespacePrefixForm( $namespace, null ) );
00094         }
00095     }
00096 
00103     protected function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ) {
00104         $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
00105         $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $this->getConfig()->get( 'Script' ) ) );
00106         $out .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
00107         $out .= Xml::openElement( 'fieldset' );
00108         $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
00109         $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
00110         $out .= "<tr>
00111                 <td class='mw-label'>" .
00112             Xml::label( $this->msg( 'allpagesprefix' )->text(), 'nsfrom' ) .
00113             "</td>
00114                 <td class='mw-input'>" .
00115             Xml::input( 'prefix', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
00116             "</td>
00117             </tr>
00118             <tr>
00119             <td class='mw-label'>" .
00120             Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
00121             "</td>
00122                 <td class='mw-input'>" .
00123             Html::namespaceSelector( array(
00124                 'selected' => $namespace,
00125             ), array(
00126                 'name' => 'namespace',
00127                 'id' => 'namespace',
00128                 'class' => 'namespaceselector',
00129             ) ) .
00130             Xml::checkLabel(
00131                 $this->msg( 'allpages-hide-redirects' )->text(),
00132                 'hideredirects',
00133                 'hideredirects',
00134                 $this->hideRedirects
00135             ) . ' ' .
00136             Xml::checkLabel(
00137                 $this->msg( 'prefixindex-strip' )->text(),
00138                 'stripprefix',
00139                 'stripprefix',
00140                 $this->stripPrefix
00141             ) . ' ' .
00142             Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
00143             "</td>
00144             </tr>";
00145         $out .= Xml::closeElement( 'table' );
00146         $out .= Xml::closeElement( 'fieldset' );
00147         $out .= Xml::closeElement( 'form' );
00148         $out .= Xml::closeElement( 'div' );
00149 
00150         return $out;
00151     }
00152 
00158     protected function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null ) {
00159         global $wgContLang;
00160 
00161         if ( $from === null ) {
00162             $from = $prefix;
00163         }
00164 
00165         $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
00166         $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
00167         $namespaces = $wgContLang->getNamespaces();
00168         $res = null;
00169 
00170         if ( !$prefixList || !$fromList ) {
00171             $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
00172         } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
00173             // Show errormessage and reset to NS_MAIN
00174             $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
00175             $namespace = NS_MAIN;
00176         } else {
00177             list( $namespace, $prefixKey, $prefix ) = $prefixList;
00178             list( /* $fromNS */, $fromKey, ) = $fromList;
00179 
00180             ### @todo FIXME: Should complain if $fromNs != $namespace
00181 
00182             $dbr = wfGetDB( DB_SLAVE );
00183 
00184             $conds = array(
00185                 'page_namespace' => $namespace,
00186                 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
00187                 'page_title >= ' . $dbr->addQuotes( $fromKey ),
00188             );
00189 
00190             if ( $this->hideRedirects ) {
00191                 $conds['page_is_redirect'] = 0;
00192             }
00193 
00194             $res = $dbr->select( 'page',
00195                 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
00196                 $conds,
00197                 __METHOD__,
00198                 array(
00199                     'ORDER BY' => 'page_title',
00200                     'LIMIT' => $this->maxPerPage + 1,
00201                     'USE INDEX' => 'name_title',
00202                 )
00203             );
00204 
00205             ### @todo FIXME: Side link to previous
00206 
00207             $n = 0;
00208             if ( $res->numRows() > 0 ) {
00209                 $out = Xml::openElement( 'table', array( 'class' => 'mw-prefixindex-list-table' ) );
00210 
00211                 $prefixLength = strlen( $prefix );
00212                 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
00213                     $t = Title::makeTitle( $s->page_namespace, $s->page_title );
00214                     if ( $t ) {
00215                         $displayed = $t->getText();
00216                         // Try not to generate unclickable links
00217                         if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) {
00218                             $displayed = substr( $displayed, $prefixLength );
00219                         }
00220                         $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
00221                             Linker::linkKnown(
00222                                 $t,
00223                                 htmlspecialchars( $displayed ),
00224                                 $s->page_is_redirect ? array( 'class' => 'mw-redirect' ) : array()
00225                             ) .
00226                             ( $s->page_is_redirect ? '</div>' : '' );
00227                     } else {
00228                         $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
00229                     }
00230                     if ( $n % $this->columns == 0 ) {
00231                         $out .= '<tr>';
00232                     }
00233                     $out .= "<td>$link</td>";
00234                     $n++;
00235                     if ( $n % $this->columns == 0 ) {
00236                         $out .= '</tr>';
00237                     }
00238                 }
00239 
00240                 if ( $n % $this->columns != 0 ) {
00241                     $out .= '</tr>';
00242                 }
00243 
00244                 $out .= Xml::closeElement( 'table' );
00245             } else {
00246                 $out = '';
00247             }
00248         }
00249 
00250         $footer = '';
00251         if ( $this->including() ) {
00252             $out2 = '';
00253         } else {
00254             $nsForm = $this->namespacePrefixForm( $namespace, $prefix );
00255             $self = $this->getPageTitle();
00256             $out2 = Xml::openElement( 'table', array( 'id' => 'mw-prefixindex-nav-table' ) ) .
00257                 '<tr>
00258                     <td>' .
00259                 $nsForm .
00260                 '</td>
00261                 <td id="mw-prefixindex-nav-form" class="mw-prefixindex-nav">';
00262 
00263             if ( $res && ( $n == $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
00264                 $query = array(
00265                     'from' => $s->page_title,
00266                     'prefix' => $prefix,
00267                     'hideredirects' => $this->hideRedirects,
00268                     'stripprefix' => $this->stripPrefix,
00269                     'columns' => $this->columns,
00270                 );
00271 
00272                 if ( $namespace || $prefix == '' ) {
00273                     // Keep the namespace even if it's 0 for empty prefixes.
00274                     // This tells us we're not just a holdover from old links.
00275                     $query['namespace'] = $namespace;
00276                 }
00277 
00278                 $nextLink = Linker::linkKnown(
00279                     $self,
00280                     $this->msg( 'nextpage', str_replace( '_', ' ', $s->page_title ) )->escaped(),
00281                     array(),
00282                     $query
00283                 );
00284 
00285                 $out2 .= $nextLink;
00286 
00287                 $footer = "\n" . Html::element( 'hr' ) .
00288                     Html::rawElement(
00289                         'div',
00290                         array( 'class' => 'mw-prefixindex-nav' ),
00291                         $nextLink
00292                     );
00293             }
00294             $out2 .= "</td></tr>" .
00295                 Xml::closeElement( 'table' );
00296         }
00297 
00298         $this->getOutput()->addHTML( $out2 . $out . $footer );
00299     }
00300 
00301     protected function getGroupName() {
00302         return 'pages';
00303     }
00304 }