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