Add a way for packagers to override some installation details
[mediawiki.git] / includes / specials / SpecialProtectedpages.php
blobd8fdbe7b90ffd2e6eb5948a00b5f67ac56b3a248
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 $this->getOutput()->addHTML(
62 $pager->getNavigationBar() .
63 '<ul>' . $pager->getBody() . '</ul>' .
64 $pager->getNavigationBar()
66 } else {
67 $this->getOutput()->addWikiMsg( 'protectedpagesempty' );
71 /**
72 * Callback function to output a restriction
73 * @param $row object Protected title
74 * @return string Formatted <li> element
76 public function formatRow( $row ) {
77 wfProfileIn( __METHOD__ );
79 static $infinity = null;
81 if( is_null( $infinity ) ){
82 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
85 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
86 $link = Linker::link( $title );
88 $description_items = array ();
90 $protType = $this->msg( 'restriction-level-' . $row->pr_level )->escaped();
92 $description_items[] = $protType;
94 if( $row->pr_cascade ) {
95 $description_items[] = $this->msg( 'protect-summary-cascade' )->text();
98 $stxt = '';
99 $lang = $this->getLanguage();
101 $expiry = $lang->formatExpiry( $row->pr_expiry, TS_MW );
102 if( $expiry != $infinity ) {
103 $user = $this->getUser();
104 $description_items[] = $this->msg(
105 'protect-expiring-local',
106 $lang->userTimeAndDate( $expiry, $user ),
107 $lang->userDate( $expiry, $user ),
108 $lang->userTime( $expiry, $user )
109 )->escaped();
112 if(!is_null($size = $row->page_len)) {
113 $stxt = $lang->getDirMark() . ' ' . Linker::formatRevisionSize( $size );
116 # Show a link to the change protection form for allowed users otherwise a link to the protection log
117 if( $this->getUser()->isAllowed( 'protect' ) ) {
118 $changeProtection = Linker::linkKnown(
119 $title,
120 $this->msg( 'protect_change' )->escaped(),
121 array(),
122 array( 'action' => 'unprotect' )
124 } else {
125 $ltitle = SpecialPage::getTitleFor( 'Log' );
126 $changeProtection = Linker::linkKnown(
127 $ltitle,
128 $this->msg( 'protectlogpage' )->escaped(),
129 array(),
130 array(
131 'type' => 'protect',
132 'page' => $title->getPrefixedText()
137 $changeProtection = ' ' . $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped();
139 wfProfileOut( __METHOD__ );
141 return Html::rawElement(
142 'li',
143 array(),
144 $lang->specialList( $link . $stxt, $lang->commaList( $description_items ), false ) . $changeProtection ) . "\n";
148 * @param $namespace Integer
149 * @param $type String: restriction type
150 * @param $level String: restriction level
151 * @param $sizetype String: "min" or "max"
152 * @param $size Integer
153 * @param $indefOnly Boolean: only indefinie protection
154 * @param $cascadeOnly Boolean: only cascading protection
155 * @return String: input form
157 protected function showOptions( $namespace, $type='edit', $level, $sizetype, $size, $indefOnly, $cascadeOnly ) {
158 global $wgScript;
159 $title = $this->getTitle();
160 return Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
161 Xml::openElement( 'fieldset' ) .
162 Xml::element( 'legend', array(), $this->msg( 'protectedpages' )->text() ) .
163 Html::hidden( 'title', $title->getPrefixedDBkey() ) . "\n" .
164 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
165 $this->getTypeMenu( $type ) . "&#160;\n" .
166 $this->getLevelMenu( $level ) . "&#160;\n" .
167 "<br /><span style='white-space: nowrap'>" .
168 $this->getExpiryCheck( $indefOnly ) . "&#160;\n" .
169 $this->getCascadeCheck( $cascadeOnly ) . "&#160;\n" .
170 "</span><br /><span style='white-space: nowrap'>" .
171 $this->getSizeLimit( $sizetype, $size ) . "&#160;\n" .
172 "</span>" .
173 "&#160;" . Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
174 Xml::closeElement( 'fieldset' ) .
175 Xml::closeElement( 'form' );
179 * Prepare the namespace filter drop-down; standard namespace
180 * selector, sans the MediaWiki namespace
182 * @param $namespace Mixed: pre-select namespace
183 * @return String
185 protected function getNamespaceMenu( $namespace = null ) {
186 return Html::rawElement( 'span', array( 'style' => 'white-space: nowrap;' ),
187 Html::namespaceSelector(
188 array(
189 'selected' => $namespace,
190 'all' => '',
191 'label' => $this->msg( 'namespace' )->text()
192 ), array(
193 'name' => 'namespace',
194 'id' => 'namespace',
195 'class' => 'namespaceselector',
202 * @return string Formatted HTML
204 protected function getExpiryCheck( $indefOnly ) {
205 return
206 Xml::checkLabel( $this->msg( 'protectedpages-indef' )->text(), 'indefonly', 'indefonly', $indefOnly ) . "\n";
210 * @return string Formatted HTML
212 protected function getCascadeCheck( $cascadeOnly ) {
213 return
214 Xml::checkLabel( $this->msg( 'protectedpages-cascade' )->text(), 'cascadeonly', 'cascadeonly', $cascadeOnly ) . "\n";
218 * @return string Formatted HTML
220 protected function getSizeLimit( $sizetype, $size ) {
221 $max = $sizetype === 'max';
223 return
224 Xml::radioLabel( $this->msg( 'minimum-size' )->text(), 'sizetype', 'min', 'wpmin', !$max ) .
225 '&#160;' .
226 Xml::radioLabel( $this->msg( 'maximum-size' )->text(), 'sizetype', 'max', 'wpmax', $max ) .
227 '&#160;' .
228 Xml::input( 'size', 9, $size, array( 'id' => 'wpsize' ) ) .
229 '&#160;' .
230 Xml::label( $this->msg( 'pagesize' )->text(), 'wpsize' );
234 * Creates the input label of the restriction type
235 * @param $pr_type string Protection type
236 * @return string Formatted HTML
238 protected function getTypeMenu( $pr_type ) {
239 $m = array(); // Temporary array
240 $options = array();
242 // First pass to load the log names
243 foreach( Title::getFilteredRestrictionTypes( true ) as $type ) {
244 $text = $this->msg( "restriction-$type" )->text();
245 $m[$text] = $type;
248 // Third pass generates sorted XHTML content
249 foreach( $m as $text => $type ) {
250 $selected = ($type == $pr_type );
251 $options[] = Xml::option( $text, $type, $selected ) . "\n";
254 return "<span style='white-space: nowrap'>" .
255 Xml::label( $this->msg( 'restriction-type' )->text(), $this->IdType ) . '&#160;' .
256 Xml::tags( 'select',
257 array( 'id' => $this->IdType, 'name' => $this->IdType ),
258 implode( "\n", $options ) ) . "</span>";
262 * Creates the input label of the restriction level
263 * @param $pr_level string Protection level
264 * @return string Formatted HTML
266 protected function getLevelMenu( $pr_level ) {
267 global $wgRestrictionLevels;
269 $m = array( $this->msg( 'restriction-level-all' )->text() => 0 ); // Temporary array
270 $options = array();
272 // First pass to load the log names
273 foreach( $wgRestrictionLevels as $type ) {
274 // Messages used can be 'restriction-level-sysop' and 'restriction-level-autoconfirmed'
275 if( $type !='' && $type !='*') {
276 $text = $this->msg( "restriction-level-$type" )->text();
277 $m[$text] = $type;
281 // Third pass generates sorted XHTML content
282 foreach( $m as $text => $type ) {
283 $selected = ($type == $pr_level );
284 $options[] = Xml::option( $text, $type, $selected );
287 return "<span style='white-space: nowrap'>" .
288 Xml::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel ) . ' ' .
289 Xml::tags( 'select',
290 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
291 implode( "\n", $options ) ) . "</span>";
296 * @todo document
297 * @ingroup Pager
299 class ProtectedPagesPager extends AlphabeticPager {
300 public $mForm, $mConds;
301 private $type, $level, $namespace, $sizetype, $size, $indefonly;
303 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0,
304 $indefonly = false, $cascadeonly = false )
306 $this->mForm = $form;
307 $this->mConds = $conds;
308 $this->type = ( $type ) ? $type : 'edit';
309 $this->level = $level;
310 $this->namespace = $namespace;
311 $this->sizetype = $sizetype;
312 $this->size = intval($size);
313 $this->indefonly = (bool)$indefonly;
314 $this->cascadeonly = (bool)$cascadeonly;
315 parent::__construct( $form->getContext() );
318 function getStartBody() {
319 # Do a link batch query
320 $lb = new LinkBatch;
321 foreach ( $this->mResult as $row ) {
322 $lb->add( $row->page_namespace, $row->page_title );
324 $lb->execute();
325 return '';
328 function formatRow( $row ) {
329 return $this->mForm->formatRow( $row );
332 function getQueryInfo() {
333 $conds = $this->mConds;
334 $conds[] = '(pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
335 'OR pr_expiry IS NULL)';
336 $conds[] = 'page_id=pr_page';
337 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
339 if( $this->sizetype=='min' ) {
340 $conds[] = 'page_len>=' . $this->size;
341 } elseif( $this->sizetype=='max' ) {
342 $conds[] = 'page_len<=' . $this->size;
345 if( $this->indefonly ) {
346 $db = wfGetDB( DB_SLAVE );
347 $conds[] = "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL";
349 if( $this->cascadeonly ) {
350 $conds[] = "pr_cascade = '1'";
353 if( $this->level )
354 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
355 if( !is_null($this->namespace) )
356 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
357 return array(
358 'tables' => array( 'page_restrictions', 'page' ),
359 'fields' => 'pr_id,page_namespace,page_title,page_len,pr_type,pr_level,pr_expiry,pr_cascade',
360 'conds' => $conds
364 function getIndexField() {
365 return 'pr_id';