(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / specials / SpecialProtectedpages.php
blob30c57eb67ac97b108ba6afd071130c595529e2c7
1 <?php
2 /**
3 * Implements Special:Protectedpages
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * A special page that lists protected pages
27 * @ingroup SpecialPage
29 class SpecialProtectedpages extends SpecialPage {
31 protected $IdLevel = 'level';
32 protected $IdType = 'type';
34 public function __construct() {
35 parent::__construct( 'Protectedpages' );
38 public function execute( $par ) {
39 $this->setHeaders();
40 $this->outputHeader();
42 // Purge expired entries on one in every 10 queries
43 if( !mt_rand( 0, 10 ) ) {
44 Title::purgeExpiredRestrictions();
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;
56 $pager = new ProtectedPagesPager( $this, array(), $type, $level, $NS, $sizetype, $size, $indefOnly, $cascadeOnly );
58 $this->getOutput()->addHTML( $this->showOptions( $NS, $type, $level, $sizetype, $size, $indefOnly, $cascadeOnly ) );
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>';
69 $this->getOutput()->addHTML( $s );
72 /**
73 * Callback function to output a restriction
74 * @param $row object Protected title
75 * @return string Formatted <li> element
77 public function formatRow( $row ) {
78 wfProfileIn( __METHOD__ );
80 static $infinity = null;
82 if( is_null( $infinity ) ){
83 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
86 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
87 $link = Linker::link( $title );
89 $description_items = array ();
91 $protType = wfMsgHtml( 'restriction-level-' . $row->pr_level );
93 $description_items[] = $protType;
95 if( $row->pr_cascade ) {
96 $description_items[] = wfMsg( 'protect-summary-cascade' );
99 $stxt = '';
100 $lang = $this->getLanguage();
102 $expiry = $lang->formatExpiry( $row->pr_expiry, TS_MW );
103 if( $expiry != $infinity ) {
105 $expiry_description = wfMsg(
106 'protect-expiring-local',
107 $lang->timeanddate( $expiry, true ),
108 $lang->date( $expiry, true ),
109 $lang->time( $expiry, true )
112 $description_items[] = htmlspecialchars($expiry_description);
115 if(!is_null($size = $row->page_len)) {
116 $stxt = $lang->getDirMark() . ' ' . Linker::formatRevisionSize( $size );
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' )
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()
140 $changeProtection = ' ' . $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped();
142 wfProfileOut( __METHOD__ );
144 return Html::rawElement(
145 'li',
146 array(),
147 $lang->specialList( $link . $stxt, $lang->commaList( $description_items ), false ) . $changeProtection ) . "\n";
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
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' );
182 * Prepare the namespace filter drop-down; standard namespace
183 * selector, sans the MediaWiki namespace
185 * @param $namespace Mixed: pre-select namespace
186 * @return String
188 protected function getNamespaceMenu( $namespace = null ) {
189 return Html::rawElement( 'span', array( 'style' => 'white-space: nowrap;' ),
190 Html::namespaceSelector(
191 array(
192 'selected' => $namespace,
193 'all' => '',
194 'label' => $this->msg( 'namespace' )->text()
195 ), array(
196 'name' => 'namespace',
197 'id' => 'namespace',
198 'class' => 'namespaceselector',
205 * @return string Formatted HTML
207 protected function getExpiryCheck( $indefOnly ) {
208 return
209 Xml::checkLabel( wfMsg('protectedpages-indef'), 'indefonly', 'indefonly', $indefOnly ) . "\n";
213 * @return string Formatted HTML
215 protected function getCascadeCheck( $cascadeOnly ) {
216 return
217 Xml::checkLabel( wfMsg('protectedpages-cascade'), 'cascadeonly', 'cascadeonly', $cascadeOnly ) . "\n";
221 * @return string Formatted HTML
223 protected function getSizeLimit( $sizetype, $size ) {
224 $max = $sizetype === 'max';
226 return
227 Xml::radioLabel( wfMsg('minimum-size'), 'sizetype', 'min', 'wpmin', !$max ) .
228 '&#160;' .
229 Xml::radioLabel( wfMsg('maximum-size'), 'sizetype', 'max', 'wpmax', $max ) .
230 '&#160;' .
231 Xml::input( 'size', 9, $size, array( 'id' => 'wpsize' ) ) .
232 '&#160;' .
233 Xml::label( wfMsg('pagesize'), 'wpsize' );
237 * Creates the input label of the restriction type
238 * @param $pr_type string Protection type
239 * @return string Formatted HTML
241 protected function getTypeMenu( $pr_type ) {
242 $m = array(); // Temporary array
243 $options = array();
245 // First pass to load the log names
246 foreach( Title::getFilteredRestrictionTypes( true ) as $type ) {
247 $text = wfMsg("restriction-$type");
248 $m[$text] = $type;
251 // Third pass generates sorted XHTML content
252 foreach( $m as $text => $type ) {
253 $selected = ($type == $pr_type );
254 $options[] = Xml::option( $text, $type, $selected ) . "\n";
257 return "<span style='white-space: nowrap'>" .
258 Xml::label( wfMsg('restriction-type') , $this->IdType ) . '&#160;' .
259 Xml::tags( 'select',
260 array( 'id' => $this->IdType, 'name' => $this->IdType ),
261 implode( "\n", $options ) ) . "</span>";
265 * Creates the input label of the restriction level
266 * @param $pr_level string Protection level
267 * @return string Formatted HTML
269 protected function getLevelMenu( $pr_level ) {
270 global $wgRestrictionLevels;
272 $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
273 $options = array();
275 // First pass to load the log names
276 foreach( $wgRestrictionLevels as $type ) {
277 // Messages used can be 'restriction-level-sysop' and 'restriction-level-autoconfirmed'
278 if( $type !='' && $type !='*') {
279 $text = wfMsg("restriction-level-$type");
280 $m[$text] = $type;
284 // Third pass generates sorted XHTML content
285 foreach( $m as $text => $type ) {
286 $selected = ($type == $pr_level );
287 $options[] = Xml::option( $text, $type, $selected );
290 return "<span style='white-space: nowrap'>" .
291 Xml::label( wfMsg( 'restriction-level' ) , $this->IdLevel ) . ' ' .
292 Xml::tags( 'select',
293 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
294 implode( "\n", $options ) ) . "</span>";
299 * @todo document
300 * @ingroup Pager
302 class ProtectedPagesPager extends AlphabeticPager {
303 public $mForm, $mConds;
304 private $type, $level, $namespace, $sizetype, $size, $indefonly;
306 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0,
307 $indefonly = false, $cascadeonly = false )
309 $this->mForm = $form;
310 $this->mConds = $conds;
311 $this->type = ( $type ) ? $type : 'edit';
312 $this->level = $level;
313 $this->namespace = $namespace;
314 $this->sizetype = $sizetype;
315 $this->size = intval($size);
316 $this->indefonly = (bool)$indefonly;
317 $this->cascadeonly = (bool)$cascadeonly;
318 parent::__construct( $form->getContext() );
321 function getStartBody() {
322 # Do a link batch query
323 $lb = new LinkBatch;
324 foreach ( $this->mResult as $row ) {
325 $lb->add( $row->page_namespace, $row->page_title );
327 $lb->execute();
328 return '';
331 function formatRow( $row ) {
332 return $this->mForm->formatRow( $row );
335 function getQueryInfo() {
336 $conds = $this->mConds;
337 $conds[] = '(pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
338 'OR pr_expiry IS NULL)';
339 $conds[] = 'page_id=pr_page';
340 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
342 if( $this->sizetype=='min' ) {
343 $conds[] = 'page_len>=' . $this->size;
344 } elseif( $this->sizetype=='max' ) {
345 $conds[] = 'page_len<=' . $this->size;
348 if( $this->indefonly ) {
349 $db = wfGetDB( DB_SLAVE );
350 $conds[] = "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL";
352 if( $this->cascadeonly ) {
353 $conds[] = "pr_cascade = '1'";
356 if( $this->level )
357 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
358 if( !is_null($this->namespace) )
359 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
360 return array(
361 'tables' => array( 'page_restrictions', 'page' ),
362 'fields' => 'pr_id,page_namespace,page_title,page_len,pr_type,pr_level,pr_expiry,pr_cascade',
363 'conds' => $conds
367 function getIndexField() {
368 return 'pr_id';