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
{
34 function __construct() {
35 parent
::__construct( 'BlockList' );
39 * Main execution point
41 * @param string $par Title fragment
43 public function execute( $par ) {
45 $this->outputHeader();
46 $out = $this->getOutput();
47 $lang = $this->getLanguage();
48 $out->setPageTitle( $this->msg( 'ipblocklist' ) );
49 $out->addModuleStyles( 'mediawiki.special' );
51 $request = $this->getRequest();
52 $par = $request->getVal( 'ip', $par );
53 $this->target
= trim( $request->getVal( 'wpTarget', $par ) );
55 $this->options
= $request->getArray( 'wpOptions', array() );
57 $action = $request->getText( 'action' );
59 if ( $action == 'unblock' ||
$action == 'submit' && $request->wasPosted() ) {
60 # B/C @since 1.18: Unblock interface is now at Special:Unblock
61 $title = SpecialPage
::getTitleFor( 'Unblock', $this->target
);
62 $out->redirect( $title->getFullURL() );
67 # Just show the block list
71 'label-message' => 'ipaddressorusername',
74 'default' => $this->target
,
77 'type' => 'multiselect',
79 $this->msg( 'blocklist-userblocks' )->text() => 'userblocks',
80 $this->msg( 'blocklist-tempblocks' )->text() => 'tempblocks',
81 $this->msg( 'blocklist-addressblocks' )->text() => 'addressblocks',
82 $this->msg( 'blocklist-rangeblocks' )->text() => 'rangeblocks',
87 'type' => 'limitselect',
88 'label-message' => 'table_pager_limit_label',
90 $lang->formatNum( 20 ) => 20,
91 $lang->formatNum( 50 ) => 50,
92 $lang->formatNum( 100 ) => 100,
93 $lang->formatNum( 250 ) => 250,
94 $lang->formatNum( 500 ) => 500,
100 $context = new DerivativeContext( $this->getContext() );
101 $context->setTitle( $this->getPageTitle() ); // Remove subpage
102 $form = new HTMLForm( $fields, $context );
103 $form->setMethod( 'get' );
104 $form->setWrapperLegendMsg( 'ipblocklist-legend' );
105 $form->setSubmitTextMsg( 'ipblocklist-submit' );
106 $form->prepareForm();
108 $form->displayForm( '' );
112 function showList() {
113 # Purge expired entries on one in every 10 queries
114 if ( !mt_rand( 0, 10 ) ) {
115 Block
::purgeExpired();
119 # Is the user allowed to see hidden blocks?
120 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
121 $conds['ipb_deleted'] = 0;
124 if ( $this->target
!== '' ) {
125 list( $target, $type ) = Block
::parseTarget( $this->target
);
129 case Block
::TYPE_AUTO
:
130 $conds['ipb_id'] = $target;
134 case Block
::TYPE_RANGE
:
135 list( $start, $end ) = IP
::parseRange( $target );
136 $dbr = wfGetDB( DB_SLAVE
);
137 $conds[] = $dbr->makeList(
139 'ipb_address' => $target,
140 Block
::getRangeCond( $start, $end )
144 $conds['ipb_auto'] = 0;
147 case Block
::TYPE_USER
:
148 $conds['ipb_address'] = $target->getName();
149 $conds['ipb_auto'] = 0;
155 if ( in_array( 'userblocks', $this->options
) ) {
156 $conds['ipb_user'] = 0;
158 if ( in_array( 'tempblocks', $this->options
) ) {
159 $conds['ipb_expiry'] = 'infinity';
161 if ( in_array( 'addressblocks', $this->options
) ) {
162 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
164 if ( in_array( 'rangeblocks', $this->options
) ) {
165 $conds[] = "ipb_range_end = ipb_range_start";
168 # Check for other blocks, i.e. global/tor blocks
169 $otherBlockLink = array();
170 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target
) );
172 $out = $this->getOutput();
174 # Show additional header for the local block only when other blocks exists.
175 # Not necessary in a standard installation without such extensions enabled
176 if ( count( $otherBlockLink ) ) {
178 Html
::element( 'h2', array(), $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
182 $pager = new BlockListPager( $this, $conds );
183 if ( $pager->getNumRows() ) {
184 $out->addParserOutputContent( $pager->getFullOutput() );
185 } elseif ( $this->target
) {
186 $out->addWikiMsg( 'ipblocklist-no-results' );
188 $out->addWikiMsg( 'ipblocklist-empty' );
191 if ( count( $otherBlockLink ) ) {
196 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
200 foreach ( $otherBlockLink as $link ) {
201 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
203 $out->addHTML( Html
::rawElement(
205 array( 'class' => 'mw-ipblocklist-otherblocks' ),
211 protected function getGroupName() {
216 class BlockListPager
extends TablePager
{
221 * @param SpecialPage $page
222 * @param array $conds
224 function __construct( $page, $conds ) {
226 $this->conds
= $conds;
227 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
228 parent
::__construct( $page->getContext() );
231 function getFieldNames() {
232 static $headers = null;
234 if ( $headers === null ) {
236 'ipb_timestamp' => 'blocklist-timestamp',
237 'ipb_target' => 'blocklist-target',
238 'ipb_expiry' => 'blocklist-expiry',
239 'ipb_by' => 'blocklist-by',
240 'ipb_params' => 'blocklist-params',
241 'ipb_reason' => 'blocklist-reason',
243 foreach ( $headers as $key => $val ) {
244 $headers[$key] = $this->msg( $val )->text();
251 function formatValue( $name, $value ) {
253 if ( $msg === null ) {
256 'createaccountblock',
259 'blocklist-nousertalk',
264 $msg = array_combine( $msg, array_map( array( $this, 'msg' ), $msg ) );
267 /** @var $row object */
268 $row = $this->mCurrentRow
;
273 case 'ipb_timestamp':
274 $formatted = $this->getLanguage()->userTimeAndDate( $value, $this->getUser() );
278 if ( $row->ipb_auto
) {
279 $formatted = $this->msg( 'autoblockid', $row->ipb_id
)->parse();
281 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
283 case Block
::TYPE_USER
:
285 $formatted = Linker
::userLink( $target->getId(), $target );
286 $formatted .= Linker
::userToolLinks(
290 Linker
::TOOL_LINKS_NOBLOCK
293 case Block
::TYPE_RANGE
:
294 $formatted = htmlspecialchars( $target );
300 $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */true );
301 if ( $this->getUser()->isAllowed( 'block' ) ) {
302 if ( $row->ipb_auto
) {
303 $links[] = Linker
::linkKnown(
304 SpecialPage
::getTitleFor( 'Unblock' ),
307 array( 'wpTarget' => "#{$row->ipb_id}" )
310 $links[] = Linker
::linkKnown(
311 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
314 $links[] = Linker
::linkKnown(
315 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
316 $msg['change-blocklink']
319 $formatted .= ' ' . Html
::rawElement(
321 array( 'class' => 'mw-blocklist-actions' ),
322 $this->msg( 'parentheses' )->rawParams(
323 $this->getLanguage()->pipeList( $links ) )->escaped()
329 if ( isset( $row->by_user_name
) ) {
330 $formatted = Linker
::userLink( $value, $row->by_user_name
);
331 $formatted .= Linker
::userToolLinks( $value, $row->by_user_name
);
333 $formatted = htmlspecialchars( $row->ipb_by_text
); // foreign user?
338 $formatted = Linker
::formatComment( $value );
342 $properties = array();
343 if ( $row->ipb_anon_only
) {
344 $properties[] = $msg['anononlyblock'];
346 if ( $row->ipb_create_account
) {
347 $properties[] = $msg['createaccountblock'];
349 if ( $row->ipb_user
&& !$row->ipb_enable_autoblock
) {
350 $properties[] = $msg['noautoblockblock'];
353 if ( $row->ipb_block_email
) {
354 $properties[] = $msg['emailblock'];
357 if ( !$row->ipb_allow_usertalk
) {
358 $properties[] = $msg['blocklist-nousertalk'];
361 $formatted = $this->getLanguage()->commaList( $properties );
365 $formatted = "Unable to format $name";
372 function getQueryInfo() {
374 'tables' => array( 'ipblocks', 'user' ),
381 'by_user_name' => 'user_name',
386 'ipb_create_account',
387 'ipb_enable_autoblock',
393 'ipb_allow_usertalk',
395 'conds' => $this->conds
,
396 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
399 # Is the user allowed to see hidden blocks?
400 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
401 $info['conds']['ipb_deleted'] = 0;
407 public function getTableClass() {
408 return parent
::getTableClass() . ' mw-blocklist';
411 function getIndexField() {
412 return 'ipb_timestamp';
415 function getDefaultSort() {
416 return 'ipb_timestamp';
419 function isFieldSortable( $name ) {
424 * Do a LinkBatch query to minimise database load when generating all these links
425 * @param ResultWrapper $result
427 function preprocessResults( $result ) {
428 wfProfileIn( __METHOD__
);
429 # Do a link batch query
431 $lb->setCaller( __METHOD__
);
435 foreach ( $result as $row ) {
436 $userids[] = $row->ipb_by
;
438 # Usernames and titles are in fact related by a simple substitution of space -> underscore
439 # The last few lines of Title::secureAndSplit() tell the story.
440 $name = str_replace( ' ', '_', $row->ipb_address
);
441 $lb->add( NS_USER
, $name );
442 $lb->add( NS_USER_TALK
, $name );
445 $ua = UserArray
::newFromIDs( $userids );
446 foreach ( $ua as $user ) {
447 $name = str_replace( ' ', '_', $user->getName() );
448 $lb->add( NS_USER
, $name );
449 $lb->add( NS_USER_TALK
, $name );
453 wfProfileOut( __METHOD__
);