Merge "Improved rate limit log to mention IP"
[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 global $wgScript;
102 $t = $this->getPageTitle();
103
104 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
105 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
106 $out .= Html::hidden( 'title', $t->getPrefixedText() );
107 $out .= Xml::openElement( 'fieldset' );
108 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
109 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
110 $out .= "<tr>
111 <td class='mw-label'>" .
112 Xml::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
113 " </td>
114 <td class='mw-input'>" .
115 Xml::input( 'from', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
116 " </td>
117 </tr>
118 <tr>
119 <td class='mw-label'>" .
120 Xml::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
121 " </td>
122 <td class='mw-input'>" .
123 Xml::input( 'to', 30, str_replace( '_', ' ', $to ), array( 'id' => 'nsto' ) ) .
124 " </td>
125 </tr>
126 <tr>
127 <td class='mw-label'>" .
128 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
129 " </td>
130 <td class='mw-input'>" .
131 Html::namespaceSelector(
132 array( 'selected' => $namespace ),
133 array( 'name' => 'namespace', 'id' => 'namespace' )
134 ) . ' ' .
135 Xml::checkLabel(
136 $this->msg( 'allpages-hide-redirects' )->text(),
137 'hideredirects',
138 'hideredirects',
139 $hideredirects
140 ) . ' ' .
141 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
142 " </td>
143 </tr>";
144 $out .= Xml::closeElement( 'table' );
145 $out .= Xml::closeElement( 'fieldset' );
146 $out .= Xml::closeElement( 'form' );
147 $out .= Xml::closeElement( 'div' );
148
149 return $out;
150 }
151
152 /**
153 * @param int $namespace (default NS_MAIN)
154 * @param string $from List all pages from this name
155 * @param string $to List all pages to this name
156 * @param bool $hideredirects Dont show redirects (default false)
157 */
158 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
159 $output = $this->getOutput();
160
161 # TODO: Either make this *much* faster or cache the title index points
162 # in the querycache table.
163
164 $dbr = wfGetDB( DB_SLAVE );
165 $out = "";
166 $where = array( 'page_namespace' => $namespace );
167
168 if ( $hideredirects ) {
169 $where['page_is_redirect'] = 0;
170 }
171
172 $from = Title::makeTitleSafe( $namespace, $from );
173 $to = Title::makeTitleSafe( $namespace, $to );
174 $from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null;
175 $to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null;
176
177 if ( isset( $from ) ) {
178 $where[] = 'page_title >= ' . $dbr->addQuotes( $from );
179 }
180
181 if ( isset( $to ) ) {
182 $where[] = 'page_title <= ' . $dbr->addQuotes( $to );
183 }
184
185 $this->showChunk( $namespace, $from, $to, $hideredirects );
186 }
187
188 /**
189 * @param int $namespace Namespace (Default NS_MAIN)
190 * @param string $from List all pages from this name (default false)
191 * @param string $to List all pages to this name (default false)
192 * @param bool $hideredirects Dont show redirects (default false)
193 */
194 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
195 $output = $this->getOutput();
196
197 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
198 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
199 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
200 $n = 0;
201
202 if ( !$fromList || !$toList ) {
203 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
204 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
205 // Show errormessage and reset to NS_MAIN
206 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
207 $namespace = NS_MAIN;
208 } else {
209 list( $namespace, $fromKey, $from ) = $fromList;
210 list( , $toKey, $to ) = $toList;
211
212 $dbr = wfGetDB( DB_SLAVE );
213 $conds = array(
214 'page_namespace' => $namespace,
215 'page_title >= ' . $dbr->addQuotes( $fromKey )
216 );
217
218 if ( $hideredirects ) {
219 $conds['page_is_redirect'] = 0;
220 }
221
222 if ( $toKey !== "" ) {
223 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
224 }
225
226 $res = $dbr->select( 'page',
227 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
228 $conds,
229 __METHOD__,
230 array(
231 'ORDER BY' => 'page_title',
232 'LIMIT' => $this->maxPerPage + 1,
233 'USE INDEX' => 'name_title',
234 )
235 );
236
237 if ( $res->numRows() > 0 ) {
238 $out = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
239 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
240 $t = Title::newFromRow( $s );
241 if ( $t ) {
242 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
243 Linker::link( $t ) .
244 ( $s->page_is_redirect ? '</div>' : '' );
245 } else {
246 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
247 }
248
249 if ( $n % 3 == 0 ) {
250 $out .= '<tr>';
251 }
252
253 $out .= "<td style=\"width:33%\">$link</td>";
254 $n++;
255 if ( $n % 3 == 0 ) {
256 $out .= "</tr>\n";
257 }
258 }
259
260 if ( ( $n % 3 ) != 0 ) {
261 $out .= "</tr>\n";
262 }
263 $out .= Xml::closeElement( 'table' );
264 } else {
265 $out = '';
266 }
267 }
268
269 if ( $this->including() ) {
270 $out2 = '';
271 } else {
272 if ( $from == '' ) {
273 // First chunk; no previous link.
274 $prevTitle = null;
275 } else {
276 # Get the last title from previous chunk
277 $dbr = wfGetDB( DB_SLAVE );
278 $res_prev = $dbr->select(
279 'page',
280 'page_title',
281 array( 'page_namespace' => $namespace, 'page_title < ' . $dbr->addQuotes( $from ) ),
282 __METHOD__,
283 array( 'ORDER BY' => 'page_title DESC',
284 'LIMIT' => $this->maxPerPage, 'OFFSET' => ( $this->maxPerPage - 1 )
285 )
286 );
287
288 # Get first title of previous complete chunk
289 if ( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
290 $pt = $dbr->fetchObject( $res_prev );
291 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
292 } else {
293 # The previous chunk is not complete, need to link to the very first title
294 # available in the database
295 $options = array( 'LIMIT' => 1 );
296 if ( !$dbr->implicitOrderby() ) {
297 $options['ORDER BY'] = 'page_title';
298 }
299 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
300 array( 'page_namespace' => $namespace ), __METHOD__, $options );
301 # Show the previous link if it s not the current requested chunk
302 if ( $from != $reallyFirstPage_title ) {
303 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
304 } else {
305 $prevTitle = null;
306 }
307 }
308 }
309
310 $self = $this->getPageTitle();
311
312 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
313 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
314 '<tr>
315 <td>' .
316 $nsForm .
317 '</td>
318 <td class="mw-allpages-nav">' .
319 Linker::link( $self, $this->msg( 'allpages' )->escaped() );
320
321 # Do we put a previous link ?
322 if ( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
323 $query = array( 'from' => $prevTitle->getText() );
324
325 if ( $namespace ) {
326 $query['namespace'] = $namespace;
327 }
328
329 if ( $hideredirects ) {
330 $query['hideredirects'] = $hideredirects;
331 }
332
333 $prevLink = Linker::linkKnown(
334 $self,
335 $this->msg( 'prevpage', $pt )->escaped(),
336 array(),
337 $query
338 );
339 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
340 }
341
342 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
343 # $s is the first link of the next chunk
344 $t = Title::makeTitle( $namespace, $s->page_title );
345 $query = array( 'from' => $t->getText() );
346
347 if ( $namespace ) {
348 $query['namespace'] = $namespace;
349 }
350
351 if ( $hideredirects ) {
352 $query['hideredirects'] = $hideredirects;
353 }
354
355 $nextLink = Linker::linkKnown(
356 $self,
357 $this->msg( 'nextpage', $t->getText() )->escaped(),
358 array(),
359 $query
360 );
361 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
362 }
363 $out2 .= "</td></tr></table>";
364 }
365
366 $output->addHTML( $out2 . $out );
367
368 $links = array();
369 if ( isset( $prevLink ) ) {
370 $links[] = $prevLink;
371 }
372
373 if ( isset( $nextLink ) ) {
374 $links[] = $nextLink;
375 }
376
377 if ( count( $links ) ) {
378 $output->addHTML(
379 Html::element( 'hr' ) .
380 Html::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
381 $this->getLanguage()->pipeList( $links )
382 )
383 );
384 }
385 }
386
387 /**
388 * @param int $ns The namespace of the article
389 * @param string $text The name of the article
390 * @return array( int namespace, string dbkey, string pagename ) or null on error
391 */
392 protected function getNamespaceKeyAndText( $ns, $text ) {
393 if ( $text == '' ) {
394 # shortcut for common case
395 return array( $ns, '', '' );
396 }
397
398 $t = Title::makeTitleSafe( $ns, $text );
399 if ( $t && $t->isLocal() ) {
400 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
401 } elseif ( $t ) {
402 return null;
403 }
404
405 # try again, in case the problem was an empty pagename
406 $text = preg_replace( '/(#|$)/', 'X$1', $text );
407 $t = Title::makeTitleSafe( $ns, $text );
408 if ( $t && $t->isLocal() ) {
409 return array( $t->getNamespace(), '', '' );
410 } else {
411 return null;
412 }
413 }
414
415 protected function getGroupName() {
416 return 'pages';
417 }
418 }