* partly fixes two issues exposed by bug 34280
[mediawiki.git] / includes / specials / SpecialPrefixindex.php
blob40a38e84e93edfd1f437f2fe36cecb434c9339a6
1 <?php
2 /**
3 * Implements Special:Prefixindex
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Implements Special:Prefixindex
27 * @ingroup SpecialPage
29 class SpecialPrefixindex extends SpecialAllpages {
30 // Inherit $maxPerPage
32 function __construct(){
33 parent::__construct( 'Prefixindex' );
36 /**
37 * Entry point : initialise variables and call subfunctions.
38 * @param $par String: becomes "FOO" when called like Special:Prefixindex/FOO (default null)
40 function execute( $par ) {
41 global $wgContLang;
43 $this->setHeaders();
44 $this->outputHeader();
46 $out = $this->getOutput();
47 $out->addModuleStyles( 'mediawiki.special' );
49 # GET values
50 $request = $this->getRequest();
51 $from = $request->getVal( 'from', '' );
52 $prefix = $request->getVal( 'prefix', '' );
53 $ns = $request->getIntOrNull( 'namespace' );
54 $namespace = (int)$ns; // if no namespace given, use 0 (NS_MAIN).
56 $namespaces = $wgContLang->getNamespaces();
57 $out->setPageTitle(
58 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) )
59 ? $this->msg( 'prefixindex-namespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
60 : $this->msg( 'prefixindex' )
63 $showme = '';
64 if( isset( $par ) ) {
65 $showme = $par;
66 } elseif( $prefix != '' ) {
67 $showme = $prefix;
68 } elseif( $from != '' && $ns === null ) {
69 // For back-compat with Special:Allpages
70 // Don't do this if namespace is passed, so paging works when doing NS views.
71 $showme = $from;
74 // Bug 27864: if transcluded, show all pages instead of the form.
75 if ( $this->including() || $showme != '' || $ns !== null ) {
76 $this->showPrefixChunk( $namespace, $showme, $from );
77 } else {
78 $out->addHTML( $this->namespacePrefixForm( $namespace, null ) );
82 /**
83 * HTML for the top form
84 * @param $namespace Integer: a namespace constant (default NS_MAIN).
85 * @param $from String: dbKey we are starting listing at.
86 * @return string
88 function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ) {
89 global $wgScript;
91 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
92 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
93 $out .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
94 $out .= Xml::openElement( 'fieldset' );
95 $out .= Xml::element( 'legend', null, wfMsg( 'allpages' ) );
96 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
97 $out .= "<tr>
98 <td class='mw-label'>" .
99 Xml::label( wfMsg( 'allpagesprefix' ), 'nsfrom' ) .
100 "</td>
101 <td class='mw-input'>" .
102 Xml::input( 'prefix', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
103 "</td>
104 </tr>
105 <tr>
106 <td class='mw-label'>" .
107 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
108 "</td>
109 <td class='mw-input'>" .
110 Xml::namespaceSelector( $namespace, null ) . ' ' .
111 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
112 "</td>
113 </tr>";
114 $out .= Xml::closeElement( 'table' );
115 $out .= Xml::closeElement( 'fieldset' );
116 $out .= Xml::closeElement( 'form' );
117 $out .= Xml::closeElement( 'div' );
118 return $out;
122 * @param $namespace Integer, default NS_MAIN
123 * @param $prefix String
124 * @param $from String: list all pages from this name (default FALSE)
126 function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null ) {
127 global $wgContLang;
129 if ( $from === null ) {
130 $from = $prefix;
133 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
134 $prefixList = $this->getNamespaceKeyAndText($namespace, $prefix);
135 $namespaces = $wgContLang->getNamespaces();
137 if ( !$prefixList || !$fromList ) {
138 $out = wfMsgExt( 'allpagesbadtitle', 'parse' );
139 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
140 // Show errormessage and reset to NS_MAIN
141 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
142 $namespace = NS_MAIN;
143 } else {
144 list( $namespace, $prefixKey, $prefix ) = $prefixList;
145 list( /* $fromNS */, $fromKey, ) = $fromList;
147 ### @todo FIXME: Should complain if $fromNs != $namespace
149 $dbr = wfGetDB( DB_SLAVE );
151 $res = $dbr->select( 'page',
152 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
153 array(
154 'page_namespace' => $namespace,
155 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
156 'page_title >= ' . $dbr->addQuotes( $fromKey ),
158 __METHOD__,
159 array(
160 'ORDER BY' => 'page_title',
161 'LIMIT' => $this->maxPerPage + 1,
162 'USE INDEX' => 'name_title',
166 ### @todo FIXME: Side link to previous
168 $n = 0;
169 if( $res->numRows() > 0 ) {
170 $out = Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-prefixindex-list-table' ) );
172 while( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
173 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
174 if( $t ) {
175 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
176 Linker::linkKnown(
178 htmlspecialchars( $t->getText() )
180 ($s->page_is_redirect ? '</div>' : '' );
181 } else {
182 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
184 if( $n % 3 == 0 ) {
185 $out .= '<tr>';
187 $out .= "<td>$link</td>";
188 $n++;
189 if( $n % 3 == 0 ) {
190 $out .= '</tr>';
193 if( ($n % 3) != 0 ) {
194 $out .= '</tr>';
196 $out .= Xml::closeElement( 'table' );
197 } else {
198 $out = '';
202 $footer = '';
203 if ( $this->including() ) {
204 $out2 = '';
205 } else {
206 $nsForm = $this->namespacePrefixForm( $namespace, $prefix );
207 $self = $this->getTitle();
208 $out2 = Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-prefixindex-nav-table' ) ) .
209 '<tr>
210 <td>' .
211 $nsForm .
212 '</td>
213 <td id="mw-prefixindex-nav-form" class="mw-prefixindex-nav">';
215 if( isset( $res ) && $res && ( $n == $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
216 $query = array(
217 'from' => $s->page_title,
218 'prefix' => $prefix
221 if( $namespace || ($prefix == '')) {
222 // Keep the namespace even if it's 0 for empty prefixes.
223 // This tells us we're not just a holdover from old links.
224 $query['namespace'] = $namespace;
226 $nextLink = Linker::linkKnown(
227 $self,
228 wfMsgHtml( 'nextpage', str_replace( '_',' ', htmlspecialchars( $s->page_title ) ) ),
229 array(),
230 $query
232 $out2 .= $nextLink;
234 $footer = "\n" . Html::element( "hr" )
235 . Html::rawElement( "div", array( "class" => "mw-prefixindex-nav" ), $nextLink );
237 $out2 .= "</td></tr>" .
238 Xml::closeElement( 'table' );
241 $this->getOutput()->addHTML( $out2 . $out . $footer );