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