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
21 * @ingroup SpecialPage
24 use MediaWiki\Linker\LinkRenderer
;
27 * A special page that lists protected pages
29 * @ingroup SpecialPage
31 class SpecialProtectedpages
extends SpecialPage
{
32 protected $IdLevel = 'level';
33 protected $IdType = 'type';
35 public function __construct() {
36 parent
::__construct( 'Protectedpages' );
39 public function execute( $par ) {
41 $this->outputHeader();
42 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
44 $request = $this->getRequest();
45 $type = $request->getVal( $this->IdType
);
46 $level = $request->getVal( $this->IdLevel
);
47 $sizetype = $request->getVal( 'sizetype' );
48 $size = $request->getIntOrNull( 'size' );
49 $ns = $request->getIntOrNull( 'namespace' );
50 $indefOnly = $request->getBool( 'indefonly' ) ?
1 : 0;
51 $cascadeOnly = $request->getBool( 'cascadeonly' ) ?
1 : 0;
52 $noRedirect = $request->getBool( 'noredirect' ) ?
1 : 0;
54 $pager = new ProtectedPagesPager(
65 $this->getLinkRenderer()
68 $this->getOutput()->addHTML( $this->showOptions(
79 if ( $pager->getNumRows() ) {
80 $this->getOutput()->addParserOutputContent( $pager->getFullOutput() );
82 $this->getOutput()->addWikiMsg( 'protectedpagesempty' );
87 * @param int $namespace
88 * @param string $type Restriction type
89 * @param string $level Restriction level
90 * @param string $sizetype "min" or "max"
92 * @param bool $indefOnly Only indefinite protection
93 * @param bool $cascadeOnly Only cascading protection
94 * @param bool $noRedirect Don't show redirects
95 * @return string Input form
97 protected function showOptions( $namespace, $type = 'edit', $level, $sizetype,
98 $size, $indefOnly, $cascadeOnly, $noRedirect
100 $title = $this->getPageTitle();
102 return Xml
::openElement( 'form', [ 'method' => 'get', 'action' => wfScript() ] ) .
103 Xml
::openElement( 'fieldset' ) .
104 Xml
::element( 'legend', [], $this->msg( 'protectedpages' )->text() ) .
105 Html
::hidden( 'title', $title->getPrefixedDBkey() ) . "\n" .
106 $this->getNamespaceMenu( $namespace ) . "\n" .
107 $this->getTypeMenu( $type ) . "\n" .
108 $this->getLevelMenu( $level ) . "\n" .
110 $this->getExpiryCheck( $indefOnly ) . "\n" .
111 $this->getCascadeCheck( $cascadeOnly ) . "\n" .
112 $this->getRedirectCheck( $noRedirect ) . "\n" .
114 $this->getSizeLimit( $sizetype, $size ) . "\n" .
115 Xml
::submitButton( $this->msg( 'protectedpages-submit' )->text() ) . "\n" .
116 Xml
::closeElement( 'fieldset' ) .
117 Xml
::closeElement( 'form' );
121 * Prepare the namespace filter drop-down; standard namespace
122 * selector, sans the MediaWiki namespace
124 * @param string|null $namespace Pre-select namespace
127 protected function getNamespaceMenu( $namespace = null ) {
128 return Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' ],
129 Html
::namespaceSelector(
131 'selected' => $namespace,
133 'label' => $this->msg( 'namespace' )->text()
135 'name' => 'namespace',
137 'class' => 'namespaceselector',
144 * @param bool $indefOnly
145 * @return string Formatted HTML
147 protected function getExpiryCheck( $indefOnly ) {
148 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
149 $this->msg( 'protectedpages-indef' )->text(),
157 * @param bool $cascadeOnly
158 * @return string Formatted HTML
160 protected function getCascadeCheck( $cascadeOnly ) {
161 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
162 $this->msg( 'protectedpages-cascade' )->text(),
170 * @param bool $noRedirect
171 * @return string Formatted HTML
173 protected function getRedirectCheck( $noRedirect ) {
174 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
175 $this->msg( 'protectedpages-noredirect' )->text(),
183 * @param string $sizetype "min" or "max"
185 * @return string Formatted HTML
187 protected function getSizeLimit( $sizetype, $size ) {
188 $max = $sizetype === 'max';
190 return '<span class="mw-input-with-label">' . Xml
::radioLabel(
191 $this->msg( 'minimum-size' )->text(),
199 $this->msg( 'maximum-size' )->text(),
206 Xml
::input( 'size', 9, $size, [ 'id' => 'wpsize' ] ) .
208 Xml
::label( $this->msg( 'pagesize' )->text(), 'wpsize' ) . "</span>\n";
212 * Creates the input label of the restriction type
213 * @param string $pr_type Protection type
214 * @return string Formatted HTML
216 protected function getTypeMenu( $pr_type ) {
217 $m = []; // Temporary array
220 // First pass to load the log names
221 foreach ( Title
::getFilteredRestrictionTypes( true ) as $type ) {
222 // Messages: restriction-edit, restriction-move, restriction-create, restriction-upload
223 $text = $this->msg( "restriction-$type" )->text();
227 // Third pass generates sorted XHTML content
228 foreach ( $m as $text => $type ) {
229 $selected = ( $type == $pr_type );
230 $options[] = Xml
::option( $text, $type, $selected ) . "\n";
233 return '<span class="mw-input-with-label">' .
234 Xml
::label( $this->msg( 'restriction-type' )->text(), $this->IdType
) . ' ' .
236 [ 'id' => $this->IdType
, 'name' => $this->IdType
],
237 implode( "\n", $options ) ) . "</span>";
241 * Creates the input label of the restriction level
242 * @param string $pr_level Protection level
243 * @return string Formatted HTML
245 protected function getLevelMenu( $pr_level ) {
247 $m = [ $this->msg( 'restriction-level-all' )->text() => 0 ];
250 // First pass to load the log names
251 foreach ( $this->getConfig()->get( 'RestrictionLevels' ) as $type ) {
252 // Messages used can be 'restriction-level-sysop' and 'restriction-level-autoconfirmed'
253 if ( $type != '' && $type != '*' ) {
254 $text = $this->msg( "restriction-level-$type" )->text();
259 // Third pass generates sorted XHTML content
260 foreach ( $m as $text => $type ) {
261 $selected = ( $type == $pr_level );
262 $options[] = Xml
::option( $text, $type, $selected );
265 return '<span class="mw-input-with-label">' .
266 Xml
::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel
) . ' ' .
268 [ 'id' => $this->IdLevel
, 'name' => $this->IdLevel
],
269 implode( "\n", $options ) ) . "</span>";
272 protected function getGroupName() {
273 return 'maintenance';
281 class ProtectedPagesPager
extends TablePager
{
282 public $mForm, $mConds;
283 private $type, $level, $namespace, $sizetype, $size, $indefonly, $cascadeonly, $noredirect;
288 private $linkRenderer;
291 * @param SpecialProtectedpages $form
292 * @param array $conds
296 * @param string $sizetype
298 * @param bool $indefonly
299 * @param bool $cascadeonly
300 * @param bool $noredirect
301 * @param LinkRenderer $linkRenderer
303 function __construct( $form, $conds = [], $type, $level, $namespace,
304 $sizetype = '', $size = 0, $indefonly = false, $cascadeonly = false, $noredirect = false,
305 LinkRenderer
$linkRenderer
307 $this->mForm
= $form;
308 $this->mConds
= $conds;
309 $this->type
= ( $type ) ?
$type : 'edit';
310 $this->level
= $level;
311 $this->namespace = $namespace;
312 $this->sizetype
= $sizetype;
313 $this->size
= intval( $size );
314 $this->indefonly
= (bool)$indefonly;
315 $this->cascadeonly
= (bool)$cascadeonly;
316 $this->noredirect
= (bool)$noredirect;
317 $this->linkRenderer
= $linkRenderer;
318 parent
::__construct( $form->getContext() );
321 function preprocessResults( $result ) {
322 # Do a link batch query
326 foreach ( $result as $row ) {
327 $lb->add( $row->page_namespace
, $row->page_title
);
328 // field is nullable, maybe null on old protections
329 if ( $row->log_user
!== null ) {
330 $userids[] = $row->log_user
;
334 // fill LinkBatch with user page and user talk
335 if ( count( $userids ) ) {
336 $userCache = UserCache
::singleton();
337 $userCache->doQuery( $userids, [], __METHOD__
);
338 foreach ( $userids as $userid ) {
339 $name = $userCache->getProp( $userid, 'name' );
340 if ( $name !== false ) {
341 $lb->add( NS_USER
, $name );
342 $lb->add( NS_USER_TALK
, $name );
350 function getFieldNames() {
351 static $headers = null;
353 if ( $headers == [] ) {
355 'log_timestamp' => 'protectedpages-timestamp',
356 'pr_page' => 'protectedpages-page',
357 'pr_expiry' => 'protectedpages-expiry',
358 'log_user' => 'protectedpages-performer',
359 'pr_params' => 'protectedpages-params',
360 'log_comment' => 'protectedpages-reason',
362 foreach ( $headers as $key => $val ) {
363 $headers[$key] = $this->msg( $val )->text();
371 * @param string $field
372 * @param string $value
373 * @return string HTML
374 * @throws MWException
376 function formatValue( $field, $value ) {
377 /** @var $row object */
378 $row = $this->mCurrentRow
;
381 case 'log_timestamp':
382 // when timestamp is null, this is a old protection row
383 if ( $value === null ) {
384 $formatted = Html
::rawElement(
386 [ 'class' => 'mw-protectedpages-unknown' ],
387 $this->msg( 'protectedpages-unknown-timestamp' )->escaped()
390 $formatted = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
391 $value, $this->getUser() ) );
396 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
398 $formatted = Html
::element(
400 [ 'class' => 'mw-invalidtitle' ],
401 Linker
::getInvalidTitleDescription(
403 $row->page_namespace
,
408 $formatted = $this->linkRenderer
->makeLink( $title );
410 if ( !is_null( $row->page_len
) ) {
411 $formatted .= $this->getLanguage()->getDirMark() .
412 ' ' . Html
::rawElement(
414 [ 'class' => 'mw-protectedpages-length' ],
415 Linker
::formatRevisionSize( $row->page_len
)
421 $formatted = htmlspecialchars( $this->getLanguage()->formatExpiry(
422 $value, /* User preference timezone */true ) );
423 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
424 if ( $this->getUser()->isAllowed( 'protect' ) && $title ) {
425 $changeProtection = $this->linkRenderer
->makeKnownLink(
427 $this->msg( 'protect_change' )->text(),
429 [ 'action' => 'unprotect' ]
431 $formatted .= ' ' . Html
::rawElement(
433 [ 'class' => 'mw-protectedpages-actions' ],
434 $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped()
440 // when timestamp is null, this is a old protection row
441 if ( $row->log_timestamp
=== null ) {
442 $formatted = Html
::rawElement(
444 [ 'class' => 'mw-protectedpages-unknown' ],
445 $this->msg( 'protectedpages-unknown-performer' )->escaped()
448 $username = UserCache
::singleton()->getProp( $value, 'name' );
449 if ( LogEventsList
::userCanBitfield(
451 LogPage
::DELETED_USER
,
454 if ( $username === false ) {
455 $formatted = htmlspecialchars( $value );
457 $formatted = Linker
::userLink( $value, $username )
458 . Linker
::userToolLinks( $value, $username );
461 $formatted = $this->msg( 'rev-deleted-user' )->escaped();
463 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
464 $formatted = '<span class="history-deleted">' . $formatted . '</span>';
471 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
472 $params[] = $this->msg( 'restriction-level-' . $row->pr_level
)->escaped();
473 if ( $row->pr_cascade
) {
474 $params[] = $this->msg( 'protect-summary-cascade' )->escaped();
476 $formatted = $this->getLanguage()->commaList( $params );
480 // when timestamp is null, this is an old protection row
481 if ( $row->log_timestamp
=== null ) {
482 $formatted = Html
::rawElement(
484 [ 'class' => 'mw-protectedpages-unknown' ],
485 $this->msg( 'protectedpages-unknown-reason' )->escaped()
488 if ( LogEventsList
::userCanBitfield(
490 LogPage
::DELETED_COMMENT
,
493 $formatted = Linker
::formatComment( $value !== null ?
$value : '' );
495 $formatted = $this->msg( 'rev-deleted-comment' )->escaped();
497 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
498 $formatted = '<span class="history-deleted">' . $formatted . '</span>';
504 throw new MWException( "Unknown field '$field'" );
510 function getQueryInfo() {
511 $conds = $this->mConds
;
512 $conds[] = 'pr_expiry > ' . $this->mDb
->addQuotes( $this->mDb
->timestamp() ) .
513 ' OR pr_expiry IS NULL';
514 $conds[] = 'page_id=pr_page';
515 $conds[] = 'pr_type=' . $this->mDb
->addQuotes( $this->type
);
517 if ( $this->sizetype
== 'min' ) {
518 $conds[] = 'page_len>=' . $this->size
;
519 } elseif ( $this->sizetype
== 'max' ) {
520 $conds[] = 'page_len<=' . $this->size
;
523 if ( $this->indefonly
) {
524 $infinity = $this->mDb
->addQuotes( $this->mDb
->getInfinity() );
525 $conds[] = "pr_expiry = $infinity OR pr_expiry IS NULL";
527 if ( $this->cascadeonly
) {
528 $conds[] = 'pr_cascade = 1';
530 if ( $this->noredirect
) {
531 $conds[] = 'page_is_redirect = 0';
534 if ( $this->level
) {
535 $conds[] = 'pr_level=' . $this->mDb
->addQuotes( $this->level
);
537 if ( !is_null( $this->namespace ) ) {
538 $conds[] = 'page_namespace=' . $this->mDb
->addQuotes( $this->namespace );
542 'tables' => [ 'page', 'page_restrictions', 'log_search', 'logging' ],
561 'ls_field' => 'pr_id', 'ls_value = ' . $this->mDb
->buildStringCast( 'pr_id' )
573 protected function getTableClass() {
574 return parent
::getTableClass() . ' mw-protectedpages';
577 function getIndexField() {
581 function getDefaultSort() {
585 function isFieldSortable( $field ) {
586 // no index for sorting exists