/// @var Block::TYPE_ constant
protected $type;
+ /// @var User|String the previous block target
+ protected $previousTarget;
+
+ /// @var Bool whether the previous submission of the form asked for HideUser
+ protected $requestedHideUser;
+
/// @var Bool
protected $alreadyBlocked;
+ /// @var Array
+ protected $preErrors = array();
+
public function __construct() {
parent::__construct( 'Block', 'block' );
}
$wgUser->getSkin()->setRelevantUser( $this->target );
}
+ list( $this->previousTarget, /*...*/ ) = Block::parseTarget( $wgRequest->getVal( 'wpPreviousTarget' ) );
+ $this->requestedHideUser = $wgRequest->getBool( 'wpHideUser' );
+
# bug 15810: blocked admins should have limited access here
$status = self::checkUnblockSelf( $this->target );
if ( $status !== true ) {
$wgOut->addModules( 'mediawiki.special', 'mediawiki.special.block' );
$fields = self::getFormFields();
- $this->alreadyBlocked = $this->maybeAlterFormDefaults( $fields );
+ $this->maybeAlterFormDefaults( $fields );
- $form = new HTMLForm( $fields );
- $form->setTitle( $this->getTitle() );
+ $form = new HTMLForm( $fields, $this->getContext() );
$form->setWrapperLegend( wfMsg( 'blockip-legend' ) );
$form->setSubmitCallback( array( __CLASS__, 'processForm' ) );
$form->setSubmitText( $t );
$this->doPreText( $form );
+ $this->doHeadertext( $form );
$this->doPostText( $form );
if( $form->show() ){
'default' => false,
);
- $a['AlreadyBlocked'] = array(
+ # This is basically a copy of the Target field, but the user can't change it, so we
+ # can see if the warnings we maybe showed to the user before still apply
+ $a['PreviousTarget'] = array(
'type' => 'hidden',
'default' => false,
);
+ # We'll turn this into a checkbox if we need to
+ $a['Confirm'] = array(
+ 'type' => 'hidden',
+ 'default' => '',
+ 'label-message' => 'ipb-confirm',
+ );
+
return $a;
}
* already blocked)
*/
protected function maybeAlterFormDefaults( &$fields ){
+ global $wgRequest, $wgUser;
+
+ # This will be overwritten by request data
$fields['Target']['default'] = (string)$this->target;
- $block = Block::newFromTargetAndType( $this->target, $this->type );
+ # This won't be
+ $fields['PreviousTarget']['default'] = (string)$this->target;
+
+ $block = Block::newFromTarget( $this->target );
if( $block instanceof Block && !$block->mAuto # The block exists and isn't an autoblock
&& ( $this->type != Block::TYPE_RANGE # The block isn't a rangeblock
- || $block->mAddress == $this->target ) # or if it is, the range is what we're about to block
+ || $block->getTarget() == $this->target ) # or if it is, the range is what we're about to block
)
{
- $fields['HardBlock']['default'] = !$block->mAnonOnly;
- $fields['CreateAccount']['default'] = $block->mCreateAccount;
- $fields['AutoBlock']['default'] = $block->mEnableAutoblock;
+ $fields['HardBlock']['default'] = $block->isHardblock();
+ $fields['CreateAccount']['default'] = $block->prevents( 'createaccount' );
+ $fields['AutoBlock']['default'] = $block->isAutoblocking();
if( isset( $fields['DisableEmail'] ) ){
- $fields['DisableEmail']['default'] = $block->mBlockEmail;
+ $fields['DisableEmail']['default'] = $block->prevents( 'sendemail' );
}
if( isset( $fields['HideUser'] ) ){
$fields['HideUser']['default'] = $block->mHideName;
}
if( isset( $fields['DisableUTEdit'] ) ){
- $fields['DisableUTEdit']['default'] = !$block->mAllowUsertalk;
+ $fields['DisableUTEdit']['default'] = $block->prevents( 'editownusertalk' );
}
$fields['Reason']['default'] = $block->mReason;
- $fields['AlreadyBlocked']['default'] = true;
+
+ if( $wgRequest->wasPosted() ){
+ # Ok, so we got a POST submission asking us to reblock a user. So show the
+ # confirm checkbox; the user will only see it if they haven't previously
+ $fields['Confirm']['type'] = 'check';
+ } else {
+ # We got a target, but it wasn't a POST request, so the user must have gone
+ # to a link like [[Special:Block/User]]. We don't need to show the checkbox
+ # as long as they go ahead and block *that* user
+ $fields['Confirm']['default'] = 1;
+ }
if( $block->mExpiry == 'infinity' ) {
$fields['Expiry']['default'] = 'indefinite';
$fields['Expiry']['default'] = wfTimestamp( TS_RFC2822, $block->mExpiry );
}
- return true;
+ $this->alreadyBlocked = true;
+ $this->preErrors[] = array( 'ipb-needreblock', (string)$block->getTarget() );
+ }
+
+ # We always need confirmation to do HideUser
+ if( $this->requestedHideUser ){
+ $fields['Confirm']['type'] = 'check';
+ unset( $fields['Confirm']['default'] );
+ $this->preErrors[] = 'ipb-confirmhideuser';
+ }
+
+ # Or if the user is trying to block themselves
+ if( (string)$this->target === $wgUser->getName() ){
+ $fields['Confirm']['type'] = 'check';
+ unset( $fields['Confirm']['default'] );
+ $this->preErrors[] = 'ipb-blockingself';
}
- return false;
}
/**
$form->addPreText( $s );
}
}
+ }
- # Username/IP is blocked already locally
- if( $this->alreadyBlocked ) {
- $form->addPreText( Html::rawElement(
- 'div',
- array( 'class' => 'mw-ipb-needreblock', ),
- wfMsgExt(
- 'ipb-needreblock',
- array( 'parseinline' ),
- $this->target
- ) ) );
+ /**
+ * Add header text inside the form, just underneath where the errors would go
+ * @param $form HTMLForm
+ * @return void
+ */
+ protected function doHeaderText( HTMLForm &$form ){
+ global $wgRequest;
+ # Don't need to do anything if the form has been posted
+ if( !$wgRequest->wasPosted() && $this->preErrors ){
+ $s = HTMLForm::formatErrors( $this->preErrors );
+ if( $s ){
+ $form->addHeaderText( Html::rawElement(
+ 'div',
+ array( 'class' => 'error' ),
+ $s
+ ) );
+ }
}
}
if( $type == Block::TYPE_USER ){
# TODO: why do we not have a User->exists() method?
if( !$target->getId() ){
- return wfMessage( 'nosuchusershort', $target->getName() );
+ return wfMessage( 'nosuchusershort',
+ wfEscapeWikiText( $target->getName() ) );
}
$status = self::checkUnblockSelf( $target );
// Handled by field validator callback
// self::validateTargetField( $data['Target'] );
+ # This might have been a hidden field or a checkbox, so interesting data
+ # can come from it
+ $data['Confirm'] = !in_array( $data['Confirm'], array( '', '0', null, false ), true );
+
list( $target, $type ) = self::getTargetAndType( $data['Target'] );
if( $type == Block::TYPE_USER ){
$user = $target;
$target = $user->getName();
$userId = $user->getId();
+
+ # Give admins a heads-up before they go and block themselves. Much messier
+ # to do this for IPs, but it's pretty unlikely they'd ever get the 'block'
+ # permission anyway, although the code does allow for it
+ if( $target === $wgUser->getName() &&
+ ( $data['PreviousTarget'] != $data['Target'] || !$data['Confirm'] ) )
+ {
+ return array( 'ipb-blockingself' );
+ }
+
} elseif( $type == Block::TYPE_RANGE ){
$userId = 0;
+
} elseif( $type == Block::TYPE_IP ){
$target = $target->getName();
$userId = 0;
+
} else {
# This should have been caught in the form field validation
return array( 'badipaddress' );
return array( 'ipb_expiry_invalid' );
}
- if( !$wgBlockAllowsUTEdit ){
- $data['DisableUTEdit'] = true;
+ if( !isset( $data['DisableEmail'] ) ){
+ $data['DisableEmail'] = false;
}
# If the user has done the form 'properly', they won't even have been given the
# Typically, the user should have a handful of edits.
# Disallow hiding users with many edits for performance.
return array( 'ipb_hide_invalid' );
+
+ } elseif( !$data['Confirm'] ){
+ return array( 'ipb-confirmhideuser' );
}
}
- # Create block object. Note that for a user block, ipb_address is only for display purposes
- # FIXME: Why do we need to pass fourteen optional parameters to do this!?!
- $block = new Block(
- $target, # IP address or User name
- $userId, # User id
- $wgUser->getId(), # Blocker id
- $data['Reason'][0], # Reason string
- wfTimestampNow(), # Block Timestamp
- 0, # Is this an autoblock (no)
- self::parseExpiryInput( $data['Expiry'] ), # Expiry time
- !$data['HardBlock'], # Block anon only
- $data['CreateAccount'],
- $data['AutoBlock'],
- $data['HideUser'],
- $data['DisableEmail'],
- !$data['DisableUTEdit'] # *Allow* UTEdit
- );
+ # Create block object.
+ $block = new Block();
+ $block->setTarget( $target );
+ $block->setBlocker( $wgUser );
+ $block->mReason = $data['Reason'][0];
+ $block->mExpiry = self::parseExpiryInput( $data['Expiry'] );
+ $block->prevents( 'createaccount', $data['CreateAccount'] );
+ $block->prevents( 'editownusertalk', ( !$wgBlockAllowsUTEdit || $data['DisableUTEdit'] ) );
+ $block->prevents( 'sendemail', $data['DisableEmail'] );
+ $block->isHardblock( $data['HardBlock'] );
+ $block->isAutoblocking( $data['AutoBlock'] );
+ $block->mHideName = $data['HideUser'];
if( !wfRunHooks( 'BlockIp', array( &$block, &$wgUser ) ) ) {
return array( 'hookaborted' );
}
# Try to insert block. Is there a conflicting block?
- if( !$block->insert() ) {
-
+ $status = $block->insert();
+ if( !$status ) {
# Show form unless the user is already aware of this...
- if( !$data['AlreadyBlocked'] ) {
- return array( array( 'ipb_already_blocked', $data['Target'] ) );
-
+ if( !$data['Confirm'] || ( array_key_exists( 'PreviousTarget', $data )
+ && $data['PreviousTarget'] !== htmlspecialchars( $block->getTarget() ) ) )
+ {
+ return array( array( 'ipb_already_blocked', $block->getTarget() ) );
# Otherwise, try to update the block...
} else {
-
# This returns direct blocks before autoblocks/rangeblocks, since we should
# be sure the user is blocked by now it should work for our purposes
- $currentBlock = Block::newFromDB( $target, $userId );
+ $currentBlock = Block::newFromTarget( $target );
if( $block->equals( $currentBlock ) ) {
- return array( 'ipb_already_blocked' );
+ return array( array( 'ipb_already_blocked', $block->getTarget() ) );
}
# If the name was hidden and the blocking user cannot hide
}
$currentBlock->delete();
- $block->insert();
+ $status = $block->insert();
$logaction = 'reblock';
# Unset _deleted fields if requested
$data['HideUser'] = true;
}
}
-
} else {
$logaction = 'block';
}
}
# Block constructor sanitizes certain block options on insert
- $data['BlockEmail'] = $block->mBlockEmail;
- $data['AutoBlock'] = $block->mEnableAutoblock;
+ $data['BlockEmail'] = $block->prevents( 'sendemail' );
+ $data['AutoBlock'] = $block->isAutoblocking();
# Prepare log parameters
$logParams = array();
# Make log entry, if the name is hidden, put it in the oversight log
$log_type = $data['HideUser'] ? 'suppress' : 'block';
$log = new LogPage( $log_type );
- $log->addEntry(
+ $log_id = $log->addEntry(
$logaction,
Title::makeTitle( NS_USER, $target ),
$data['Reason'][0],
$logParams
);
+ # Relate log ID to block IDs (bug 25763)
+ $blockIds = array_merge( array( $status['id'] ), $status['autoIds'] );
+ $log->addRelations( 'ipb_id', $blockIds, $log_id );
# Report to the user
return true;
/**
* Get an array of suggested block durations from MediaWiki:Ipboptions
- * FIXME: this uses a rather odd syntax for the options, should it be converted
+ * @todo FIXME: This uses a rather odd syntax for the options, should it be converted
* to the standard "**<duration>|<displayname>" format?
* @return Array
*/
public static function getSuggestedDurations( $lang = null ){
$a = array();
$msg = $lang === null
- ? wfMessage( 'ipboptions' )->inContentLanguage()
- : wfMessage( 'ipboptions' )->inLanguage( $lang );
+ ? wfMessage( 'ipboptions' )->inContentLanguage()->text()
+ : wfMessage( 'ipboptions' )->inLanguage( $lang )->text();
if( $msg == '-' ){
return array();
public static function parseExpiryInput( $expiry ) {
static $infinity;
if( $infinity == null ){
- $infinity = wfGetDB( DB_READ )->getInfinity();
+ $infinity = wfGetDB( DB_SLAVE )->getInfinity();
}
if ( $expiry == 'infinite' || $expiry == 'indefinite' ) {
$expiry = $infinity;
$flags[] = 'noemail';
}
- if( $data['DisableUTEdit'] && $wgBlockAllowsUTEdit ){
+ if( $wgBlockAllowsUTEdit && $data['DisableUTEdit'] ){
$flags[] = 'nousertalk';
}