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