SpecialLinkSearch: clean up munged query variable handling
[mediawiki.git] / includes / specials / SpecialBlockList.php
blob458343028e07ac0bb7dd02bb9a472ba4c61371a2
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 {
30 protected $target;
32 protected $options;
34 function __construct() {
35 parent::__construct( 'BlockList' );
38 /**
39 * Main execution point
41 * @param string $par Title fragment
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 $out->addModules( 'mediawiki.userSuggest' );
52 $request = $this->getRequest();
53 $par = $request->getVal( 'ip', $par );
54 $this->target = trim( $request->getVal( 'wpTarget', $par ) );
56 $this->options = $request->getArray( 'wpOptions', array() );
58 $action = $request->getText( 'action' );
60 if ( $action == 'unblock' || $action == 'submit' && $request->wasPosted() ) {
61 # B/C @since 1.18: Unblock interface is now at Special:Unblock
62 $title = SpecialPage::getTitleFor( 'Unblock', $this->target );
63 $out->redirect( $title->getFullURL() );
65 return;
68 # Just show the block list
69 $fields = array(
70 'Target' => array(
71 'type' => 'text',
72 'label-message' => 'ipaddressorusername',
73 'tabindex' => '1',
74 'size' => '45',
75 'default' => $this->target,
76 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
78 'Options' => array(
79 'type' => 'multiselect',
80 'options' => array(
81 $this->msg( 'blocklist-userblocks' )->text() => 'userblocks',
82 $this->msg( 'blocklist-tempblocks' )->text() => 'tempblocks',
83 $this->msg( 'blocklist-addressblocks' )->text() => 'addressblocks',
84 $this->msg( 'blocklist-rangeblocks' )->text() => 'rangeblocks',
86 'flatlist' => true,
88 'Limit' => array(
89 'type' => 'limitselect',
90 'label-message' => 'table_pager_limit_label',
91 'options' => array(
92 $lang->formatNum( 20 ) => 20,
93 $lang->formatNum( 50 ) => 50,
94 $lang->formatNum( 100 ) => 100,
95 $lang->formatNum( 250 ) => 250,
96 $lang->formatNum( 500 ) => 500,
98 'name' => 'limit',
99 'default' => 50,
102 $context = new DerivativeContext( $this->getContext() );
103 $context->setTitle( $this->getPageTitle() ); // Remove subpage
104 $form = new HTMLForm( $fields, $context );
105 $form->setMethod( 'get' );
106 $form->setWrapperLegendMsg( 'ipblocklist-legend' );
107 $form->setSubmitTextMsg( 'ipblocklist-submit' );
108 $form->setSubmitProgressive();
109 $form->prepareForm();
111 $form->displayForm( '' );
112 $this->showList();
115 function showList() {
116 # Purge expired entries on one in every 10 queries
117 if ( !mt_rand( 0, 10 ) ) {
118 Block::purgeExpired();
121 $conds = array();
122 # Is the user allowed to see hidden blocks?
123 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
124 $conds['ipb_deleted'] = 0;
127 if ( $this->target !== '' ) {
128 list( $target, $type ) = Block::parseTarget( $this->target );
130 switch ( $type ) {
131 case Block::TYPE_ID:
132 case Block::TYPE_AUTO:
133 $conds['ipb_id'] = $target;
134 break;
136 case Block::TYPE_IP:
137 case Block::TYPE_RANGE:
138 list( $start, $end ) = IP::parseRange( $target );
139 $dbr = wfGetDB( DB_SLAVE );
140 $conds[] = $dbr->makeList(
141 array(
142 'ipb_address' => $target,
143 Block::getRangeCond( $start, $end )
145 LIST_OR
147 $conds['ipb_auto'] = 0;
148 break;
150 case Block::TYPE_USER:
151 $conds['ipb_address'] = $target->getName();
152 $conds['ipb_auto'] = 0;
153 break;
157 # Apply filters
158 if ( in_array( 'userblocks', $this->options ) ) {
159 $conds['ipb_user'] = 0;
161 if ( in_array( 'tempblocks', $this->options ) ) {
162 $conds['ipb_expiry'] = 'infinity';
164 if ( in_array( 'addressblocks', $this->options ) ) {
165 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
167 if ( in_array( 'rangeblocks', $this->options ) ) {
168 $conds[] = "ipb_range_end = ipb_range_start";
171 # Check for other blocks, i.e. global/tor blocks
172 $otherBlockLink = array();
173 Hooks::run( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target ) );
175 $out = $this->getOutput();
177 # Show additional header for the local block only when other blocks exists.
178 # Not necessary in a standard installation without such extensions enabled
179 if ( count( $otherBlockLink ) ) {
180 $out->addHTML(
181 Html::element( 'h2', array(), $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
185 $pager = new BlockListPager( $this, $conds );
186 if ( $pager->getNumRows() ) {
187 $out->addParserOutputContent( $pager->getFullOutput() );
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(
207 'ul',
208 array( 'class' => 'mw-ipblocklist-otherblocks' ),
209 $list
210 ) . "\n" );
214 protected function getGroupName() {
215 return 'users';
219 class BlockListPager extends TablePager {
220 protected $conds;
221 protected $page;
224 * @param SpecialPage $page
225 * @param array $conds
227 function __construct( $page, $conds ) {
228 $this->page = $page;
229 $this->conds = $conds;
230 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
231 parent::__construct( $page->getContext() );
234 function getFieldNames() {
235 static $headers = null;
237 if ( $headers === null ) {
238 $headers = array(
239 'ipb_timestamp' => 'blocklist-timestamp',
240 'ipb_target' => 'blocklist-target',
241 'ipb_expiry' => 'blocklist-expiry',
242 'ipb_by' => 'blocklist-by',
243 'ipb_params' => 'blocklist-params',
244 'ipb_reason' => 'blocklist-reason',
246 foreach ( $headers as $key => $val ) {
247 $headers[$key] = $this->msg( $val )->text();
251 return $headers;
254 function formatValue( $name, $value ) {
255 static $msg = null;
256 if ( $msg === null ) {
257 $msg = array(
258 'anononlyblock',
259 'createaccountblock',
260 'noautoblockblock',
261 'emailblock',
262 'blocklist-nousertalk',
263 'unblocklink',
264 'change-blocklink',
266 $msg = array_combine( $msg, array_map( array( $this, 'msg' ), $msg ) );
269 /** @var $row object */
270 $row = $this->mCurrentRow;
272 $formatted = '';
274 switch ( $name ) {
275 case 'ipb_timestamp':
276 $formatted = $this->getLanguage()->userTimeAndDate( $value, $this->getUser() );
277 break;
279 case 'ipb_target':
280 if ( $row->ipb_auto ) {
281 $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
282 } else {
283 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
284 switch ( $type ) {
285 case Block::TYPE_USER:
286 case Block::TYPE_IP:
287 $formatted = Linker::userLink( $target->getId(), $target );
288 $formatted .= Linker::userToolLinks(
289 $target->getId(),
290 $target,
291 false,
292 Linker::TOOL_LINKS_NOBLOCK
294 break;
295 case Block::TYPE_RANGE:
296 $formatted = htmlspecialchars( $target );
299 break;
301 case 'ipb_expiry':
302 $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */true );
303 if ( $this->getUser()->isAllowed( 'block' ) ) {
304 if ( $row->ipb_auto ) {
305 $links[] = Linker::linkKnown(
306 SpecialPage::getTitleFor( 'Unblock' ),
307 $msg['unblocklink'],
308 array(),
309 array( 'wpTarget' => "#{$row->ipb_id}" )
311 } else {
312 $links[] = Linker::linkKnown(
313 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
314 $msg['unblocklink']
316 $links[] = Linker::linkKnown(
317 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
318 $msg['change-blocklink']
321 $formatted .= ' ' . Html::rawElement(
322 'span',
323 array( 'class' => 'mw-blocklist-actions' ),
324 $this->msg( 'parentheses' )->rawParams(
325 $this->getLanguage()->pipeList( $links ) )->escaped()
328 break;
330 case 'ipb_by':
331 if ( isset( $row->by_user_name ) ) {
332 $formatted = Linker::userLink( $value, $row->by_user_name );
333 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
334 } else {
335 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
337 break;
339 case 'ipb_reason':
340 $formatted = Linker::formatComment( $value );
341 break;
343 case 'ipb_params':
344 $properties = array();
345 if ( $row->ipb_anon_only ) {
346 $properties[] = $msg['anononlyblock'];
348 if ( $row->ipb_create_account ) {
349 $properties[] = $msg['createaccountblock'];
351 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
352 $properties[] = $msg['noautoblockblock'];
355 if ( $row->ipb_block_email ) {
356 $properties[] = $msg['emailblock'];
359 if ( !$row->ipb_allow_usertalk ) {
360 $properties[] = $msg['blocklist-nousertalk'];
363 $formatted = $this->getLanguage()->commaList( $properties );
364 break;
366 default:
367 $formatted = "Unable to format $name";
368 break;
371 return $formatted;
374 function getQueryInfo() {
375 $info = array(
376 'tables' => array( 'ipblocks', 'user' ),
377 'fields' => array(
378 'ipb_id',
379 'ipb_address',
380 'ipb_user',
381 'ipb_by',
382 'ipb_by_text',
383 'by_user_name' => 'user_name',
384 'ipb_reason',
385 'ipb_timestamp',
386 'ipb_auto',
387 'ipb_anon_only',
388 'ipb_create_account',
389 'ipb_enable_autoblock',
390 'ipb_expiry',
391 'ipb_range_start',
392 'ipb_range_end',
393 'ipb_deleted',
394 'ipb_block_email',
395 'ipb_allow_usertalk',
397 'conds' => $this->conds,
398 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
401 # Is the user allowed to see hidden blocks?
402 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
403 $info['conds']['ipb_deleted'] = 0;
406 return $info;
409 public function getTableClass() {
410 return parent::getTableClass() . ' mw-blocklist';
413 function getIndexField() {
414 return 'ipb_timestamp';
417 function getDefaultSort() {
418 return 'ipb_timestamp';
421 function isFieldSortable( $name ) {
422 return false;
426 * Do a LinkBatch query to minimise database load when generating all these links
427 * @param ResultWrapper $result
429 function preprocessResults( $result ) {
430 # Do a link batch query
431 $lb = new LinkBatch;
432 $lb->setCaller( __METHOD__ );
434 $userids = array();
436 foreach ( $result as $row ) {
437 $userids[] = $row->ipb_by;
439 # Usernames and titles are in fact related by a simple substitution of space -> underscore
440 # The last few lines of Title::secureAndSplit() tell the story.
441 $name = str_replace( ' ', '_', $row->ipb_address );
442 $lb->add( NS_USER, $name );
443 $lb->add( NS_USER_TALK, $name );
446 $ua = UserArray::newFromIDs( $userids );
447 foreach ( $ua as $user ) {
448 $name = str_replace( ' ', '_', $user->getName() );
449 $lb->add( NS_USER, $name );
450 $lb->add( NS_USER_TALK, $name );
453 $lb->execute();