3 * Implements Special:Blockip
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 allows users with 'block' right to block users from
26 * editing pages and other actions
28 * @ingroup SpecialPage
30 class IPBlockForm extends SpecialPage {
31 var $BlockAddress, $BlockExpiry, $BlockReason, $BlockReasonList, $BlockOther, $BlockAnonOnly, $BlockCreateAccount,
32 $BlockEnableAutoblock, $BlockEmail, $BlockHideName, $BlockAllowUsertalk, $BlockReblock;
33 // The maximum number of edits a user can have and still be hidden
34 const HIDEUSER_CONTRIBLIMIT = 1000;
36 public function __construct() {
37 parent::__construct( 'Blockip', 'block' );
40 public function execute( $par ) {
41 global $wgUser, $wgOut, $wgRequest;
43 # Can't block when the database is locked
45 $wgOut->readOnlyPage();
49 if( !$this->userCanExecute( $wgUser ) ) {
50 $wgOut->permissionRequired( 'block' );
56 # bug 15810: blocked admins should have limited access here
57 if ( $wgUser->isBlocked() ) {
58 $status = IPBlockForm::checkUnblockSelf( $this->BlockAddress );
59 if ( $status !== true ) {
60 throw new ErrorPageError( 'badaccess', $status );
64 $action = $wgRequest->getVal( 'action' );
65 if( 'success' == $action ) {
67 } elseif( $wgRequest->wasPosted() && 'submit' == $action &&
68 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
71 $this->showForm( '' );
75 private function setup( $par ) {
76 global $wgRequest, $wgUser, $wgBlockAllowsUTEdit;
78 $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
79 $this->BlockAddress = strtr( $this->BlockAddress, '_', ' ' );
80 $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
81 $this->BlockReasonList = $wgRequest->getText( 'wpBlockReasonList' );
82 $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry', wfMsg( 'ipbotheroption' ) );
83 $this->BlockOther = $wgRequest->getVal( 'wpBlockOther', '' );
85 # Unchecked checkboxes are not included in the form data at all, so having one
86 # that is true by default is a bit tricky
87 $byDefault = !$wgRequest->wasPosted();
88 $this->BlockAnonOnly = $wgRequest->getBool( 'wpAnonOnly', $byDefault );
89 $this->BlockCreateAccount = $wgRequest->getBool( 'wpCreateAccount', $byDefault );
90 $this->BlockEnableAutoblock = $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
91 $this->BlockEmail = false;
92 if( self::canBlockEmail( $wgUser ) ) {
93 $this->BlockEmail = $wgRequest->getBool( 'wpEmailBan', false );
95 $this->BlockWatchUser = $wgRequest->getBool( 'wpWatchUser', false ) && $wgUser->isLoggedIn();
96 # Re-check user's rights to hide names, very serious, defaults to null
97 if( $wgUser->isAllowed( 'hideuser' ) ) {
98 $this->BlockHideName = $wgRequest->getBool( 'wpHideName', null );
100 $this->BlockHideName = false;
102 $this->BlockAllowUsertalk = ( $wgRequest->getBool( 'wpAllowUsertalk', $byDefault ) && $wgBlockAllowsUTEdit );
103 $this->BlockReblock = $wgRequest->getBool( 'wpChangeBlock', false );
105 $this->wasPosted = $wgRequest->wasPosted();
108 public function showForm( $err ) {
109 global $wgOut, $wgUser, $wgSysopUserBans;
111 $wgOut->setPageTitle( wfMsg( 'blockip-title' ) );
112 $wgOut->addWikiMsg( 'blockiptext' );
114 if( $wgSysopUserBans ) {
115 $mIpaddress = Xml::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
117 $mIpaddress = Xml::label( wfMsg( 'ipaddress' ), 'mw-bi-target' );
119 $mIpbexpiry = Xml::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
120 $mIpbother = Xml::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
121 $mIpbreasonother = Xml::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
122 $mIpbreason = Xml::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
124 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
125 $user = User::newFromName( $this->BlockAddress );
127 $alreadyBlocked = false;
128 $otherBlockedMsgs = array();
129 if( $err && $err[0] != 'ipb_already_blocked' ) {
130 $key = array_shift( $err );
131 $msg = wfMsgReal( $key, $err );
132 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
133 $wgOut->addHTML( Xml::tags( 'p', array( 'class' => 'error' ), $msg ) );
134 } elseif( $this->BlockAddress !== null ) {
135 # Get other blocks, i.e. from GlobalBlocking or TorBlock extension
136 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockedMsgs, $this->BlockAddress ) );
138 $userId = is_object( $user ) ? $user->getId() : 0;
139 $currentBlock = Block::newFromDB( $this->BlockAddress, $userId );
140 if( !is_null( $currentBlock ) && !$currentBlock->mAuto && # The block exists and isn't an autoblock
141 ( $currentBlock->mRangeStart == $currentBlock->mRangeEnd || # The block isn't a rangeblock
142 # or if it is, the range is what we're about to block
143 ( $currentBlock->mAddress == $this->BlockAddress ) )
145 $alreadyBlocked = true;
146 # Set the block form settings to the existing block
147 if( !$this->wasPosted ) {
148 $this->BlockAnonOnly = $currentBlock->mAnonOnly;
149 $this->BlockCreateAccount = $currentBlock->mCreateAccount;
150 $this->BlockEnableAutoblock = $currentBlock->mEnableAutoblock;
151 $this->BlockEmail = $currentBlock->mBlockEmail;
152 $this->BlockHideName = $currentBlock->mHideName;
153 $this->BlockAllowUsertalk = $currentBlock->mAllowUsertalk;
154 if( $currentBlock->mExpiry == 'infinity' ) {
155 $this->BlockOther = 'indefinite';
157 $this->BlockOther = wfTimestamp( TS_ISO_8601, $currentBlock->mExpiry );
159 $this->BlockReason = $currentBlock->mReason;
164 # Show other blocks from extensions, i.e. GlockBlocking and TorBlock
165 if( count( $otherBlockedMsgs ) ) {
167 Html::rawElement( 'h2', array(), wfMsgExt( 'ipb-otherblocks-header', 'parseinline', count( $otherBlockedMsgs ) ) ) . "\n"
170 foreach( $otherBlockedMsgs as $link ) {
171 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
173 $wgOut->addHTML( Html::rawElement( 'ul', array( 'class' => 'mw-blockip-alreadyblocked' ), $list ) . "\n" );
176 # Username/IP is blocked already locally
177 if( $alreadyBlocked ) {
178 $wgOut->wrapWikiMsg( "<div class='mw-ipb-needreblock'>\n$1\n</div>", array( 'ipb-needreblock', $this->BlockAddress ) );
181 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
183 $showblockoptions = $scBlockExpiryOptions != '-';
184 if( !$showblockoptions ) $mIpbother = $mIpbexpiry;
186 $blockExpiryFormOptions = Xml::option( wfMsg( 'ipbotheroption' ), 'other' );
187 foreach( explode( ',', $scBlockExpiryOptions ) as $option ) {
188 if( strpos( $option, ':' ) === false ) $option = "$option:$option";
189 list( $show, $value ) = explode( ':', $option );
190 $show = htmlspecialchars( $show );
191 $value = htmlspecialchars( $value );
192 $blockExpiryFormOptions .= Xml::option( $show, $value, $this->BlockExpiry === $value ) . "\n";
195 $reasonDropDown = Xml::listDropDown( 'wpBlockReasonList',
196 wfMsgForContent( 'ipbreason-dropdown' ),
197 wfMsgForContent( 'ipbreasonotherlist' ), $this->BlockReasonList, 'wpBlockDropDown', 4 );
199 $wgOut->addModules( 'mediawiki.legacy.block' );
201 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'blockip' ) ) .
202 Xml::openElement( 'fieldset' ) .
203 Xml::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
204 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
206 <td class='mw-label'>
209 <td class='mw-input'>" .
210 Html::input( 'wpBlockAddress', $this->BlockAddress, 'text', array(
212 'id' => 'mw-bi-target',
213 'onchange' => 'updateBlockOptions()',
216 ) + ( $this->BlockAddress ? array() : array( 'autofocus' ) ) ). "
221 if( $showblockoptions ) {
223 <td class='mw-label'>
226 <td class='mw-input'>" .
229 'id' => 'wpBlockExpiry',
230 'name' => 'wpBlockExpiry',
231 'onchange' => 'considerChangingExpiryFocus()',
233 $blockExpiryFormOptions ) .
239 <tr id='wpBlockOther'>
240 <td class='mw-label'>
243 <td class='mw-input'>" .
244 Xml::input( 'wpBlockOther', 45, $this->BlockOther,
245 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
249 <td class='mw-label'>
252 <td class='mw-input'>
256 <tr id=\"wpBlockReason\">
257 <td class='mw-label'>
260 <td class='mw-input'>" .
261 Html::input( 'wpBlockReason', $this->BlockReason, 'text', array(
263 'id' => 'mw-bi-reason',
264 'maxlength' => '200',
266 ) + ( $this->BlockAddress ? array( 'autofocus' ) : array() ) ) . "
269 <tr id='wpAnonOnlyRow'>
271 <td class='mw-input'>" .
272 Xml::checkLabel( wfMsg( 'ipbanononly' ),
273 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
274 array( 'tabindex' => '6' ) ) . "
277 <tr id='wpCreateAccountRow'>
279 <td class='mw-input'>" .
280 Xml::checkLabel( wfMsg( 'ipbcreateaccount' ),
281 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
282 array( 'tabindex' => '7' ) ) . "
285 <tr id='wpEnableAutoblockRow'>
287 <td class='mw-input'>" .
288 Xml::checkLabel( wfMsg( 'ipbenableautoblock' ),
289 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
290 array( 'tabindex' => '8' ) ) . "
295 if( self::canBlockEmail( $wgUser ) ) {
297 <tr id='wpEnableEmailBan'>
299 <td class='mw-input'>" .
300 Xml::checkLabel( wfMsg( 'ipbemailban' ),
301 'wpEmailBan', 'wpEmailBan', $this->BlockEmail,
302 array( 'tabindex' => '9' ) ) . "
308 // Allow some users to hide name from block log, blocklist and listusers
309 if( $wgUser->isAllowed( 'hideuser' ) ) {
311 <tr id='wpEnableHideUser'>
313 <td class='mw-input'><strong>" .
314 Xml::checkLabel( wfMsg( 'ipbhidename' ),
315 'wpHideName', 'wpHideName', $this->BlockHideName,
316 array( 'tabindex' => '10' )
323 # Watchlist their user page? (Only if user is logged in)
324 if( $wgUser->isLoggedIn() ) {
326 <tr id='wpEnableWatchUser'>
328 <td class='mw-input'>" .
329 Xml::checkLabel( wfMsg( 'ipbwatchuser' ),
330 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser,
331 array( 'tabindex' => '11' ) ) . "
337 # Can we explicitly disallow the use of user_talk?
338 global $wgBlockAllowsUTEdit;
339 if( $wgBlockAllowsUTEdit ){
341 <tr id='wpAllowUsertalkRow'>
343 <td class='mw-input'>" .
344 Xml::checkLabel( wfMsg( 'ipballowusertalk' ),
345 'wpAllowUsertalk', 'wpAllowUsertalk', $this->BlockAllowUsertalk,
346 array( 'tabindex' => '12' ) ) . "
354 <td style='padding-top: 1em'> </td>
355 <td class='mw-submit' style='padding-top: 1em'>" .
356 Xml::submitButton( wfMsg( $alreadyBlocked ? 'ipb-change-block' : 'ipbsubmit' ),
357 array( 'name' => 'wpBlock', 'tabindex' => '13' )
358 + $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'blockip-block' ) ). "
361 Xml::closeElement( 'table' ) .
362 Html::hidden( 'wpEditToken', $wgUser->editToken() ) .
363 ( $alreadyBlocked ? Html::hidden( 'wpChangeBlock', 1 ) : "" ) .
364 Xml::closeElement( 'fieldset' ) .
365 Xml::closeElement( 'form' )
368 $wgOut->addHTML( $this->getConvenienceLinks() );
370 if( is_object( $user ) ) {
371 $this->showLogFragment( $wgOut, $user->getUserPage() );
372 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
373 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
374 } elseif( preg_match( '/^\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}/', $this->BlockAddress ) ) {
375 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
380 * Can we do an email block?
381 * @param $user User: the sysop wanting to make a block
384 public static function canBlockEmail( $user ) {
385 global $wgEnableUserEmail, $wgSysopEmailBans;
386 return ( $wgEnableUserEmail && $wgSysopEmailBans && $user->isAllowed( 'blockemail' ) );
390 * bug 15810: blocked admins should not be able to block/unblock
391 * others, and probably shouldn't be able to unblock themselves
393 * @param $user User, Int or String
395 public static function checkUnblockSelf( $user ) {
397 if ( is_int( $user ) ) {
398 $user = User::newFromId( $user );
399 } elseif ( is_string( $user ) ) {
400 $user = User::newFromName( $user );
402 if( $user instanceof User && $user->getId() == $wgUser->getId() ) {
403 # User is trying to unblock themselves
404 if ( $wgUser->isAllowed( 'unblockself' ) ) {
407 return 'ipbnounblockself';
410 # User is trying to block/unblock someone else
416 * Backend block code.
417 * $userID and $expiry will be filled accordingly
418 * @return array(message key, arguments) on failure, empty array on success
420 function doBlock( &$userId = null, &$expiry = null ) {
421 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans, $wgBlockAllowsUTEdit, $wgBlockCIDRLimit;
424 # Expand valid IPv6 addresses, usernames are left as is
425 $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
426 # isIPv4() and IPv6() are used for final validation
427 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
428 $rxIP6 = '\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}';
429 $rxIP = "($rxIP4|$rxIP6)";
431 # Check for invalid specifications
432 if( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
434 if( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
436 if( $wgSysopRangeBans && $wgBlockCIDRLimit['IPv4'] != 32 ) {
437 if( !IP::isIPv4( $this->BlockAddress ) || $matches[2] > 32 ) {
438 return array( 'ip_range_invalid' );
439 } elseif ( $matches[2] < $wgBlockCIDRLimit['IPv4'] ) {
440 return array( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv4'] );
442 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
444 # Range block illegal
445 return array( 'range_block_disabled' );
447 } elseif( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
449 if( $wgSysopRangeBans && $wgBlockCIDRLimit['IPv6'] != 128 ) {
450 if( !IP::isIPv6( $this->BlockAddress ) || $matches[2] > 128 ) {
451 return array( 'ip_range_invalid' );
452 } elseif( $matches[2] < $wgBlockCIDRLimit['IPv6'] ) {
453 return array( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv6'] );
455 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
457 # Range block illegal
458 return array('range_block_disabled');
462 if( $wgSysopUserBans ) {
463 $user = User::newFromName( $this->BlockAddress );
464 if( $user instanceof User && $user->getId() ) {
466 $userId = $user->getId();
467 $this->BlockAddress = $user->getName();
469 return array( 'nosuchusershort', htmlspecialchars( $user ? $user->getName() : $this->BlockAddress ) );
472 return array( 'badipaddress' );
477 if( $wgUser->isBlocked() && ( $wgUser->getId() !== $userId ) ) {
478 return array( 'cant-block-while-blocked' );
481 $reasonstr = $this->BlockReasonList;
482 if( $reasonstr != 'other' && $this->BlockReason != '' ) {
483 // Entry from drop down menu + additional comment
484 $reasonstr .= wfMsgForContent( 'colon-separator' ) . $this->BlockReason;
485 } elseif( $reasonstr == 'other' ) {
486 $reasonstr = $this->BlockReason;
489 $expirestr = $this->BlockExpiry;
490 if( $expirestr == 'other' )
491 $expirestr = $this->BlockOther;
493 if( ( strlen( $expirestr ) == 0) || ( strlen( $expirestr ) > 50 ) ) {
494 return array( 'ipb_expiry_invalid' );
497 if( false === ( $expiry = Block::parseExpiryInput( $expirestr ) ) ) {
499 return array( 'ipb_expiry_invalid' );
502 if( $this->BlockHideName ) {
503 // Recheck params here...
504 if( !$userId || !$wgUser->isAllowed('hideuser') ) {
505 $this->BlockHideName = false; // IP users should not be hidden
506 } elseif( $expiry !== 'infinity' ) {
508 return array( 'ipb_expiry_temp' );
509 } elseif( User::edits( $userId ) > self::HIDEUSER_CONTRIBLIMIT ) {
510 // Typically, the user should have a handful of edits.
511 // Disallow hiding users with many edits for performance.
512 return array( 'ipb_hide_invalid' );
516 # Create block object
517 # Note: for a user block, ipb_address is only for display purposes
518 $block = new Block( $this->BlockAddress, $userId, $wgUser->getId(),
519 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
520 $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName,
522 isset( $this->BlockAllowUsertalk ) ? $this->BlockAllowUsertalk : $wgBlockAllowsUTEdit
525 # Should this be privately logged?
526 $suppressLog = (bool)$this->BlockHideName;
527 if( wfRunHooks( 'BlockIp', array( &$block, &$wgUser ) ) ) {
528 # Try to insert block. Is there a conflicting block?
529 if( !$block->insert() ) {
530 # Show form unless the user is already aware of this...
531 if( !$this->BlockReblock ) {
532 return array( 'ipb_already_blocked' );
533 # Otherwise, try to update the block...
535 # This returns direct blocks before autoblocks/rangeblocks, since we should
536 # be sure the user is blocked by now it should work for our purposes
537 $currentBlock = Block::newFromDB( $this->BlockAddress, $userId );
538 if( $block->equals( $currentBlock ) ) {
539 return array( 'ipb_already_blocked' );
541 # If the name was hidden and the blocking user cannot hide
542 # names, then don't allow any block changes...
543 if( $currentBlock->mHideName && !$wgUser->isAllowed( 'hideuser' ) ) {
544 return array( 'cant-see-hidden-user' );
546 $currentBlock->delete();
548 # If hiding/unhiding a name, this should go in the private logs
549 $suppressLog = $suppressLog || (bool)$currentBlock->mHideName;
550 $log_action = 'reblock';
551 # Unset _deleted fields if requested
552 if( $currentBlock->mHideName && !$this->BlockHideName ) {
553 self::unsuppressUserName( $this->BlockAddress, $userId );
557 $log_action = 'block';
559 wfRunHooks( 'BlockIpComplete', array( $block, $wgUser ) );
561 # Set *_deleted fields if requested
562 if( $this->BlockHideName ) {
563 self::suppressUserName( $this->BlockAddress, $userId );
566 # Only show watch link when this is no range block
567 if( $this->BlockWatchUser && $block->mRangeStart == $block->mRangeEnd ) {
568 $wgUser->addWatch( Title::makeTitle( NS_USER, $this->BlockAddress ) );
571 # Block constructor sanitizes certain block options on insert
572 $this->BlockEmail = $block->mBlockEmail;
573 $this->BlockEnableAutoblock = $block->mEnableAutoblock;
575 # Prepare log parameters
576 $logParams = array();
577 $logParams[] = $expirestr;
578 $logParams[] = $this->blockLogFlags();
580 # Make log entry, if the name is hidden, put it in the oversight log
581 $log_type = $suppressLog ? 'suppress' : 'block';
582 $log = new LogPage( $log_type );
583 $log->addEntry( $log_action, Title::makeTitle( NS_USER, $this->BlockAddress ),
584 $reasonstr, $logParams );
589 return array( 'hookaborted' );
593 public static function suppressUserName( $name, $userId, $dbw = null ) {
594 $op = '|'; // bitwise OR
595 return self::setUsernameBitfields( $name, $userId, $op, $dbw );
598 public static function unsuppressUserName( $name, $userId, $dbw = null ) {
599 $op = '&'; // bitwise AND
600 return self::setUsernameBitfields( $name, $userId, $op, $dbw );
603 private static function setUsernameBitfields( $name, $userId, $op, $dbw ) {
604 if( $op !== '|' && $op !== '&' ) return false; // sanity check
606 $dbw = wfGetDB( DB_MASTER );
607 $delUser = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
608 $delAction = LogPage::DELETED_ACTION | Revision::DELETED_RESTRICTED;
609 # Normalize user name
610 $userTitle = Title::makeTitleSafe( NS_USER, $name );
611 $userDbKey = $userTitle->getDBkey();
612 # To suppress, we OR the current bitfields with Revision::DELETED_USER
613 # to put a 1 in the username *_deleted bit. To unsuppress we AND the
614 # current bitfields with the inverse of Revision::DELETED_USER. The
615 # username bit is made to 0 (x & 0 = 0), while others are unchanged (x & 1 = x).
616 # The same goes for the sysop-restricted *_deleted bit.
618 $delUser = "~{$delUser}";
619 $delAction = "~{$delAction}";
621 # Hide name from live edits
622 $dbw->update( 'revision', array( "rev_deleted = rev_deleted $op $delUser" ),
623 array( 'rev_user' => $userId ), __METHOD__ );
624 # Hide name from deleted edits
625 $dbw->update( 'archive', array( "ar_deleted = ar_deleted $op $delUser" ),
626 array( 'ar_user_text' => $name ), __METHOD__ );
627 # Hide name from logs
628 $dbw->update( 'logging', array( "log_deleted = log_deleted $op $delUser" ),
629 array( 'log_user' => $userId, "log_type != 'suppress'" ), __METHOD__ );
630 $dbw->update( 'logging', array( "log_deleted = log_deleted $op $delAction" ),
631 array( 'log_namespace' => NS_USER, 'log_title' => $userDbKey,
632 "log_type != 'suppress'" ), __METHOD__ );
634 $dbw->update( 'recentchanges', array( "rc_deleted = rc_deleted $op $delUser" ),
635 array( 'rc_user_text' => $name ), __METHOD__ );
636 $dbw->update( 'recentchanges', array( "rc_deleted = rc_deleted $op $delAction" ),
637 array( 'rc_namespace' => NS_USER, 'rc_title' => $userDbKey, 'rc_logid > 0' ), __METHOD__ );
638 # Hide name from live images
639 $dbw->update( 'oldimage', array( "oi_deleted = oi_deleted $op $delUser" ),
640 array( 'oi_user_text' => $name ), __METHOD__ );
641 # Hide name from deleted images
642 # WMF - schema change pending
643 # $dbw->update( 'filearchive', array( "fa_deleted = fa_deleted $op $delUser" ),
644 # array( 'fa_user_text' => $name ), __METHOD__ );
650 * UI entry point for blocking
651 * Wraps around doBlock()
653 public function doSubmit() {
655 $retval = $this->doBlock();
656 if( empty( $retval ) ) {
657 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
658 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
659 urlencode( $this->BlockAddress ) ) );
662 $this->showForm( $retval );
665 public function showSuccess() {
668 $wgOut->setPageTitle( wfMsg( 'blockip-title' ) );
669 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
670 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress );
671 $wgOut->addHTML( $text );
674 private function showLogFragment( $out, $title ) {
677 // Used to support GENDER in 'blocklog-showlog' and 'blocklog-showsuppresslog'
678 $userBlocked = $title->getText();
680 LogEventsList::showLogExtract(
683 $title->getPrefixedText(),
691 'showIfEmpty' => false
695 // Add suppression block entries if allowed
696 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
697 LogEventsList::showLogExtract( $out, 'suppress', $title->getPrefixedText(), '',
701 'log_action' => array(
708 'blocklog-showsuppresslog',
711 'showIfEmpty' => false
718 * Return a comma-delimited list of "flags" to be passed to the log
719 * reader for this block, to provide more information in the logs
723 private function blockLogFlags() {
724 global $wgBlockAllowsUTEdit;
726 if( $this->BlockAnonOnly && IP::isIPAddress( $this->BlockAddress ) )
727 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
728 $flags[] = 'anononly';
729 if( $this->BlockCreateAccount )
730 $flags[] = 'nocreate';
731 if( !$this->BlockEnableAutoblock && !IP::isIPAddress( $this->BlockAddress ) )
732 // Same as anononly, this is not displayed when blocking an IP address
733 $flags[] = 'noautoblock';
734 if( $this->BlockEmail )
735 $flags[] = 'noemail';
736 if( !$this->BlockAllowUsertalk && $wgBlockAllowsUTEdit )
737 $flags[] = 'nousertalk';
738 if( $this->BlockHideName )
739 $flags[] = 'hiddenname';
740 return implode( ',', $flags );
744 * Builds unblock and block list links
748 private function getConvenienceLinks() {
749 global $wgUser, $wgLang;
750 $skin = $wgUser->getSkin();
751 if( $this->BlockAddress )
752 $links[] = $this->getContribsLink( $skin );
753 $links[] = $this->getUnblockLink( $skin );
754 $links[] = $this->getBlockListLink( $skin );
755 if ( $wgUser->isAllowed( 'editinterface' ) ) {
756 $title = Title::makeTitle( NS_MEDIAWIKI, 'Ipbreason-dropdown' );
757 $links[] = $skin->link(
759 wfMsgHtml( 'ipb-edit-dropdown' ),
761 array( 'action' => 'edit' )
764 return '<p class="mw-ipb-conveniencelinks">' . $wgLang->pipeList( $links ) . '</p>';
768 * Build a convenient link to a user or IP's contribs
771 * @param $skin Skin to use
774 private function getContribsLink( $skin ) {
775 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->BlockAddress );
776 return $skin->link( $contribsPage, wfMsgExt( 'ipb-blocklist-contribs', 'escape', $this->BlockAddress ) );
780 * Build a convenient link to unblock the given username or IP
781 * address, if available; otherwise link to a blank unblock
784 * @param $skin Skin to use
787 private function getUnblockLink( $skin ) {
788 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
789 $query = array( 'action' => 'unblock' );
791 if( $this->BlockAddress ) {
792 $addr = strtr( $this->BlockAddress, '_', ' ' );
793 $message = wfMsg( 'ipb-unblock-addr', $addr );
794 $query['ip'] = $this->BlockAddress;
796 $message = wfMsg( 'ipb-unblock' );
798 return $skin->linkKnown(
800 htmlspecialchars( $message ),
807 * Build a convenience link to the block list
809 * @param $skin Skin to use
812 private function getBlockListLink( $skin ) {
813 return $skin->linkKnown(
814 SpecialPage::getTitleFor( 'Ipblocklist' ),
815 wfMsg( 'ipb-blocklist' )
820 * Block a list of selected users
822 * @param $users Array
823 * @param $reason String
824 * @param $tag String: replaces user pages
825 * @param $talkTag String: replaces user talk pages
826 * @return Array: list of html-safe usernames
828 public static function doMassUserBlock( $users, $reason = '', $tag = '', $talkTag = '' ) {
830 $counter = $blockSize = 0;
831 $safeUsers = array();
832 $log = new LogPage( 'block' );
833 foreach( $users as $name ) {
837 # Lets not go *too* fast
838 if( $blockSize >= 20 ) {
840 wfWaitForSlaves( 5 );
842 $u = User::newFromName( $name, false );
843 // If user doesn't exist, it ought to be an IP then
844 if( is_null( $u ) || ( !$u->getId() && !IP::isIPAddress( $u->getName() ) ) ) {
847 $userTitle = $u->getUserPage();
848 $userTalkTitle = $u->getTalkPage();
849 $userpage = new Article( $userTitle );
850 $usertalk = new Article( $userTalkTitle );
851 $safeUsers[] = '[[' . $userTitle->getPrefixedText() . '|' . $userTitle->getText() . ']]';
852 $expirestr = $u->getId() ? 'indefinite' : '1 week';
853 $expiry = Block::parseExpiryInput( $expirestr );
854 $anonOnly = IP::isIPAddress( $u->getName() ) ? 1 : 0;
856 $block = new Block( $u->getName(), // victim
858 $wgUser->getId(), // blocker
860 wfTimestampNow(), // block time
863 $anonOnly, // anononly?
864 1, // block account creation?
867 0 // block from sending email?
869 $oldblock = Block::newFromDB( $u->getName(), $u->getId() );
872 # Prepare log parameters
873 $logParams = array();
874 $logParams[] = $expirestr;
876 $logParams[] = 'anononly';
878 $logParams[] = 'nocreate';
880 $log->addEntry( 'block', $userTitle, $reason, $logParams );
882 # Tag userpage! (check length to avoid mistakes)
883 if( strlen( $tag ) > 2 ) {
884 $userpage->doEdit( $tag, $reason, EDIT_MINOR );
886 if( strlen( $talkTag ) > 2 ) {
887 $usertalk->doEdit( $talkTag, $reason, EDIT_MINOR );