(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / specials / SpecialBlockList.php
blob0a3a28fe19bd508c693d43c88963bddddcb70b67
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 $par String 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() );
62 return;
65 # Just show the block list
66 $fields = array(
67 'Target' => array(
68 'type' => 'text',
69 'label-message' => 'ipadressorusername',
70 'tabindex' => '1',
71 'size' => '45',
72 'default' => $this->target,
74 'Options' => array(
75 'type' => 'multiselect',
76 'options' => array(
77 $this->msg( 'blocklist-userblocks' )->text() => 'userblocks',
78 $this->msg( 'blocklist-tempblocks' )->text() => 'tempblocks',
79 $this->msg( 'blocklist-addressblocks' )->text() => 'addressblocks',
80 $this->msg( 'blocklist-rangeblocks' )->text() => 'rangeblocks',
82 'flatlist' => true,
84 'Limit' => array(
85 'class' => 'HTMLBlockedUsersItemSelect',
86 'label-message' => 'table_pager_limit_label',
87 'options' => array(
88 $lang->formatNum( 20 ) => 20,
89 $lang->formatNum( 50 ) => 50,
90 $lang->formatNum( 100 ) => 100,
91 $lang->formatNum( 250 ) => 250,
92 $lang->formatNum( 500 ) => 500,
94 'name' => 'limit',
95 'default' => 50,
98 $form = new HTMLForm( $fields, $this->getContext() );
99 $form->setMethod( 'get' );
100 $form->setWrapperLegendMsg( 'ipblocklist-legend' );
101 $form->setSubmitTextMsg( 'ipblocklist-submit' );
102 $form->prepareForm();
104 $form->displayForm( '' );
105 $this->showList();
108 function showList() {
109 # Purge expired entries on one in every 10 queries
110 if ( !mt_rand( 0, 10 ) ) {
111 Block::purgeExpired();
114 $conds = array();
115 # Is the user allowed to see hidden blocks?
116 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
117 $conds['ipb_deleted'] = 0;
120 if ( $this->target !== '' ){
121 list( $target, $type ) = Block::parseTarget( $this->target );
123 switch( $type ){
124 case Block::TYPE_ID:
125 case Block::TYPE_AUTO:
126 $conds['ipb_id'] = $target;
127 break;
129 case Block::TYPE_IP:
130 case Block::TYPE_RANGE:
131 list( $start, $end ) = IP::parseRange( $target );
132 $dbr = wfGetDB( DB_SLAVE );
133 $conds[] = $dbr->makeList(
134 array(
135 'ipb_address' => $target,
136 Block::getRangeCond( $start, $end )
138 LIST_OR
140 $conds['ipb_auto'] = 0;
141 break;
143 case Block::TYPE_USER:
144 $conds['ipb_address'] = (string)$this->target;
145 $conds['ipb_auto'] = 0;
146 break;
150 # Apply filters
151 if( in_array( 'userblocks', $this->options ) ) {
152 $conds['ipb_user'] = 0;
154 if( in_array( 'tempblocks', $this->options ) ) {
155 $conds['ipb_expiry'] = 'infinity';
157 if( in_array( 'addressblocks', $this->options ) ) {
158 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
160 if( in_array( 'rangeblocks', $this->options ) ) {
161 $conds[] = "ipb_range_end = ipb_range_start";
164 # Check for other blocks, i.e. global/tor blocks
165 $otherBlockLink = array();
166 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target ) );
168 $out = $this->getOutput();
170 # Show additional header for the local block only when other blocks exists.
171 # Not necessary in a standard installation without such extensions enabled
172 if( count( $otherBlockLink ) ) {
173 $out->addHTML(
174 Html::element( 'h2', array(), $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
178 $pager = new BlockListPager( $this, $conds );
179 if ( $pager->getNumRows() ) {
180 $out->addHTML(
181 $pager->getNavigationBar() .
182 $pager->getBody().
183 $pager->getNavigationBar()
186 } elseif ( $this->target ) {
187 $out->addWikiMsg( 'ipblocklist-no-results' );
189 } else {
190 $out->addWikiMsg( 'ipblocklist-empty' );
193 if( count( $otherBlockLink ) ) {
194 $out->addHTML(
195 Html::rawElement(
196 'h2',
197 array(),
198 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
199 ) . "\n"
201 $list = '';
202 foreach( $otherBlockLink as $link ) {
203 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
205 $out->addHTML( Html::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
210 class BlockListPager extends TablePager {
211 protected $conds;
212 protected $page;
215 * @param $page SpecialPage
216 * @param $conds Array
218 function __construct( $page, $conds ) {
219 $this->page = $page;
220 $this->conds = $conds;
221 $this->mDefaultDirection = true;
222 parent::__construct( $page->getContext() );
225 function getFieldNames() {
226 static $headers = null;
228 if ( $headers == array() ) {
229 $headers = array(
230 'ipb_timestamp' => 'blocklist-timestamp',
231 'ipb_target' => 'blocklist-target',
232 'ipb_expiry' => 'blocklist-expiry',
233 'ipb_by' => 'blocklist-by',
234 'ipb_params' => 'blocklist-params',
235 'ipb_reason' => 'blocklist-reason',
237 foreach( $headers as $key => $val ) {
238 $headers[$key] = $this->msg( $val )->text();
242 return $headers;
245 function formatValue( $name, $value ) {
246 static $msg = null;
247 if ( $msg === null ) {
248 $msg = array(
249 'anononlyblock',
250 'createaccountblock',
251 'noautoblockblock',
252 'emailblock',
253 'blocklist-nousertalk',
254 'unblocklink',
255 'change-blocklink',
256 'infiniteblock',
258 $msg = array_combine( $msg, array_map( array( $this, 'msg' ), $msg ) );
261 /** @var $row object */
262 $row = $this->mCurrentRow;
264 $formatted = '';
266 switch( $name ) {
267 case 'ipb_timestamp':
268 $formatted = $this->getLanguage()->userTimeAndDate( $value, $this->getUser() );
269 break;
271 case 'ipb_target':
272 if( $row->ipb_auto ){
273 $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
274 } else {
275 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
276 switch( $type ){
277 case Block::TYPE_USER:
278 case Block::TYPE_IP:
279 $formatted = Linker::userLink( $target->getId(), $target );
280 $formatted .= Linker::userToolLinks(
281 $target->getId(),
282 $target,
283 false,
284 Linker::TOOL_LINKS_NOBLOCK
286 break;
287 case Block::TYPE_RANGE:
288 $formatted = htmlspecialchars( $target );
291 break;
293 case 'ipb_expiry':
294 $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */ true );
295 if( $this->getUser()->isAllowed( 'block' ) ){
296 if( $row->ipb_auto ){
297 $links[] = Linker::linkKnown(
298 SpecialPage::getTitleFor( 'Unblock' ),
299 $msg['unblocklink'],
300 array(),
301 array( 'wpTarget' => "#{$row->ipb_id}" )
303 } else {
304 $links[] = Linker::linkKnown(
305 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
306 $msg['unblocklink']
308 $links[] = Linker::linkKnown(
309 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
310 $msg['change-blocklink']
313 $formatted .= ' ' . Html::rawElement(
314 'span',
315 array( 'class' => 'mw-blocklist-actions' ),
316 $this->msg( 'parentheses' )->rawParams(
317 $this->getLanguage()->pipeList( $links ) )->escaped()
320 break;
322 case 'ipb_by':
323 if ( isset( $row->by_user_name ) ) {
324 $formatted = Linker::userLink( $value, $row->by_user_name );
325 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
326 } else {
327 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
329 break;
331 case 'ipb_reason':
332 $formatted = Linker::commentBlock( $value );
333 break;
335 case 'ipb_params':
336 $properties = array();
337 if ( $row->ipb_anon_only ) {
338 $properties[] = $msg['anononlyblock'];
340 if ( $row->ipb_create_account ) {
341 $properties[] = $msg['createaccountblock'];
343 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
344 $properties[] = $msg['noautoblockblock'];
347 if ( $row->ipb_block_email ) {
348 $properties[] = $msg['emailblock'];
351 if ( !$row->ipb_allow_usertalk ) {
352 $properties[] = $msg['blocklist-nousertalk'];
355 $formatted = $this->getLanguage()->commaList( $properties );
356 break;
358 default:
359 $formatted = "Unable to format $name";
360 break;
363 return $formatted;
366 function getQueryInfo() {
367 $info = array(
368 'tables' => array( 'ipblocks', 'user' ),
369 'fields' => array(
370 'ipb_id',
371 'ipb_address',
372 'ipb_user',
373 'ipb_by',
374 'ipb_by_text',
375 'user_name AS by_user_name',
376 'ipb_reason',
377 'ipb_timestamp',
378 'ipb_auto',
379 'ipb_anon_only',
380 'ipb_create_account',
381 'ipb_enable_autoblock',
382 'ipb_expiry',
383 'ipb_range_start',
384 'ipb_range_end',
385 'ipb_deleted',
386 'ipb_block_email',
387 'ipb_allow_usertalk',
389 'conds' => $this->conds,
390 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
393 # Is the user allowed to see hidden blocks?
394 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
395 $info['conds']['ipb_deleted'] = 0;
398 return $info;
401 public function getTableClass(){
402 return 'TablePager mw-blocklist';
405 function getIndexField() {
406 return 'ipb_timestamp';
409 function getDefaultSort() {
410 return 'ipb_timestamp';
413 function isFieldSortable( $name ) {
414 return false;
418 * Do a LinkBatch query to minimise database load when generating all these links
419 * @param $result
421 function preprocessResults( $result ){
422 wfProfileIn( __METHOD__ );
423 # Do a link batch query
424 $lb = new LinkBatch;
425 $lb->setCaller( __METHOD__ );
427 $userids = array();
429 foreach ( $result as $row ) {
430 $userids[] = $row->ipb_by;
432 # Usernames and titles are in fact related by a simple substitution of space -> underscore
433 # The last few lines of Title::secureAndSplit() tell the story.
434 $name = str_replace( ' ', '_', $row->ipb_address );
435 $lb->add( NS_USER, $name );
436 $lb->add( NS_USER_TALK, $name );
439 $ua = UserArray::newFromIDs( $userids );
440 foreach( $ua as $user ){
441 $name = str_replace( ' ', '_', $user->getName() );
442 $lb->add( NS_USER, $name );
443 $lb->add( NS_USER_TALK, $name );
446 $lb->execute();
447 wfProfileOut( __METHOD__ );
452 * Items per page dropdown. Essentially a crap workaround for bug 32603.
454 * @todo Do not release 1.19 with this.
456 class HTMLBlockedUsersItemSelect extends HTMLSelectField {
458 * Basically don't do any validation. If it's a number that's fine. Also,
459 * add it to the list if it's not there already
461 * @param $value
462 * @param $alldata
463 * @return bool
465 function validate( $value, $alldata ) {
466 if ( $value == '' ) {
467 return true;
470 // Let folks pick an explicit limit not from our list, as long as it's a real numbr.
471 if ( !in_array( $value, $this->mParams['options'] ) && $value == intval( $value ) && $value > 0 ) {
472 // This adds the explicitly requested limit value to the drop-down,
473 // then makes sure it's sorted correctly so when we output the list
474 // later, the custom option doesn't just show up last.
475 $this->mParams['options'][ $this->mParent->getLanguage()->formatNum( $value ) ] = intval($value);
476 asort( $this->mParams['options'] );
479 return true;