Merge "Pass phpcs-strict on includes/site/"
[lhc/web/wiklou.git] / includes / specials / SpecialBlockList.php
1 <?php
2 /**
3 * Implements Special:BlockList
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 existing blocks
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialBlockList extends SpecialPage {
30 protected $target;
31
32 protected $options;
33
34 function __construct() {
35 parent::__construct( 'BlockList' );
36 }
37
38 /**
39 * Main execution point
40 *
41 * @param string $par Title fragment
42 */
43 public function execute( $par ) {
44 $this->setHeaders();
45 $this->outputHeader();
46 $out = $this->getOutput();
47 $lang = $this->getLanguage();
48 $out->setPageTitle( $this->msg( 'ipblocklist' ) );
49 $out->addModuleStyles( 'mediawiki.special' );
50
51 $request = $this->getRequest();
52 $par = $request->getVal( 'ip', $par );
53 $this->target = trim( $request->getVal( 'wpTarget', $par ) );
54
55 $this->options = $request->getArray( 'wpOptions', array() );
56
57 $action = $request->getText( 'action' );
58
59 if ( $action == 'unblock' || $action == 'submit' && $request->wasPosted() ) {
60 # B/C @since 1.18: Unblock interface is now at Special:Unblock
61 $title = SpecialPage::getTitleFor( 'Unblock', $this->target );
62 $out->redirect( $title->getFullURL() );
63
64 return;
65 }
66
67 # Just show the block list
68 $fields = array(
69 'Target' => array(
70 'type' => 'text',
71 'label-message' => 'ipaddressorusername',
72 'tabindex' => '1',
73 'size' => '45',
74 'default' => $this->target,
75 ),
76 'Options' => array(
77 'type' => 'multiselect',
78 'options' => array(
79 $this->msg( 'blocklist-userblocks' )->text() => 'userblocks',
80 $this->msg( 'blocklist-tempblocks' )->text() => 'tempblocks',
81 $this->msg( 'blocklist-addressblocks' )->text() => 'addressblocks',
82 $this->msg( 'blocklist-rangeblocks' )->text() => 'rangeblocks',
83 ),
84 'flatlist' => true,
85 ),
86 'Limit' => array(
87 'class' => 'HTMLBlockedUsersItemSelect',
88 'label-message' => 'table_pager_limit_label',
89 'options' => array(
90 $lang->formatNum( 20 ) => 20,
91 $lang->formatNum( 50 ) => 50,
92 $lang->formatNum( 100 ) => 100,
93 $lang->formatNum( 250 ) => 250,
94 $lang->formatNum( 500 ) => 500,
95 ),
96 'name' => 'limit',
97 'default' => 50,
98 ),
99 );
100 $context = new DerivativeContext( $this->getContext() );
101 $context->setTitle( $this->getPageTitle() ); // Remove subpage
102 $form = new HTMLForm( $fields, $context );
103 $form->setMethod( 'get' );
104 $form->setWrapperLegendMsg( 'ipblocklist-legend' );
105 $form->setSubmitTextMsg( 'ipblocklist-submit' );
106 $form->prepareForm();
107
108 $form->displayForm( '' );
109 $this->showList();
110 }
111
112 function showList() {
113 # Purge expired entries on one in every 10 queries
114 if ( !mt_rand( 0, 10 ) ) {
115 Block::purgeExpired();
116 }
117
118 $conds = array();
119 # Is the user allowed to see hidden blocks?
120 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
121 $conds['ipb_deleted'] = 0;
122 }
123
124 if ( $this->target !== '' ) {
125 list( $target, $type ) = Block::parseTarget( $this->target );
126
127 switch ( $type ) {
128 case Block::TYPE_ID:
129 case Block::TYPE_AUTO:
130 $conds['ipb_id'] = $target;
131 break;
132
133 case Block::TYPE_IP:
134 case Block::TYPE_RANGE:
135 list( $start, $end ) = IP::parseRange( $target );
136 $dbr = wfGetDB( DB_SLAVE );
137 $conds[] = $dbr->makeList(
138 array(
139 'ipb_address' => $target,
140 Block::getRangeCond( $start, $end )
141 ),
142 LIST_OR
143 );
144 $conds['ipb_auto'] = 0;
145 break;
146
147 case Block::TYPE_USER:
148 $conds['ipb_address'] = $target->getName();
149 $conds['ipb_auto'] = 0;
150 break;
151 }
152 }
153
154 # Apply filters
155 if ( in_array( 'userblocks', $this->options ) ) {
156 $conds['ipb_user'] = 0;
157 }
158 if ( in_array( 'tempblocks', $this->options ) ) {
159 $conds['ipb_expiry'] = 'infinity';
160 }
161 if ( in_array( 'addressblocks', $this->options ) ) {
162 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
163 }
164 if ( in_array( 'rangeblocks', $this->options ) ) {
165 $conds[] = "ipb_range_end = ipb_range_start";
166 }
167
168 # Check for other blocks, i.e. global/tor blocks
169 $otherBlockLink = array();
170 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target ) );
171
172 $out = $this->getOutput();
173
174 # Show additional header for the local block only when other blocks exists.
175 # Not necessary in a standard installation without such extensions enabled
176 if ( count( $otherBlockLink ) ) {
177 $out->addHTML(
178 Html::element( 'h2', array(), $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
179 );
180 }
181
182 $pager = new BlockListPager( $this, $conds );
183 if ( $pager->getNumRows() ) {
184 $out->addHTML(
185 $pager->getNavigationBar() .
186 $pager->getBody() .
187 $pager->getNavigationBar()
188 );
189 } elseif ( $this->target ) {
190 $out->addWikiMsg( 'ipblocklist-no-results' );
191 } else {
192 $out->addWikiMsg( 'ipblocklist-empty' );
193 }
194
195 if ( count( $otherBlockLink ) ) {
196 $out->addHTML(
197 Html::rawElement(
198 'h2',
199 array(),
200 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
201 ) . "\n"
202 );
203 $list = '';
204 foreach ( $otherBlockLink as $link ) {
205 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
206 }
207 $out->addHTML( Html::rawElement(
208 'ul',
209 array( 'class' => 'mw-ipblocklist-otherblocks' ),
210 $list
211 ) . "\n" );
212 }
213 }
214
215 protected function getGroupName() {
216 return 'users';
217 }
218 }
219
220 class BlockListPager extends TablePager {
221 protected $conds;
222 protected $page;
223
224 /**
225 * @param SpecialPage $page
226 * @param array $conds
227 */
228 function __construct( $page, $conds ) {
229 $this->page = $page;
230 $this->conds = $conds;
231 $this->mDefaultDirection = true;
232 parent::__construct( $page->getContext() );
233 }
234
235 function getFieldNames() {
236 static $headers = null;
237
238 if ( $headers === null ) {
239 $headers = array(
240 'ipb_timestamp' => 'blocklist-timestamp',
241 'ipb_target' => 'blocklist-target',
242 'ipb_expiry' => 'blocklist-expiry',
243 'ipb_by' => 'blocklist-by',
244 'ipb_params' => 'blocklist-params',
245 'ipb_reason' => 'blocklist-reason',
246 );
247 foreach ( $headers as $key => $val ) {
248 $headers[$key] = $this->msg( $val )->text();
249 }
250 }
251
252 return $headers;
253 }
254
255 function formatValue( $name, $value ) {
256 static $msg = null;
257 if ( $msg === null ) {
258 $msg = array(
259 'anononlyblock',
260 'createaccountblock',
261 'noautoblockblock',
262 'emailblock',
263 'blocklist-nousertalk',
264 'unblocklink',
265 'change-blocklink',
266 'infiniteblock',
267 );
268 $msg = array_combine( $msg, array_map( array( $this, 'msg' ), $msg ) );
269 }
270
271 /** @var $row object */
272 $row = $this->mCurrentRow;
273
274 $formatted = '';
275
276 switch ( $name ) {
277 case 'ipb_timestamp':
278 $formatted = $this->getLanguage()->userTimeAndDate( $value, $this->getUser() );
279 break;
280
281 case 'ipb_target':
282 if ( $row->ipb_auto ) {
283 $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
284 } else {
285 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
286 switch ( $type ) {
287 case Block::TYPE_USER:
288 case Block::TYPE_IP:
289 $formatted = Linker::userLink( $target->getId(), $target );
290 $formatted .= Linker::userToolLinks(
291 $target->getId(),
292 $target,
293 false,
294 Linker::TOOL_LINKS_NOBLOCK
295 );
296 break;
297 case Block::TYPE_RANGE:
298 $formatted = htmlspecialchars( $target );
299 }
300 }
301 break;
302
303 case 'ipb_expiry':
304 $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */true );
305 if ( $this->getUser()->isAllowed( 'block' ) ) {
306 if ( $row->ipb_auto ) {
307 $links[] = Linker::linkKnown(
308 SpecialPage::getTitleFor( 'Unblock' ),
309 $msg['unblocklink'],
310 array(),
311 array( 'wpTarget' => "#{$row->ipb_id}" )
312 );
313 } else {
314 $links[] = Linker::linkKnown(
315 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
316 $msg['unblocklink']
317 );
318 $links[] = Linker::linkKnown(
319 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
320 $msg['change-blocklink']
321 );
322 }
323 $formatted .= ' ' . Html::rawElement(
324 'span',
325 array( 'class' => 'mw-blocklist-actions' ),
326 $this->msg( 'parentheses' )->rawParams(
327 $this->getLanguage()->pipeList( $links ) )->escaped()
328 );
329 }
330 break;
331
332 case 'ipb_by':
333 if ( isset( $row->by_user_name ) ) {
334 $formatted = Linker::userLink( $value, $row->by_user_name );
335 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
336 } else {
337 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
338 }
339 break;
340
341 case 'ipb_reason':
342 $formatted = Linker::formatComment( $value );
343 break;
344
345 case 'ipb_params':
346 $properties = array();
347 if ( $row->ipb_anon_only ) {
348 $properties[] = $msg['anononlyblock'];
349 }
350 if ( $row->ipb_create_account ) {
351 $properties[] = $msg['createaccountblock'];
352 }
353 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
354 $properties[] = $msg['noautoblockblock'];
355 }
356
357 if ( $row->ipb_block_email ) {
358 $properties[] = $msg['emailblock'];
359 }
360
361 if ( !$row->ipb_allow_usertalk ) {
362 $properties[] = $msg['blocklist-nousertalk'];
363 }
364
365 $formatted = $this->getLanguage()->commaList( $properties );
366 break;
367
368 default:
369 $formatted = "Unable to format $name";
370 break;
371 }
372
373 return $formatted;
374 }
375
376 function getQueryInfo() {
377 $info = array(
378 'tables' => array( 'ipblocks', 'user' ),
379 'fields' => array(
380 'ipb_id',
381 'ipb_address',
382 'ipb_user',
383 'ipb_by',
384 'ipb_by_text',
385 'by_user_name' => 'user_name',
386 'ipb_reason',
387 'ipb_timestamp',
388 'ipb_auto',
389 'ipb_anon_only',
390 'ipb_create_account',
391 'ipb_enable_autoblock',
392 'ipb_expiry',
393 'ipb_range_start',
394 'ipb_range_end',
395 'ipb_deleted',
396 'ipb_block_email',
397 'ipb_allow_usertalk',
398 ),
399 'conds' => $this->conds,
400 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
401 );
402
403 # Is the user allowed to see hidden blocks?
404 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
405 $info['conds']['ipb_deleted'] = 0;
406 }
407
408 return $info;
409 }
410
411 public function getTableClass() {
412 return 'TablePager mw-blocklist';
413 }
414
415 function getIndexField() {
416 return 'ipb_timestamp';
417 }
418
419 function getDefaultSort() {
420 return 'ipb_timestamp';
421 }
422
423 function isFieldSortable( $name ) {
424 return false;
425 }
426
427 /**
428 * Do a LinkBatch query to minimise database load when generating all these links
429 * @param ResultWrapper $result
430 */
431 function preprocessResults( $result ) {
432 wfProfileIn( __METHOD__ );
433 # Do a link batch query
434 $lb = new LinkBatch;
435 $lb->setCaller( __METHOD__ );
436
437 $userids = array();
438
439 foreach ( $result as $row ) {
440 $userids[] = $row->ipb_by;
441
442 # Usernames and titles are in fact related by a simple substitution of space -> underscore
443 # The last few lines of Title::secureAndSplit() tell the story.
444 $name = str_replace( ' ', '_', $row->ipb_address );
445 $lb->add( NS_USER, $name );
446 $lb->add( NS_USER_TALK, $name );
447 }
448
449 $ua = UserArray::newFromIDs( $userids );
450 foreach ( $ua as $user ) {
451 $name = str_replace( ' ', '_', $user->getName() );
452 $lb->add( NS_USER, $name );
453 $lb->add( NS_USER_TALK, $name );
454 }
455
456 $lb->execute();
457 wfProfileOut( __METHOD__ );
458 }
459 }
460
461 /**
462 * Items per page dropdown. Essentially a crap workaround for bug 32603.
463 */
464 class HTMLBlockedUsersItemSelect extends HTMLSelectField {
465 /**
466 * Basically don't do any validation. If it's a number that's fine. Also,
467 * add it to the list if it's not there already
468 *
469 * @param string $value
470 * @param array $alldata
471 * @return bool
472 */
473 function validate( $value, $alldata ) {
474 if ( $value == '' ) {
475 return true;
476 }
477
478 // Let folks pick an explicit limit not from our list, as long as it's a real numbr.
479 if ( !in_array( $value, $this->mParams['options'] )
480 && $value == intval( $value )
481 && $value > 0
482 ) {
483 // This adds the explicitly requested limit value to the drop-down,
484 // then makes sure it's sorted correctly so when we output the list
485 // later, the custom option doesn't just show up last.
486 $this->mParams['options'][$this->mParent->getLanguage()->formatNum( $value )] =
487 intval( $value );
488 asort( $this->mParams['options'] );
489 }
490
491 return true;
492 }
493 }