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