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