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