Allow hiding redirects in Special:ProtectedPages
[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
42 // Purge expired entries on one in every 10 queries
43 if ( !mt_rand( 0, 10 ) ) {
44 Title::purgeExpiredRestrictions();
45 }
46
47 $request = $this->getRequest();
48 $type = $request->getVal( $this->IdType );
49 $level = $request->getVal( $this->IdLevel );
50 $sizetype = $request->getVal( 'sizetype' );
51 $size = $request->getIntOrNull( 'size' );
52 $ns = $request->getIntOrNull( 'namespace' );
53 $indefOnly = $request->getBool( 'indefonly' ) ? 1 : 0;
54 $cascadeOnly = $request->getBool( 'cascadeonly' ) ? 1 : 0;
55 $noRedirect = $request->getBool( 'noredirect' ) ? 1 : 0;
56
57 $pager = new ProtectedPagesPager(
58 $this,
59 array(),
60 $type,
61 $level,
62 $ns,
63 $sizetype,
64 $size,
65 $indefOnly,
66 $cascadeOnly,
67 $noRedirect
68 );
69
70 $this->getOutput()->addHTML( $this->showOptions(
71 $ns,
72 $type,
73 $level,
74 $sizetype,
75 $size,
76 $indefOnly,
77 $cascadeOnly,
78 $noRedirect
79 ) );
80
81 if ( $pager->getNumRows() ) {
82 $this->getOutput()->addHTML(
83 $pager->getNavigationBar() .
84 '<ul>' . $pager->getBody() . '</ul>' .
85 $pager->getNavigationBar()
86 );
87 } else {
88 $this->getOutput()->addWikiMsg( 'protectedpagesempty' );
89 }
90 }
91
92 /**
93 * Callback function to output a restriction
94 * @param Title $row Protected title
95 * @return string Formatted "<li>" element
96 */
97 public function formatRow( $row ) {
98 wfProfileIn( __METHOD__ );
99
100 static $infinity = null;
101
102 if ( is_null( $infinity ) ) {
103 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
104 }
105
106 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
107 if ( !$title ) {
108 wfProfileOut( __METHOD__ );
109
110 return Html::rawElement(
111 'li',
112 array(),
113 Html::element(
114 'span',
115 array( 'class' => 'mw-invalidtitle' ),
116 Linker::getInvalidTitleDescription(
117 $this->getContext(),
118 $row->page_namespace,
119 $row->page_title
120 )
121 )
122 ) . "\n";
123 }
124
125 $link = Linker::link( $title );
126
127 $description_items = array();
128
129 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
130 $protType = $this->msg( 'restriction-level-' . $row->pr_level )->escaped();
131
132 $description_items[] = $protType;
133
134 if ( $row->pr_cascade ) {
135 $description_items[] = $this->msg( 'protect-summary-cascade' )->text();
136 }
137
138 $stxt = '';
139 $lang = $this->getLanguage();
140
141 $expiry = $lang->formatExpiry( $row->pr_expiry, TS_MW );
142 if ( $expiry != $infinity ) {
143 $user = $this->getUser();
144 $description_items[] = $this->msg(
145 'protect-expiring-local',
146 $lang->userTimeAndDate( $expiry, $user ),
147 $lang->userDate( $expiry, $user ),
148 $lang->userTime( $expiry, $user )
149 )->escaped();
150 }
151
152 if ( !is_null( $size = $row->page_len ) ) {
153 $stxt = $lang->getDirMark() . ' ' . Linker::formatRevisionSize( $size );
154 }
155
156 // Show a link to the change protection form for allowed users otherwise
157 // a link to the protection log
158 if ( $this->getUser()->isAllowed( 'protect' ) ) {
159 $changeProtection = Linker::linkKnown(
160 $title,
161 $this->msg( 'protect_change' )->escaped(),
162 array(),
163 array( 'action' => 'unprotect' )
164 );
165 } else {
166 $ltitle = SpecialPage::getTitleFor( 'Log' );
167 $changeProtection = Linker::linkKnown(
168 $ltitle,
169 $this->msg( 'protectlogpage' )->escaped(),
170 array(),
171 array(
172 'type' => 'protect',
173 'page' => $title->getPrefixedText()
174 )
175 );
176 }
177
178 $changeProtection = ' ' . $this->msg( 'parentheses' )->rawParams( $changeProtection )
179 ->escaped();
180
181 wfProfileOut( __METHOD__ );
182
183 return Html::rawElement(
184 'li',
185 array(),
186 $lang->specialList( $link . $stxt, $lang->commaList( $description_items ), false ) .
187 $changeProtection
188 ) . "\n";
189 }
190
191 /**
192 * @param int $namespace
193 * @param string $type Restriction type
194 * @param string $level Restriction level
195 * @param string $sizetype "min" or "max"
196 * @param int $size
197 * @param bool $indefOnly Only indefinite protection
198 * @param bool $cascadeOnly Only cascading protection
199 * @param bool $noRedirect Don't show redirects
200 * @return String: input form
201 */
202 protected function showOptions( $namespace, $type = 'edit', $level, $sizetype,
203 $size, $indefOnly, $cascadeOnly, $noRedirect
204 ) {
205 global $wgScript;
206
207 $title = $this->getPageTitle();
208
209 return Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
210 Xml::openElement( 'fieldset' ) .
211 Xml::element( 'legend', array(), $this->msg( 'protectedpages' )->text() ) .
212 Html::hidden( 'title', $title->getPrefixedDBkey() ) . "\n" .
213 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
214 $this->getTypeMenu( $type ) . "&#160;\n" .
215 $this->getLevelMenu( $level ) . "&#160;\n" .
216 "<br /><span style='white-space: nowrap'>" .
217 $this->getExpiryCheck( $indefOnly ) . "&#160;\n" .
218 $this->getCascadeCheck( $cascadeOnly ) . "&#160;\n" .
219 $this->getRedirectCheck( $noRedirect ) . "&#160;\n" .
220 "</span><br /><span style='white-space: nowrap'>" .
221 $this->getSizeLimit( $sizetype, $size ) . "&#160;\n" .
222 "</span>" .
223 "&#160;" . Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
224 Xml::closeElement( 'fieldset' ) .
225 Xml::closeElement( 'form' );
226 }
227
228 /**
229 * Prepare the namespace filter drop-down; standard namespace
230 * selector, sans the MediaWiki namespace
231 *
232 * @param $namespace Mixed: pre-select namespace
233 * @return String
234 */
235 protected function getNamespaceMenu( $namespace = null ) {
236 return Html::rawElement( 'span', array( 'style' => 'white-space: nowrap;' ),
237 Html::namespaceSelector(
238 array(
239 'selected' => $namespace,
240 'all' => '',
241 'label' => $this->msg( 'namespace' )->text()
242 ), array(
243 'name' => 'namespace',
244 'id' => 'namespace',
245 'class' => 'namespaceselector',
246 )
247 )
248 );
249 }
250
251 /**
252 * @param bool $indefOnly
253 * @return string Formatted HTML
254 */
255 protected function getExpiryCheck( $indefOnly ) {
256 return Xml::checkLabel(
257 $this->msg( 'protectedpages-indef' )->text(),
258 'indefonly',
259 'indefonly',
260 $indefOnly
261 ) . "\n";
262 }
263
264 /**
265 * @param bool $cascadeOnly
266 * @return string Formatted HTML
267 */
268 protected function getCascadeCheck( $cascadeOnly ) {
269 return Xml::checkLabel(
270 $this->msg( 'protectedpages-cascade' )->text(),
271 'cascadeonly',
272 'cascadeonly',
273 $cascadeOnly
274 ) . "\n";
275 }
276
277 /**
278 * @param bool $noRedirect
279 * @return string Formatted HTML
280 */
281 protected function getRedirectCheck( $noRedirect ) {
282 return Xml::checkLabel(
283 $this->msg( 'protectedpages-noredirect' )->text(),
284 'noredirect',
285 'noredirect',
286 $noRedirect
287 ) . "\n";
288 }
289
290 /**
291 * @param string $sizetype "min" or "max"
292 * @param mixed $size
293 * @return string Formatted HTML
294 */
295 protected function getSizeLimit( $sizetype, $size ) {
296 $max = $sizetype === 'max';
297
298 return Xml::radioLabel(
299 $this->msg( 'minimum-size' )->text(),
300 'sizetype',
301 'min',
302 'wpmin',
303 !$max
304 ) .
305 '&#160;' .
306 Xml::radioLabel(
307 $this->msg( 'maximum-size' )->text(),
308 'sizetype',
309 'max',
310 'wpmax',
311 $max
312 ) .
313 '&#160;' .
314 Xml::input( 'size', 9, $size, array( 'id' => 'wpsize' ) ) .
315 '&#160;' .
316 Xml::label( $this->msg( 'pagesize' )->text(), 'wpsize' );
317 }
318
319 /**
320 * Creates the input label of the restriction type
321 * @param $pr_type string Protection type
322 * @return string Formatted HTML
323 */
324 protected function getTypeMenu( $pr_type ) {
325 $m = array(); // Temporary array
326 $options = array();
327
328 // First pass to load the log names
329 foreach ( Title::getFilteredRestrictionTypes( true ) as $type ) {
330 // Messages: restriction-edit, restriction-move, restriction-create, restriction-upload
331 $text = $this->msg( "restriction-$type" )->text();
332 $m[$text] = $type;
333 }
334
335 // Third pass generates sorted XHTML content
336 foreach ( $m as $text => $type ) {
337 $selected = ( $type == $pr_type );
338 $options[] = Xml::option( $text, $type, $selected ) . "\n";
339 }
340
341 return "<span style='white-space: nowrap'>" .
342 Xml::label( $this->msg( 'restriction-type' )->text(), $this->IdType ) . '&#160;' .
343 Xml::tags( 'select',
344 array( 'id' => $this->IdType, 'name' => $this->IdType ),
345 implode( "\n", $options ) ) . "</span>";
346 }
347
348 /**
349 * Creates the input label of the restriction level
350 * @param $pr_level string Protection level
351 * @return string Formatted HTML
352 */
353 protected function getLevelMenu( $pr_level ) {
354 global $wgRestrictionLevels;
355
356 // Temporary array
357 $m = array( $this->msg( 'restriction-level-all' )->text() => 0 );
358 $options = array();
359
360 // First pass to load the log names
361 foreach ( $wgRestrictionLevels as $type ) {
362 // Messages used can be 'restriction-level-sysop' and 'restriction-level-autoconfirmed'
363 if ( $type != '' && $type != '*' ) {
364 $text = $this->msg( "restriction-level-$type" )->text();
365 $m[$text] = $type;
366 }
367 }
368
369 // Third pass generates sorted XHTML content
370 foreach ( $m as $text => $type ) {
371 $selected = ( $type == $pr_level );
372 $options[] = Xml::option( $text, $type, $selected );
373 }
374
375 return "<span style='white-space: nowrap'>" .
376 Xml::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel ) . ' ' .
377 Xml::tags( 'select',
378 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
379 implode( "\n", $options ) ) . "</span>";
380 }
381
382 protected function getGroupName() {
383 return 'maintenance';
384 }
385 }
386
387 /**
388 * @todo document
389 * @ingroup Pager
390 */
391 class ProtectedPagesPager extends AlphabeticPager {
392 public $mForm, $mConds;
393 private $type, $level, $namespace, $sizetype, $size, $indefonly, $cascadeonly, $noredirect;
394
395 function __construct( $form, $conds = array(), $type, $level, $namespace,
396 $sizetype = '', $size = 0, $indefonly = false, $cascadeonly = false, $noredirect = false
397 ) {
398 $this->mForm = $form;
399 $this->mConds = $conds;
400 $this->type = ( $type ) ? $type : 'edit';
401 $this->level = $level;
402 $this->namespace = $namespace;
403 $this->sizetype = $sizetype;
404 $this->size = intval( $size );
405 $this->indefonly = (bool)$indefonly;
406 $this->cascadeonly = (bool)$cascadeonly;
407 $this->noredirect = (bool)$noredirect;
408 parent::__construct( $form->getContext() );
409 }
410
411 function getStartBody() {
412 # Do a link batch query
413 $lb = new LinkBatch;
414 foreach ( $this->mResult as $row ) {
415 $lb->add( $row->page_namespace, $row->page_title );
416 }
417 $lb->execute();
418
419 return '';
420 }
421
422 function formatRow( $row ) {
423 return $this->mForm->formatRow( $row );
424 }
425
426 function getQueryInfo() {
427 $conds = $this->mConds;
428 $conds[] = '(pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
429 'OR pr_expiry IS NULL)';
430 $conds[] = 'page_id=pr_page';
431 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
432
433 if ( $this->sizetype == 'min' ) {
434 $conds[] = 'page_len>=' . $this->size;
435 } elseif ( $this->sizetype == 'max' ) {
436 $conds[] = 'page_len<=' . $this->size;
437 }
438
439 if ( $this->indefonly ) {
440 $infinity = $this->mDb->addQuotes( $this->mDb->getInfinity() );
441 $conds[] = "pr_expiry = $infinity OR pr_expiry IS NULL";
442 }
443 if ( $this->cascadeonly ) {
444 $conds[] = 'pr_cascade = 1';
445 }
446 if ( $this->noredirect ) {
447 $conds[] = 'page_is_redirect = 0';
448 }
449
450 if ( $this->level ) {
451 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
452 }
453 if ( !is_null( $this->namespace ) ) {
454 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
455 }
456
457 return array(
458 'tables' => array( 'page_restrictions', 'page' ),
459 'fields' => array( 'pr_id', 'page_namespace', 'page_title', 'page_len',
460 'pr_type', 'pr_level', 'pr_expiry', 'pr_cascade' ),
461 'conds' => $conds
462 );
463 }
464
465 function getIndexField() {
466 return 'pr_id';
467 }
468 }