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 # setup BlockListPager here to get the actual default Limit
69 $pager = $this->getBlockListPager();
71 # Just show the block list
75 'label-message' => 'ipaddressorusername',
78 'default' => $this->target
,
79 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
82 'type' => 'multiselect',
83 'options-messages' => array(
84 'blocklist-userblocks' => 'userblocks',
85 'blocklist-tempblocks' => 'tempblocks',
86 'blocklist-addressblocks' => 'addressblocks',
87 'blocklist-rangeblocks' => 'rangeblocks',
92 'type' => 'limitselect',
93 'label-message' => 'table_pager_limit_label',
95 $lang->formatNum( 20 ) => 20,
96 $lang->formatNum( 50 ) => 50,
97 $lang->formatNum( 100 ) => 100,
98 $lang->formatNum( 250 ) => 250,
99 $lang->formatNum( 500 ) => 500,
102 'default' => $pager->getLimit(),
105 $context = new DerivativeContext( $this->getContext() );
106 $context->setTitle( $this->getPageTitle() ); // Remove subpage
107 $form = new HTMLForm( $fields, $context );
108 $form->setMethod( 'get' );
109 $form->setWrapperLegendMsg( 'ipblocklist-legend' );
110 $form->setSubmitTextMsg( 'ipblocklist-submit' );
111 $form->setSubmitProgressive();
112 $form->prepareForm();
114 $form->displayForm( '' );
115 $this->showList( $pager );
119 * Setup a new BlockListPager instance.
120 * @return BlockListPager
122 protected function getBlockListPager() {
124 # Is the user allowed to see hidden blocks?
125 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
126 $conds['ipb_deleted'] = 0;
129 if ( $this->target
!== '' ) {
130 list( $target, $type ) = Block
::parseTarget( $this->target
);
134 case Block
::TYPE_AUTO
:
135 $conds['ipb_id'] = $target;
139 case Block
::TYPE_RANGE
:
140 list( $start, $end ) = IP
::parseRange( $target );
141 $dbr = wfGetDB( DB_SLAVE
);
142 $conds[] = $dbr->makeList(
144 'ipb_address' => $target,
145 Block
::getRangeCond( $start, $end )
149 $conds['ipb_auto'] = 0;
152 case Block
::TYPE_USER
:
153 $conds['ipb_address'] = $target->getName();
154 $conds['ipb_auto'] = 0;
160 if ( in_array( 'userblocks', $this->options
) ) {
161 $conds['ipb_user'] = 0;
163 if ( in_array( 'tempblocks', $this->options
) ) {
164 $conds['ipb_expiry'] = 'infinity';
166 if ( in_array( 'addressblocks', $this->options
) ) {
167 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
169 if ( in_array( 'rangeblocks', $this->options
) ) {
170 $conds[] = "ipb_range_end = ipb_range_start";
173 return new BlockListPager( $this, $conds );
177 * Show the list of blocked accounts matching the actual filter.
178 * @param BlockListPager $pager The BlockListPager instance for this page
180 protected function showList( BlockListPager
$pager ) {
181 $out = $this->getOutput();
183 # Check for other blocks, i.e. global/tor blocks
184 $otherBlockLink = array();
185 Hooks
::run( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target
) );
187 # Show additional header for the local block only when other blocks exists.
188 # Not necessary in a standard installation without such extensions enabled
189 if ( count( $otherBlockLink ) ) {
191 Html
::element( 'h2', array(), $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
195 if ( $pager->getNumRows() ) {
196 $out->addParserOutputContent( $pager->getFullOutput() );
197 } elseif ( $this->target
) {
198 $out->addWikiMsg( 'ipblocklist-no-results' );
200 $out->addWikiMsg( 'ipblocklist-empty' );
203 if ( count( $otherBlockLink ) ) {
208 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
212 foreach ( $otherBlockLink as $link ) {
213 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
215 $out->addHTML( Html
::rawElement(
217 array( 'class' => 'mw-ipblocklist-otherblocks' ),
223 protected function getGroupName() {
228 class BlockListPager
extends TablePager
{
233 * @param SpecialPage $page
234 * @param array $conds
236 function __construct( $page, $conds ) {
238 $this->conds
= $conds;
239 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
240 parent
::__construct( $page->getContext() );
243 function getFieldNames() {
244 static $headers = null;
246 if ( $headers === null ) {
248 'ipb_timestamp' => 'blocklist-timestamp',
249 'ipb_target' => 'blocklist-target',
250 'ipb_expiry' => 'blocklist-expiry',
251 'ipb_by' => 'blocklist-by',
252 'ipb_params' => 'blocklist-params',
253 'ipb_reason' => 'blocklist-reason',
255 foreach ( $headers as $key => $val ) {
256 $headers[$key] = $this->msg( $val )->text();
263 function formatValue( $name, $value ) {
265 if ( $msg === null ) {
268 'createaccountblock',
271 'blocklist-nousertalk',
276 foreach ( $keys as $key ) {
277 $msg[$key] = $this->msg( $key )->escaped();
281 /** @var $row object */
282 $row = $this->mCurrentRow
;
284 $language = $this->getLanguage();
289 case 'ipb_timestamp':
290 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
294 if ( $row->ipb_auto
) {
295 $formatted = $this->msg( 'autoblockid', $row->ipb_id
)->parse();
297 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
299 case Block
::TYPE_USER
:
301 $formatted = Linker
::userLink( $target->getId(), $target );
302 $formatted .= Linker
::userToolLinks(
306 Linker
::TOOL_LINKS_NOBLOCK
309 case Block
::TYPE_RANGE
:
310 $formatted = htmlspecialchars( $target );
316 $formatted = htmlspecialchars( $language->formatExpiry(
318 /* User preference timezone */true
320 if ( $this->getUser()->isAllowed( 'block' ) ) {
321 if ( $row->ipb_auto
) {
322 $links[] = Linker
::linkKnown(
323 SpecialPage
::getTitleFor( 'Unblock' ),
326 array( 'wpTarget' => "#{$row->ipb_id}" )
329 $links[] = Linker
::linkKnown(
330 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
333 $links[] = Linker
::linkKnown(
334 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
335 $msg['change-blocklink']
338 $formatted .= ' ' . Html
::rawElement(
340 array( 'class' => 'mw-blocklist-actions' ),
341 $this->msg( 'parentheses' )->rawParams(
342 $language->pipeList( $links ) )->escaped()
348 if ( isset( $row->by_user_name
) ) {
349 $formatted = Linker
::userLink( $value, $row->by_user_name
);
350 $formatted .= Linker
::userToolLinks( $value, $row->by_user_name
);
352 $formatted = htmlspecialchars( $row->ipb_by_text
); // foreign user?
357 $formatted = Linker
::formatComment( $value );
361 $properties = array();
362 if ( $row->ipb_anon_only
) {
363 $properties[] = $msg['anononlyblock'];
365 if ( $row->ipb_create_account
) {
366 $properties[] = $msg['createaccountblock'];
368 if ( $row->ipb_user
&& !$row->ipb_enable_autoblock
) {
369 $properties[] = $msg['noautoblockblock'];
372 if ( $row->ipb_block_email
) {
373 $properties[] = $msg['emailblock'];
376 if ( !$row->ipb_allow_usertalk
) {
377 $properties[] = $msg['blocklist-nousertalk'];
380 $formatted = $language->commaList( $properties );
384 $formatted = "Unable to format $name";
391 function getQueryInfo() {
393 'tables' => array( 'ipblocks', 'user' ),
400 'by_user_name' => 'user_name',
405 'ipb_create_account',
406 'ipb_enable_autoblock',
412 'ipb_allow_usertalk',
414 'conds' => $this->conds
,
415 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
418 # Filter out any expired blocks
419 $db = $this->getDatabase();
420 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
422 # Is the user allowed to see hidden blocks?
423 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
424 $info['conds']['ipb_deleted'] = 0;
430 public function getTableClass() {
431 return parent
::getTableClass() . ' mw-blocklist';
434 function getIndexField() {
435 return 'ipb_timestamp';
438 function getDefaultSort() {
439 return 'ipb_timestamp';
442 function isFieldSortable( $name ) {
447 * Do a LinkBatch query to minimise database load when generating all these links
448 * @param ResultWrapper $result
450 function preprocessResults( $result ) {
451 # Do a link batch query
453 $lb->setCaller( __METHOD__
);
455 foreach ( $result as $row ) {
456 $lb->add( NS_USER
, $row->ipb_address
);
457 $lb->add( NS_USER_TALK
, $row->ipb_address
);
459 if ( isset( $row->by_user_name
) ) {
460 $lb->add( NS_USER
, $row->by_user_name
);
461 $lb->add( NS_USER_TALK
, $row->by_user_name
);