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
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 ) {
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(
65 $this->getOutput()->addHTML( $this->showOptions(
76 if ( $pager->getNumRows() ) {
77 $this->getOutput()->addParserOutputContent( $pager->getFullOutput() );
79 $this->getOutput()->addWikiMsg( 'protectedpagesempty' );
84 * @param int $namespace
85 * @param string $type Restriction type
86 * @param string $level Restriction level
87 * @param string $sizetype "min" or "max"
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
97 $title = $this->getPageTitle();
99 return Xml
::openElement( 'form', [ 'method' => 'get', 'action' => wfScript() ] ) .
100 Xml
::openElement( 'fieldset' ) .
101 Xml
::element( 'legend', [], $this->msg( 'protectedpages' )->text() ) .
102 Html
::hidden( 'title', $title->getPrefixedDBkey() ) . "\n" .
103 $this->getNamespaceMenu( $namespace ) . "\n" .
104 $this->getTypeMenu( $type ) . "\n" .
105 $this->getLevelMenu( $level ) . "\n" .
107 $this->getExpiryCheck( $indefOnly ) . "\n" .
108 $this->getCascadeCheck( $cascadeOnly ) . "\n" .
109 $this->getRedirectCheck( $noRedirect ) . "\n" .
111 $this->getSizeLimit( $sizetype, $size ) . "\n" .
112 Xml
::submitButton( $this->msg( 'protectedpages-submit' )->text() ) . "\n" .
113 Xml
::closeElement( 'fieldset' ) .
114 Xml
::closeElement( 'form' );
118 * Prepare the namespace filter drop-down; standard namespace
119 * selector, sans the MediaWiki namespace
121 * @param string|null $namespace Pre-select namespace
124 protected function getNamespaceMenu( $namespace = null ) {
125 return Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' ],
126 Html
::namespaceSelector(
128 'selected' => $namespace,
130 'label' => $this->msg( 'namespace' )->text()
132 'name' => 'namespace',
134 'class' => 'namespaceselector',
141 * @param bool $indefOnly
142 * @return string Formatted HTML
144 protected function getExpiryCheck( $indefOnly ) {
145 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
146 $this->msg( 'protectedpages-indef' )->text(),
154 * @param bool $cascadeOnly
155 * @return string Formatted HTML
157 protected function getCascadeCheck( $cascadeOnly ) {
158 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
159 $this->msg( 'protectedpages-cascade' )->text(),
167 * @param bool $noRedirect
168 * @return string Formatted HTML
170 protected function getRedirectCheck( $noRedirect ) {
171 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
172 $this->msg( 'protectedpages-noredirect' )->text(),
180 * @param string $sizetype "min" or "max"
182 * @return string Formatted HTML
184 protected function getSizeLimit( $sizetype, $size ) {
185 $max = $sizetype === 'max';
187 return '<span class="mw-input-with-label">' . Xml
::radioLabel(
188 $this->msg( 'minimum-size' )->text(),
196 $this->msg( 'maximum-size' )->text(),
203 Xml
::input( 'size', 9, $size, [ 'id' => 'wpsize' ] ) .
205 Xml
::label( $this->msg( 'pagesize' )->text(), 'wpsize' ) . "</span>\n";
209 * Creates the input label of the restriction type
210 * @param string $pr_type Protection type
211 * @return string Formatted HTML
213 protected function getTypeMenu( $pr_type ) {
214 $m = []; // Temporary array
217 // First pass to load the log names
218 foreach ( Title
::getFilteredRestrictionTypes( true ) as $type ) {
219 // Messages: restriction-edit, restriction-move, restriction-create, restriction-upload
220 $text = $this->msg( "restriction-$type" )->text();
224 // Third pass generates sorted XHTML content
225 foreach ( $m as $text => $type ) {
226 $selected = ( $type == $pr_type );
227 $options[] = Xml
::option( $text, $type, $selected ) . "\n";
230 return '<span class="mw-input-with-label">' .
231 Xml
::label( $this->msg( 'restriction-type' )->text(), $this->IdType
) . ' ' .
233 [ 'id' => $this->IdType
, 'name' => $this->IdType
],
234 implode( "\n", $options ) ) . "</span>";
238 * Creates the input label of the restriction level
239 * @param string $pr_level Protection level
240 * @return string Formatted HTML
242 protected function getLevelMenu( $pr_level ) {
244 $m = [ $this->msg( 'restriction-level-all' )->text() => 0 ];
247 // First pass to load the log names
248 foreach ( $this->getConfig()->get( 'RestrictionLevels' ) as $type ) {
249 // Messages used can be 'restriction-level-sysop' and 'restriction-level-autoconfirmed'
250 if ( $type != '' && $type != '*' ) {
251 $text = $this->msg( "restriction-level-$type" )->text();
256 // Third pass generates sorted XHTML content
257 foreach ( $m as $text => $type ) {
258 $selected = ( $type == $pr_level );
259 $options[] = Xml
::option( $text, $type, $selected );
262 return '<span class="mw-input-with-label">' .
263 Xml
::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel
) . ' ' .
265 [ 'id' => $this->IdLevel
, 'name' => $this->IdLevel
],
266 implode( "\n", $options ) ) . "</span>";
269 protected function getGroupName() {
270 return 'maintenance';
278 class ProtectedPagesPager
extends TablePager
{
279 public $mForm, $mConds;
280 private $type, $level, $namespace, $sizetype, $size, $indefonly, $cascadeonly, $noredirect;
282 function __construct( $form, $conds = [], $type, $level, $namespace,
283 $sizetype = '', $size = 0, $indefonly = false, $cascadeonly = false, $noredirect = false
285 $this->mForm
= $form;
286 $this->mConds
= $conds;
287 $this->type
= ( $type ) ?
$type : 'edit';
288 $this->level
= $level;
289 $this->namespace = $namespace;
290 $this->sizetype
= $sizetype;
291 $this->size
= intval( $size );
292 $this->indefonly
= (bool)$indefonly;
293 $this->cascadeonly
= (bool)$cascadeonly;
294 $this->noredirect
= (bool)$noredirect;
295 parent
::__construct( $form->getContext() );
298 function preprocessResults( $result ) {
299 # Do a link batch query
303 foreach ( $result as $row ) {
304 $lb->add( $row->page_namespace
, $row->page_title
);
305 // field is nullable, maybe null on old protections
306 if ( $row->log_user
!== null ) {
307 $userids[] = $row->log_user
;
311 // fill LinkBatch with user page and user talk
312 if ( count( $userids ) ) {
313 $userCache = UserCache
::singleton();
314 $userCache->doQuery( $userids, [], __METHOD__
);
315 foreach ( $userids as $userid ) {
316 $name = $userCache->getProp( $userid, 'name' );
317 if ( $name !== false ) {
318 $lb->add( NS_USER
, $name );
319 $lb->add( NS_USER_TALK
, $name );
327 function getFieldNames() {
328 static $headers = null;
330 if ( $headers == [] ) {
332 'log_timestamp' => 'protectedpages-timestamp',
333 'pr_page' => 'protectedpages-page',
334 'pr_expiry' => 'protectedpages-expiry',
335 'log_user' => 'protectedpages-performer',
336 'pr_params' => 'protectedpages-params',
337 'log_comment' => 'protectedpages-reason',
339 foreach ( $headers as $key => $val ) {
340 $headers[$key] = $this->msg( $val )->text();
348 * @param string $field
349 * @param string $value
350 * @return string HTML
351 * @throws MWException
353 function formatValue( $field, $value ) {
354 /** @var $row object */
355 $row = $this->mCurrentRow
;
360 case 'log_timestamp':
361 // when timestamp is null, this is a old protection row
362 if ( $value === null ) {
363 $formatted = Html
::rawElement(
365 [ 'class' => 'mw-protectedpages-unknown' ],
366 $this->msg( 'protectedpages-unknown-timestamp' )->escaped()
369 $formatted = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
370 $value, $this->getUser() ) );
375 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
377 $formatted = Html
::element(
379 [ 'class' => 'mw-invalidtitle' ],
380 Linker
::getInvalidTitleDescription(
382 $row->page_namespace
,
387 $formatted = Linker
::link( $title );
389 if ( !is_null( $row->page_len
) ) {
390 $formatted .= $this->getLanguage()->getDirMark() .
391 ' ' . Html
::rawElement(
393 [ 'class' => 'mw-protectedpages-length' ],
394 Linker
::formatRevisionSize( $row->page_len
)
400 $formatted = htmlspecialchars( $this->getLanguage()->formatExpiry(
401 $value, /* User preference timezone */true ) );
402 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
403 if ( $this->getUser()->isAllowed( 'protect' ) && $title ) {
404 $changeProtection = Linker
::linkKnown(
406 $this->msg( 'protect_change' )->escaped(),
408 [ 'action' => 'unprotect' ]
410 $formatted .= ' ' . Html
::rawElement(
412 [ 'class' => 'mw-protectedpages-actions' ],
413 $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped()
419 // when timestamp is null, this is a old protection row
420 if ( $row->log_timestamp
=== null ) {
421 $formatted = Html
::rawElement(
423 [ 'class' => 'mw-protectedpages-unknown' ],
424 $this->msg( 'protectedpages-unknown-performer' )->escaped()
427 $username = UserCache
::singleton()->getProp( $value, 'name' );
428 if ( LogEventsList
::userCanBitfield(
430 LogPage
::DELETED_USER
,
433 if ( $username === false ) {
434 $formatted = htmlspecialchars( $value );
436 $formatted = Linker
::userLink( $value, $username )
437 . Linker
::userToolLinks( $value, $username );
440 $formatted = $this->msg( 'rev-deleted-user' )->escaped();
442 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
443 $formatted = '<span class="history-deleted">' . $formatted . '</span>';
450 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
451 $params[] = $this->msg( 'restriction-level-' . $row->pr_level
)->escaped();
452 if ( $row->pr_cascade
) {
453 $params[] = $this->msg( 'protect-summary-cascade' )->escaped();
455 $formatted = $this->getLanguage()->commaList( $params );
459 // when timestamp is null, this is an old protection row
460 if ( $row->log_timestamp
=== null ) {
461 $formatted = Html
::rawElement(
463 [ 'class' => 'mw-protectedpages-unknown' ],
464 $this->msg( 'protectedpages-unknown-reason' )->escaped()
467 if ( LogEventsList
::userCanBitfield(
469 LogPage
::DELETED_COMMENT
,
472 $formatted = Linker
::formatComment( $value !== null ?
$value : '' );
474 $formatted = $this->msg( 'rev-deleted-comment' )->escaped();
476 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
477 $formatted = '<span class="history-deleted">' . $formatted . '</span>';
483 throw new MWException( "Unknown field '$field'" );
489 function getQueryInfo() {
490 $conds = $this->mConds
;
491 $conds[] = 'pr_expiry > ' . $this->mDb
->addQuotes( $this->mDb
->timestamp() ) .
492 ' OR pr_expiry IS NULL';
493 $conds[] = 'page_id=pr_page';
494 $conds[] = 'pr_type=' . $this->mDb
->addQuotes( $this->type
);
496 if ( $this->sizetype
== 'min' ) {
497 $conds[] = 'page_len>=' . $this->size
;
498 } elseif ( $this->sizetype
== 'max' ) {
499 $conds[] = 'page_len<=' . $this->size
;
502 if ( $this->indefonly
) {
503 $infinity = $this->mDb
->addQuotes( $this->mDb
->getInfinity() );
504 $conds[] = "pr_expiry = $infinity OR pr_expiry IS NULL";
506 if ( $this->cascadeonly
) {
507 $conds[] = 'pr_cascade = 1';
509 if ( $this->noredirect
) {
510 $conds[] = 'page_is_redirect = 0';
513 if ( $this->level
) {
514 $conds[] = 'pr_level=' . $this->mDb
->addQuotes( $this->level
);
516 if ( !is_null( $this->namespace ) ) {
517 $conds[] = 'page_namespace=' . $this->mDb
->addQuotes( $this->namespace );
521 'tables' => [ 'page', 'page_restrictions', 'log_search', 'logging' ],
540 'ls_field' => 'pr_id', 'ls_value = pr_id'
552 protected function getTableClass() {
553 return parent
::getTableClass() . ' mw-protectedpages';
556 function getIndexField() {
560 function getDefaultSort() {
564 function isFieldSortable( $field ) {
565 // no index for sorting exists