Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / includes / specials / SpecialBlockList.php
blob9defaba7f1b8517e4d69631b7cfc3e92d660bcb3
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 # setup BlockListPager here to get the actual default Limit
69 $pager = $this->getBlockListPager();
71 # Just show the block list
72 $fields = array(
73 'Target' => array(
74 'type' => 'text',
75 'label-message' => 'ipaddressorusername',
76 'tabindex' => '1',
77 'size' => '45',
78 'default' => $this->target,
79 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
81 'Options' => array(
82 'type' => 'multiselect',
83 'options-messages' => array(
84 'blocklist-userblocks' => 'userblocks',
85 'blocklist-tempblocks' => 'tempblocks',
86 'blocklist-addressblocks' => 'addressblocks',
87 'blocklist-rangeblocks' => 'rangeblocks',
89 'flatlist' => true,
91 'Limit' => array(
92 'type' => 'limitselect',
93 'label-message' => 'table_pager_limit_label',
94 'options' => array(
95 $lang->formatNum( 20 ) => 20,
96 $lang->formatNum( 50 ) => 50,
97 $lang->formatNum( 100 ) => 100,
98 $lang->formatNum( 250 ) => 250,
99 $lang->formatNum( 500 ) => 500,
101 'name' => 'limit',
102 'default' => $pager->getLimit(),
105 $context = new DerivativeContext( $this->getContext() );
106 $context->setTitle( $this->getPageTitle() ); // Remove subpage
107 $form = new HTMLForm( $fields, $context );
108 $form->setMethod( 'get' );
109 $form->setWrapperLegendMsg( 'ipblocklist-legend' );
110 $form->setSubmitTextMsg( 'ipblocklist-submit' );
111 $form->setSubmitProgressive();
112 $form->prepareForm();
114 $form->displayForm( '' );
115 $this->showList( $pager );
119 * Setup a new BlockListPager instance.
120 * @return BlockListPager
122 protected function getBlockListPager() {
123 $conds = array();
124 # Is the user allowed to see hidden blocks?
125 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
126 $conds['ipb_deleted'] = 0;
129 if ( $this->target !== '' ) {
130 list( $target, $type ) = Block::parseTarget( $this->target );
132 switch ( $type ) {
133 case Block::TYPE_ID:
134 case Block::TYPE_AUTO:
135 $conds['ipb_id'] = $target;
136 break;
138 case Block::TYPE_IP:
139 case Block::TYPE_RANGE:
140 list( $start, $end ) = IP::parseRange( $target );
141 $dbr = wfGetDB( DB_SLAVE );
142 $conds[] = $dbr->makeList(
143 array(
144 'ipb_address' => $target,
145 Block::getRangeCond( $start, $end )
147 LIST_OR
149 $conds['ipb_auto'] = 0;
150 break;
152 case Block::TYPE_USER:
153 $conds['ipb_address'] = $target->getName();
154 $conds['ipb_auto'] = 0;
155 break;
159 # Apply filters
160 if ( in_array( 'userblocks', $this->options ) ) {
161 $conds['ipb_user'] = 0;
163 if ( in_array( 'tempblocks', $this->options ) ) {
164 $conds['ipb_expiry'] = 'infinity';
166 if ( in_array( 'addressblocks', $this->options ) ) {
167 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
169 if ( in_array( 'rangeblocks', $this->options ) ) {
170 $conds[] = "ipb_range_end = ipb_range_start";
173 return new BlockListPager( $this, $conds );
177 * Show the list of blocked accounts matching the actual filter.
178 * @param BlockListPager $pager The BlockListPager instance for this page
180 protected function showList( BlockListPager $pager ) {
181 $out = $this->getOutput();
183 # Check for other blocks, i.e. global/tor blocks
184 $otherBlockLink = array();
185 Hooks::run( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target ) );
187 # Show additional header for the local block only when other blocks exists.
188 # Not necessary in a standard installation without such extensions enabled
189 if ( count( $otherBlockLink ) ) {
190 $out->addHTML(
191 Html::element( 'h2', array(), $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
195 if ( $pager->getNumRows() ) {
196 $out->addParserOutputContent( $pager->getFullOutput() );
197 } elseif ( $this->target ) {
198 $out->addWikiMsg( 'ipblocklist-no-results' );
199 } else {
200 $out->addWikiMsg( 'ipblocklist-empty' );
203 if ( count( $otherBlockLink ) ) {
204 $out->addHTML(
205 Html::rawElement(
206 'h2',
207 array(),
208 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
209 ) . "\n"
211 $list = '';
212 foreach ( $otherBlockLink as $link ) {
213 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
215 $out->addHTML( Html::rawElement(
216 'ul',
217 array( 'class' => 'mw-ipblocklist-otherblocks' ),
218 $list
219 ) . "\n" );
223 protected function getGroupName() {
224 return 'users';
228 class BlockListPager extends TablePager {
229 protected $conds;
230 protected $page;
233 * @param SpecialPage $page
234 * @param array $conds
236 function __construct( $page, $conds ) {
237 $this->page = $page;
238 $this->conds = $conds;
239 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
240 parent::__construct( $page->getContext() );
243 function getFieldNames() {
244 static $headers = null;
246 if ( $headers === null ) {
247 $headers = array(
248 'ipb_timestamp' => 'blocklist-timestamp',
249 'ipb_target' => 'blocklist-target',
250 'ipb_expiry' => 'blocklist-expiry',
251 'ipb_by' => 'blocklist-by',
252 'ipb_params' => 'blocklist-params',
253 'ipb_reason' => 'blocklist-reason',
255 foreach ( $headers as $key => $val ) {
256 $headers[$key] = $this->msg( $val )->text();
260 return $headers;
263 function formatValue( $name, $value ) {
264 static $msg = null;
265 if ( $msg === null ) {
266 $keys = array(
267 'anononlyblock',
268 'createaccountblock',
269 'noautoblockblock',
270 'emailblock',
271 'blocklist-nousertalk',
272 'unblocklink',
273 'change-blocklink',
276 foreach ( $keys as $key ) {
277 $msg[$key] = $this->msg( $key )->escaped();
281 /** @var $row object */
282 $row = $this->mCurrentRow;
284 $language = $this->getLanguage();
286 $formatted = '';
288 switch ( $name ) {
289 case 'ipb_timestamp':
290 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
291 break;
293 case 'ipb_target':
294 if ( $row->ipb_auto ) {
295 $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
296 } else {
297 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
298 switch ( $type ) {
299 case Block::TYPE_USER:
300 case Block::TYPE_IP:
301 $formatted = Linker::userLink( $target->getId(), $target );
302 $formatted .= Linker::userToolLinks(
303 $target->getId(),
304 $target,
305 false,
306 Linker::TOOL_LINKS_NOBLOCK
308 break;
309 case Block::TYPE_RANGE:
310 $formatted = htmlspecialchars( $target );
313 break;
315 case 'ipb_expiry':
316 $formatted = htmlspecialchars( $language->formatExpiry(
317 $value,
318 /* User preference timezone */true
319 ) );
320 if ( $this->getUser()->isAllowed( 'block' ) ) {
321 if ( $row->ipb_auto ) {
322 $links[] = Linker::linkKnown(
323 SpecialPage::getTitleFor( 'Unblock' ),
324 $msg['unblocklink'],
325 array(),
326 array( 'wpTarget' => "#{$row->ipb_id}" )
328 } else {
329 $links[] = Linker::linkKnown(
330 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
331 $msg['unblocklink']
333 $links[] = Linker::linkKnown(
334 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
335 $msg['change-blocklink']
338 $formatted .= ' ' . Html::rawElement(
339 'span',
340 array( 'class' => 'mw-blocklist-actions' ),
341 $this->msg( 'parentheses' )->rawParams(
342 $language->pipeList( $links ) )->escaped()
345 break;
347 case 'ipb_by':
348 if ( isset( $row->by_user_name ) ) {
349 $formatted = Linker::userLink( $value, $row->by_user_name );
350 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
351 } else {
352 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
354 break;
356 case 'ipb_reason':
357 $formatted = Linker::formatComment( $value );
358 break;
360 case 'ipb_params':
361 $properties = array();
362 if ( $row->ipb_anon_only ) {
363 $properties[] = $msg['anononlyblock'];
365 if ( $row->ipb_create_account ) {
366 $properties[] = $msg['createaccountblock'];
368 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
369 $properties[] = $msg['noautoblockblock'];
372 if ( $row->ipb_block_email ) {
373 $properties[] = $msg['emailblock'];
376 if ( !$row->ipb_allow_usertalk ) {
377 $properties[] = $msg['blocklist-nousertalk'];
380 $formatted = $language->commaList( $properties );
381 break;
383 default:
384 $formatted = "Unable to format $name";
385 break;
388 return $formatted;
391 function getQueryInfo() {
392 $info = array(
393 'tables' => array( 'ipblocks', 'user' ),
394 'fields' => array(
395 'ipb_id',
396 'ipb_address',
397 'ipb_user',
398 'ipb_by',
399 'ipb_by_text',
400 'by_user_name' => 'user_name',
401 'ipb_reason',
402 'ipb_timestamp',
403 'ipb_auto',
404 'ipb_anon_only',
405 'ipb_create_account',
406 'ipb_enable_autoblock',
407 'ipb_expiry',
408 'ipb_range_start',
409 'ipb_range_end',
410 'ipb_deleted',
411 'ipb_block_email',
412 'ipb_allow_usertalk',
414 'conds' => $this->conds,
415 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
418 # Filter out any expired blocks
419 $db = $this->getDatabase();
420 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
422 # Is the user allowed to see hidden blocks?
423 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
424 $info['conds']['ipb_deleted'] = 0;
427 return $info;
430 public function getTableClass() {
431 return parent::getTableClass() . ' mw-blocklist';
434 function getIndexField() {
435 return 'ipb_timestamp';
438 function getDefaultSort() {
439 return 'ipb_timestamp';
442 function isFieldSortable( $name ) {
443 return false;
447 * Do a LinkBatch query to minimise database load when generating all these links
448 * @param ResultWrapper $result
450 function preprocessResults( $result ) {
451 # Do a link batch query
452 $lb = new LinkBatch;
453 $lb->setCaller( __METHOD__ );
455 foreach ( $result as $row ) {
456 $lb->add( NS_USER, $row->ipb_address );
457 $lb->add( NS_USER_TALK, $row->ipb_address );
459 if ( isset( $row->by_user_name ) ) {
460 $lb->add( NS_USER, $row->by_user_name );
461 $lb->add( NS_USER_TALK, $row->by_user_name );
465 $lb->execute();