Merge "Add tests for API's assert={user|bot}"
[lhc/web/wiklou.git] / includes / specials / SpecialProtectedpages.php
1 <?php
2 /**
3 * Implements Special:Protectedpages
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 * A special page that lists protected pages
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialProtectedpages extends SpecialPage {
30 protected $IdLevel = 'level';
31 protected $IdType = 'type';
32
33 public function __construct() {
34 parent::__construct( 'Protectedpages' );
35 }
36
37 public function execute( $par ) {
38 $this->setHeaders();
39 $this->outputHeader();
40 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
41
42 // Purge expired entries on one in every 10 queries
43 if ( !mt_rand( 0, 10 ) ) {
44 Title::purgeExpiredRestrictions();
45 }
46
47 $request = $this->getRequest();
48 $type = $request->getVal( $this->IdType );
49 $level = $request->getVal( $this->IdLevel );
50 $sizetype = $request->getVal( 'sizetype' );
51 $size = $request->getIntOrNull( 'size' );
52 $ns = $request->getIntOrNull( 'namespace' );
53 $indefOnly = $request->getBool( 'indefonly' ) ? 1 : 0;
54 $cascadeOnly = $request->getBool( 'cascadeonly' ) ? 1 : 0;
55 $noRedirect = $request->getBool( 'noredirect' ) ? 1 : 0;
56
57 $pager = new ProtectedPagesPager(
58 $this,
59 array(),
60 $type,
61 $level,
62 $ns,
63 $sizetype,
64 $size,
65 $indefOnly,
66 $cascadeOnly,
67 $noRedirect
68 );
69
70 $this->getOutput()->addHTML( $this->showOptions(
71 $ns,
72 $type,
73 $level,
74 $sizetype,
75 $size,
76 $indefOnly,
77 $cascadeOnly,
78 $noRedirect
79 ) );
80
81 if ( $pager->getNumRows() ) {
82 $this->getOutput()->addHTML(
83 $pager->getNavigationBar() .
84 $pager->getBody() .
85 $pager->getNavigationBar()
86 );
87 } else {
88 $this->getOutput()->addWikiMsg( 'protectedpagesempty' );
89 }
90 }
91
92 /**
93 * @param int $namespace
94 * @param string $type Restriction type
95 * @param string $level Restriction level
96 * @param string $sizetype "min" or "max"
97 * @param int $size
98 * @param bool $indefOnly Only indefinite protection
99 * @param bool $cascadeOnly Only cascading protection
100 * @param bool $noRedirect Don't show redirects
101 * @return string Input form
102 */
103 protected function showOptions( $namespace, $type = 'edit', $level, $sizetype,
104 $size, $indefOnly, $cascadeOnly, $noRedirect
105 ) {
106 global $wgScript;
107
108 $title = $this->getPageTitle();
109
110 return Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
111 Xml::openElement( 'fieldset' ) .
112 Xml::element( 'legend', array(), $this->msg( 'protectedpages' )->text() ) .
113 Html::hidden( 'title', $title->getPrefixedDBkey() ) . "\n" .
114 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
115 $this->getTypeMenu( $type ) . "&#160;\n" .
116 $this->getLevelMenu( $level ) . "&#160;\n" .
117 "<br /><span style='white-space: nowrap'>" .
118 $this->getExpiryCheck( $indefOnly ) . "&#160;\n" .
119 $this->getCascadeCheck( $cascadeOnly ) . "&#160;\n" .
120 $this->getRedirectCheck( $noRedirect ) . "&#160;\n" .
121 "</span><br /><span style='white-space: nowrap'>" .
122 $this->getSizeLimit( $sizetype, $size ) . "&#160;\n" .
123 "</span>" .
124 "&#160;" . Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
125 Xml::closeElement( 'fieldset' ) .
126 Xml::closeElement( 'form' );
127 }
128
129 /**
130 * Prepare the namespace filter drop-down; standard namespace
131 * selector, sans the MediaWiki namespace
132 *
133 * @param string|null $namespace Pre-select namespace
134 * @return string
135 */
136 protected function getNamespaceMenu( $namespace = null ) {
137 return Html::rawElement( 'span', array( 'style' => 'white-space: nowrap;' ),
138 Html::namespaceSelector(
139 array(
140 'selected' => $namespace,
141 'all' => '',
142 'label' => $this->msg( 'namespace' )->text()
143 ), array(
144 'name' => 'namespace',
145 'id' => 'namespace',
146 'class' => 'namespaceselector',
147 )
148 )
149 );
150 }
151
152 /**
153 * @param bool $indefOnly
154 * @return string Formatted HTML
155 */
156 protected function getExpiryCheck( $indefOnly ) {
157 return Xml::checkLabel(
158 $this->msg( 'protectedpages-indef' )->text(),
159 'indefonly',
160 'indefonly',
161 $indefOnly
162 ) . "\n";
163 }
164
165 /**
166 * @param bool $cascadeOnly
167 * @return string Formatted HTML
168 */
169 protected function getCascadeCheck( $cascadeOnly ) {
170 return Xml::checkLabel(
171 $this->msg( 'protectedpages-cascade' )->text(),
172 'cascadeonly',
173 'cascadeonly',
174 $cascadeOnly
175 ) . "\n";
176 }
177
178 /**
179 * @param bool $noRedirect
180 * @return string Formatted HTML
181 */
182 protected function getRedirectCheck( $noRedirect ) {
183 return Xml::checkLabel(
184 $this->msg( 'protectedpages-noredirect' )->text(),
185 'noredirect',
186 'noredirect',
187 $noRedirect
188 ) . "\n";
189 }
190
191 /**
192 * @param string $sizetype "min" or "max"
193 * @param mixed $size
194 * @return string Formatted HTML
195 */
196 protected function getSizeLimit( $sizetype, $size ) {
197 $max = $sizetype === 'max';
198
199 return Xml::radioLabel(
200 $this->msg( 'minimum-size' )->text(),
201 'sizetype',
202 'min',
203 'wpmin',
204 !$max
205 ) .
206 '&#160;' .
207 Xml::radioLabel(
208 $this->msg( 'maximum-size' )->text(),
209 'sizetype',
210 'max',
211 'wpmax',
212 $max
213 ) .
214 '&#160;' .
215 Xml::input( 'size', 9, $size, array( 'id' => 'wpsize' ) ) .
216 '&#160;' .
217 Xml::label( $this->msg( 'pagesize' )->text(), 'wpsize' );
218 }
219
220 /**
221 * Creates the input label of the restriction type
222 * @param string $pr_type Protection type
223 * @return string Formatted HTML
224 */
225 protected function getTypeMenu( $pr_type ) {
226 $m = array(); // Temporary array
227 $options = array();
228
229 // First pass to load the log names
230 foreach ( Title::getFilteredRestrictionTypes( true ) as $type ) {
231 // Messages: restriction-edit, restriction-move, restriction-create, restriction-upload
232 $text = $this->msg( "restriction-$type" )->text();
233 $m[$text] = $type;
234 }
235
236 // Third pass generates sorted XHTML content
237 foreach ( $m as $text => $type ) {
238 $selected = ( $type == $pr_type );
239 $options[] = Xml::option( $text, $type, $selected ) . "\n";
240 }
241
242 return "<span style='white-space: nowrap'>" .
243 Xml::label( $this->msg( 'restriction-type' )->text(), $this->IdType ) . '&#160;' .
244 Xml::tags( 'select',
245 array( 'id' => $this->IdType, 'name' => $this->IdType ),
246 implode( "\n", $options ) ) . "</span>";
247 }
248
249 /**
250 * Creates the input label of the restriction level
251 * @param string $pr_level Protection level
252 * @return string Formatted HTML
253 */
254 protected function getLevelMenu( $pr_level ) {
255 global $wgRestrictionLevels;
256
257 // Temporary array
258 $m = array( $this->msg( 'restriction-level-all' )->text() => 0 );
259 $options = array();
260
261 // First pass to load the log names
262 foreach ( $wgRestrictionLevels as $type ) {
263 // Messages used can be 'restriction-level-sysop' and 'restriction-level-autoconfirmed'
264 if ( $type != '' && $type != '*' ) {
265 $text = $this->msg( "restriction-level-$type" )->text();
266 $m[$text] = $type;
267 }
268 }
269
270 // Third pass generates sorted XHTML content
271 foreach ( $m as $text => $type ) {
272 $selected = ( $type == $pr_level );
273 $options[] = Xml::option( $text, $type, $selected );
274 }
275
276 return "<span style='white-space: nowrap'>" .
277 Xml::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel ) . ' ' .
278 Xml::tags( 'select',
279 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
280 implode( "\n", $options ) ) . "</span>";
281 }
282
283 protected function getGroupName() {
284 return 'maintenance';
285 }
286 }
287
288 /**
289 * @todo document
290 * @ingroup Pager
291 */
292 class ProtectedPagesPager extends TablePager {
293 public $mForm, $mConds;
294 private $type, $level, $namespace, $sizetype, $size, $indefonly, $cascadeonly, $noredirect;
295
296 function __construct( $form, $conds = array(), $type, $level, $namespace,
297 $sizetype = '', $size = 0, $indefonly = false, $cascadeonly = false, $noredirect = false
298 ) {
299 $this->mForm = $form;
300 $this->mConds = $conds;
301 $this->type = ( $type ) ? $type : 'edit';
302 $this->level = $level;
303 $this->namespace = $namespace;
304 $this->sizetype = $sizetype;
305 $this->size = intval( $size );
306 $this->indefonly = (bool)$indefonly;
307 $this->cascadeonly = (bool)$cascadeonly;
308 $this->noredirect = (bool)$noredirect;
309 parent::__construct( $form->getContext() );
310 }
311
312 function preprocessResults( $result ) {
313 # Do a link batch query
314 $lb = new LinkBatch;
315 $userids = array();
316
317 foreach ( $result as $row ) {
318 $lb->add( $row->page_namespace, $row->page_title );
319 // field is nullable, maybe null on old protections
320 if ( $row->log_user !== null ) {
321 $userids[] = $row->log_user;
322 }
323 }
324
325 // fill LinkBatch with user page and user talk
326 if ( count( $userids ) ) {
327 $userCache = UserCache::singleton();
328 $userCache->doQuery( $userids, array(), __METHOD__ );
329 foreach ( $userids as $userid ) {
330 $name = $userCache->getProp( $userid, 'name' );
331 if ( $name !== false ) {
332 $lb->add( NS_USER, $name );
333 $lb->add( NS_USER_TALK, $name );
334 }
335 }
336 }
337
338 $lb->execute();
339 }
340
341 function getFieldNames() {
342 static $headers = null;
343
344 if ( $headers == array() ) {
345 $headers = array(
346 'log_timestamp' => 'protectedpages-timestamp',
347 'pr_page' => 'protectedpages-page',
348 'pr_expiry' => 'protectedpages-expiry',
349 'log_user' => 'protectedpages-performer',
350 'pr_params' => 'protectedpages-params',
351 'log_comment' => 'protectedpages-reason',
352 );
353 foreach ( $headers as $key => $val ) {
354 $headers[$key] = $this->msg( $val )->text();
355 }
356 }
357
358 return $headers;
359 }
360
361 /**
362 * @param string $field
363 * @param string $value
364 * @return string
365 * @throws MWException
366 */
367 function formatValue( $field, $value ) {
368 /** @var $row object */
369 $row = $this->mCurrentRow;
370
371 $formatted = '';
372
373 switch ( $field ) {
374 case 'log_timestamp':
375 // when timestamp is null, this is a old protection row
376 if ( $value === null ) {
377 $formatted = Html::rawElement(
378 'span',
379 array( 'class' => 'mw-protectedpages-unknown' ),
380 $this->msg( 'protectedpages-unknown-timestamp' )->escaped()
381 );
382 } else {
383 $formatted = $this->getLanguage()->userTimeAndDate( $value, $this->getUser() );
384 }
385 break;
386
387 case 'pr_page':
388 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
389 if ( !$title ) {
390 $formatted = Html::element(
391 'span',
392 array( 'class' => 'mw-invalidtitle' ),
393 Linker::getInvalidTitleDescription(
394 $this->getContext(),
395 $row->page_namespace,
396 $row->page_title
397 )
398 );
399 } else {
400 $formatted = Linker::link( $title );
401 }
402 if ( !is_null( $row->page_len ) ) {
403 $formatted .= $this->getLanguage()->getDirMark() .
404 ' ' . Html::rawElement(
405 'span',
406 array( 'class' => 'mw-protectedpages-length' ),
407 Linker::formatRevisionSize( $row->page_len )
408 );
409 }
410 break;
411
412 case 'pr_expiry':
413 $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */true );
414 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
415 if ( $this->getUser()->isAllowed( 'protect' ) && $title ) {
416 $changeProtection = Linker::linkKnown(
417 $title,
418 $this->msg( 'protect_change' )->escaped(),
419 array(),
420 array( 'action' => 'unprotect' )
421 );
422 $formatted .= ' ' . Html::rawElement(
423 'span',
424 array( 'class' => 'mw-protectedpages-actions' ),
425 $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped()
426 );
427 }
428 break;
429
430 case 'log_user':
431 // when timestamp is null, this is a old protection row
432 if ( $row->log_timestamp === null ) {
433 $formatted = Html::rawElement(
434 'span',
435 array( 'class' => 'mw-protectedpages-unknown' ),
436 $this->msg( 'protectedpages-unknown-performer' )->escaped()
437 );
438 } else {
439 $username = UserCache::singleton()->getProp( $value, 'name' );
440 if ( LogEventsList::userCanBitfield(
441 $row->log_deleted,
442 LogPage::DELETED_USER,
443 $this->getUser()
444 ) ) {
445 if ( $username === false ) {
446 $formatted = htmlspecialchars( $value );
447 } else {
448 $formatted = Linker::userLink( $value, $username )
449 . Linker::userToolLinks( $value, $username );
450 }
451 } else {
452 $formatted = $this->msg( 'rev-deleted-user' )->escaped();
453 }
454 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
455 $formatted = '<span class="history-deleted">' . $formatted . '</span>';
456 }
457 }
458 break;
459
460 case 'pr_params':
461 $params = array();
462 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
463 $params[] = $this->msg( 'restriction-level-' . $row->pr_level )->escaped();
464 if ( $row->pr_cascade ) {
465 $params[] = $this->msg( 'protect-summary-cascade' )->text();
466 }
467 $formatted = $this->getLanguage()->commaList( $params );
468 break;
469
470 case 'log_comment':
471 // when timestamp is null, this is an old protection row
472 if ( $row->log_timestamp === null ) {
473 $formatted = Html::rawElement(
474 'span',
475 array( 'class' => 'mw-protectedpages-unknown' ),
476 $this->msg( 'protectedpages-unknown-reason' )->escaped()
477 );
478 } else {
479 if ( LogEventsList::userCanBitfield(
480 $row->log_deleted,
481 LogPage::DELETED_COMMENT,
482 $this->getUser()
483 ) ) {
484 $formatted = Linker::formatComment( $value !== null ? $value : '' );
485 } else {
486 $formatted = $this->msg( 'rev-deleted-comment' )->escaped();
487 }
488 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
489 $formatted = '<span class="history-deleted">' . $formatted . '</span>';
490 }
491 }
492 break;
493
494 default:
495 throw new MWException( "Unknown field '$field'" );
496 }
497
498 return $formatted;
499 }
500
501 function getQueryInfo() {
502 $conds = $this->mConds;
503 $conds[] = 'pr_expiry > ' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
504 'OR pr_expiry IS NULL';
505 $conds[] = 'page_id=pr_page';
506 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
507
508 if ( $this->sizetype == 'min' ) {
509 $conds[] = 'page_len>=' . $this->size;
510 } elseif ( $this->sizetype == 'max' ) {
511 $conds[] = 'page_len<=' . $this->size;
512 }
513
514 if ( $this->indefonly ) {
515 $infinity = $this->mDb->addQuotes( $this->mDb->getInfinity() );
516 $conds[] = "pr_expiry = $infinity OR pr_expiry IS NULL";
517 }
518 if ( $this->cascadeonly ) {
519 $conds[] = 'pr_cascade = 1';
520 }
521 if ( $this->noredirect ) {
522 $conds[] = 'page_is_redirect = 0';
523 }
524
525 if ( $this->level ) {
526 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
527 }
528 if ( !is_null( $this->namespace ) ) {
529 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
530 }
531
532 return array(
533 'tables' => array( 'page', 'page_restrictions', 'log_search', 'logging' ),
534 'fields' => array(
535 'pr_id',
536 'page_namespace',
537 'page_title',
538 'page_len',
539 'pr_type',
540 'pr_level',
541 'pr_expiry',
542 'pr_cascade',
543 'log_timestamp',
544 'log_user',
545 'log_comment',
546 'log_deleted',
547 ),
548 'conds' => $conds,
549 'join_conds' => array(
550 'log_search' => array(
551 'LEFT JOIN', array(
552 'ls_field' => 'pr_id', 'ls_value = pr_id'
553 )
554 ),
555 'logging' => array(
556 'LEFT JOIN', array(
557 'ls_log_id = log_id'
558 )
559 )
560 )
561 );
562 }
563
564 public function getTableClass() {
565 return 'TablePager mw-protectedpages';
566 }
567
568 function getIndexField() {
569 return 'pr_id';
570 }
571
572 function getDefaultSort() {
573 return 'pr_id';
574 }
575
576 function isFieldSortable( $field ) {
577 // no index for sorting exists
578 return false;
579 }
580 }