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' );
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() );
68 # Just show the block list
72 'label-message' => 'ipaddressorusername',
75 'default' => $this->target
,
76 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
79 'type' => 'multiselect',
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',
89 'type' => 'limitselect',
90 'label-message' => 'table_pager_limit_label',
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,
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( '' );
115 function showList() {
117 # Is the user allowed to see hidden blocks?
118 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
119 $conds['ipb_deleted'] = 0;
122 if ( $this->target
!== '' ) {
123 list( $target, $type ) = Block
::parseTarget( $this->target
);
127 case Block
::TYPE_AUTO
:
128 $conds['ipb_id'] = $target;
132 case Block
::TYPE_RANGE
:
133 list( $start, $end ) = IP
::parseRange( $target );
134 $dbr = wfGetDB( DB_SLAVE
);
135 $conds[] = $dbr->makeList(
137 'ipb_address' => $target,
138 Block
::getRangeCond( $start, $end )
142 $conds['ipb_auto'] = 0;
145 case Block
::TYPE_USER
:
146 $conds['ipb_address'] = $target->getName();
147 $conds['ipb_auto'] = 0;
153 if ( in_array( 'userblocks', $this->options
) ) {
154 $conds['ipb_user'] = 0;
156 if ( in_array( 'tempblocks', $this->options
) ) {
157 $conds['ipb_expiry'] = 'infinity';
159 if ( in_array( 'addressblocks', $this->options
) ) {
160 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
162 if ( in_array( 'rangeblocks', $this->options
) ) {
163 $conds[] = "ipb_range_end = ipb_range_start";
166 # Check for other blocks, i.e. global/tor blocks
167 $otherBlockLink = array();
168 Hooks
::run( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target
) );
170 $out = $this->getOutput();
172 # Show additional header for the local block only when other blocks exists.
173 # Not necessary in a standard installation without such extensions enabled
174 if ( count( $otherBlockLink ) ) {
176 Html
::element( 'h2', array(), $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
180 $pager = new BlockListPager( $this, $conds );
181 if ( $pager->getNumRows() ) {
182 $out->addParserOutputContent( $pager->getFullOutput() );
183 } elseif ( $this->target
) {
184 $out->addWikiMsg( 'ipblocklist-no-results' );
186 $out->addWikiMsg( 'ipblocklist-empty' );
189 if ( count( $otherBlockLink ) ) {
194 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
198 foreach ( $otherBlockLink as $link ) {
199 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
201 $out->addHTML( Html
::rawElement(
203 array( 'class' => 'mw-ipblocklist-otherblocks' ),
209 protected function getGroupName() {
214 class BlockListPager
extends TablePager
{
219 * @param SpecialPage $page
220 * @param array $conds
222 function __construct( $page, $conds ) {
224 $this->conds
= $conds;
225 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
226 parent
::__construct( $page->getContext() );
229 function getFieldNames() {
230 static $headers = null;
232 if ( $headers === null ) {
234 'ipb_timestamp' => 'blocklist-timestamp',
235 'ipb_target' => 'blocklist-target',
236 'ipb_expiry' => 'blocklist-expiry',
237 'ipb_by' => 'blocklist-by',
238 'ipb_params' => 'blocklist-params',
239 'ipb_reason' => 'blocklist-reason',
241 foreach ( $headers as $key => $val ) {
242 $headers[$key] = $this->msg( $val )->text();
249 function formatValue( $name, $value ) {
251 if ( $msg === null ) {
254 'createaccountblock',
257 'blocklist-nousertalk',
261 $msg = array_combine( $msg, array_map( array( $this, 'msg' ), $msg ) );
264 /** @var $row object */
265 $row = $this->mCurrentRow
;
270 case 'ipb_timestamp':
271 $formatted = $this->getLanguage()->userTimeAndDate( $value, $this->getUser() );
275 if ( $row->ipb_auto
) {
276 $formatted = $this->msg( 'autoblockid', $row->ipb_id
)->parse();
278 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
280 case Block
::TYPE_USER
:
282 $formatted = Linker
::userLink( $target->getId(), $target );
283 $formatted .= Linker
::userToolLinks(
287 Linker
::TOOL_LINKS_NOBLOCK
290 case Block
::TYPE_RANGE
:
291 $formatted = htmlspecialchars( $target );
297 $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */true );
298 if ( $this->getUser()->isAllowed( 'block' ) ) {
299 if ( $row->ipb_auto
) {
300 $links[] = Linker
::linkKnown(
301 SpecialPage
::getTitleFor( 'Unblock' ),
304 array( 'wpTarget' => "#{$row->ipb_id}" )
307 $links[] = Linker
::linkKnown(
308 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
311 $links[] = Linker
::linkKnown(
312 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
313 $msg['change-blocklink']
316 $formatted .= ' ' . Html
::rawElement(
318 array( 'class' => 'mw-blocklist-actions' ),
319 $this->msg( 'parentheses' )->rawParams(
320 $this->getLanguage()->pipeList( $links ) )->escaped()
326 if ( isset( $row->by_user_name
) ) {
327 $formatted = Linker
::userLink( $value, $row->by_user_name
);
328 $formatted .= Linker
::userToolLinks( $value, $row->by_user_name
);
330 $formatted = htmlspecialchars( $row->ipb_by_text
); // foreign user?
335 $formatted = Linker
::formatComment( $value );
339 $properties = array();
340 if ( $row->ipb_anon_only
) {
341 $properties[] = $msg['anononlyblock'];
343 if ( $row->ipb_create_account
) {
344 $properties[] = $msg['createaccountblock'];
346 if ( $row->ipb_user
&& !$row->ipb_enable_autoblock
) {
347 $properties[] = $msg['noautoblockblock'];
350 if ( $row->ipb_block_email
) {
351 $properties[] = $msg['emailblock'];
354 if ( !$row->ipb_allow_usertalk
) {
355 $properties[] = $msg['blocklist-nousertalk'];
358 $formatted = $this->getLanguage()->commaList( $properties );
362 $formatted = "Unable to format $name";
369 function getQueryInfo() {
371 'tables' => array( 'ipblocks', 'user' ),
378 'by_user_name' => 'user_name',
383 'ipb_create_account',
384 'ipb_enable_autoblock',
390 'ipb_allow_usertalk',
392 'conds' => $this->conds
,
393 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
396 # Filter out any expired blocks
397 $db = $this->getDatabase();
398 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
400 # Is the user allowed to see hidden blocks?
401 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
402 $info['conds']['ipb_deleted'] = 0;
408 public function getTableClass() {
409 return parent
::getTableClass() . ' mw-blocklist';
412 function getIndexField() {
413 return 'ipb_timestamp';
416 function getDefaultSort() {
417 return 'ipb_timestamp';
420 function isFieldSortable( $name ) {
425 * Do a LinkBatch query to minimise database load when generating all these links
426 * @param ResultWrapper $result
428 function preprocessResults( $result ) {
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 );