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 ) {
43 global $wgUser, $wgOut, $wgRequest;
46 $this->outputHeader();
47 $wgOut->setPageTitle( wfMsg( 'ipblocklist' ) );
48 $wgOut->addModuleStyles( 'mediawiki.special' );
50 $par = $wgRequest->getVal( 'ip', $par );
51 $this->target
= trim( $wgRequest->getVal( 'wpTarget', $par ) );
53 $this->options
= $wgRequest->getArray( 'wpOptions', array() );
55 $action = $wgRequest->getText( 'action' );
57 if( $action == 'unblock' ||
$action == 'submit' && $wgRequest->wasPosted() ) {
58 # B/C @since 1.18: Unblock interface is now at Special:Unblock
59 $title = SpecialPage
::getTitleFor( 'Unblock', $this->target
);
60 $wgOut->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',
79 'cssclass' => 'mw-htmlform-multiselect-flatlist',
82 $form = new HTMLForm( $fields );
83 $form->setTitle( $this->getTitle() );
84 $form->setMethod( 'get' );
85 $form->setWrapperLegend( wfMsg( 'ipblocklist-legend' ) );
86 $form->setSubmitText( wfMsg( 'ipblocklist-submit' ) );
89 $form->displayForm( '' );
94 * Get the component of an IP address which is certain to be the same between an IP
95 * address and a rangeblock containing that IP address.
96 * @todo: should be in IP.php??
100 protected static function getIpFragment( $ip ){
101 global $wgBlockCIDRLimit;
102 if( IP
::isIPv4( $ip ) ){
103 $hexAddress = IP
::toHex( $ip );
104 return substr( $hexAddress, 0, wfBaseconvert( $wgBlockCIDRLimit['IPv4'], 10, 16 ) );
105 } elseif( IP
::isIPv6( $ip ) ) {
106 $hexAddress = substr( IP
::toHex( $ip ), 2 );
107 return 'v6-' . substr( $hexAddress, 0, wfBaseconvert( $wgBlockCIDRLimit['IPv6'], 10, 16 ) );
113 function showList() {
114 global $wgOut, $wgUser;
116 # Purge expired entries on one in every 10 queries
117 if ( !mt_rand( 0, 10 ) ) {
118 Block
::purgeExpired();
122 # Is the user allowed to see hidden blocks?
123 if ( !$wgUser->isAllowed( 'hideuser' ) ){
124 $conds['ipb_deleted'] = 0;
127 if ( $this->target
!== '' ){
128 list( $target, $type ) = Block
::parseTarget( $this->target
);
132 $conds['ipb_id'] = $target;
136 case BLock
::TYPE_RANGE
:
137 list( $start, $end ) = IP
::parseRange( $target );
138 # Per bug 14634, we want to include relevant active rangeblocks; for
139 # rangeblocks, we want to include larger ranges which enclose the given
140 # range. We know that all blocks must be smaller than $wgBlockCIDRLimit,
141 # so we can improve performance by filtering on a LIKE clause
142 $chunk = self
::getIpFragment( $start );
143 $dbr = wfGetDB( DB_SLAVE
);
144 $like = $dbr->buildLike( $chunk, $dbr->anyString() );
146 # Fairly hard to make a malicious SQL statement out of hex characters,
147 # but stranger things have happened...
148 $safeStart = $dbr->addQuotes( IP
::toHex( $start ) );
149 $safeEnd = $dbr->addQuotes( IP
::toHex( $end ) );
150 $safeTarget = $dbr->addQuotes( IP
::toHex( $target ) );
152 # TODO: abstract this away
153 $conds[] = "(ipb_address = $safeTarget) OR
154 (ipb_range_start $like AND ipb_range_start <= $safeStart AND ipb_range_end >= $safeEnd)";
155 $conds['ipb_auto'] = 0;
158 case Block
::TYPE_USER
:
159 $conds['ipb_address'] = (string)$this->target
;
160 $conds['ipb_auto'] = 0;
166 if( in_array( 'userblocks', $this->options
) ) {
167 $conds['ipb_user'] = 0;
169 if( in_array( 'tempblocks', $this->options
) ) {
170 $conds['ipb_expiry'] = 'infinity';
172 if( in_array( 'addressblocks', $this->options
) ) {
173 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
176 # Check for other blocks, i.e. global/tor blocks
177 $otherBlockLink = array();
178 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target
) );
180 # Show additional header for the local block only when other blocks exists.
181 # Not necessary in a standard installation without such extensions enabled
182 if( count( $otherBlockLink ) ) {
184 Html
::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
188 $pager = new BlockListPager( $this, $conds );
189 if ( $pager->getNumRows() ) {
191 $pager->getNavigationBar() .
193 $pager->getNavigationBar()
196 } elseif ( $this->target
) {
197 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
200 $wgOut->addWikiMsg( 'ipblocklist-empty' );
203 if( count( $otherBlockLink ) ) {
209 'ipblocklist-otherblocks',
211 count( $otherBlockLink )
216 foreach( $otherBlockLink as $link ) {
217 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
219 $wgOut->addHTML( Html
::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
224 class BlockListPager
extends TablePager
{
228 function __construct( $page, $conds ) {
230 $this->conds
= $conds;
231 $this->mDefaultDirection
= true;
232 parent
::__construct();
235 function getFieldNames() {
237 static $headers = null;
239 if ( $headers == array() ) {
241 'ipb_timestamp' => 'blocklist-timestamp',
242 'ipb_target' => 'blocklist-target',
243 'ipb_expiry' => 'blocklist-expiry',
244 'ipb_by' => 'blocklist-by',
245 'ipb_params' => 'blocklist-params',
246 'ipb_reason' => 'blocklist-reason',
248 $headers = array_map( 'wfMsg', $headers );
254 function formatValue( $name, $value ) {
255 global $wgOut, $wgLang, $wgUser;
258 if ( empty( $sk ) ) {
259 $sk = $wgUser->getSkin();
262 'createaccountblock',
265 'blocklist-nousertalk',
270 $msg = array_combine( $msg, array_map( 'wfMessage', $msg ) );
273 $row = $this->mCurrentRow
;
277 case 'ipb_timestamp':
278 $formatted = $wgLang->timeanddate( $value );
282 if( $row->ipb_auto
){
283 $formatted = wfMessage( 'autoblockid', $row->ipb_id
);
285 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
287 case Block
::TYPE_USER
:
289 $formatted = $sk->userLink( $target->getId(), $target );
290 $formatted .= $sk->userToolLinks(
294 Linker
::TOOL_LINKS_NOBLOCK
297 case Block
::TYPE_RANGE
:
298 $formatted = htmlspecialchars( $target );
304 $formatted = $value == Block
::infinity()
305 ?
$msg['infiniteblock']
306 : $wgLang->timeanddate( $value );
307 if( $wgUser->isAllowed( 'block' ) ){
308 if( $row->ipb_auto
){
309 $links[] = $sk->linkKnown(
310 SpecialPage
::getTitleFor( 'Unblock' ),
313 array( 'wpTarget' => "#{$row->ipb_id}" )
316 $links[] = $sk->linkKnown(
317 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
320 $links[] = $sk->linkKnown(
321 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
322 $msg['change-blocklink']
325 $formatted .= ' ' . Html
::rawElement(
327 array( 'class' => 'mw-blocklist-actions' ),
328 "[{$wgLang->pipeList($links)}]"
334 $user = User
::newFromId( $value );
335 if( $user instanceof User
){
336 $formatted = $sk->userLink( $user->getId(), $user->getName() );
337 $formatted .= $sk->userToolLinks( $user->getId(), $user->getName() );
342 $formatted = $sk->commentBlock( $value );
346 $properties = array();
347 if ( $row->ipb_anon_only
) {
348 $properties[] = $msg['anononlyblock'];
350 if ( $row->ipb_create_account
) {
351 $properties[] = $msg['createaccountblock'];
353 if ( !$row->ipb_enable_autoblock
) {
354 $properties[] = $msg['noautoblockblock'];
357 if ( $row->ipb_block_email
) {
358 $properties[] = $msg['emailblock'];
361 if ( !$row->ipb_allow_usertalk
) {
362 $properties[] = $msg['blocklist-nousertalk'];
365 $formatted = $wgLang->commaList( $properties );
369 $formatted = "Unable to format $name";
376 function getQueryInfo() {
378 'tables' => array( 'ipblocks' ),
387 'ipb_create_account',
388 'ipb_enable_autoblock',
394 'ipb_allow_usertalk',
396 'conds' => $this->conds
,
400 # Is the user allowed to see hidden blocks?
401 if ( !$wgUser->isAllowed( 'hideuser' ) ){
402 $conds['ipb_deleted'] = 0;
408 public function getTableClass(){
409 return 'TablePager mw-blocklist';
412 function getIndexField() {
413 return 'ipb_timestamp';
416 function getDefaultSort() {
417 return 'ipb_timestamp';
420 function isFieldSortable( $name ) {
424 function getTitle() {
425 return $this->page
->getTitle();