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
21 * @ingroup SpecialPage
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' );
38 * Main execution point
40 * @param $par String title fragment
42 public function execute( $par ) {
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() );
65 # Just show the block list
69 'label-message' => 'ipadressorusername',
72 'default' => $this->target
,
75 'type' => 'multiselect',
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',
85 'class' => 'HTMLBlockedUsersItemSelect',
86 'label-message' => 'table_pager_limit_label',
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,
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( '' );
108 function showList() {
109 # Purge expired entries on one in every 10 queries
110 if ( !mt_rand( 0, 10 ) ) {
111 Block
::purgeExpired();
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
);
125 case Block
::TYPE_AUTO
:
126 $conds['ipb_id'] = $target;
130 case Block
::TYPE_RANGE
:
131 list( $start, $end ) = IP
::parseRange( $target );
132 $dbr = wfGetDB( DB_SLAVE
);
133 $conds[] = $dbr->makeList(
135 'ipb_address' => $target,
136 Block
::getRangeCond( $start, $end )
140 $conds['ipb_auto'] = 0;
143 case Block
::TYPE_USER
:
144 $conds['ipb_address'] = (string)$this->target
;
145 $conds['ipb_auto'] = 0;
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 ) ) {
174 Html
::element( 'h2', array(), $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
178 $pager = new BlockListPager( $this, $conds );
179 if ( $pager->getNumRows() ) {
181 $pager->getNavigationBar() .
183 $pager->getNavigationBar()
186 } elseif ( $this->target
) {
187 $out->addWikiMsg( 'ipblocklist-no-results' );
190 $out->addWikiMsg( 'ipblocklist-empty' );
193 if( count( $otherBlockLink ) ) {
198 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
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
{
215 * @param $page SpecialPage
216 * @param $conds Array
218 function __construct( $page, $conds ) {
220 $this->conds
= $conds;
221 $this->mDefaultDirection
= true;
222 parent
::__construct( $page->getContext() );
225 function getFieldNames() {
226 static $headers = null;
228 if ( $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();
245 function formatValue( $name, $value ) {
247 if ( $msg === null ) {
250 'createaccountblock',
253 'blocklist-nousertalk',
258 $msg = array_combine( $msg, array_map( array( $this, 'msg' ), $msg ) );
261 /** @var $row object */
262 $row = $this->mCurrentRow
;
267 case 'ipb_timestamp':
268 $formatted = $this->getLanguage()->userTimeAndDate( $value, $this->getUser() );
272 if( $row->ipb_auto
){
273 $formatted = $this->msg( 'autoblockid', $row->ipb_id
)->parse();
275 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
277 case Block
::TYPE_USER
:
279 $formatted = Linker
::userLink( $target->getId(), $target );
280 $formatted .= Linker
::userToolLinks(
284 Linker
::TOOL_LINKS_NOBLOCK
287 case Block
::TYPE_RANGE
:
288 $formatted = htmlspecialchars( $target );
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' ),
301 array( 'wpTarget' => "#{$row->ipb_id}" )
304 $links[] = Linker
::linkKnown(
305 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
308 $links[] = Linker
::linkKnown(
309 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
310 $msg['change-blocklink']
313 $formatted .= ' ' . Html
::rawElement(
315 array( 'class' => 'mw-blocklist-actions' ),
316 $this->msg( 'parentheses' )->rawParams(
317 $this->getLanguage()->pipeList( $links ) )->escaped()
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
);
327 $formatted = htmlspecialchars( $row->ipb_by_text
); // foreign user?
332 $formatted = Linker
::commentBlock( $value );
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 );
359 $formatted = "Unable to format $name";
366 function getQueryInfo() {
368 'tables' => array( 'ipblocks', 'user' ),
375 'user_name AS by_user_name',
380 'ipb_create_account',
381 'ipb_enable_autoblock',
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;
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 ) {
418 * Do a LinkBatch query to minimise database load when generating all these links
421 function preprocessResults( $result ){
422 wfProfileIn( __METHOD__
);
423 # Do a link batch query
425 $lb->setCaller( __METHOD__
);
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 );
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
465 function validate( $value, $alldata ) {
466 if ( $value == '' ) {
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'] );