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