Merge "Set lang in api createaccount regardless of $wgLoginLanguageSelector"
[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 $this->getOutput()->addHTML(
62 $pager->getNavigationBar() .
63 '<ul>' . $pager->getBody() . '</ul>' .
64 $pager->getNavigationBar()
65 );
66 } else {
67 $this->getOutput()->addWikiMsg( 'protectedpagesempty' );
68 }
69 }
70
71 /**
72 * Callback function to output a restriction
73 * @param Title $row Protected title
74 * @return string Formatted "<li>" element
75 */
76 public function formatRow( $row ) {
77 wfProfileIn( __METHOD__ );
78
79 static $infinity = null;
80
81 if( is_null( $infinity ) ) {
82 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
83 }
84
85 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
86 if( !$title ) {
87 wfProfileOut( __METHOD__ );
88 return Html::rawElement( 'li', array(),
89 Html::element( 'span', array( 'class' => 'mw-invalidtitle' ),
90 Linker::getInvalidTitleDescription( $this->getContext(), $row->page_namespace, $row->page_title ) ) ) . "\n";
91 }
92
93 $link = Linker::link( $title );
94
95 $description_items = array ();
96
97 $protType = $this->msg( 'restriction-level-' . $row->pr_level )->escaped();
98
99 $description_items[] = $protType;
100
101 if( $row->pr_cascade ) {
102 $description_items[] = $this->msg( 'protect-summary-cascade' )->text();
103 }
104
105 $stxt = '';
106 $lang = $this->getLanguage();
107
108 $expiry = $lang->formatExpiry( $row->pr_expiry, TS_MW );
109 if( $expiry != $infinity ) {
110 $user = $this->getUser();
111 $description_items[] = $this->msg(
112 'protect-expiring-local',
113 $lang->userTimeAndDate( $expiry, $user ),
114 $lang->userDate( $expiry, $user ),
115 $lang->userTime( $expiry, $user )
116 )->escaped();
117 }
118
119 if( !is_null( $size = $row->page_len ) ) {
120 $stxt = $lang->getDirMark() . ' ' . Linker::formatRevisionSize( $size );
121 }
122
123 # Show a link to the change protection form for allowed users otherwise a link to the protection log
124 if( $this->getUser()->isAllowed( 'protect' ) ) {
125 $changeProtection = Linker::linkKnown(
126 $title,
127 $this->msg( 'protect_change' )->escaped(),
128 array(),
129 array( 'action' => 'unprotect' )
130 );
131 } else {
132 $ltitle = SpecialPage::getTitleFor( 'Log' );
133 $changeProtection = Linker::linkKnown(
134 $ltitle,
135 $this->msg( 'protectlogpage' )->escaped(),
136 array(),
137 array(
138 'type' => 'protect',
139 'page' => $title->getPrefixedText()
140 )
141 );
142 }
143
144 $changeProtection = ' ' . $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped();
145
146 wfProfileOut( __METHOD__ );
147
148 return Html::rawElement(
149 'li',
150 array(),
151 $lang->specialList( $link . $stxt, $lang->commaList( $description_items ), false ) . $changeProtection ) . "\n";
152 }
153
154 /**
155 * @param $namespace Integer
156 * @param string $type restriction type
157 * @param string $level restriction level
158 * @param string $sizetype "min" or "max"
159 * @param $size Integer
160 * @param $indefOnly Boolean: only indefinie protection
161 * @param $cascadeOnly Boolean: only cascading protection
162 * @return String: input form
163 */
164 protected function showOptions( $namespace, $type = 'edit', $level, $sizetype, $size, $indefOnly, $cascadeOnly ) {
165 global $wgScript;
166 $title = $this->getTitle();
167 return Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
168 Xml::openElement( 'fieldset' ) .
169 Xml::element( 'legend', array(), $this->msg( 'protectedpages' )->text() ) .
170 Html::hidden( 'title', $title->getPrefixedDBkey() ) . "\n" .
171 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
172 $this->getTypeMenu( $type ) . "&#160;\n" .
173 $this->getLevelMenu( $level ) . "&#160;\n" .
174 "<br /><span style='white-space: nowrap'>" .
175 $this->getExpiryCheck( $indefOnly ) . "&#160;\n" .
176 $this->getCascadeCheck( $cascadeOnly ) . "&#160;\n" .
177 "</span><br /><span style='white-space: nowrap'>" .
178 $this->getSizeLimit( $sizetype, $size ) . "&#160;\n" .
179 "</span>" .
180 "&#160;" . Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
181 Xml::closeElement( 'fieldset' ) .
182 Xml::closeElement( 'form' );
183 }
184
185 /**
186 * Prepare the namespace filter drop-down; standard namespace
187 * selector, sans the MediaWiki namespace
188 *
189 * @param $namespace Mixed: pre-select namespace
190 * @return String
191 */
192 protected function getNamespaceMenu( $namespace = null ) {
193 return Html::rawElement( 'span', array( 'style' => 'white-space: nowrap;' ),
194 Html::namespaceSelector(
195 array(
196 'selected' => $namespace,
197 'all' => '',
198 'label' => $this->msg( 'namespace' )->text()
199 ), array(
200 'name' => 'namespace',
201 'id' => 'namespace',
202 'class' => 'namespaceselector',
203 )
204 )
205 );
206 }
207
208 /**
209 * @return string Formatted HTML
210 */
211 protected function getExpiryCheck( $indefOnly ) {
212 return Xml::checkLabel( $this->msg( 'protectedpages-indef' )->text(), 'indefonly', 'indefonly', $indefOnly ) . "\n";
213 }
214
215 /**
216 * @return string Formatted HTML
217 */
218 protected function getCascadeCheck( $cascadeOnly ) {
219 return Xml::checkLabel( $this->msg( 'protectedpages-cascade' )->text(), 'cascadeonly', 'cascadeonly', $cascadeOnly ) . "\n";
220 }
221
222 /**
223 * @return string Formatted HTML
224 */
225 protected function getSizeLimit( $sizetype, $size ) {
226 $max = $sizetype === 'max';
227
228 return Xml::radioLabel( $this->msg( 'minimum-size' )->text(), 'sizetype', 'min', 'wpmin', !$max ) .
229 '&#160;' .
230 Xml::radioLabel( $this->msg( 'maximum-size' )->text(), 'sizetype', 'max', 'wpmax', $max ) .
231 '&#160;' .
232 Xml::input( 'size', 9, $size, array( 'id' => 'wpsize' ) ) .
233 '&#160;' .
234 Xml::label( $this->msg( 'pagesize' )->text(), 'wpsize' );
235 }
236
237 /**
238 * Creates the input label of the restriction type
239 * @param $pr_type string Protection type
240 * @return string Formatted HTML
241 */
242 protected function getTypeMenu( $pr_type ) {
243 $m = array(); // Temporary array
244 $options = array();
245
246 // First pass to load the log names
247 foreach( Title::getFilteredRestrictionTypes( true ) as $type ) {
248 $text = $this->msg( "restriction-$type" )->text();
249 $m[$text] = $type;
250 }
251
252 // Third pass generates sorted XHTML content
253 foreach( $m as $text => $type ) {
254 $selected = ($type == $pr_type );
255 $options[] = Xml::option( $text, $type, $selected ) . "\n";
256 }
257
258 return "<span style='white-space: nowrap'>" .
259 Xml::label( $this->msg( 'restriction-type' )->text(), $this->IdType ) . '&#160;' .
260 Xml::tags( 'select',
261 array( 'id' => $this->IdType, 'name' => $this->IdType ),
262 implode( "\n", $options ) ) . "</span>";
263 }
264
265 /**
266 * Creates the input label of the restriction level
267 * @param $pr_level string Protection level
268 * @return string Formatted HTML
269 */
270 protected function getLevelMenu( $pr_level ) {
271 global $wgRestrictionLevels;
272
273 $m = array( $this->msg( 'restriction-level-all' )->text() => 0 ); // Temporary array
274 $options = array();
275
276 // First pass to load the log names
277 foreach( $wgRestrictionLevels as $type ) {
278 // Messages used can be 'restriction-level-sysop' and 'restriction-level-autoconfirmed'
279 if( $type != '' && $type != '*' ) {
280 $text = $this->msg( "restriction-level-$type" )->text();
281 $m[$text] = $type;
282 }
283 }
284
285 // Third pass generates sorted XHTML content
286 foreach( $m as $text => $type ) {
287 $selected = ($type == $pr_level );
288 $options[] = Xml::option( $text, $type, $selected );
289 }
290
291 return "<span style='white-space: nowrap'>" .
292 Xml::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel ) . ' ' .
293 Xml::tags( 'select',
294 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
295 implode( "\n", $options ) ) . "</span>";
296 }
297
298 protected function getGroupName() {
299 return 'maintenance';
300 }
301 }
302
303 /**
304 * @todo document
305 * @ingroup Pager
306 */
307 class ProtectedPagesPager extends AlphabeticPager {
308 public $mForm, $mConds;
309 private $type, $level, $namespace, $sizetype, $size, $indefonly;
310
311 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype = '', $size = 0,
312 $indefonly = false, $cascadeonly = false )
313 {
314 $this->mForm = $form;
315 $this->mConds = $conds;
316 $this->type = ( $type ) ? $type : 'edit';
317 $this->level = $level;
318 $this->namespace = $namespace;
319 $this->sizetype = $sizetype;
320 $this->size = intval( $size );
321 $this->indefonly = (bool)$indefonly;
322 $this->cascadeonly = (bool)$cascadeonly;
323 parent::__construct( $form->getContext() );
324 }
325
326 function getStartBody() {
327 # Do a link batch query
328 $lb = new LinkBatch;
329 foreach ( $this->mResult as $row ) {
330 $lb->add( $row->page_namespace, $row->page_title );
331 }
332 $lb->execute();
333 return '';
334 }
335
336 function formatRow( $row ) {
337 return $this->mForm->formatRow( $row );
338 }
339
340 function getQueryInfo() {
341 $conds = $this->mConds;
342 $conds[] = '(pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
343 'OR pr_expiry IS NULL)';
344 $conds[] = 'page_id=pr_page';
345 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
346
347 if( $this->sizetype == 'min' ) {
348 $conds[] = 'page_len>=' . $this->size;
349 } elseif( $this->sizetype == 'max' ) {
350 $conds[] = 'page_len<=' . $this->size;
351 }
352
353 if( $this->indefonly ) {
354 $conds[] = "pr_expiry = {$this->mDb->addQuotes( $this->mDb->getInfinity() )} OR pr_expiry IS NULL";
355 }
356 if( $this->cascadeonly ) {
357 $conds[] = 'pr_cascade = 1';
358 }
359
360 if( $this->level )
361 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
362 if( !is_null( $this->namespace ) )
363 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
364 return array(
365 'tables' => array( 'page_restrictions', 'page' ),
366 'fields' => array( 'pr_id', 'page_namespace', 'page_title', 'page_len',
367 'pr_type', 'pr_level', 'pr_expiry', 'pr_cascade' ),
368 'conds' => $conds
369 );
370 }
371
372 function getIndexField() {
373 return 'pr_id';
374 }
375 }