Merge "Removed odd "partitionsNoPush" setting to simplify the code"
[lhc/web/wiklou.git] / includes / specials / SpecialAllPages.php
1 <?php
2 /**
3 * Implements Special:Allpages
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Implements Special:Allpages
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialAllPages extends IncludableSpecialPage {
30
31 /**
32 * Maximum number of pages to show on single subpage.
33 *
34 * @var int $maxPerPage
35 */
36 protected $maxPerPage = 345;
37
38 /**
39 * Determines, which message describes the input field 'nsfrom'.
40 *
41 * @var string $nsfromMsg
42 */
43 protected $nsfromMsg = 'allpagesfrom';
44
45 /**
46 * Constructor
47 *
48 * @param string $name Name of the special page, as seen in links and URLs (default: 'Allpages')
49 */
50 function __construct( $name = 'Allpages' ) {
51 parent::__construct( $name );
52 }
53
54 /**
55 * Entry point : initialise variables and call subfunctions.
56 *
57 * @param string $par Becomes "FOO" when called like Special:Allpages/FOO (default null)
58 */
59 function execute( $par ) {
60 $request = $this->getRequest();
61 $out = $this->getOutput();
62
63 $this->setHeaders();
64 $this->outputHeader();
65 $out->allowClickjacking();
66
67 # GET values
68 $from = $request->getVal( 'from', null );
69 $to = $request->getVal( 'to', null );
70 $namespace = $request->getInt( 'namespace' );
71 $hideredirects = $request->getBool( 'hideredirects', false );
72
73 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
74
75 $out->setPageTitle(
76 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) ?
77 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
78 $this->msg( 'allarticles' )
79 );
80 $out->addModuleStyles( 'mediawiki.special' );
81
82 if ( $par !== null ) {
83 $this->showChunk( $namespace, $par, $to, $hideredirects );
84 } elseif ( $from !== null && $to === null ) {
85 $this->showChunk( $namespace, $from, $to, $hideredirects );
86 } else {
87 $this->showToplevel( $namespace, $from, $to, $hideredirects );
88 }
89 }
90
91 /**
92 * HTML for the top form
93 *
94 * @param int $namespace A namespace constant (default NS_MAIN).
95 * @param string $from DbKey we are starting listing at.
96 * @param string $to DbKey we are ending listing at.
97 * @param bool $hideredirects Dont show redirects (default false)
98 * @return string
99 */
100 function namespaceForm( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
101 $t = $this->getPageTitle();
102
103 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
104 $out .= Xml::openElement(
105 'form',
106 array( 'method' => 'get', 'action' => $this->getConfig()->get( 'Script' ) )
107 );
108 $out .= Html::hidden( 'title', $t->getPrefixedText() );
109 $out .= Xml::openElement( 'fieldset' );
110 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
111 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
112 $out .= "<tr>
113 <td class='mw-label'>" .
114 Xml::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
115 " </td>
116 <td class='mw-input'>" .
117 Xml::input( 'from', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
118 " </td>
119 </tr>
120 <tr>
121 <td class='mw-label'>" .
122 Xml::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
123 " </td>
124 <td class='mw-input'>" .
125 Xml::input( 'to', 30, str_replace( '_', ' ', $to ), array( 'id' => 'nsto' ) ) .
126 " </td>
127 </tr>
128 <tr>
129 <td class='mw-label'>" .
130 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
131 " </td>
132 <td class='mw-input'>" .
133 Html::namespaceSelector(
134 array( 'selected' => $namespace ),
135 array( 'name' => 'namespace', 'id' => 'namespace' )
136 ) . ' ' .
137 Xml::checkLabel(
138 $this->msg( 'allpages-hide-redirects' )->text(),
139 'hideredirects',
140 'hideredirects',
141 $hideredirects
142 ) . ' ' .
143 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
144 " </td>
145 </tr>";
146 $out .= Xml::closeElement( 'table' );
147 $out .= Xml::closeElement( 'fieldset' );
148 $out .= Xml::closeElement( 'form' );
149 $out .= Xml::closeElement( 'div' );
150
151 return $out;
152 }
153
154 /**
155 * @param int $namespace (default NS_MAIN)
156 * @param string $from List all pages from this name
157 * @param string $to List all pages to this name
158 * @param bool $hideredirects Dont show redirects (default false)
159 */
160 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
161 $from = Title::makeTitleSafe( $namespace, $from );
162 $to = Title::makeTitleSafe( $namespace, $to );
163 $from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null;
164 $to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null;
165
166 $this->showChunk( $namespace, $from, $to, $hideredirects );
167 }
168
169 /**
170 * @param int $namespace Namespace (Default NS_MAIN)
171 * @param string $from List all pages from this name (default false)
172 * @param string $to List all pages to this name (default false)
173 * @param bool $hideredirects Dont show redirects (default false)
174 */
175 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
176 $output = $this->getOutput();
177
178 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
179 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
180 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
181 $n = 0;
182
183 if ( !$fromList || !$toList ) {
184 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
185 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
186 // Show errormessage and reset to NS_MAIN
187 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
188 $namespace = NS_MAIN;
189 } else {
190 list( $namespace, $fromKey, $from ) = $fromList;
191 list( , $toKey, $to ) = $toList;
192
193 $dbr = wfGetDB( DB_SLAVE );
194 $conds = array(
195 'page_namespace' => $namespace,
196 'page_title >= ' . $dbr->addQuotes( $fromKey )
197 );
198
199 if ( $hideredirects ) {
200 $conds['page_is_redirect'] = 0;
201 }
202
203 if ( $toKey !== "" ) {
204 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
205 }
206
207 $res = $dbr->select( 'page',
208 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
209 $conds,
210 __METHOD__,
211 array(
212 'ORDER BY' => 'page_title',
213 'LIMIT' => $this->maxPerPage + 1,
214 'USE INDEX' => 'name_title',
215 )
216 );
217
218 if ( $res->numRows() > 0 ) {
219 $out = Xml::openElement( 'ul', array( 'class' => 'mw-allpages-chunk' ) );
220 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
221 $t = Title::newFromRow( $s );
222 if ( $t ) {
223 $out .= '<li' .
224 ( $s->page_is_redirect ? ' class="allpagesredirect"' : '' ) .
225 '>' .
226 Linker::link( $t ) .
227 "</li>\n";
228 } else {
229 $out .= '<li>[[' . htmlspecialchars( $s->page_title ) . "]]</li>\n";
230 }
231 $n++;
232 }
233 $out .= Xml::closeElement( 'ul' );
234 } else {
235 $out = '';
236 }
237 }
238
239 if ( $this->including() ) {
240 $output->addHTML( $out );
241 return;
242 }
243
244 if ( $from == '' ) {
245 // First chunk; no previous link.
246 $prevTitle = null;
247 } else {
248 # Get the last title from previous chunk
249 $dbr = wfGetDB( DB_SLAVE );
250 $res_prev = $dbr->select(
251 'page',
252 'page_title',
253 array( 'page_namespace' => $namespace, 'page_title < ' . $dbr->addQuotes( $from ) ),
254 __METHOD__,
255 array( 'ORDER BY' => 'page_title DESC',
256 'LIMIT' => $this->maxPerPage, 'OFFSET' => ( $this->maxPerPage - 1 )
257 )
258 );
259
260 # Get first title of previous complete chunk
261 if ( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
262 $pt = $dbr->fetchObject( $res_prev );
263 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
264 } else {
265 # The previous chunk is not complete, need to link to the very first title
266 # available in the database
267 $options = array( 'LIMIT' => 1 );
268 if ( !$dbr->implicitOrderby() ) {
269 $options['ORDER BY'] = 'page_title';
270 }
271 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
272 array( 'page_namespace' => $namespace ), __METHOD__, $options );
273 # Show the previous link if it s not the current requested chunk
274 if ( $from != $reallyFirstPage_title ) {
275 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
276 } else {
277 $prevTitle = null;
278 }
279 }
280 }
281
282 $self = $this->getPageTitle();
283
284 $topLinks = array(
285 Linker::link( $self, $this->msg( 'allpages' )->escaped() )
286 );
287 $bottomLinks = array();
288
289 # Do we put a previous link ?
290 if ( $prevTitle && $pt = $prevTitle->getText() ) {
291 $query = array( 'from' => $prevTitle->getText() );
292
293 if ( $namespace ) {
294 $query['namespace'] = $namespace;
295 }
296
297 if ( $hideredirects ) {
298 $query['hideredirects'] = $hideredirects;
299 }
300
301 $prevLink = Linker::linkKnown(
302 $self,
303 $this->msg( 'prevpage', $pt )->escaped(),
304 array(),
305 $query
306 );
307 $topLinks[] = $prevLink;
308 $bottomLinks[] = $prevLink;
309 }
310
311 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
312 # $s is the first link of the next chunk
313 $t = Title::makeTitle( $namespace, $s->page_title );
314 $query = array( 'from' => $t->getText() );
315
316 if ( $namespace ) {
317 $query['namespace'] = $namespace;
318 }
319
320 if ( $hideredirects ) {
321 $query['hideredirects'] = $hideredirects;
322 }
323
324 $nextLink = Linker::linkKnown(
325 $self,
326 $this->msg( 'nextpage', $t->getText() )->escaped(),
327 array(),
328 $query
329 );
330 $topLinks[] = $nextLink;
331 $bottomLinks[] = $nextLink;
332 }
333
334 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
335 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
336 '<tr>
337 <td>' .
338 $nsForm .
339 '</td>
340 <td class="mw-allpages-nav">' .
341 $this->getLanguage()->pipeList( $topLinks ) .
342 '</td></tr></table>';
343
344 $output->addHTML( $out2 . $out );
345
346 if ( count( $bottomLinks ) ) {
347 $output->addHTML(
348 Html::element( 'hr' ) .
349 Html::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
350 $this->getLanguage()->pipeList( $bottomLinks )
351 )
352 );
353 }
354 }
355
356 /**
357 * @param int $ns The namespace of the article
358 * @param string $text The name of the article
359 * @return array( int namespace, string dbkey, string pagename ) or null on error
360 */
361 protected function getNamespaceKeyAndText( $ns, $text ) {
362 if ( $text == '' ) {
363 # shortcut for common case
364 return array( $ns, '', '' );
365 }
366
367 $t = Title::makeTitleSafe( $ns, $text );
368 if ( $t && $t->isLocal() ) {
369 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
370 } elseif ( $t ) {
371 return null;
372 }
373
374 # try again, in case the problem was an empty pagename
375 $text = preg_replace( '/(#|$)/', 'X$1', $text );
376 $t = Title::makeTitleSafe( $ns, $text );
377 if ( $t && $t->isLocal() ) {
378 return array( $t->getNamespace(), '', '' );
379 } else {
380 return null;
381 }
382 }
383
384 protected function getGroupName() {
385 return 'pages';
386 }
387 }