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