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