* Added specific page header when showing "search deleted pages" form
[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 $s = $pager->getNavigationBar();
62 $s .= "<ul>" .
63 $pager->getBody() .
64 "</ul>";
65 $s .= $pager->getNavigationBar();
66 } else {
67 $s = '<p>' . wfMsgHtml( 'protectedpagesempty' ) . '</p>';
68 }
69 $this->getOutput()->addHTML( $s );
70 }
71
72 /**
73 * Callback function to output a restriction
74 * @param $row object Protected title
75 * @return string Formatted <li> element
76 */
77 public function formatRow( $row ) {
78 wfProfileIn( __METHOD__ );
79
80 static $infinity = null;
81
82 if( is_null( $infinity ) ){
83 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
84 }
85
86 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
87 $link = Linker::link( $title );
88
89 $description_items = array ();
90
91 $protType = wfMsgHtml( 'restriction-level-' . $row->pr_level );
92
93 $description_items[] = $protType;
94
95 if( $row->pr_cascade ) {
96 $description_items[] = wfMsg( 'protect-summary-cascade' );
97 }
98
99 $stxt = '';
100 $lang = $this->getLang();
101
102 $expiry = $lang->formatExpiry( $row->pr_expiry, TS_MW );
103 if( $expiry != $infinity ) {
104
105 $expiry_description = wfMsg(
106 'protect-expiring-local',
107 $lang->timeanddate( $expiry, true ),
108 $lang->date( $expiry, true ),
109 $lang->time( $expiry, true )
110 );
111
112 $description_items[] = htmlspecialchars($expiry_description);
113 }
114
115 if(!is_null($size = $row->page_len)) {
116 $stxt = $lang->getDirMark() . ' ' . Linker::formatRevisionSize( $size );
117 }
118
119 # Show a link to the change protection form for allowed users otherwise a link to the protection log
120 if( $this->getUser()->isAllowed( 'protect' ) ) {
121 $changeProtection = ' (' . Linker::linkKnown(
122 $title,
123 wfMsgHtml( 'protect_change' ),
124 array(),
125 array( 'action' => 'unprotect' )
126 ) . ')';
127 } else {
128 $ltitle = SpecialPage::getTitleFor( 'Log' );
129 $changeProtection = ' (' . Linker::linkKnown(
130 $ltitle,
131 wfMsgHtml( 'protectlogpage' ),
132 array(),
133 array(
134 'type' => 'protect',
135 'page' => $title->getPrefixedText()
136 )
137 ) . ')';
138 }
139
140 wfProfileOut( __METHOD__ );
141
142 return Html::rawElement(
143 'li',
144 array(),
145 $lang->specialList( $link . $stxt, $lang->commaList( $description_items ), false ) . $changeProtection ) . "\n";
146 }
147
148 /**
149 * @param $namespace Integer
150 * @param $type String: restriction type
151 * @param $level String: restriction level
152 * @param $sizetype String: "min" or "max"
153 * @param $size Integer
154 * @param $indefOnly Boolean: only indefinie protection
155 * @param $cascadeOnly Boolean: only cascading protection
156 * @return String: input form
157 */
158 protected function showOptions( $namespace, $type='edit', $level, $sizetype, $size, $indefOnly, $cascadeOnly ) {
159 global $wgScript;
160 $title = $this->getTitle();
161 return Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
162 Xml::openElement( 'fieldset' ) .
163 Xml::element( 'legend', array(), wfMsg( 'protectedpages' ) ) .
164 Html::hidden( 'title', $title->getPrefixedDBkey() ) . "\n" .
165 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
166 $this->getTypeMenu( $type ) . "&#160;\n" .
167 $this->getLevelMenu( $level ) . "&#160;\n" .
168 "<br /><span style='white-space: nowrap'>" .
169 $this->getExpiryCheck( $indefOnly ) . "&#160;\n" .
170 $this->getCascadeCheck( $cascadeOnly ) . "&#160;\n" .
171 "</span><br /><span style='white-space: nowrap'>" .
172 $this->getSizeLimit( $sizetype, $size ) . "&#160;\n" .
173 "</span>" .
174 "&#160;" . Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
175 Xml::closeElement( 'fieldset' ) .
176 Xml::closeElement( 'form' );
177 }
178
179 /**
180 * Prepare the namespace filter drop-down; standard namespace
181 * selector, sans the MediaWiki namespace
182 *
183 * @param $namespace Mixed: pre-select namespace
184 * @return String
185 */
186 protected function getNamespaceMenu( $namespace = null ) {
187 return "<span style='white-space: nowrap'>" .
188 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;'
189 . Xml::namespaceSelector( $namespace, '' ) . "</span>";
190 }
191
192 /**
193 * @return string Formatted HTML
194 */
195 protected function getExpiryCheck( $indefOnly ) {
196 return
197 Xml::checkLabel( wfMsg('protectedpages-indef'), 'indefonly', 'indefonly', $indefOnly ) . "\n";
198 }
199
200 /**
201 * @return string Formatted HTML
202 */
203 protected function getCascadeCheck( $cascadeOnly ) {
204 return
205 Xml::checkLabel( wfMsg('protectedpages-cascade'), 'cascadeonly', 'cascadeonly', $cascadeOnly ) . "\n";
206 }
207
208 /**
209 * @return string Formatted HTML
210 */
211 protected function getSizeLimit( $sizetype, $size ) {
212 $max = $sizetype === 'max';
213
214 return
215 Xml::radioLabel( wfMsg('minimum-size'), 'sizetype', 'min', 'wpmin', !$max ) .
216 '&#160;' .
217 Xml::radioLabel( wfMsg('maximum-size'), 'sizetype', 'max', 'wpmax', $max ) .
218 '&#160;' .
219 Xml::input( 'size', 9, $size, array( 'id' => 'wpsize' ) ) .
220 '&#160;' .
221 Xml::label( wfMsg('pagesize'), 'wpsize' );
222 }
223
224 /**
225 * Creates the input label of the restriction type
226 * @param $pr_type string Protection type
227 * @return string Formatted HTML
228 */
229 protected function getTypeMenu( $pr_type ) {
230 $m = array(); // Temporary array
231 $options = array();
232
233 // First pass to load the log names
234 foreach( Title::getFilteredRestrictionTypes( true ) 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( $form->getContext() );
307 }
308
309 function getStartBody() {
310 # Do a link batch query
311 $lb = new LinkBatch;
312 foreach ( $this->mResult as $row ) {
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 } elseif( $this->sizetype=='max' ) {
333 $conds[] = 'page_len<=' . $this->size;
334 }
335
336 if( $this->indefonly ) {
337 $db = wfGetDB( DB_SLAVE );
338 $conds[] = "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL";
339 }
340 if( $this->cascadeonly ) {
341 $conds[] = "pr_cascade = '1'";
342 }
343
344 if( $this->level )
345 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
346 if( !is_null($this->namespace) )
347 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
348 return array(
349 'tables' => array( 'page_restrictions', 'page' ),
350 'fields' => 'pr_id,page_namespace,page_title,page_len,pr_type,pr_level,pr_expiry,pr_cascade',
351 'conds' => $conds
352 );
353 }
354
355 function getIndexField() {
356 return 'pr_id';
357 }
358 }