moved release notes for r113737 to 1.19 (a 1.19-regression fix)
[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->getLanguage();
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 $changeProtection = ' ' . $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped();
141
142 wfProfileOut( __METHOD__ );
143
144 return Html::rawElement(
145 'li',
146 array(),
147 $lang->specialList( $link . $stxt, $lang->commaList( $description_items ), false ) . $changeProtection ) . "\n";
148 }
149
150 /**
151 * @param $namespace Integer
152 * @param $type String: restriction type
153 * @param $level String: restriction level
154 * @param $sizetype String: "min" or "max"
155 * @param $size Integer
156 * @param $indefOnly Boolean: only indefinie protection
157 * @param $cascadeOnly Boolean: only cascading protection
158 * @return String: input form
159 */
160 protected function showOptions( $namespace, $type='edit', $level, $sizetype, $size, $indefOnly, $cascadeOnly ) {
161 global $wgScript;
162 $title = $this->getTitle();
163 return Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
164 Xml::openElement( 'fieldset' ) .
165 Xml::element( 'legend', array(), wfMsg( 'protectedpages' ) ) .
166 Html::hidden( 'title', $title->getPrefixedDBkey() ) . "\n" .
167 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
168 $this->getTypeMenu( $type ) . "&#160;\n" .
169 $this->getLevelMenu( $level ) . "&#160;\n" .
170 "<br /><span style='white-space: nowrap'>" .
171 $this->getExpiryCheck( $indefOnly ) . "&#160;\n" .
172 $this->getCascadeCheck( $cascadeOnly ) . "&#160;\n" .
173 "</span><br /><span style='white-space: nowrap'>" .
174 $this->getSizeLimit( $sizetype, $size ) . "&#160;\n" .
175 "</span>" .
176 "&#160;" . Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
177 Xml::closeElement( 'fieldset' ) .
178 Xml::closeElement( 'form' );
179 }
180
181 /**
182 * Prepare the namespace filter drop-down; standard namespace
183 * selector, sans the MediaWiki namespace
184 *
185 * @param $namespace Mixed: pre-select namespace
186 * @return String
187 */
188 protected function getNamespaceMenu( $namespace = null ) {
189 return "<span style='white-space: nowrap'>" .
190 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;'
191 . Xml::namespaceSelector( $namespace, '' ) . "</span>";
192 }
193
194 /**
195 * @return string Formatted HTML
196 */
197 protected function getExpiryCheck( $indefOnly ) {
198 return
199 Xml::checkLabel( wfMsg('protectedpages-indef'), 'indefonly', 'indefonly', $indefOnly ) . "\n";
200 }
201
202 /**
203 * @return string Formatted HTML
204 */
205 protected function getCascadeCheck( $cascadeOnly ) {
206 return
207 Xml::checkLabel( wfMsg('protectedpages-cascade'), 'cascadeonly', 'cascadeonly', $cascadeOnly ) . "\n";
208 }
209
210 /**
211 * @return string Formatted HTML
212 */
213 protected function getSizeLimit( $sizetype, $size ) {
214 $max = $sizetype === 'max';
215
216 return
217 Xml::radioLabel( wfMsg('minimum-size'), 'sizetype', 'min', 'wpmin', !$max ) .
218 '&#160;' .
219 Xml::radioLabel( wfMsg('maximum-size'), 'sizetype', 'max', 'wpmax', $max ) .
220 '&#160;' .
221 Xml::input( 'size', 9, $size, array( 'id' => 'wpsize' ) ) .
222 '&#160;' .
223 Xml::label( wfMsg('pagesize'), 'wpsize' );
224 }
225
226 /**
227 * Creates the input label of the restriction type
228 * @param $pr_type string Protection type
229 * @return string Formatted HTML
230 */
231 protected function getTypeMenu( $pr_type ) {
232 $m = array(); // Temporary array
233 $options = array();
234
235 // First pass to load the log names
236 foreach( Title::getFilteredRestrictionTypes( true ) as $type ) {
237 $text = wfMsg("restriction-$type");
238 $m[$text] = $type;
239 }
240
241 // Third pass generates sorted XHTML content
242 foreach( $m as $text => $type ) {
243 $selected = ($type == $pr_type );
244 $options[] = Xml::option( $text, $type, $selected ) . "\n";
245 }
246
247 return "<span style='white-space: nowrap'>" .
248 Xml::label( wfMsg('restriction-type') , $this->IdType ) . '&#160;' .
249 Xml::tags( 'select',
250 array( 'id' => $this->IdType, 'name' => $this->IdType ),
251 implode( "\n", $options ) ) . "</span>";
252 }
253
254 /**
255 * Creates the input label of the restriction level
256 * @param $pr_level string Protection level
257 * @return string Formatted HTML
258 */
259 protected function getLevelMenu( $pr_level ) {
260 global $wgRestrictionLevels;
261
262 $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
263 $options = array();
264
265 // First pass to load the log names
266 foreach( $wgRestrictionLevels as $type ) {
267 // Messages used can be 'restriction-level-sysop' and 'restriction-level-autoconfirmed'
268 if( $type !='' && $type !='*') {
269 $text = wfMsg("restriction-level-$type");
270 $m[$text] = $type;
271 }
272 }
273
274 // Third pass generates sorted XHTML content
275 foreach( $m as $text => $type ) {
276 $selected = ($type == $pr_level );
277 $options[] = Xml::option( $text, $type, $selected );
278 }
279
280 return "<span style='white-space: nowrap'>" .
281 Xml::label( wfMsg( 'restriction-level' ) , $this->IdLevel ) . ' ' .
282 Xml::tags( 'select',
283 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
284 implode( "\n", $options ) ) . "</span>";
285 }
286 }
287
288 /**
289 * @todo document
290 * @ingroup Pager
291 */
292 class ProtectedPagesPager extends AlphabeticPager {
293 public $mForm, $mConds;
294 private $type, $level, $namespace, $sizetype, $size, $indefonly;
295
296 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0,
297 $indefonly = false, $cascadeonly = false )
298 {
299 $this->mForm = $form;
300 $this->mConds = $conds;
301 $this->type = ( $type ) ? $type : 'edit';
302 $this->level = $level;
303 $this->namespace = $namespace;
304 $this->sizetype = $sizetype;
305 $this->size = intval($size);
306 $this->indefonly = (bool)$indefonly;
307 $this->cascadeonly = (bool)$cascadeonly;
308 parent::__construct( $form->getContext() );
309 }
310
311 function getStartBody() {
312 # Do a link batch query
313 $lb = new LinkBatch;
314 foreach ( $this->mResult as $row ) {
315 $lb->add( $row->page_namespace, $row->page_title );
316 }
317 $lb->execute();
318 return '';
319 }
320
321 function formatRow( $row ) {
322 return $this->mForm->formatRow( $row );
323 }
324
325 function getQueryInfo() {
326 $conds = $this->mConds;
327 $conds[] = '(pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
328 'OR pr_expiry IS NULL)';
329 $conds[] = 'page_id=pr_page';
330 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
331
332 if( $this->sizetype=='min' ) {
333 $conds[] = 'page_len>=' . $this->size;
334 } elseif( $this->sizetype=='max' ) {
335 $conds[] = 'page_len<=' . $this->size;
336 }
337
338 if( $this->indefonly ) {
339 $db = wfGetDB( DB_SLAVE );
340 $conds[] = "pr_expiry = {$db->addQuotes( $db->getInfinity() )} 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 }