* changed display function for length to Linker::formatRevisionSize
[mediawiki.git] / includes / specials / SpecialProtectedpages.php
bloba67b7e27165ab0aea40242eb89b1ef595dce9bae
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 global $wgOut, $wgRequest;
41 $this->setHeaders();
42 $this->outputHeader();
44 // Purge expired entries on one in every 10 queries
45 if( !mt_rand( 0, 10 ) ) {
46 Title::purgeExpiredRestrictions();
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;
57 $pager = new ProtectedPagesPager( $this, array(), $type, $level, $NS, $sizetype, $size, $indefOnly, $cascadeOnly );
59 $wgOut->addHTML( $this->showOptions( $NS, $type, $level, $sizetype, $size, $indefOnly, $cascadeOnly ) );
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>';
70 $wgOut->addHTML( $s );
73 /**
74 * Callback function to output a restriction
75 * @param $row object Protected title
76 * @return string Formatted <li> element
78 public function formatRow( $row ) {
79 global $wgUser, $wgLang, $wgContLang;
81 wfProfileIn( __METHOD__ );
83 static $skin = null, $infinity = null;
85 if( is_null( $skin ) ){
86 $skin = $wgUser->getSkin();
87 $infinity = wfGetDB( DB_READ )->getInfinity();
90 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
91 $link = $skin->link( $title );
93 $description_items = array ();
95 $protType = wfMsgHtml( 'restriction-level-' . $row->pr_level );
97 $description_items[] = $protType;
99 if( $row->pr_cascade ) {
100 $description_items[] = wfMsg( 'protect-summary-cascade' );
103 $stxt = '';
105 $expiry = $wgLang->formatExpiry( $row->pr_expiry, TS_MW );
106 if( $expiry != $infinity ) {
108 $expiry_description = wfMsg(
109 'protect-expiring',
110 $wgLang->timeanddate( $expiry ),
111 $wgLang->date( $expiry ),
112 $wgLang->time( $expiry )
115 $description_items[] = htmlspecialchars($expiry_description);
118 if(!is_null($size = $row->page_len)) {
119 $stxt = $wgContLang->getDirMark() . ' ' . $skin->formatRevisionSize( $size );
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()
140 ) . ')';
143 wfProfileOut( __METHOD__ );
145 return Html::rawElement(
146 'li',
147 array(),
148 wfSpecialList( $link . $stxt, $wgLang->commaList( $description_items ) ) . $changeProtection ) . "\n";
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
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' );
183 * Prepare the namespace filter drop-down; standard namespace
184 * selector, sans the MediaWiki namespace
186 * @param $namespace Mixed: pre-select namespace
187 * @return String
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>";
196 * @return string Formatted HTML
198 protected function getExpiryCheck( $indefOnly ) {
199 return
200 Xml::checkLabel( wfMsg('protectedpages-indef'), 'indefonly', 'indefonly', $indefOnly ) . "\n";
204 * @return string Formatted HTML
206 protected function getCascadeCheck( $cascadeOnly ) {
207 return
208 Xml::checkLabel( wfMsg('protectedpages-cascade'), 'cascadeonly', 'cascadeonly', $cascadeOnly ) . "\n";
212 * @return string Formatted HTML
214 protected function getSizeLimit( $sizetype, $size ) {
215 $max = $sizetype === 'max';
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' );
228 * Creates the input label of the restriction type
229 * @param $pr_type string Protection type
230 * @return string Formatted HTML
232 protected function getTypeMenu( $pr_type ) {
233 $m = array(); // Temporary array
234 $options = array();
236 // First pass to load the log names
237 foreach( Title::getFilteredRestrictionTypes( true ) as $type ) {
238 $text = wfMsg("restriction-$type");
239 $m[$text] = $type;
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";
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>";
256 * Creates the input label of the restriction level
257 * @param $pr_level string Protection level
258 * @return string Formatted HTML
260 protected function getLevelMenu( $pr_level ) {
261 global $wgRestrictionLevels;
263 $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
264 $options = array();
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;
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 );
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>";
290 * @todo document
291 * @ingroup Pager
293 class ProtectedPagesPager extends AlphabeticPager {
294 public $mForm, $mConds;
295 private $type, $level, $namespace, $sizetype, $size, $indefonly;
297 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0,
298 $indefonly = false, $cascadeonly = false )
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();
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 );
318 $lb->execute();
319 return '';
322 function formatRow( $row ) {
323 return $this->mForm->formatRow( $row );
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 );
333 if( $this->sizetype=='min' ) {
334 $conds[] = 'page_len>=' . $this->size;
335 } else if( $this->sizetype=='max' ) {
336 $conds[] = 'page_len<=' . $this->size;
339 if( $this->indefonly ) {
340 $db = wfGetDB( DB_SLAVE );
341 $conds[] = "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL";
343 if( $this->cascadeonly ) {
344 $conds[] = "pr_cascade = '1'";
347 if( $this->level )
348 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
349 if( !is_null($this->namespace) )
350 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
351 return array(
352 'tables' => array( 'page_restrictions', 'page' ),
353 'fields' => 'pr_id,page_namespace,page_title,page_len,pr_type,pr_level,pr_expiry,pr_cascade',
354 'conds' => $conds
358 function getIndexField() {
359 return 'pr_id';