SpecialLinkSearch: clean up munged query variable handling
[mediawiki.git] / includes / specials / SpecialProtectedpages.php
blob00e56c1c79224bb094d2d93f92562f9873dab376
1 <?php
2 /**
3 * Implements Special:Protectedpages
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * A special page that lists protected pages
27 * @ingroup SpecialPage
29 class SpecialProtectedpages extends SpecialPage {
30 protected $IdLevel = 'level';
31 protected $IdType = 'type';
33 public function __construct() {
34 parent::__construct( 'Protectedpages' );
37 public function execute( $par ) {
38 $this->setHeaders();
39 $this->outputHeader();
40 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
42 $request = $this->getRequest();
43 $type = $request->getVal( $this->IdType );
44 $level = $request->getVal( $this->IdLevel );
45 $sizetype = $request->getVal( 'sizetype' );
46 $size = $request->getIntOrNull( 'size' );
47 $ns = $request->getIntOrNull( 'namespace' );
48 $indefOnly = $request->getBool( 'indefonly' ) ? 1 : 0;
49 $cascadeOnly = $request->getBool( 'cascadeonly' ) ? 1 : 0;
50 $noRedirect = $request->getBool( 'noredirect' ) ? 1 : 0;
52 $pager = new ProtectedPagesPager(
53 $this,
54 array(),
55 $type,
56 $level,
57 $ns,
58 $sizetype,
59 $size,
60 $indefOnly,
61 $cascadeOnly,
62 $noRedirect
65 $this->getOutput()->addHTML( $this->showOptions(
66 $ns,
67 $type,
68 $level,
69 $sizetype,
70 $size,
71 $indefOnly,
72 $cascadeOnly,
73 $noRedirect
74 ) );
76 if ( $pager->getNumRows() ) {
77 $this->getOutput()->addParserOutputContent( $pager->getFullOutput() );
78 } else {
79 $this->getOutput()->addWikiMsg( 'protectedpagesempty' );
83 /**
84 * @param int $namespace
85 * @param string $type Restriction type
86 * @param string $level Restriction level
87 * @param string $sizetype "min" or "max"
88 * @param int $size
89 * @param bool $indefOnly Only indefinite protection
90 * @param bool $cascadeOnly Only cascading protection
91 * @param bool $noRedirect Don't show redirects
92 * @return string Input form
94 protected function showOptions( $namespace, $type = 'edit', $level, $sizetype,
95 $size, $indefOnly, $cascadeOnly, $noRedirect
96 ) {
97 $title = $this->getPageTitle();
99 return Xml::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) ) .
100 Xml::openElement( 'fieldset' ) .
101 Xml::element( 'legend', array(), $this->msg( 'protectedpages' )->text() ) .
102 Html::hidden( 'title', $title->getPrefixedDBkey() ) . "\n" .
103 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
104 $this->getTypeMenu( $type ) . "&#160;\n" .
105 $this->getLevelMenu( $level ) . "&#160;\n" .
106 "<br /><span style='white-space: nowrap'>" .
107 $this->getExpiryCheck( $indefOnly ) . "&#160;\n" .
108 $this->getCascadeCheck( $cascadeOnly ) . "&#160;\n" .
109 $this->getRedirectCheck( $noRedirect ) . "&#160;\n" .
110 "</span><br /><span style='white-space: nowrap'>" .
111 $this->getSizeLimit( $sizetype, $size ) . "&#160;\n" .
112 "</span>" .
113 "&#160;" . Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
114 Xml::closeElement( 'fieldset' ) .
115 Xml::closeElement( 'form' );
119 * Prepare the namespace filter drop-down; standard namespace
120 * selector, sans the MediaWiki namespace
122 * @param string|null $namespace Pre-select namespace
123 * @return string
125 protected function getNamespaceMenu( $namespace = null ) {
126 return Html::rawElement( 'span', array( 'style' => 'white-space: nowrap;' ),
127 Html::namespaceSelector(
128 array(
129 'selected' => $namespace,
130 'all' => '',
131 'label' => $this->msg( 'namespace' )->text()
132 ), array(
133 'name' => 'namespace',
134 'id' => 'namespace',
135 'class' => 'namespaceselector',
142 * @param bool $indefOnly
143 * @return string Formatted HTML
145 protected function getExpiryCheck( $indefOnly ) {
146 return Xml::checkLabel(
147 $this->msg( 'protectedpages-indef' )->text(),
148 'indefonly',
149 'indefonly',
150 $indefOnly
151 ) . "\n";
155 * @param bool $cascadeOnly
156 * @return string Formatted HTML
158 protected function getCascadeCheck( $cascadeOnly ) {
159 return Xml::checkLabel(
160 $this->msg( 'protectedpages-cascade' )->text(),
161 'cascadeonly',
162 'cascadeonly',
163 $cascadeOnly
164 ) . "\n";
168 * @param bool $noRedirect
169 * @return string Formatted HTML
171 protected function getRedirectCheck( $noRedirect ) {
172 return Xml::checkLabel(
173 $this->msg( 'protectedpages-noredirect' )->text(),
174 'noredirect',
175 'noredirect',
176 $noRedirect
177 ) . "\n";
181 * @param string $sizetype "min" or "max"
182 * @param mixed $size
183 * @return string Formatted HTML
185 protected function getSizeLimit( $sizetype, $size ) {
186 $max = $sizetype === 'max';
188 return Xml::radioLabel(
189 $this->msg( 'minimum-size' )->text(),
190 'sizetype',
191 'min',
192 'wpmin',
193 !$max
195 '&#160;' .
196 Xml::radioLabel(
197 $this->msg( 'maximum-size' )->text(),
198 'sizetype',
199 'max',
200 'wpmax',
201 $max
203 '&#160;' .
204 Xml::input( 'size', 9, $size, array( 'id' => 'wpsize' ) ) .
205 '&#160;' .
206 Xml::label( $this->msg( 'pagesize' )->text(), 'wpsize' );
210 * Creates the input label of the restriction type
211 * @param string $pr_type Protection type
212 * @return string Formatted HTML
214 protected function getTypeMenu( $pr_type ) {
215 $m = array(); // Temporary array
216 $options = array();
218 // First pass to load the log names
219 foreach ( Title::getFilteredRestrictionTypes( true ) as $type ) {
220 // Messages: restriction-edit, restriction-move, restriction-create, restriction-upload
221 $text = $this->msg( "restriction-$type" )->text();
222 $m[$text] = $type;
225 // Third pass generates sorted XHTML content
226 foreach ( $m as $text => $type ) {
227 $selected = ( $type == $pr_type );
228 $options[] = Xml::option( $text, $type, $selected ) . "\n";
231 return "<span style='white-space: nowrap'>" .
232 Xml::label( $this->msg( 'restriction-type' )->text(), $this->IdType ) . '&#160;' .
233 Xml::tags( 'select',
234 array( 'id' => $this->IdType, 'name' => $this->IdType ),
235 implode( "\n", $options ) ) . "</span>";
239 * Creates the input label of the restriction level
240 * @param string $pr_level Protection level
241 * @return string Formatted HTML
243 protected function getLevelMenu( $pr_level ) {
244 // Temporary array
245 $m = array( $this->msg( 'restriction-level-all' )->text() => 0 );
246 $options = array();
248 // First pass to load the log names
249 foreach ( $this->getConfig()->get( 'RestrictionLevels' ) as $type ) {
250 // Messages used can be 'restriction-level-sysop' and 'restriction-level-autoconfirmed'
251 if ( $type != '' && $type != '*' ) {
252 $text = $this->msg( "restriction-level-$type" )->text();
253 $m[$text] = $type;
257 // Third pass generates sorted XHTML content
258 foreach ( $m as $text => $type ) {
259 $selected = ( $type == $pr_level );
260 $options[] = Xml::option( $text, $type, $selected );
263 return "<span style='white-space: nowrap'>" .
264 Xml::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel ) . ' ' .
265 Xml::tags( 'select',
266 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
267 implode( "\n", $options ) ) . "</span>";
270 protected function getGroupName() {
271 return 'maintenance';
276 * @todo document
277 * @ingroup Pager
279 class ProtectedPagesPager extends TablePager {
280 public $mForm, $mConds;
281 private $type, $level, $namespace, $sizetype, $size, $indefonly, $cascadeonly, $noredirect;
283 function __construct( $form, $conds = array(), $type, $level, $namespace,
284 $sizetype = '', $size = 0, $indefonly = false, $cascadeonly = false, $noredirect = false
286 $this->mForm = $form;
287 $this->mConds = $conds;
288 $this->type = ( $type ) ? $type : 'edit';
289 $this->level = $level;
290 $this->namespace = $namespace;
291 $this->sizetype = $sizetype;
292 $this->size = intval( $size );
293 $this->indefonly = (bool)$indefonly;
294 $this->cascadeonly = (bool)$cascadeonly;
295 $this->noredirect = (bool)$noredirect;
296 parent::__construct( $form->getContext() );
299 function preprocessResults( $result ) {
300 # Do a link batch query
301 $lb = new LinkBatch;
302 $userids = array();
304 foreach ( $result as $row ) {
305 $lb->add( $row->page_namespace, $row->page_title );
306 // field is nullable, maybe null on old protections
307 if ( $row->log_user !== null ) {
308 $userids[] = $row->log_user;
312 // fill LinkBatch with user page and user talk
313 if ( count( $userids ) ) {
314 $userCache = UserCache::singleton();
315 $userCache->doQuery( $userids, array(), __METHOD__ );
316 foreach ( $userids as $userid ) {
317 $name = $userCache->getProp( $userid, 'name' );
318 if ( $name !== false ) {
319 $lb->add( NS_USER, $name );
320 $lb->add( NS_USER_TALK, $name );
325 $lb->execute();
328 function getFieldNames() {
329 static $headers = null;
331 if ( $headers == array() ) {
332 $headers = array(
333 'log_timestamp' => 'protectedpages-timestamp',
334 'pr_page' => 'protectedpages-page',
335 'pr_expiry' => 'protectedpages-expiry',
336 'log_user' => 'protectedpages-performer',
337 'pr_params' => 'protectedpages-params',
338 'log_comment' => 'protectedpages-reason',
340 foreach ( $headers as $key => $val ) {
341 $headers[$key] = $this->msg( $val )->text();
345 return $headers;
349 * @param string $field
350 * @param string $value
351 * @return string HTML
352 * @throws MWException
354 function formatValue( $field, $value ) {
355 /** @var $row object */
356 $row = $this->mCurrentRow;
358 $formatted = '';
360 switch ( $field ) {
361 case 'log_timestamp':
362 // when timestamp is null, this is a old protection row
363 if ( $value === null ) {
364 $formatted = Html::rawElement(
365 'span',
366 array( 'class' => 'mw-protectedpages-unknown' ),
367 $this->msg( 'protectedpages-unknown-timestamp' )->escaped()
369 } else {
370 $formatted = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
371 $value, $this->getUser() ) );
373 break;
375 case 'pr_page':
376 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
377 if ( !$title ) {
378 $formatted = Html::element(
379 'span',
380 array( 'class' => 'mw-invalidtitle' ),
381 Linker::getInvalidTitleDescription(
382 $this->getContext(),
383 $row->page_namespace,
384 $row->page_title
387 } else {
388 $formatted = Linker::link( $title );
390 if ( !is_null( $row->page_len ) ) {
391 $formatted .= $this->getLanguage()->getDirMark() .
392 ' ' . Html::rawElement(
393 'span',
394 array( 'class' => 'mw-protectedpages-length' ),
395 Linker::formatRevisionSize( $row->page_len )
398 break;
400 case 'pr_expiry':
401 $formatted = htmlspecialchars( $this->getLanguage()->formatExpiry(
402 $value, /* User preference timezone */true ) );
403 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
404 if ( $this->getUser()->isAllowed( 'protect' ) && $title ) {
405 $changeProtection = Linker::linkKnown(
406 $title,
407 $this->msg( 'protect_change' )->escaped(),
408 array(),
409 array( 'action' => 'unprotect' )
411 $formatted .= ' ' . Html::rawElement(
412 'span',
413 array( 'class' => 'mw-protectedpages-actions' ),
414 $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped()
417 break;
419 case 'log_user':
420 // when timestamp is null, this is a old protection row
421 if ( $row->log_timestamp === null ) {
422 $formatted = Html::rawElement(
423 'span',
424 array( 'class' => 'mw-protectedpages-unknown' ),
425 $this->msg( 'protectedpages-unknown-performer' )->escaped()
427 } else {
428 $username = UserCache::singleton()->getProp( $value, 'name' );
429 if ( LogEventsList::userCanBitfield(
430 $row->log_deleted,
431 LogPage::DELETED_USER,
432 $this->getUser()
433 ) ) {
434 if ( $username === false ) {
435 $formatted = htmlspecialchars( $value );
436 } else {
437 $formatted = Linker::userLink( $value, $username )
438 . Linker::userToolLinks( $value, $username );
440 } else {
441 $formatted = $this->msg( 'rev-deleted-user' )->escaped();
443 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
444 $formatted = '<span class="history-deleted">' . $formatted . '</span>';
447 break;
449 case 'pr_params':
450 $params = array();
451 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
452 $params[] = $this->msg( 'restriction-level-' . $row->pr_level )->escaped();
453 if ( $row->pr_cascade ) {
454 $params[] = $this->msg( 'protect-summary-cascade' )->escaped();
456 $formatted = $this->getLanguage()->commaList( $params );
457 break;
459 case 'log_comment':
460 // when timestamp is null, this is an old protection row
461 if ( $row->log_timestamp === null ) {
462 $formatted = Html::rawElement(
463 'span',
464 array( 'class' => 'mw-protectedpages-unknown' ),
465 $this->msg( 'protectedpages-unknown-reason' )->escaped()
467 } else {
468 if ( LogEventsList::userCanBitfield(
469 $row->log_deleted,
470 LogPage::DELETED_COMMENT,
471 $this->getUser()
472 ) ) {
473 $formatted = Linker::formatComment( $value !== null ? $value : '' );
474 } else {
475 $formatted = $this->msg( 'rev-deleted-comment' )->escaped();
477 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
478 $formatted = '<span class="history-deleted">' . $formatted . '</span>';
481 break;
483 default:
484 throw new MWException( "Unknown field '$field'" );
487 return $formatted;
490 function getQueryInfo() {
491 $conds = $this->mConds;
492 $conds[] = 'pr_expiry > ' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
493 ' OR pr_expiry IS NULL';
494 $conds[] = 'page_id=pr_page';
495 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
497 if ( $this->sizetype == 'min' ) {
498 $conds[] = 'page_len>=' . $this->size;
499 } elseif ( $this->sizetype == 'max' ) {
500 $conds[] = 'page_len<=' . $this->size;
503 if ( $this->indefonly ) {
504 $infinity = $this->mDb->addQuotes( $this->mDb->getInfinity() );
505 $conds[] = "pr_expiry = $infinity OR pr_expiry IS NULL";
507 if ( $this->cascadeonly ) {
508 $conds[] = 'pr_cascade = 1';
510 if ( $this->noredirect ) {
511 $conds[] = 'page_is_redirect = 0';
514 if ( $this->level ) {
515 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
517 if ( !is_null( $this->namespace ) ) {
518 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
521 return array(
522 'tables' => array( 'page', 'page_restrictions', 'log_search', 'logging' ),
523 'fields' => array(
524 'pr_id',
525 'page_namespace',
526 'page_title',
527 'page_len',
528 'pr_type',
529 'pr_level',
530 'pr_expiry',
531 'pr_cascade',
532 'log_timestamp',
533 'log_user',
534 'log_comment',
535 'log_deleted',
537 'conds' => $conds,
538 'join_conds' => array(
539 'log_search' => array(
540 'LEFT JOIN', array(
541 'ls_field' => 'pr_id', 'ls_value = pr_id'
544 'logging' => array(
545 'LEFT JOIN', array(
546 'ls_log_id = log_id'
553 public function getTableClass() {
554 return parent::getTableClass() . ' mw-protectedpages';
557 function getIndexField() {
558 return 'pr_id';
561 function getDefaultSort() {
562 return 'pr_id';
565 function isFieldSortable( $field ) {
566 // no index for sorting exists
567 return false;