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 ) );
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() );
68 # Purge expired entries on one in every 10 queries
69 if ( !mt_rand( 0, 10 ) ) {
70 Block
::purgeExpired();
74 # Is the user allowed to see hidden blocks?
75 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
76 $conds['ipb_deleted'] = 0;
79 if ( $this->target
!== '' ){
80 list( $target, $type ) = Block
::parseTarget( $this->target
);
84 case Block
::TYPE_AUTO
:
85 $conds['ipb_id'] = $target;
89 case Block
::TYPE_RANGE
:
90 list( $start, $end ) = IP
::parseRange( $target );
91 $dbr = wfGetDB( DB_SLAVE
);
92 $conds[] = $dbr->makeList(
94 'ipb_address' => $target,
95 Block
::getRangeCond( $start, $end )
99 $conds['ipb_auto'] = 0;
102 case Block
::TYPE_USER
:
103 $conds['ipb_address'] = (string)$this->target
;
104 $conds['ipb_auto'] = 0;
110 if( in_array( 'userblocks', $this->options
) ) {
111 $conds['ipb_user'] = 0;
113 if( in_array( 'tempblocks', $this->options
) ) {
114 $conds['ipb_expiry'] = 'infinity';
116 if( in_array( 'addressblocks', $this->options
) ) {
117 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
119 if( in_array( 'rangeblocks', $this->options
) ) {
120 $conds[] = "ipb_range_end = ipb_range_start";
123 # Check for other blocks, i.e. global/tor blocks
124 $otherBlockLink = array();
125 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target
) );
127 $out = $this->getOutput();
129 # Show additional header for the local block only when other blocks exists.
130 # Not necessary in a standard installation without such extensions enabled
131 if( count( $otherBlockLink ) ) {
133 Html
::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
137 $pager = new BlockListPager( $this, $conds );
138 $out->addHTML( $pager->buildHTMLForm() );
139 if ( $pager->getNumRows() ) {
141 $pager->getNavigationBar() .
143 $pager->getNavigationBar()
146 } elseif ( $this->target
) {
147 $out->addWikiMsg( 'ipblocklist-no-results' );
150 $out->addWikiMsg( 'ipblocklist-empty' );
153 if( count( $otherBlockLink ) ) {
159 'ipblocklist-otherblocks',
161 count( $otherBlockLink )
166 foreach( $otherBlockLink as $link ) {
167 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
169 $out->addHTML( Html
::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
174 class BlockListPager
extends TablePager
{
179 * @param $page SpecialPage
180 * @param $conds Array
182 function __construct( $page, $conds ) {
184 $this->conds
= $conds;
185 $this->mDefaultDirection
= true;
186 parent
::__construct( $page->getContext() );
189 function getFieldNames() {
190 static $headers = null;
192 if ( $headers == array() ) {
194 'ipb_timestamp' => 'blocklist-timestamp',
195 'ipb_target' => 'blocklist-target',
196 'ipb_expiry' => 'blocklist-expiry',
197 'ipb_by' => 'blocklist-by',
198 'ipb_params' => 'blocklist-params',
199 'ipb_reason' => 'blocklist-reason',
201 $headers = array_map( 'wfMsg', $headers );
207 function formatValue( $name, $value ) {
209 if ( $msg === null ) {
212 'createaccountblock',
215 'blocklist-nousertalk',
220 $msg = array_combine( $msg, array_map( 'wfMessage', $msg ) );
223 /** @var $row object */
224 $row = $this->mCurrentRow
;
229 case 'ipb_timestamp':
230 $formatted = $this->getLanguage()->timeanddate( $value, /* User preference timezone */ true );
234 if( $row->ipb_auto
){
235 $formatted = wfMessage( 'autoblockid', $row->ipb_id
)->parse();
237 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
239 case Block
::TYPE_USER
:
241 $formatted = Linker
::userLink( $target->getId(), $target );
242 $formatted .= Linker
::userToolLinks(
246 Linker
::TOOL_LINKS_NOBLOCK
249 case Block
::TYPE_RANGE
:
250 $formatted = htmlspecialchars( $target );
256 $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */ true );
257 if( $this->getUser()->isAllowed( 'block' ) ){
258 if( $row->ipb_auto
){
259 $links[] = Linker
::linkKnown(
260 SpecialPage
::getTitleFor( 'Unblock' ),
263 array( 'wpTarget' => "#{$row->ipb_id}" )
266 $links[] = Linker
::linkKnown(
267 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
270 $links[] = Linker
::linkKnown(
271 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
272 $msg['change-blocklink']
275 $formatted .= ' ' . Html
::rawElement(
277 array( 'class' => 'mw-blocklist-actions' ),
278 wfMsg( 'parentheses', $this->getLanguage()->pipeList( $links ) )
284 if ( isset( $row->by_user_name
) ) {
285 $formatted = Linker
::userLink( $value, $row->by_user_name
);
286 $formatted .= Linker
::userToolLinks( $value, $row->by_user_name
);
288 $formatted = htmlspecialchars( $row->ipb_by_text
); // foreign user?
293 $formatted = Linker
::commentBlock( $value );
297 $properties = array();
298 if ( $row->ipb_anon_only
) {
299 $properties[] = $msg['anononlyblock'];
301 if ( $row->ipb_create_account
) {
302 $properties[] = $msg['createaccountblock'];
304 if ( $row->ipb_user
&& !$row->ipb_enable_autoblock
) {
305 $properties[] = $msg['noautoblockblock'];
308 if ( $row->ipb_block_email
) {
309 $properties[] = $msg['emailblock'];
312 if ( !$row->ipb_allow_usertalk
) {
313 $properties[] = $msg['blocklist-nousertalk'];
316 $formatted = $this->getLanguage()->commaList( $properties );
320 $formatted = "Unable to format $name";
327 function getQueryInfo() {
329 'tables' => array( 'ipblocks', 'user' ),
336 'user_name AS by_user_name',
341 'ipb_create_account',
342 'ipb_enable_autoblock',
348 'ipb_allow_usertalk',
350 'conds' => $this->conds
,
351 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
354 # Is the user allowed to see hidden blocks?
355 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
356 $info['conds']['ipb_deleted'] = 0;
362 protected function getHTMLFormFields() {
366 'label-message' => 'ipadressorusername',
369 //'default' => $this->target,
372 'type' => 'multiselect',
374 wfMsg( 'blocklist-userblocks' ) => 'userblocks',
375 wfMsg( 'blocklist-tempblocks' ) => 'tempblocks',
376 wfMsg( 'blocklist-addressblocks' ) => 'addressblocks',
377 wfMsg( 'blocklist-rangeblocks' ) => 'rangeblocks',
381 'Limit' => $this->getHTMLFormLimitSelect(),
385 protected function getHTMLFormSubmit() {
386 return 'ipblocklist-submit';
389 protected function getHTMLFormLegend() {
390 return 'ipblocklist-legend';
393 public function getTableClass(){
394 return 'TablePager mw-blocklist';
397 function getIndexField() {
398 return 'ipb_timestamp';
401 function getDefaultSort() {
402 return 'ipb_timestamp';
405 function isFieldSortable( $name ) {
410 * Do a LinkBatch query to minimise database load when generating all these links
413 function preprocessResults( $result ){
414 wfProfileIn( __METHOD__
);
415 # Do a link batch query
417 $lb->setCaller( __METHOD__
);
421 foreach ( $result as $row ) {
422 $userids[] = $row->ipb_by
;
424 # Usernames and titles are in fact related by a simple substitution of space -> underscore
425 # The last few lines of Title::secureAndSplit() tell the story.
426 $name = str_replace( ' ', '_', $row->ipb_address
);
427 $lb->add( NS_USER
, $name );
428 $lb->add( NS_USER_TALK
, $name );
431 $ua = UserArray
::newFromIDs( $userids );
432 foreach( $ua as $user ){
433 $name = str_replace( ' ', '_', $user->getName() );
434 $lb->add( NS_USER
, $name );
435 $lb->add( NS_USER_TALK
, $name );
439 wfProfileOut( __METHOD__
);