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 $out->setPageTitle( wfMsg( 'ipblocklist' ) );
47 $out->addModuleStyles( 'mediawiki.special' );
49 $request = $this->getRequest();
50 $par = $request->getVal( 'ip', $par );
51 $this->target
= trim( $request->getVal( 'wpTarget', $par ) );
53 $this->options
= $request->getArray( 'wpOptions', array() );
55 $action = $request->getText( 'action' );
57 if( $action == 'unblock' ||
$action == 'submit' && $request->wasPosted() ) {
58 # B/C @since 1.18: Unblock interface is now at Special:Unblock
59 $title = SpecialPage
::getTitleFor( 'Unblock', $this->target
);
60 $out->redirect( $title->getFullUrl() );
64 # Just show the block list
68 'label-message' => 'ipadressorusername',
73 'type' => 'multiselect',
75 wfMsg( 'blocklist-userblocks' ) => 'userblocks',
76 wfMsg( 'blocklist-tempblocks' ) => 'tempblocks',
77 wfMsg( 'blocklist-addressblocks' ) => 'addressblocks',
78 wfMsg( 'blocklist-rangeblocks' ) => 'rangeblocks',
83 $form = new HTMLForm( $fields, $this->getContext() );
84 $form->setMethod( 'get' );
85 $form->setWrapperLegend( wfMsg( 'ipblocklist-legend' ) );
86 $form->setSubmitText( wfMsg( 'ipblocklist-submit' ) );
89 $form->displayForm( '' );
94 # Purge expired entries on one in every 10 queries
95 if ( !mt_rand( 0, 10 ) ) {
96 Block
::purgeExpired();
100 # Is the user allowed to see hidden blocks?
101 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
102 $conds['ipb_deleted'] = 0;
105 if ( $this->target
!== '' ){
106 list( $target, $type ) = Block
::parseTarget( $this->target
);
110 case Block
::TYPE_AUTO
:
111 $conds['ipb_id'] = $target;
115 case Block
::TYPE_RANGE
:
116 list( $start, $end ) = IP
::parseRange( $target );
117 $dbr = wfGetDB( DB_SLAVE
);
118 $conds[] = $dbr->makeList(
120 'ipb_address' => $target,
121 Block
::getRangeCond( $start, $end )
125 $conds['ipb_auto'] = 0;
128 case Block
::TYPE_USER
:
129 $conds['ipb_address'] = (string)$this->target
;
130 $conds['ipb_auto'] = 0;
136 if( in_array( 'userblocks', $this->options
) ) {
137 $conds['ipb_user'] = 0;
139 if( in_array( 'tempblocks', $this->options
) ) {
140 $conds['ipb_expiry'] = 'infinity';
142 if( in_array( 'addressblocks', $this->options
) ) {
143 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
145 if( in_array( 'rangeblocks', $this->options
) ) {
146 $conds[] = "ipb_range_end = ipb_range_start";
149 # Check for other blocks, i.e. global/tor blocks
150 $otherBlockLink = array();
151 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target
) );
153 $out = $this->getOutput();
155 # Show additional header for the local block only when other blocks exists.
156 # Not necessary in a standard installation without such extensions enabled
157 if( count( $otherBlockLink ) ) {
159 Html
::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
163 $pager = new BlockListPager( $this, $conds );
164 if ( $pager->getNumRows() ) {
166 $pager->getNavigationBar() .
168 $pager->getNavigationBar()
171 } elseif ( $this->target
) {
172 $out->addWikiMsg( 'ipblocklist-no-results' );
175 $out->addWikiMsg( 'ipblocklist-empty' );
178 if( count( $otherBlockLink ) ) {
184 'ipblocklist-otherblocks',
186 count( $otherBlockLink )
191 foreach( $otherBlockLink as $link ) {
192 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
194 $out->addHTML( Html
::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
199 class BlockListPager
extends TablePager
{
204 * Getting the user names from the userids stored in the ipb_by column can be
205 * expensive, so we cache the data here.
206 * @var Array of ID => Name
208 private $userNameCache;
211 * @param $page SpecialPage
212 * @param $conds Array
214 function __construct( $page, $conds ) {
216 $this->conds
= $conds;
217 $this->mDefaultDirection
= true;
218 parent
::__construct( $page->getContext() );
221 function getFieldNames() {
222 static $headers = null;
224 if ( $headers == array() ) {
226 'ipb_timestamp' => 'blocklist-timestamp',
227 'ipb_target' => 'blocklist-target',
228 'ipb_expiry' => 'blocklist-expiry',
229 'ipb_by' => 'blocklist-by',
230 'ipb_params' => 'blocklist-params',
231 'ipb_reason' => 'blocklist-reason',
233 $headers = array_map( 'wfMsg', $headers );
239 function formatValue( $name, $value ) {
241 if ( $msg === null ) {
244 'createaccountblock',
247 'blocklist-nousertalk',
252 $msg = array_combine( $msg, array_map( 'wfMessage', $msg ) );
255 /** @var $row object */
256 $row = $this->mCurrentRow
;
261 case 'ipb_timestamp':
262 $formatted = $this->getLang()->timeanddate( $value, /* User preference timezone */ true );
266 if( $row->ipb_auto
){
267 $formatted = wfMessage( 'autoblockid', $row->ipb_id
)->parse();
269 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
271 case Block
::TYPE_USER
:
273 $formatted = Linker
::userLink( $target->getId(), $target );
274 $formatted .= Linker
::userToolLinks(
278 Linker
::TOOL_LINKS_NOBLOCK
281 case Block
::TYPE_RANGE
:
282 $formatted = htmlspecialchars( $target );
288 $formatted = $this->getLang()->formatExpiry( $value, /* User preference timezone */ true );
289 if( $this->getUser()->isAllowed( 'block' ) ){
290 if( $row->ipb_auto
){
291 $links[] = Linker
::linkKnown(
292 SpecialPage
::getTitleFor( 'Unblock' ),
295 array( 'wpTarget' => "#{$row->ipb_id}" )
298 $links[] = Linker
::linkKnown(
299 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
302 $links[] = Linker
::linkKnown(
303 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
304 $msg['change-blocklink']
307 $formatted .= ' ' . Html
::rawElement(
309 array( 'class' => 'mw-blocklist-actions' ),
310 wfMsg( 'parentheses', $this->getLang()->pipeList( $links ) )
316 $username = array_key_exists( $value, $this->userNameCache
)
317 ?
$this->userNameCache
[$value]
318 : User
::newFromId( $value )->getName();
320 $formatted = Linker
::userLink( $value, $username );
321 $formatted .= Linker
::userToolLinks( $value, $username );
325 $formatted = Linker
::commentBlock( $value );
329 $properties = array();
330 if ( $row->ipb_anon_only
) {
331 $properties[] = $msg['anononlyblock'];
333 if ( $row->ipb_create_account
) {
334 $properties[] = $msg['createaccountblock'];
336 if ( $row->ipb_user
&& !$row->ipb_enable_autoblock
) {
337 $properties[] = $msg['noautoblockblock'];
340 if ( $row->ipb_block_email
) {
341 $properties[] = $msg['emailblock'];
344 if ( !$row->ipb_allow_usertalk
) {
345 $properties[] = $msg['blocklist-nousertalk'];
348 $formatted = $this->getLang()->commaList( $properties );
352 $formatted = "Unable to format $name";
359 function getQueryInfo() {
361 'tables' => array( 'ipblocks' ),
371 'ipb_create_account',
372 'ipb_enable_autoblock',
378 'ipb_allow_usertalk',
380 'conds' => $this->conds
,
383 # Is the user allowed to see hidden blocks?
384 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
385 $info['conds']['ipb_deleted'] = 0;
391 public function getTableClass(){
392 return 'TablePager mw-blocklist';
395 function getIndexField() {
396 return 'ipb_timestamp';
399 function getDefaultSort() {
400 return 'ipb_timestamp';
403 function isFieldSortable( $name ) {
408 * Do a LinkBatch query to minimise database load when generating all these links
411 function preprocessResults( $result ){
412 wfProfileIn( __METHOD__
);
413 # Do a link batch query
415 $lb->setCaller( __METHOD__
);
419 foreach ( $result as $row ) {
420 $userids[] = $row->ipb_by
;
422 # Usernames and titles are in fact related by a simple substitution of space -> underscore
423 # The last few lines of Title::secureAndSplit() tell the story.
424 $name = str_replace( ' ', '_', $row->ipb_address
);
425 $lb->add( NS_USER
, $name );
426 $lb->add( NS_USER_TALK
, $name );
429 $ua = UserArray
::newFromIDs( $userids );
430 foreach( $ua as $user ){
431 /* @var $user User */
432 $this->userNameCache
[$user->getID()] = $user->getName();
434 $name = str_replace( ' ', '_', $user->getName() );
435 $lb->add( NS_USER
, $name );
436 $lb->add( NS_USER_TALK
, $name );
440 wfProfileOut( __METHOD__
);