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 $lang = $this->getLanguage();
47 $out->setPageTitle( $this->msg( 'ipblocklist' ) );
48 $out->addModuleStyles( 'mediawiki.special' );
50 $request = $this->getRequest();
51 $par = $request->getVal( 'ip', $par );
52 $this->target
= trim( $request->getVal( 'wpTarget', $par ) );
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() );
65 # Just show the block list
69 'label-message' => 'ipadressorusername',
72 'default' => $this->target
,
75 'type' => 'multiselect',
77 wfMsg( 'blocklist-userblocks' ) => 'userblocks',
78 wfMsg( 'blocklist-tempblocks' ) => 'tempblocks',
79 wfMsg( 'blocklist-addressblocks' ) => 'addressblocks',
80 wfMsg( 'blocklist-rangeblocks' ) => 'rangeblocks',
85 'class' => 'HTMLBlockedUsersItemSelect',
86 'label-message' => 'table_pager_limit_label',
88 $lang->formatNum( 20 ) => 20,
89 $lang->formatNum( 50 ) => 50,
90 $lang->formatNum( 100 ) => 100,
91 $lang->formatNum( 250 ) => 250,
92 $lang->formatNum( 500 ) => 500,
98 $form = new HTMLForm( $fields, $this->getContext() );
99 $form->setMethod( 'get' );
100 $form->setWrapperLegend( wfMsg( 'ipblocklist-legend' ) );
101 $form->setSubmitText( wfMsg( 'ipblocklist-submit' ) );
102 $form->prepareForm();
104 $form->displayForm( '' );
108 function showList() {
109 # Purge expired entries on one in every 10 queries
110 if ( !mt_rand( 0, 10 ) ) {
111 Block
::purgeExpired();
115 # Is the user allowed to see hidden blocks?
116 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
117 $conds['ipb_deleted'] = 0;
120 if ( $this->target
!== '' ){
121 list( $target, $type ) = Block
::parseTarget( $this->target
);
125 case Block
::TYPE_AUTO
:
126 $conds['ipb_id'] = $target;
130 case Block
::TYPE_RANGE
:
131 list( $start, $end ) = IP
::parseRange( $target );
132 $dbr = wfGetDB( DB_SLAVE
);
133 $conds[] = $dbr->makeList(
135 'ipb_address' => $target,
136 Block
::getRangeCond( $start, $end )
140 $conds['ipb_auto'] = 0;
143 case Block
::TYPE_USER
:
144 $conds['ipb_address'] = (string)$this->target
;
145 $conds['ipb_auto'] = 0;
151 if( in_array( 'userblocks', $this->options
) ) {
152 $conds['ipb_user'] = 0;
154 if( in_array( 'tempblocks', $this->options
) ) {
155 $conds['ipb_expiry'] = 'infinity';
157 if( in_array( 'addressblocks', $this->options
) ) {
158 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
160 if( in_array( 'rangeblocks', $this->options
) ) {
161 $conds[] = "ipb_range_end = ipb_range_start";
164 # Check for other blocks, i.e. global/tor blocks
165 $otherBlockLink = array();
166 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target
) );
168 $out = $this->getOutput();
170 # Show additional header for the local block only when other blocks exists.
171 # Not necessary in a standard installation without such extensions enabled
172 if( count( $otherBlockLink ) ) {
174 Html
::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
178 $pager = new BlockListPager( $this, $conds );
179 if ( $pager->getNumRows() ) {
181 $pager->getNavigationBar() .
183 $pager->getNavigationBar()
186 } elseif ( $this->target
) {
187 $out->addWikiMsg( 'ipblocklist-no-results' );
190 $out->addWikiMsg( 'ipblocklist-empty' );
193 if( count( $otherBlockLink ) ) {
199 'ipblocklist-otherblocks',
201 count( $otherBlockLink )
206 foreach( $otherBlockLink as $link ) {
207 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
209 $out->addHTML( Html
::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
214 class BlockListPager
extends TablePager
{
219 * @param $page SpecialPage
220 * @param $conds Array
222 function __construct( $page, $conds ) {
224 $this->conds
= $conds;
225 $this->mDefaultDirection
= true;
226 parent
::__construct( $page->getContext() );
229 function getFieldNames() {
230 static $headers = null;
232 if ( $headers == array() ) {
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 $headers = array_map( 'wfMsg', $headers );
247 function formatValue( $name, $value ) {
249 if ( $msg === null ) {
252 'createaccountblock',
255 'blocklist-nousertalk',
260 $msg = array_combine( $msg, array_map( 'wfMessage', $msg ) );
263 /** @var $row object */
264 $row = $this->mCurrentRow
;
269 case 'ipb_timestamp':
270 $formatted = $this->getLanguage()->timeanddate( $value, /* User preference timezone */ true );
274 if( $row->ipb_auto
){
275 $formatted = wfMessage( 'autoblockid', $row->ipb_id
)->parse();
277 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
279 case Block
::TYPE_USER
:
281 $formatted = Linker
::userLink( $target->getId(), $target );
282 $formatted .= Linker
::userToolLinks(
286 Linker
::TOOL_LINKS_NOBLOCK
289 case Block
::TYPE_RANGE
:
290 $formatted = htmlspecialchars( $target );
296 $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */ true );
297 if( $this->getUser()->isAllowed( 'block' ) ){
298 if( $row->ipb_auto
){
299 $links[] = Linker
::linkKnown(
300 SpecialPage
::getTitleFor( 'Unblock' ),
303 array( 'wpTarget' => "#{$row->ipb_id}" )
306 $links[] = Linker
::linkKnown(
307 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
310 $links[] = Linker
::linkKnown(
311 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
312 $msg['change-blocklink']
315 $formatted .= ' ' . Html
::rawElement(
317 array( 'class' => 'mw-blocklist-actions' ),
318 wfMsg( 'parentheses', $this->getLanguage()->pipeList( $links ) )
324 if ( isset( $row->by_user_name
) ) {
325 $formatted = Linker
::userLink( $value, $row->by_user_name
);
326 $formatted .= Linker
::userToolLinks( $value, $row->by_user_name
);
328 $formatted = htmlspecialchars( $row->ipb_by_text
); // foreign user?
333 $formatted = Linker
::commentBlock( $value );
337 $properties = array();
338 if ( $row->ipb_anon_only
) {
339 $properties[] = $msg['anononlyblock'];
341 if ( $row->ipb_create_account
) {
342 $properties[] = $msg['createaccountblock'];
344 if ( $row->ipb_user
&& !$row->ipb_enable_autoblock
) {
345 $properties[] = $msg['noautoblockblock'];
348 if ( $row->ipb_block_email
) {
349 $properties[] = $msg['emailblock'];
352 if ( !$row->ipb_allow_usertalk
) {
353 $properties[] = $msg['blocklist-nousertalk'];
356 $formatted = $this->getLanguage()->commaList( $properties );
360 $formatted = "Unable to format $name";
367 function getQueryInfo() {
369 'tables' => array( 'ipblocks', 'user' ),
376 'user_name AS by_user_name',
381 'ipb_create_account',
382 'ipb_enable_autoblock',
388 'ipb_allow_usertalk',
390 'conds' => $this->conds
,
391 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
394 # Is the user allowed to see hidden blocks?
395 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
396 $info['conds']['ipb_deleted'] = 0;
402 public function getTableClass(){
403 return 'TablePager mw-blocklist';
406 function getIndexField() {
407 return 'ipb_timestamp';
410 function getDefaultSort() {
411 return 'ipb_timestamp';
414 function isFieldSortable( $name ) {
419 * Do a LinkBatch query to minimise database load when generating all these links
422 function preprocessResults( $result ){
423 wfProfileIn( __METHOD__
);
424 # Do a link batch query
426 $lb->setCaller( __METHOD__
);
430 foreach ( $result as $row ) {
431 $userids[] = $row->ipb_by
;
433 # Usernames and titles are in fact related by a simple substitution of space -> underscore
434 # The last few lines of Title::secureAndSplit() tell the story.
435 $name = str_replace( ' ', '_', $row->ipb_address
);
436 $lb->add( NS_USER
, $name );
437 $lb->add( NS_USER_TALK
, $name );
440 $ua = UserArray
::newFromIDs( $userids );
441 foreach( $ua as $user ){
442 $name = str_replace( ' ', '_', $user->getName() );
443 $lb->add( NS_USER
, $name );
444 $lb->add( NS_USER_TALK
, $name );
448 wfProfileOut( __METHOD__
);
453 * Items per page dropdown. Essentially a crap workaround for bug 32603.
455 * @todo Do not release 1.19 with this.
457 class HTMLBlockedUsersItemSelect
extends HTMLSelectField
{
459 * Basically don't do any validation. If it's a number that's fine. Also,
460 * add it to the list if it's not there already
466 function validate( $value, $alldata ) {
467 if ( $value == '' ) {
471 // Let folks pick an explicit limit not from our list, as long as it's a real numbr.
472 if ( !in_array( $value, $this->mParams
['options'] ) && $value == intval( $value ) && $value > 0 ) {
473 // This adds the explicitly requested limit value to the drop-down,
474 // then makes sure it's sorted correctly so when we output the list
475 // later, the custom option doesn't just show up last.
476 $this->mParams
['options'][ $this->mParent
->getLanguage()->formatNum( $value ) ] = intval($value);
477 asort( $this->mParams
['options'] );