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