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