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() {
116 # Purge expired entries on one in every 10 queries
117 if ( !mt_rand( 0, 10 ) ) {
118 Block
::purgeExpired();
122 # Is the user allowed to see hidden blocks?
123 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
124 $conds['ipb_deleted'] = 0;
127 if ( $this->target
!== '' ) {
128 list( $target, $type ) = Block
::parseTarget( $this->target
);
132 case Block
::TYPE_AUTO
:
133 $conds['ipb_id'] = $target;
137 case Block
::TYPE_RANGE
:
138 list( $start, $end ) = IP
::parseRange( $target );
139 $dbr = wfGetDB( DB_SLAVE
);
140 $conds[] = $dbr->makeList(
142 'ipb_address' => $target,
143 Block
::getRangeCond( $start, $end )
147 $conds['ipb_auto'] = 0;
150 case Block
::TYPE_USER
:
151 $conds['ipb_address'] = $target->getName();
152 $conds['ipb_auto'] = 0;
158 if ( in_array( 'userblocks', $this->options
) ) {
159 $conds['ipb_user'] = 0;
161 if ( in_array( 'tempblocks', $this->options
) ) {
162 $conds['ipb_expiry'] = 'infinity';
164 if ( in_array( 'addressblocks', $this->options
) ) {
165 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
167 if ( in_array( 'rangeblocks', $this->options
) ) {
168 $conds[] = "ipb_range_end = ipb_range_start";
171 # Check for other blocks, i.e. global/tor blocks
172 $otherBlockLink = array();
173 Hooks
::run( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target
) );
175 $out = $this->getOutput();
177 # Show additional header for the local block only when other blocks exists.
178 # Not necessary in a standard installation without such extensions enabled
179 if ( count( $otherBlockLink ) ) {
181 Html
::element( 'h2', array(), $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
185 $pager = new BlockListPager( $this, $conds );
186 if ( $pager->getNumRows() ) {
187 $out->addParserOutputContent( $pager->getFullOutput() );
188 } elseif ( $this->target
) {
189 $out->addWikiMsg( 'ipblocklist-no-results' );
191 $out->addWikiMsg( 'ipblocklist-empty' );
194 if ( count( $otherBlockLink ) ) {
199 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
203 foreach ( $otherBlockLink as $link ) {
204 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
206 $out->addHTML( Html
::rawElement(
208 array( 'class' => 'mw-ipblocklist-otherblocks' ),
214 protected function getGroupName() {
219 class BlockListPager
extends TablePager
{
224 * @param SpecialPage $page
225 * @param array $conds
227 function __construct( $page, $conds ) {
229 $this->conds
= $conds;
230 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
231 parent
::__construct( $page->getContext() );
234 function getFieldNames() {
235 static $headers = null;
237 if ( $headers === null ) {
239 'ipb_timestamp' => 'blocklist-timestamp',
240 'ipb_target' => 'blocklist-target',
241 'ipb_expiry' => 'blocklist-expiry',
242 'ipb_by' => 'blocklist-by',
243 'ipb_params' => 'blocklist-params',
244 'ipb_reason' => 'blocklist-reason',
246 foreach ( $headers as $key => $val ) {
247 $headers[$key] = $this->msg( $val )->text();
254 function formatValue( $name, $value ) {
256 if ( $msg === null ) {
259 'createaccountblock',
262 'blocklist-nousertalk',
266 $msg = array_combine( $msg, array_map( array( $this, 'msg' ), $msg ) );
269 /** @var $row object */
270 $row = $this->mCurrentRow
;
275 case 'ipb_timestamp':
276 $formatted = $this->getLanguage()->userTimeAndDate( $value, $this->getUser() );
280 if ( $row->ipb_auto
) {
281 $formatted = $this->msg( 'autoblockid', $row->ipb_id
)->parse();
283 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
285 case Block
::TYPE_USER
:
287 $formatted = Linker
::userLink( $target->getId(), $target );
288 $formatted .= Linker
::userToolLinks(
292 Linker
::TOOL_LINKS_NOBLOCK
295 case Block
::TYPE_RANGE
:
296 $formatted = htmlspecialchars( $target );
302 $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */true );
303 if ( $this->getUser()->isAllowed( 'block' ) ) {
304 if ( $row->ipb_auto
) {
305 $links[] = Linker
::linkKnown(
306 SpecialPage
::getTitleFor( 'Unblock' ),
309 array( 'wpTarget' => "#{$row->ipb_id}" )
312 $links[] = Linker
::linkKnown(
313 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
316 $links[] = Linker
::linkKnown(
317 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
318 $msg['change-blocklink']
321 $formatted .= ' ' . Html
::rawElement(
323 array( 'class' => 'mw-blocklist-actions' ),
324 $this->msg( 'parentheses' )->rawParams(
325 $this->getLanguage()->pipeList( $links ) )->escaped()
331 if ( isset( $row->by_user_name
) ) {
332 $formatted = Linker
::userLink( $value, $row->by_user_name
);
333 $formatted .= Linker
::userToolLinks( $value, $row->by_user_name
);
335 $formatted = htmlspecialchars( $row->ipb_by_text
); // foreign user?
340 $formatted = Linker
::formatComment( $value );
344 $properties = array();
345 if ( $row->ipb_anon_only
) {
346 $properties[] = $msg['anononlyblock'];
348 if ( $row->ipb_create_account
) {
349 $properties[] = $msg['createaccountblock'];
351 if ( $row->ipb_user
&& !$row->ipb_enable_autoblock
) {
352 $properties[] = $msg['noautoblockblock'];
355 if ( $row->ipb_block_email
) {
356 $properties[] = $msg['emailblock'];
359 if ( !$row->ipb_allow_usertalk
) {
360 $properties[] = $msg['blocklist-nousertalk'];
363 $formatted = $this->getLanguage()->commaList( $properties );
367 $formatted = "Unable to format $name";
374 function getQueryInfo() {
376 'tables' => array( 'ipblocks', 'user' ),
383 'by_user_name' => 'user_name',
388 'ipb_create_account',
389 'ipb_enable_autoblock',
395 'ipb_allow_usertalk',
397 'conds' => $this->conds
,
398 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
401 # Is the user allowed to see hidden blocks?
402 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
403 $info['conds']['ipb_deleted'] = 0;
409 public function getTableClass() {
410 return parent
::getTableClass() . ' mw-blocklist';
413 function getIndexField() {
414 return 'ipb_timestamp';
417 function getDefaultSort() {
418 return 'ipb_timestamp';
421 function isFieldSortable( $name ) {
426 * Do a LinkBatch query to minimise database load when generating all these links
427 * @param ResultWrapper $result
429 function preprocessResults( $result ) {
430 # Do a link batch query
432 $lb->setCaller( __METHOD__
);
436 foreach ( $result as $row ) {
437 $userids[] = $row->ipb_by
;
439 # Usernames and titles are in fact related by a simple substitution of space -> underscore
440 # The last few lines of Title::secureAndSplit() tell the story.
441 $name = str_replace( ' ', '_', $row->ipb_address
);
442 $lb->add( NS_USER
, $name );
443 $lb->add( NS_USER_TALK
, $name );
446 $ua = UserArray
::newFromIDs( $userids );
447 foreach ( $ua as $user ) {
448 $name = str_replace( ' ', '_', $user->getName() );
449 $lb->add( NS_USER
, $name );
450 $lb->add( NS_USER_TALK
, $name );