Introduce Language::getMessageKeysFor() and use it in ApiQueryAllmessages
[lhc/web/wiklou.git] / includes / specials / SpecialBlock.php
index 91d8af9..cece6b5 100644 (file)
@@ -28,7 +28,6 @@
  * @ingroup SpecialPage
  */
 class SpecialBlock extends SpecialPage {
-
        /** The maximum number of edits a user can have and still be hidden
         * TODO: config setting? */
        const HIDEUSER_CONTRIBLIMIT = 1000;
@@ -40,51 +39,62 @@ class SpecialBlock extends SpecialPage {
        /// @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' );
        }
 
        public function execute( $par ) {
-               global $wgUser, $wgOut, $wgRequest;
+               # Permission check
+               if( !$this->userCanExecute( $this->getUser() ) ) {
+                       $this->displayRestrictionError();
+                       return;
+               }
 
                # Can't block when the database is locked
                if( wfReadOnly() ) {
-                       $wgOut->readOnlyPage();
-                       return;
-               }
-               # Permission check
-               if( !$this->userCanExecute( $wgUser ) ) {
-                       $wgOut->permissionRequired( 'block' );
-                       return;
+                       throw new ReadOnlyError;
                }
 
                # Extract variables from the request.  Try not to get into a situation where we
                # need to extract *every* variable from the form just for processing here, but
                # there are legitimate uses for some variables
-               list( $this->target, $this->type ) = self::getTargetAndType( $par, $wgRequest );
+               $request = $this->getRequest();
+               list( $this->target, $this->type ) = self::getTargetAndType( $par, $request );
                if ( $this->target instanceof User ) {
                        # Set the 'relevant user' in the skin, so it displays links like Contributions,
                        # User logs, UserRights, etc.
-                       $wgUser->getSkin()->setRelevantUser( $this->target );
+                       $this->getSkin()->setRelevantUser( $this->target );
                }
-       
+
+               list( $this->previousTarget, /*...*/ ) = Block::parseTarget( $request->getVal( 'wpPreviousTarget' ) );
+               $this->requestedHideUser = $request->getBool( 'wpHideUser' );
+
                # bug 15810: blocked admins should have limited access here
                $status = self::checkUnblockSelf( $this->target );
                if ( $status !== true ) {
                        throw new ErrorPageError( 'badaccess', $status );
                }
 
-               $wgOut->setPageTitle( wfMsg( 'blockip-title' ) );
-               $wgOut->addModules( 'mediawiki.special', 'mediawiki.special.block' );
+               $out = $this->getOutput();
+               $out->setPageTitle( wfMsg( 'blockip-title' ) );
+               $out->addModules( 'mediawiki.special', 'mediawiki.special.block' );
 
-               $fields = self::getFormFields();
-               $this->alreadyBlocked = $this->maybeAlterFormDefaults( $fields );
+               $fields = $this->getFormFields();
+               $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' ) );
 
@@ -94,11 +104,12 @@ class SpecialBlock extends SpecialPage {
                $form->setSubmitText( $t );
 
                $this->doPreText( $form );
+               $this->doHeadertext( $form );
                $this->doPostText( $form );
 
                if( $form->show() ){
-                       $wgOut->setPageTitle( wfMsg( 'blockipsuccesssub' ) );
-                       $wgOut->addWikiMsg( 'blockipsuccesstext',  $this->target );
+                       $out->setPageTitle( wfMsg( 'blockipsuccesssub' ) );
+                       $out->addWikiMsg( 'blockipsuccesstext',  $this->target );
                }
        }
 
@@ -106,8 +117,10 @@ class SpecialBlock extends SpecialPage {
         * Get the HTMLForm descriptor array for the block form
         * @return Array
         */
-       protected static function getFormFields(){
-               global $wgUser, $wgBlockAllowsUTEdit;
+       protected function getFormFields(){
+               global $wgBlockAllowsUTEdit;
+
+               $user = $this->getUser();
 
                $a = array(
                        'Target' => array(
@@ -120,7 +133,7 @@ class SpecialBlock extends SpecialPage {
                                'validation-callback' => array( __CLASS__, 'validateTargetField' ),
                        ),
                        'Expiry' => array(
-                               'type' => 'selectorother',
+                               'type' => !count( self::getSuggestedDurations() ) ? 'text' : 'selectorother',
                                'label-message' => 'ipbexpiry',
                                'required' => true,
                                'tabindex' => '2',
@@ -139,11 +152,7 @@ class SpecialBlock extends SpecialPage {
                        ),
                );
 
-               if( wfMsgForContent( 'ipboptions' ) == '-' ){
-                       $a['Expiry']['type'] = 'text';
-               }
-
-               if( self::canBlockEmail( $wgUser ) ) {
+               if( self::canBlockEmail( $user ) ) {
                        $a['DisableEmail'] = array(
                                'type' => 'check',
                                'label-message' => 'ipbemailban',
@@ -165,7 +174,7 @@ class SpecialBlock extends SpecialPage {
                );
 
                # Allow some users to hide name from block log, blocklist and listusers
-               if( $wgUser->isAllowed( 'hideuser' ) ) {
+               if( $user->isAllowed( 'hideuser' ) ) {
                        $a['HideUser'] = array(
                                'type' => 'check',
                                'label-message' => 'ipbhidename',
@@ -174,7 +183,7 @@ class SpecialBlock extends SpecialPage {
                }
 
                # Watchlist their user page? (Only if user is logged in)
-               if( $wgUser->isLoggedIn() ) {
+               if( $user->isLoggedIn() ) {
                        $a['Watch'] = array(
                                'type' => 'check',
                                'label-message' => 'ipbwatchuser',
@@ -187,11 +196,20 @@ class SpecialBlock extends SpecialPage {
                        '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;
        }
 
@@ -203,23 +221,47 @@ class SpecialBlock extends SpecialPage {
         *     already blocked)
         */
        protected function maybeAlterFormDefaults( &$fields ){
-               $fields['Target']['default'] = $this->target;
+               # This will be overwritten by request data
+               $fields['Target']['default'] = (string)$this->target;
+
+               # This won't be
+               $fields['PreviousTarget']['default'] = (string)$this->target;
 
-               $block = self::getBlockFromTargetAndType( $this->target, $this->type );
+               $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['DisableEmail']['default'] = $block->mBlockEmail;
-                       $fields['HideUser']['default'] = $block->mHideName;
-                       $fields['DisableUTEdit']['default'] = !$block->mAllowUsertalk;
+                       $fields['HardBlock']['default'] = $block->isHardblock();
+                       $fields['CreateAccount']['default'] = $block->prevents( 'createaccount' );
+                       $fields['AutoBlock']['default'] = $block->isAutoblocking();
+
+                       if( isset( $fields['DisableEmail'] ) ){
+                               $fields['DisableEmail']['default'] = $block->prevents( 'sendemail' );
+                       }
+
+                       if( isset( $fields['HideUser'] ) ){
+                               $fields['HideUser']['default'] = $block->mHideName;
+                       }
+
+                       if( isset( $fields['DisableUTEdit'] ) ){
+                               $fields['DisableUTEdit']['default'] = $block->prevents( 'editownusertalk' );
+                       }
+
                        $fields['Reason']['default'] = $block->mReason;
-                       $fields['AlreadyBlocked']['default'] = true;
+
+                       if( $this->getRequest()->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';
@@ -227,9 +269,23 @@ class SpecialBlock extends SpecialPage {
                                $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 === $this->getUser()->getName() ){
+                       $fields['Confirm']['type'] = 'check';
+                       unset( $fields['Confirm']['default'] );
+                       $this->preErrors[] = 'ipb-blockingself';
                }
-               return false;
        }
 
        /**
@@ -251,29 +307,40 @@ class SpecialBlock extends SpecialPage {
                                        array(),
                                        wfMsgExt( 'ipb-otherblocks-header', 'parseinline', count( $otherBlockMessages ) )
                                ) . "\n";
+
                                $list = '';
+
                                foreach( $otherBlockMessages as $link ) {
                                        $list .= Html::rawElement( 'li', array(), $link ) . "\n";
                                }
+
                                $s .= Html::rawElement(
                                        'ul',
                                        array( 'class' => 'mw-blockip-alreadyblocked' ),
                                        $list
                                ) . "\n";
+
                                $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 ){
+               # Don't need to do anything if the form has been posted
+               if( !$this->getRequest()->wasPosted() && $this->preErrors ){
+                       $s = HTMLForm::formatErrors( $this->preErrors );
+                       if( $s ){
+                               $form->addHeaderText( Html::rawElement(
+                                       'div',
+                                       array( 'class' => 'error' ),
+                                       $s
+                               ) );
+                       }
                }
        }
 
@@ -283,39 +350,36 @@ class SpecialBlock extends SpecialPage {
         * @return void
         */
        protected function doPostText( HTMLForm &$form ){
-               global $wgUser, $wgLang;
-
-               $skin = $wgUser->getSkin();
-
                # Link to the user's contributions, if applicable
                if( $this->target instanceof User ){
                        $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->target->getName() );
-                       $links[] = $skin->link(
+                       $links[] = Linker::link(
                                $contribsPage,
                                wfMsgExt( 'ipb-blocklist-contribs', 'escape', $this->target->getName() )
                        );
                }
 
                # Link to unblock the specified user, or to a blank unblock form
-               $list = SpecialPage::getTitleFor( 'Ipblocklist' );
-               $query = array( 'action' => 'unblock' );
                if( $this->target instanceof User ) {
                        $message = wfMsgExt( 'ipb-unblock-addr', array( 'parseinline' ), $this->target->getName() );
-                       $query['ip'] = $this->target->getName();
+                       $list = SpecialPage::getTitleFor( 'Unblock', $this->target->getName() );
                } else {
                        $message = wfMsgExt( 'ipb-unblock', array( 'parseinline' ) );
+                       $list = SpecialPage::getTitleFor( 'Unblock' );
                }
-               $links[] = $skin->linkKnown( $list, $message, array(), $query );
+               $links[] = Linker::linkKnown( $list, $message, array() );
 
                # Link to the block list
-               $links[] = $skin->linkKnown(
-                       SpecialPage::getTitleFor( 'Ipblocklist' ),
+               $links[] = Linker::linkKnown(
+                       SpecialPage::getTitleFor( 'BlockList' ),
                        wfMsg( 'ipb-blocklist' )
                );
 
+               $user = $this->getUser();
+
                # Link to edit the block dropdown reasons, if applicable
-               if ( $wgUser->isAllowed( 'editinterface' ) ) {
-                       $links[] = $skin->link(
+               if ( $user->isAllowed( 'editinterface' ) ) {
+                       $links[] = Linker::link(
                                Title::makeTitle( NS_MEDIAWIKI, 'Ipbreason-dropdown' ),
                                wfMsgHtml( 'ipb-edit-dropdown' ),
                                array(),
@@ -326,7 +390,7 @@ class SpecialBlock extends SpecialPage {
                $form->addPostText( Html::rawElement(
                        'p',
                        array( 'class' => 'mw-ipb-conveniencelinks' ),
-                       $wgLang->pipeList( $links )
+                       $this->getLang()->pipeList( $links )
                ) );
 
                if( $this->target instanceof User ){
@@ -348,7 +412,7 @@ class SpecialBlock extends SpecialPage {
                        $form->addPostText( $out );
 
                        # Add suppression block entries if allowed
-                       if( $wgUser->isAllowed( 'suppressionlog' ) ) {
+                       if( $user->isAllowed( 'suppressionlog' ) ) {
                                LogEventsList::showLogExtract(
                                        $out,
                                        'suppress',
@@ -361,6 +425,7 @@ class SpecialBlock extends SpecialPage {
                                                'showIfEmpty' => false
                                        )
                                );
+
                                $form->addPostText( $out );
                        }
                }
@@ -372,11 +437,12 @@ class SpecialBlock extends SpecialPage {
         * @param $par String subpage parameter passed to setup, or data value from
         *     the HTMLForm
         * @param $request WebRequest optionally try and get data from a request too
-        * @return void
+        * @return array( User|string|null, Block::TYPE_ constant|null )
         */
        public static function getTargetAndType( $par, WebRequest $request = null ){
                $i = 0;
                $target = null;
+
                while( true ){
                        switch( $i++ ){
                                case 0:
@@ -387,7 +453,7 @@ class SpecialBlock extends SpecialPage {
                                        if( $request instanceof WebRequest ){
                                                $target = $request->getText( 'wpTarget', null );
                                        }
-                               break;
+                                       break;
                                case 1:
                                        $target = $par;
                                        break;
@@ -395,69 +461,44 @@ class SpecialBlock extends SpecialPage {
                                        if( $request instanceof WebRequest ){
                                                $target = $request->getText( 'ip', null );
                                        }
-                               break;
+                                       break;
                                case 3:
                                        # B/C @since 1.18
                                        if( $request instanceof WebRequest ){
                                                $target = $request->getText( 'wpBlockAddress', null );
                                        }
-                               break;
+                                       break;
                                case 4:
                                        break 2;
                        }
 
-                       $userObj = User::newFromName( $target );
-                       if( $userObj instanceof User ){
-                               return array( $userObj, Block::TYPE_USER );
-                       } elseif( IP::isValid( $target ) ){
-                               # We can still create a User if it's an IP address, but we need to turn
-                               # off validation checking (which would exclude IP addresses)
-                               return array(
-                                       User::newFromName( IP::sanitizeIP( $target ), false ),
-                                       Block::TYPE_IP
-                               );
-                               break;
-                       } elseif( IP::isValidBlock( $target ) ){
-                               # Can't create a User from an IP range
-                               return array( Block::normaliseRange( $target ), Block::TYPE_RANGE );
+                       list( $target, $type ) = Block::parseTarget( $target );
+
+                       if( $type !== null ){
+                               return array( $target, $type );
                        }
                }
+
                return array( null, null );
        }
 
        /**
-        * Given a target and the target's type, get a block object if possible
-        * @param $target String|User
-        * @param $type Block::TYPE_ constant
-        * @return Block|null
-        * TODO: this probably belongs in Block.php when that mess is cleared up
+        * HTMLForm field validation-callback for Target field.
+        * @since 1.18
+        * @param $value String
+        * @param $alldata Array
+        * @return Message
         */
-       public static function getBlockFromTargetAndType( $target, $type ){
-               if( $target instanceof User ){
-                       if( $type == Block::TYPE_IP ){
-                               return Block::newFromDB( $target->getName(), 0 );
-                       } elseif( $type == Block::TYPE_USER ) {
-                               return Block::newFromDB( '', $target->getId() );
-                       } else {
-                               # Should be unreachable;
-                               return null;
-                       }
-               } elseif( $type == Block::TYPE_RANGE ){
-                       return Block::newFromDB( '', $target );
-               } else {
-                       return null;
-               }
-       }
-
-       public static function validateTargetField( $data, $alldata = null ) {
+       public static function validateTargetField( $value, $alldata = null ) {
                global $wgBlockCIDRLimit;
 
-               list( $target, $type ) = self::getTargetAndType( $data );
+               list( $target, $type ) = self::getTargetAndType( $value );
 
                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 );
@@ -465,10 +506,6 @@ class SpecialBlock extends SpecialPage {
                                return wfMessage( 'badaccess', $status );
                        }
 
-                       $user = $target;
-                       $target = $user->getName();
-                       $userId = $user->getId();
-
                } elseif( $type == Block::TYPE_RANGE ){
                        list( $ip, $range ) = explode( '/', $target, 2 );
 
@@ -493,17 +530,13 @@ class SpecialBlock extends SpecialPage {
                        if( IP::isIPv6( $ip ) && $range < $wgBlockCIDRLimit['IPv6'] ) {
                                return wfMessage( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv6'] );
                        }
-
-                       $userId = 0;
-
                } elseif( $type == Block::TYPE_IP ){
                        # All is well
-                       $target = $target->getName();
-                       $userId = 0;
-
                } else {
                        return wfMessage( 'badipaddress' );
                }
+
+               return true;
        }
 
        /**
@@ -517,14 +550,42 @@ class SpecialBlock extends SpecialPage {
                // 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' );
+               }
+
                if( ( strlen( $data['Expiry'] ) == 0) || ( strlen( $data['Expiry'] ) > 50 )
-                       || !Block::parseExpiryInput( $data['Expiry'] ) )
+                       || !self::parseExpiryInput( $data['Expiry'] ) )
                {
                        return array( 'ipb_expiry_invalid' );
                }
 
-               if( !$wgBlockAllowsUTEdit ){
-                       $data['PreventUTEdit'] = true;
+               if( !isset( $data['DisableEmail'] ) ){
+                       $data['DisableEmail'] = false;
                }
 
                # If the user has done the form 'properly', they won't even have been given the
@@ -532,6 +593,7 @@ class SpecialBlock extends SpecialPage {
                if( !isset( $data['HideUser'] ) ){
                        $data['HideUser'] = false;
                }
+
                if( $data['HideUser'] ) {
                        if( !$wgUser->isAllowed('hideuser') ){
                                # this codepath is unreachable except by a malicious user spoofing forms,
@@ -544,56 +606,51 @@ class SpecialBlock extends SpecialPage {
                        # Recheck params here...
                        if( $type != Block::TYPE_USER ) {
                                $data['HideUser'] = false; # IP users should not be hidden
-
-                       } elseif( !in_array( $data['Expiry'], array( 'inifinite', 'infinity', 'indefinite' ) ) ) {
+                       } elseif( !in_array( $data['Expiry'], array( 'infinite', 'infinity', 'indefinite' ) ) ) {
                                # Bad expiry.
                                return array( 'ipb_expiry_temp' );
-
                        } elseif( $user->getEditCount() > self::HIDEUSER_CONTRIBLIMIT ) {
                                # 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)
-                       Block::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'] !== $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
@@ -603,7 +660,7 @@ class SpecialBlock extends SpecialPage {
                                }
 
                                $currentBlock->delete();
-                               $block->insert();
+                               $status = $block->insert();
                                $logaction = 'reblock';
 
                                # Unset _deleted fields if requested
@@ -616,7 +673,6 @@ class SpecialBlock extends SpecialPage {
                                        $data['HideUser'] = true;
                                }
                        }
-
                } else {
                        $logaction = 'block';
                }
@@ -634,8 +690,8 @@ class SpecialBlock extends SpecialPage {
                }
 
                # 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();
@@ -645,12 +701,15 @@ class SpecialBlock extends SpecialPage {
                # 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;
@@ -658,20 +717,61 @@ class SpecialBlock extends SpecialPage {
 
        /**
         * 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?
+        * @param $lang Language|null the language to get the durations in, or null to use
+        *     the wiki's content language
         * @return Array
         */
-       protected static function getSuggestedDurations(){
+       public static function getSuggestedDurations( $lang = null ){
                $a = array();
-               foreach( explode( ',', wfMsgForContent( 'ipboptions' ) ) as $option ) {
-                       if( strpos( $option, ':' ) === false ) $option = "$option:$option";
+               $msg = $lang === null
+                       ? wfMessage( 'ipboptions' )->inContentLanguage()->text()
+                       : wfMessage( 'ipboptions' )->inLanguage( $lang )->text();
+
+               if( $msg == '-' ){
+                       return array();
+               }
+
+               foreach( explode( ',', $msg ) as $option ) {
+                       if( strpos( $option, ':' ) === false ){
+                               $option = "$option:$option";
+                       }
+
                        list( $show, $value ) = explode( ':', $option );
                        $a[htmlspecialchars( $show )] = htmlspecialchars( $value );
                }
+
                return $a;
        }
 
+       /**
+        * Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute
+        * ("24 May 2034", etc), into an absolute timestamp we can put into the database.
+        * @param $expiry String: whatever was typed into the form
+        * @return String: timestamp or "infinity" string for the DB implementation
+        */
+       public static function parseExpiryInput( $expiry ) {
+               static $infinity;
+               if( $infinity == null ){
+                       $infinity = wfGetDB( DB_SLAVE )->getInfinity();
+               }
+
+               if ( $expiry == 'infinite' || $expiry == 'indefinite' ) {
+                       $expiry = $infinity;
+               } else {
+                       $expiry = strtotime( $expiry );
+
+                       if ( $expiry < 0 || $expiry === false ) {
+                               return false;
+                       }
+
+                       $expiry = wfTimestamp( TS_MW, $expiry );
+               }
+
+               return $expiry;
+       }
+
        /**
         * Can we do an email block?
         * @param $user User: the sysop wanting to make a block
@@ -679,6 +779,7 @@ class SpecialBlock extends SpecialPage {
         */
        public static function canBlockEmail( $user ) {
                global $wgEnableUserEmail, $wgSysopEmailBans;
+
                return ( $wgEnableUserEmail && $wgSysopEmailBans && $user->isAllowed( 'blockemail' ) );
        }
 
@@ -687,19 +788,25 @@ class SpecialBlock extends SpecialPage {
         * others, and probably shouldn't be able to unblock themselves
         * either.
         * @param $user User|Int|String
+        * @return Bool|String true or error message key
         */
        public static function checkUnblockSelf( $user ) {
                global $wgUser;
+
                if ( is_int( $user ) ) {
                        $user = User::newFromId( $user );
                } elseif ( is_string( $user ) ) {
                        $user = User::newFromName( $user );
                }
+
                if( $wgUser->isBlocked() ){
                        if( $user instanceof User && $user->getId() == $wgUser->getId() ) {
                                # User is trying to unblock themselves
                                if ( $wgUser->isAllowed( 'unblockself' ) ) {
                                        return true;
+                               # User blocked themselves and is now trying to reverse it
+                               } elseif ( $wgUser->blockedBy() === $wgUser->getName() ) {
+                                       return true;
                                } else {
                                        return 'ipbnounblockself';
                                }
@@ -725,27 +832,33 @@ class SpecialBlock extends SpecialPage {
 
                # when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
                if( !$data['HardBlock'] && $type != Block::TYPE_USER ){
+                       // For grepping: message block-log-flags-anononly
                        $flags[] = 'anononly';
                }
 
                if( $data['CreateAccount'] ){
+                       // For grepping: message block-log-flags-nocreate
                        $flags[] = 'nocreate';
                }
 
                # Same as anononly, this is not displayed when blocking an IP address
                if( !$data['AutoBlock'] && $type != Block::TYPE_IP ){
+                       // For grepping: message block-log-flags-noautoblock
                        $flags[] = 'noautoblock';
                }
 
                if( $data['DisableEmail'] ){
+                       // For grepping: message block-log-flags-noemail
                        $flags[] = 'noemail';
                }
 
-               if( $data['DisableUTEdit'] && $wgBlockAllowsUTEdit ){
+               if( $wgBlockAllowsUTEdit && $data['DisableUTEdit'] ){
+                       // For grepping: message block-log-flags-nousertalk
                        $flags[] = 'nousertalk';
                }
 
                if( $data['HideUser'] ){
+                       // For grepping: message block-log-flags-hiddenname
                        $flags[] = 'hiddenname';
                }