MediaWiki  REL1_23
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 ( isset( $par ) ) {
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         global $wgScript;
00105 
00106         $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
00107         $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
00108         $out .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
00109         $out .= Xml::openElement( 'fieldset' );
00110         $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
00111         $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
00112         $out .= "<tr>
00113                 <td class='mw-label'>" .
00114             Xml::label( $this->msg( 'allpagesprefix' )->text(), 'nsfrom' ) .
00115             "</td>
00116                 <td class='mw-input'>" .
00117             Xml::input( 'prefix', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
00118             "</td>
00119             </tr>
00120             <tr>
00121             <td class='mw-label'>" .
00122             Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
00123             "</td>
00124                 <td class='mw-input'>" .
00125             Html::namespaceSelector( array(
00126                 'selected' => $namespace,
00127             ), array(
00128                 'name' => 'namespace',
00129                 'id' => 'namespace',
00130                 'class' => 'namespaceselector',
00131             ) ) .
00132             Xml::checkLabel(
00133                 $this->msg( 'allpages-hide-redirects' )->text(),
00134                 'hideredirects',
00135                 'hideredirects',
00136                 $this->hideRedirects
00137             ) . ' ' .
00138             Xml::checkLabel(
00139                 $this->msg( 'prefixindex-strip' )->text(),
00140                 'stripprefix',
00141                 'stripprefix',
00142                 $this->stripPrefix
00143             ) . ' ' .
00144             Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
00145             "</td>
00146             </tr>";
00147         $out .= Xml::closeElement( 'table' );
00148         $out .= Xml::closeElement( 'fieldset' );
00149         $out .= Xml::closeElement( 'form' );
00150         $out .= Xml::closeElement( 'div' );
00151 
00152         return $out;
00153     }
00154 
00160     protected function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null ) {
00161         global $wgContLang;
00162 
00163         if ( $from === null ) {
00164             $from = $prefix;
00165         }
00166 
00167         $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
00168         $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
00169         $namespaces = $wgContLang->getNamespaces();
00170 
00171         if ( !$prefixList || !$fromList ) {
00172             $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
00173         } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
00174             // Show errormessage and reset to NS_MAIN
00175             $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
00176             $namespace = NS_MAIN;
00177         } else {
00178             list( $namespace, $prefixKey, $prefix ) = $prefixList;
00179             list( /* $fromNS */, $fromKey, ) = $fromList;
00180 
00181             ### @todo FIXME: Should complain if $fromNs != $namespace
00182 
00183             $dbr = wfGetDB( DB_SLAVE );
00184 
00185             $conds = array(
00186                 'page_namespace' => $namespace,
00187                 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
00188                 'page_title >= ' . $dbr->addQuotes( $fromKey ),
00189             );
00190 
00191             if ( $this->hideRedirects ) {
00192                 $conds['page_is_redirect'] = 0;
00193             }
00194 
00195             $res = $dbr->select( 'page',
00196                 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
00197                 $conds,
00198                 __METHOD__,
00199                 array(
00200                     'ORDER BY' => 'page_title',
00201                     'LIMIT' => $this->maxPerPage + 1,
00202                     'USE INDEX' => 'name_title',
00203                 )
00204             );
00205 
00206             ### @todo FIXME: Side link to previous
00207 
00208             $n = 0;
00209             if ( $res->numRows() > 0 ) {
00210                 $out = Xml::openElement( 'table', array( 'class' => 'mw-prefixindex-list-table' ) );
00211 
00212                 $prefixLength = strlen( $prefix );
00213                 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
00214                     $t = Title::makeTitle( $s->page_namespace, $s->page_title );
00215                     if ( $t ) {
00216                         $displayed = $t->getText();
00217                         // Try not to generate unclickable links
00218                         if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) {
00219                             $displayed = substr( $displayed, $prefixLength );
00220                         }
00221                         $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
00222                             Linker::linkKnown(
00223                                 $t,
00224                                 htmlspecialchars( $displayed ),
00225                                 $s->page_is_redirect ? array( 'class' => 'mw-redirect' ) : array()
00226                             ) .
00227                             ( $s->page_is_redirect ? '</div>' : '' );
00228                     } else {
00229                         $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
00230                     }
00231                     if ( $n % $this->columns == 0 ) {
00232                         $out .= '<tr>';
00233                     }
00234                     $out .= "<td>$link</td>";
00235                     $n++;
00236                     if ( $n % $this->columns == 0 ) {
00237                         $out .= '</tr>';
00238                     }
00239                 }
00240 
00241                 if ( $n % $this->columns != 0 ) {
00242                     $out .= '</tr>';
00243                 }
00244 
00245                 $out .= Xml::closeElement( 'table' );
00246             } else {
00247                 $out = '';
00248             }
00249         }
00250 
00251         $footer = '';
00252         if ( $this->including() ) {
00253             $out2 = '';
00254         } else {
00255             $nsForm = $this->namespacePrefixForm( $namespace, $prefix );
00256             $self = $this->getPageTitle();
00257             $out2 = Xml::openElement( 'table', array( 'id' => 'mw-prefixindex-nav-table' ) ) .
00258                 '<tr>
00259                     <td>' .
00260                 $nsForm .
00261                 '</td>
00262                 <td id="mw-prefixindex-nav-form" class="mw-prefixindex-nav">';
00263 
00264             if ( isset( $res ) && $res && ( $n == $this->maxPerPage ) &&
00265                 ( $s = $res->fetchObject() )
00266             ) {
00267                 $query = array(
00268                     'from' => $s->page_title,
00269                     'prefix' => $prefix,
00270                     'hideredirects' => $this->hideRedirects,
00271                     'stripprefix' => $this->stripPrefix,
00272                     'columns' => $this->columns,
00273                 );
00274 
00275                 if ( $namespace || $prefix == '' ) {
00276                     // Keep the namespace even if it's 0 for empty prefixes.
00277                     // This tells us we're not just a holdover from old links.
00278                     $query['namespace'] = $namespace;
00279                 }
00280 
00281                 $nextLink = Linker::linkKnown(
00282                     $self,
00283                     $this->msg( 'nextpage', str_replace( '_', ' ', $s->page_title ) )->escaped(),
00284                     array(),
00285                     $query
00286                 );
00287 
00288                 $out2 .= $nextLink;
00289 
00290                 $footer = "\n" . Html::element( 'hr' ) .
00291                     Html::rawElement(
00292                         'div',
00293                         array( 'class' => 'mw-prefixindex-nav' ),
00294                         $nextLink
00295                     );
00296             }
00297             $out2 .= "</td></tr>" .
00298                 Xml::closeElement( 'table' );
00299         }
00300 
00301         $this->getOutput()->addHTML( $out2 . $out . $footer );
00302     }
00303 
00304     protected function getGroupName() {
00305         return 'pages';
00306     }
00307 }