Merge "Http::getProxy() method to get proxy configuration"
[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 $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;
51
52 $pager = new ProtectedPagesPager(
53 $this,
54 [],
55 $type,
56 $level,
57 $ns,
58 $sizetype,
59 $size,
60 $indefOnly,
61 $cascadeOnly,
62 $noRedirect
63 );
64
65 $this->getOutput()->addHTML( $this->showOptions(
66 $ns,
67 $type,
68 $level,
69 $sizetype,
70 $size,
71 $indefOnly,
72 $cascadeOnly,
73 $noRedirect
74 ) );
75
76 if ( $pager->getNumRows() ) {
77 $this->getOutput()->addParserOutputContent( $pager->getFullOutput() );
78 } else {
79 $this->getOutput()->addWikiMsg( 'protectedpagesempty' );
80 }
81 }
82
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
93 */
94 protected function showOptions( $namespace, $type = 'edit', $level, $sizetype,
95 $size, $indefOnly, $cascadeOnly, $noRedirect
96 ) {
97 $title = $this->getPageTitle();
98
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" .
106 "<br />\n" .
107 $this->getExpiryCheck( $indefOnly ) . "\n" .
108 $this->getCascadeCheck( $cascadeOnly ) . "\n" .
109 $this->getRedirectCheck( $noRedirect ) . "\n" .
110 "<br />\n" .
111 $this->getSizeLimit( $sizetype, $size ) . "\n" .
112 Xml::submitButton( $this->msg( 'protectedpages-submit' )->text() ) . "\n" .
113 Xml::closeElement( 'fieldset' ) .
114 Xml::closeElement( 'form' );
115 }
116
117 /**
118 * Prepare the namespace filter drop-down; standard namespace
119 * selector, sans the MediaWiki namespace
120 *
121 * @param string|null $namespace Pre-select namespace
122 * @return string
123 */
124 protected function getNamespaceMenu( $namespace = null ) {
125 return Html::rawElement( 'span', [ 'class' => 'mw-input-with-label' ],
126 Html::namespaceSelector(
127 [
128 'selected' => $namespace,
129 'all' => '',
130 'label' => $this->msg( 'namespace' )->text()
131 ], [
132 'name' => 'namespace',
133 'id' => 'namespace',
134 'class' => 'namespaceselector',
135 ]
136 )
137 );
138 }
139
140 /**
141 * @param bool $indefOnly
142 * @return string Formatted HTML
143 */
144 protected function getExpiryCheck( $indefOnly ) {
145 return '<span class="mw-input-with-label">' . Xml::checkLabel(
146 $this->msg( 'protectedpages-indef' )->text(),
147 'indefonly',
148 'indefonly',
149 $indefOnly
150 ) . "</span>\n";
151 }
152
153 /**
154 * @param bool $cascadeOnly
155 * @return string Formatted HTML
156 */
157 protected function getCascadeCheck( $cascadeOnly ) {
158 return '<span class="mw-input-with-label">' . Xml::checkLabel(
159 $this->msg( 'protectedpages-cascade' )->text(),
160 'cascadeonly',
161 'cascadeonly',
162 $cascadeOnly
163 ) . "</span>\n";
164 }
165
166 /**
167 * @param bool $noRedirect
168 * @return string Formatted HTML
169 */
170 protected function getRedirectCheck( $noRedirect ) {
171 return '<span class="mw-input-with-label">' . Xml::checkLabel(
172 $this->msg( 'protectedpages-noredirect' )->text(),
173 'noredirect',
174 'noredirect',
175 $noRedirect
176 ) . "</span>\n";
177 }
178
179 /**
180 * @param string $sizetype "min" or "max"
181 * @param mixed $size
182 * @return string Formatted HTML
183 */
184 protected function getSizeLimit( $sizetype, $size ) {
185 $max = $sizetype === 'max';
186
187 return '<span class="mw-input-with-label">' . Xml::radioLabel(
188 $this->msg( 'minimum-size' )->text(),
189 'sizetype',
190 'min',
191 'wpmin',
192 !$max
193 ) .
194 ' ' .
195 Xml::radioLabel(
196 $this->msg( 'maximum-size' )->text(),
197 'sizetype',
198 'max',
199 'wpmax',
200 $max
201 ) .
202 ' ' .
203 Xml::input( 'size', 9, $size, [ 'id' => 'wpsize' ] ) .
204 ' ' .
205 Xml::label( $this->msg( 'pagesize' )->text(), 'wpsize' ) . "</span>\n";
206 }
207
208 /**
209 * Creates the input label of the restriction type
210 * @param string $pr_type Protection type
211 * @return string Formatted HTML
212 */
213 protected function getTypeMenu( $pr_type ) {
214 $m = []; // Temporary array
215 $options = [];
216
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();
221 $m[$text] = $type;
222 }
223
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";
228 }
229
230 return '<span class="mw-input-with-label">' .
231 Xml::label( $this->msg( 'restriction-type' )->text(), $this->IdType ) . ' ' .
232 Xml::tags( 'select',
233 [ 'id' => $this->IdType, 'name' => $this->IdType ],
234 implode( "\n", $options ) ) . "</span>";
235 }
236
237 /**
238 * Creates the input label of the restriction level
239 * @param string $pr_level Protection level
240 * @return string Formatted HTML
241 */
242 protected function getLevelMenu( $pr_level ) {
243 // Temporary array
244 $m = [ $this->msg( 'restriction-level-all' )->text() => 0 ];
245 $options = [];
246
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();
252 $m[$text] = $type;
253 }
254 }
255
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 );
260 }
261
262 return '<span class="mw-input-with-label">' .
263 Xml::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel ) . ' ' .
264 Xml::tags( 'select',
265 [ 'id' => $this->IdLevel, 'name' => $this->IdLevel ],
266 implode( "\n", $options ) ) . "</span>";
267 }
268
269 protected function getGroupName() {
270 return 'maintenance';
271 }
272 }
273
274 /**
275 * @todo document
276 * @ingroup Pager
277 */
278 class ProtectedPagesPager extends TablePager {
279 public $mForm, $mConds;
280 private $type, $level, $namespace, $sizetype, $size, $indefonly, $cascadeonly, $noredirect;
281
282 function __construct( $form, $conds = [], $type, $level, $namespace,
283 $sizetype = '', $size = 0, $indefonly = false, $cascadeonly = false, $noredirect = false
284 ) {
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() );
296 }
297
298 function preprocessResults( $result ) {
299 # Do a link batch query
300 $lb = new LinkBatch;
301 $userids = [];
302
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;
308 }
309 }
310
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 );
320 }
321 }
322 }
323
324 $lb->execute();
325 }
326
327 function getFieldNames() {
328 static $headers = null;
329
330 if ( $headers == [] ) {
331 $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',
338 ];
339 foreach ( $headers as $key => $val ) {
340 $headers[$key] = $this->msg( $val )->text();
341 }
342 }
343
344 return $headers;
345 }
346
347 /**
348 * @param string $field
349 * @param string $value
350 * @return string HTML
351 * @throws MWException
352 */
353 function formatValue( $field, $value ) {
354 /** @var $row object */
355 $row = $this->mCurrentRow;
356
357 $formatted = '';
358
359 switch ( $field ) {
360 case 'log_timestamp':
361 // when timestamp is null, this is a old protection row
362 if ( $value === null ) {
363 $formatted = Html::rawElement(
364 'span',
365 [ 'class' => 'mw-protectedpages-unknown' ],
366 $this->msg( 'protectedpages-unknown-timestamp' )->escaped()
367 );
368 } else {
369 $formatted = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
370 $value, $this->getUser() ) );
371 }
372 break;
373
374 case 'pr_page':
375 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
376 if ( !$title ) {
377 $formatted = Html::element(
378 'span',
379 [ 'class' => 'mw-invalidtitle' ],
380 Linker::getInvalidTitleDescription(
381 $this->getContext(),
382 $row->page_namespace,
383 $row->page_title
384 )
385 );
386 } else {
387 $formatted = Linker::link( $title );
388 }
389 if ( !is_null( $row->page_len ) ) {
390 $formatted .= $this->getLanguage()->getDirMark() .
391 ' ' . Html::rawElement(
392 'span',
393 [ 'class' => 'mw-protectedpages-length' ],
394 Linker::formatRevisionSize( $row->page_len )
395 );
396 }
397 break;
398
399 case 'pr_expiry':
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(
405 $title,
406 $this->msg( 'protect_change' )->escaped(),
407 [],
408 [ 'action' => 'unprotect' ]
409 );
410 $formatted .= ' ' . Html::rawElement(
411 'span',
412 [ 'class' => 'mw-protectedpages-actions' ],
413 $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped()
414 );
415 }
416 break;
417
418 case 'log_user':
419 // when timestamp is null, this is a old protection row
420 if ( $row->log_timestamp === null ) {
421 $formatted = Html::rawElement(
422 'span',
423 [ 'class' => 'mw-protectedpages-unknown' ],
424 $this->msg( 'protectedpages-unknown-performer' )->escaped()
425 );
426 } else {
427 $username = UserCache::singleton()->getProp( $value, 'name' );
428 if ( LogEventsList::userCanBitfield(
429 $row->log_deleted,
430 LogPage::DELETED_USER,
431 $this->getUser()
432 ) ) {
433 if ( $username === false ) {
434 $formatted = htmlspecialchars( $value );
435 } else {
436 $formatted = Linker::userLink( $value, $username )
437 . Linker::userToolLinks( $value, $username );
438 }
439 } else {
440 $formatted = $this->msg( 'rev-deleted-user' )->escaped();
441 }
442 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
443 $formatted = '<span class="history-deleted">' . $formatted . '</span>';
444 }
445 }
446 break;
447
448 case 'pr_params':
449 $params = [];
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();
454 }
455 $formatted = $this->getLanguage()->commaList( $params );
456 break;
457
458 case 'log_comment':
459 // when timestamp is null, this is an old protection row
460 if ( $row->log_timestamp === null ) {
461 $formatted = Html::rawElement(
462 'span',
463 [ 'class' => 'mw-protectedpages-unknown' ],
464 $this->msg( 'protectedpages-unknown-reason' )->escaped()
465 );
466 } else {
467 if ( LogEventsList::userCanBitfield(
468 $row->log_deleted,
469 LogPage::DELETED_COMMENT,
470 $this->getUser()
471 ) ) {
472 $formatted = Linker::formatComment( $value !== null ? $value : '' );
473 } else {
474 $formatted = $this->msg( 'rev-deleted-comment' )->escaped();
475 }
476 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
477 $formatted = '<span class="history-deleted">' . $formatted . '</span>';
478 }
479 }
480 break;
481
482 default:
483 throw new MWException( "Unknown field '$field'" );
484 }
485
486 return $formatted;
487 }
488
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 );
495
496 if ( $this->sizetype == 'min' ) {
497 $conds[] = 'page_len>=' . $this->size;
498 } elseif ( $this->sizetype == 'max' ) {
499 $conds[] = 'page_len<=' . $this->size;
500 }
501
502 if ( $this->indefonly ) {
503 $infinity = $this->mDb->addQuotes( $this->mDb->getInfinity() );
504 $conds[] = "pr_expiry = $infinity OR pr_expiry IS NULL";
505 }
506 if ( $this->cascadeonly ) {
507 $conds[] = 'pr_cascade = 1';
508 }
509 if ( $this->noredirect ) {
510 $conds[] = 'page_is_redirect = 0';
511 }
512
513 if ( $this->level ) {
514 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
515 }
516 if ( !is_null( $this->namespace ) ) {
517 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
518 }
519
520 return [
521 'tables' => [ 'page', 'page_restrictions', 'log_search', 'logging' ],
522 'fields' => [
523 'pr_id',
524 'page_namespace',
525 'page_title',
526 'page_len',
527 'pr_type',
528 'pr_level',
529 'pr_expiry',
530 'pr_cascade',
531 'log_timestamp',
532 'log_user',
533 'log_comment',
534 'log_deleted',
535 ],
536 'conds' => $conds,
537 'join_conds' => [
538 'log_search' => [
539 'LEFT JOIN', [
540 'ls_field' => 'pr_id', 'ls_value = pr_id'
541 ]
542 ],
543 'logging' => [
544 'LEFT JOIN', [
545 'ls_log_id = log_id'
546 ]
547 ]
548 ]
549 ];
550 }
551
552 public function getTableClass() {
553 return parent::getTableClass() . ' mw-protectedpages';
554 }
555
556 function getIndexField() {
557 return 'pr_id';
558 }
559
560 function getDefaultSort() {
561 return 'pr_id';
562 }
563
564 function isFieldSortable( $field ) {
565 // no index for sorting exists
566 return false;
567 }
568 }