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( $this->msg( 'ipblocklist' ) );
47 $out->addModuleStyles( 'mediawiki.special' );
49 $request = $this->getRequest();
50 $par = $request->getVal( 'ip', $par );
51 $this->target
= trim( $request->getVal( 'wpTarget', $par ) );
52 $request->setVal( 'wpTarget', $this->target
);
54 $this->options
= $request->getArray( 'wpOptions', array() );
56 $action = $request->getText( 'action' );
58 if( $action == 'unblock' ||
$action == 'submit' && $request->wasPosted() ) {
59 # B/C @since 1.18: Unblock interface is now at Special:Unblock
60 $title = SpecialPage
::getTitleFor( 'Unblock', $this->target
);
61 $out->redirect( $title->getFullUrl() );
69 # Purge expired entries on one in every 10 queries
70 if ( !mt_rand( 0, 10 ) ) {
71 Block
::purgeExpired();
75 # Is the user allowed to see hidden blocks?
76 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
77 $conds['ipb_deleted'] = 0;
80 if ( $this->target
!== '' ){
81 list( $target, $type ) = Block
::parseTarget( $this->target
);
85 case Block
::TYPE_AUTO
:
86 $conds['ipb_id'] = $target;
90 case Block
::TYPE_RANGE
:
91 list( $start, $end ) = IP
::parseRange( $target );
92 $dbr = wfGetDB( DB_SLAVE
);
93 $conds[] = $dbr->makeList(
95 'ipb_address' => $target,
96 Block
::getRangeCond( $start, $end )
100 $conds['ipb_auto'] = 0;
103 case Block
::TYPE_USER
:
104 $conds['ipb_address'] = (string)$this->target
;
105 $conds['ipb_auto'] = 0;
111 if( in_array( 'userblocks', $this->options
) ) {
112 $conds['ipb_user'] = 0;
114 if( in_array( 'tempblocks', $this->options
) ) {
115 $conds['ipb_expiry'] = 'infinity';
117 if( in_array( 'addressblocks', $this->options
) ) {
118 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
120 if( in_array( 'rangeblocks', $this->options
) ) {
121 $conds[] = "ipb_range_end = ipb_range_start";
124 # Check for other blocks, i.e. global/tor blocks
125 $otherBlockLink = array();
126 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target
) );
128 $out = $this->getOutput();
130 # Show additional header for the local block only when other blocks exists.
131 # Not necessary in a standard installation without such extensions enabled
132 if( count( $otherBlockLink ) ) {
134 Html
::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
138 $pager = new BlockListPager( $this, $conds );
139 $out->addHTML( $pager->buildHTMLForm() );
140 if ( $pager->getNumRows() ) {
142 $pager->getNavigationBar() .
144 $pager->getNavigationBar()
147 } elseif ( $this->target
) {
148 $out->addWikiMsg( 'ipblocklist-no-results' );
151 $out->addWikiMsg( 'ipblocklist-empty' );
154 if( count( $otherBlockLink ) ) {
160 'ipblocklist-otherblocks',
162 count( $otherBlockLink )
167 foreach( $otherBlockLink as $link ) {
168 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
170 $out->addHTML( Html
::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
175 class BlockListPager
extends TablePager
{
180 * @param $page SpecialPage
181 * @param $conds Array
183 function __construct( $page, $conds ) {
185 $this->conds
= $conds;
186 $this->mDefaultDirection
= true;
187 parent
::__construct( $page->getContext() );
190 function getFieldNames() {
191 static $headers = null;
193 if ( $headers == array() ) {
195 'ipb_timestamp' => 'blocklist-timestamp',
196 'ipb_target' => 'blocklist-target',
197 'ipb_expiry' => 'blocklist-expiry',
198 'ipb_by' => 'blocklist-by',
199 'ipb_params' => 'blocklist-params',
200 'ipb_reason' => 'blocklist-reason',
202 $headers = array_map( 'wfMsg', $headers );
208 function formatValue( $name, $value ) {
210 if ( $msg === null ) {
213 'createaccountblock',
216 'blocklist-nousertalk',
221 $msg = array_combine( $msg, array_map( 'wfMessage', $msg ) );
224 /** @var $row object */
225 $row = $this->mCurrentRow
;
230 case 'ipb_timestamp':
231 $formatted = $this->getLanguage()->timeanddate( $value, /* User preference timezone */ true );
235 if( $row->ipb_auto
){
236 $formatted = wfMessage( 'autoblockid', $row->ipb_id
)->parse();
238 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
240 case Block
::TYPE_USER
:
242 $formatted = Linker
::userLink( $target->getId(), $target );
243 $formatted .= Linker
::userToolLinks(
247 Linker
::TOOL_LINKS_NOBLOCK
250 case Block
::TYPE_RANGE
:
251 $formatted = htmlspecialchars( $target );
257 $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */ true );
258 if( $this->getUser()->isAllowed( 'block' ) ){
259 if( $row->ipb_auto
){
260 $links[] = Linker
::linkKnown(
261 SpecialPage
::getTitleFor( 'Unblock' ),
264 array( 'wpTarget' => "#{$row->ipb_id}" )
267 $links[] = Linker
::linkKnown(
268 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
271 $links[] = Linker
::linkKnown(
272 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
273 $msg['change-blocklink']
276 $formatted .= ' ' . Html
::rawElement(
278 array( 'class' => 'mw-blocklist-actions' ),
279 wfMsg( 'parentheses', $this->getLanguage()->pipeList( $links ) )
285 if ( isset( $row->by_user_name
) ) {
286 $formatted = Linker
::userLink( $value, $row->by_user_name
);
287 $formatted .= Linker
::userToolLinks( $value, $row->by_user_name
);
289 $formatted = htmlspecialchars( $row->ipb_by_text
); // foreign user?
294 $formatted = Linker
::commentBlock( $value );
298 $properties = array();
299 if ( $row->ipb_anon_only
) {
300 $properties[] = $msg['anononlyblock'];
302 if ( $row->ipb_create_account
) {
303 $properties[] = $msg['createaccountblock'];
305 if ( $row->ipb_user
&& !$row->ipb_enable_autoblock
) {
306 $properties[] = $msg['noautoblockblock'];
309 if ( $row->ipb_block_email
) {
310 $properties[] = $msg['emailblock'];
313 if ( !$row->ipb_allow_usertalk
) {
314 $properties[] = $msg['blocklist-nousertalk'];
317 $formatted = $this->getLanguage()->commaList( $properties );
321 $formatted = "Unable to format $name";
328 function getQueryInfo() {
330 'tables' => array( 'ipblocks', 'user' ),
337 'user_name AS by_user_name',
342 'ipb_create_account',
343 'ipb_enable_autoblock',
349 'ipb_allow_usertalk',
351 'conds' => $this->conds
,
352 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
355 # Is the user allowed to see hidden blocks?
356 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
357 $info['conds']['ipb_deleted'] = 0;
363 protected function getHTMLFormFields() {
367 'label-message' => 'ipadressorusername',
370 //'default' => $this->target,
373 'type' => 'multiselect',
375 wfMsg( 'blocklist-userblocks' ) => 'userblocks',
376 wfMsg( 'blocklist-tempblocks' ) => 'tempblocks',
377 wfMsg( 'blocklist-addressblocks' ) => 'addressblocks',
378 wfMsg( 'blocklist-rangeblocks' ) => 'rangeblocks',
382 'Limit' => $this->getHTMLFormLimitSelect(),
386 protected function getHTMLFormSubmit() {
387 return 'ipblocklist-submit';
390 protected function getHTMLFormLegend() {
391 return 'ipblocklist-legend';
394 public function getTableClass(){
395 return 'TablePager mw-blocklist';
398 function getIndexField() {
399 return 'ipb_timestamp';
402 function getDefaultSort() {
403 return 'ipb_timestamp';
406 function isFieldSortable( $name ) {
411 * Do a LinkBatch query to minimise database load when generating all these links
414 function preprocessResults( $result ){
415 wfProfileIn( __METHOD__
);
416 # Do a link batch query
418 $lb->setCaller( __METHOD__
);
422 foreach ( $result as $row ) {
423 $userids[] = $row->ipb_by
;
425 # Usernames and titles are in fact related by a simple substitution of space -> underscore
426 # The last few lines of Title::secureAndSplit() tell the story.
427 $name = str_replace( ' ', '_', $row->ipb_address
);
428 $lb->add( NS_USER
, $name );
429 $lb->add( NS_USER_TALK
, $name );
432 $ua = UserArray
::newFromIDs( $userids );
433 foreach( $ua as $user ){
434 $name = str_replace( ' ', '_', $user->getName() );
435 $lb->add( NS_USER
, $name );
436 $lb->add( NS_USER_TALK
, $name );
440 wfProfileOut( __METHOD__
);