fix some spacing
[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 $type String: restriction type
150 * @param $level String: restriction level
151 * @param $sizetype String: "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
206 Xml::checkLabel( $this->msg( 'protectedpages-indef' )->text(), 'indefonly', 'indefonly', $indefOnly ) . "\n";
207 }
208
209 /**
210 * @return string Formatted HTML
211 */
212 protected function getCascadeCheck( $cascadeOnly ) {
213 return
214 Xml::checkLabel( $this->msg( 'protectedpages-cascade' )->text(), 'cascadeonly', 'cascadeonly', $cascadeOnly ) . "\n";
215 }
216
217 /**
218 * @return string Formatted HTML
219 */
220 protected function getSizeLimit( $sizetype, $size ) {
221 $max = $sizetype === 'max';
222
223 return
224 Xml::radioLabel( $this->msg( 'minimum-size' )->text(), 'sizetype', 'min', 'wpmin', !$max ) .
225 '&#160;' .
226 Xml::radioLabel( $this->msg( 'maximum-size' )->text(), 'sizetype', 'max', 'wpmax', $max ) .
227 '&#160;' .
228 Xml::input( 'size', 9, $size, array( 'id' => 'wpsize' ) ) .
229 '&#160;' .
230 Xml::label( $this->msg( 'pagesize' )->text(), 'wpsize' );
231 }
232
233 /**
234 * Creates the input label of the restriction type
235 * @param $pr_type string Protection type
236 * @return string Formatted HTML
237 */
238 protected function getTypeMenu( $pr_type ) {
239 $m = array(); // Temporary array
240 $options = array();
241
242 // First pass to load the log names
243 foreach( Title::getFilteredRestrictionTypes( true ) as $type ) {
244 $text = $this->msg( "restriction-$type" )->text();
245 $m[$text] = $type;
246 }
247
248 // Third pass generates sorted XHTML content
249 foreach( $m as $text => $type ) {
250 $selected = ($type == $pr_type );
251 $options[] = Xml::option( $text, $type, $selected ) . "\n";
252 }
253
254 return "<span style='white-space: nowrap'>" .
255 Xml::label( $this->msg( 'restriction-type' )->text(), $this->IdType ) . '&#160;' .
256 Xml::tags( 'select',
257 array( 'id' => $this->IdType, 'name' => $this->IdType ),
258 implode( "\n", $options ) ) . "</span>";
259 }
260
261 /**
262 * Creates the input label of the restriction level
263 * @param $pr_level string Protection level
264 * @return string Formatted HTML
265 */
266 protected function getLevelMenu( $pr_level ) {
267 global $wgRestrictionLevels;
268
269 $m = array( $this->msg( 'restriction-level-all' )->text() => 0 ); // Temporary array
270 $options = array();
271
272 // First pass to load the log names
273 foreach( $wgRestrictionLevels as $type ) {
274 // Messages used can be 'restriction-level-sysop' and 'restriction-level-autoconfirmed'
275 if( $type !='' && $type !='*') {
276 $text = $this->msg( "restriction-level-$type" )->text();
277 $m[$text] = $type;
278 }
279 }
280
281 // Third pass generates sorted XHTML content
282 foreach( $m as $text => $type ) {
283 $selected = ($type == $pr_level );
284 $options[] = Xml::option( $text, $type, $selected );
285 }
286
287 return "<span style='white-space: nowrap'>" .
288 Xml::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel ) . ' ' .
289 Xml::tags( 'select',
290 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
291 implode( "\n", $options ) ) . "</span>";
292 }
293 }
294
295 /**
296 * @todo document
297 * @ingroup Pager
298 */
299 class ProtectedPagesPager extends AlphabeticPager {
300 public $mForm, $mConds;
301 private $type, $level, $namespace, $sizetype, $size, $indefonly;
302
303 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0,
304 $indefonly = false, $cascadeonly = false )
305 {
306 $this->mForm = $form;
307 $this->mConds = $conds;
308 $this->type = ( $type ) ? $type : 'edit';
309 $this->level = $level;
310 $this->namespace = $namespace;
311 $this->sizetype = $sizetype;
312 $this->size = intval($size);
313 $this->indefonly = (bool)$indefonly;
314 $this->cascadeonly = (bool)$cascadeonly;
315 parent::__construct( $form->getContext() );
316 }
317
318 function getStartBody() {
319 # Do a link batch query
320 $lb = new LinkBatch;
321 foreach ( $this->mResult as $row ) {
322 $lb->add( $row->page_namespace, $row->page_title );
323 }
324 $lb->execute();
325 return '';
326 }
327
328 function formatRow( $row ) {
329 return $this->mForm->formatRow( $row );
330 }
331
332 function getQueryInfo() {
333 $conds = $this->mConds;
334 $conds[] = '(pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
335 'OR pr_expiry IS NULL)';
336 $conds[] = 'page_id=pr_page';
337 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
338
339 if( $this->sizetype=='min' ) {
340 $conds[] = 'page_len>=' . $this->size;
341 } elseif( $this->sizetype=='max' ) {
342 $conds[] = 'page_len<=' . $this->size;
343 }
344
345 if( $this->indefonly ) {
346 $conds[] = "pr_expiry = {$this->mDb->addQuotes( $this->mDb->getInfinity() )} OR pr_expiry IS NULL";
347 }
348 if( $this->cascadeonly ) {
349 $conds[] = 'pr_cascade = 1';
350 }
351
352 if( $this->level )
353 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
354 if( !is_null($this->namespace) )
355 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
356 return array(
357 'tables' => array( 'page_restrictions', 'page' ),
358 'fields' => array( 'pr_id', 'page_namespace', 'page_title', 'page_len',
359 'pr_type', 'pr_level', 'pr_expiry', 'pr_cascade' ),
360 'conds' => $conds
361 );
362 }
363
364 function getIndexField() {
365 return 'pr_id';
366 }
367 }