Introduce Language::getMessageKeysFor() and use it in ApiQueryAllmessages
[lhc/web/wiklou.git] / includes / specials / SpecialBlock.php
index a98b1b6..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;
@@ -57,10 +56,8 @@ class SpecialBlock extends SpecialPage {
        }
 
        public function execute( $par ) {
-               global $wgUser, $wgOut, $wgRequest;
-
                # Permission check
-               if( !$this->userCanExecute( $wgUser ) ) {
+               if( !$this->userCanExecute( $this->getUser() ) ) {
                        $this->displayRestrictionError();
                        return;
                }
@@ -73,15 +70,16 @@ class SpecialBlock extends SpecialPage {
                # 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.
                        $this->getSkin()->setRelevantUser( $this->target );
                }
 
-               list( $this->previousTarget, /*...*/ ) = Block::parseTarget( $wgRequest->getVal( 'wpPreviousTarget' ) );
-               $this->requestedHideUser = $wgRequest->getBool( 'wpHideUser' );
+               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 );
@@ -89,10 +87,11 @@ class SpecialBlock extends SpecialPage {
                        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();
+               $fields = $this->getFormFields();
                $this->maybeAlterFormDefaults( $fields );
 
                $form = new HTMLForm( $fields, $this->getContext() );
@@ -109,8 +108,8 @@ class SpecialBlock extends SpecialPage {
                $this->doPostText( $form );
 
                if( $form->show() ){
-                       $wgOut->setPageTitle( wfMsg( 'blockipsuccesssub' ) );
-                       $wgOut->addWikiMsg( 'blockipsuccesstext',  $this->target );
+                       $out->setPageTitle( wfMsg( 'blockipsuccesssub' ) );
+                       $out->addWikiMsg( 'blockipsuccesstext',  $this->target );
                }
        }
 
@@ -118,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(
@@ -151,7 +152,7 @@ class SpecialBlock extends SpecialPage {
                        ),
                );
 
-               if( self::canBlockEmail( $wgUser ) ) {
+               if( self::canBlockEmail( $user ) ) {
                        $a['DisableEmail'] = array(
                                'type' => 'check',
                                'label-message' => 'ipbemailban',
@@ -173,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',
@@ -182,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',
@@ -220,8 +221,6 @@ class SpecialBlock extends SpecialPage {
         *     already blocked)
         */
        protected function maybeAlterFormDefaults( &$fields ){
-               global $wgRequest, $wgUser;
-
                # This will be overwritten by request data
                $fields['Target']['default'] = (string)$this->target;
 
@@ -238,18 +237,22 @@ class SpecialBlock extends SpecialPage {
                        $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;
 
-                       if( $wgRequest->wasPosted() ){
+                       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';
@@ -278,7 +281,7 @@ class SpecialBlock extends SpecialPage {
                }
 
                # Or if the user is trying to block themselves
-               if( (string)$this->target === $wgUser->getName() ){
+               if( (string)$this->target === $this->getUser()->getName() ){
                        $fields['Confirm']['type'] = 'check';
                        unset( $fields['Confirm']['default'] );
                        $this->preErrors[] = 'ipb-blockingself';
@@ -304,15 +307,19 @@ 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 );
                        }
                }
@@ -324,9 +331,8 @@ class SpecialBlock extends SpecialPage {
         * @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 ){
+               if( !$this->getRequest()->wasPosted() && $this->preErrors ){
                        $s = HTMLForm::formatErrors( $this->preErrors );
                        if( $s ){
                                $form->addHeaderText( Html::rawElement(
@@ -344,14 +350,10 @@ 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() )
                        );
@@ -365,17 +367,19 @@ class SpecialBlock extends SpecialPage {
                        $message = wfMsgExt( 'ipb-unblock', array( 'parseinline' ) );
                        $list = SpecialPage::getTitleFor( 'Unblock' );
                }
-               $links[] = $skin->linkKnown( $list, $message, array() );
+               $links[] = Linker::linkKnown( $list, $message, array() );
 
                # Link to the block list
-               $links[] = $skin->linkKnown(
+               $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(),
@@ -386,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 ){
@@ -408,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',
@@ -421,6 +425,7 @@ class SpecialBlock extends SpecialPage {
                                                'showIfEmpty' => false
                                        )
                                );
+
                                $form->addPostText( $out );
                        }
                }
@@ -432,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:
@@ -465,17 +471,22 @@ class SpecialBlock extends SpecialPage {
                                case 4:
                                        break 2;
                        }
+
                        list( $target, $type ) = Block::parseTarget( $target );
+
                        if( $type !== null ){
                                return array( $target, $type );
                        }
                }
+
                return array( null, null );
        }
 
        /**
         * HTMLForm field validation-callback for Target field.
         * @since 1.18
+        * @param $value String
+        * @param $alldata Array
         * @return Message
         */
        public static function validateTargetField( $value, $alldata = null ) {
@@ -519,10 +530,8 @@ class SpecialBlock extends SpecialPage {
                        if( IP::isIPv6( $ip ) && $range < $wgBlockCIDRLimit['IPv6'] ) {
                                return wfMessage( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv6'] );
                        }
-
                } elseif( $type == Block::TYPE_IP ){
                        # All is well
-
                } else {
                        return wfMessage( 'badipaddress' );
                }
@@ -555,18 +564,15 @@ class SpecialBlock extends SpecialPage {
                        # 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'] ) )
+                               ( $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' );
@@ -587,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,
@@ -599,16 +606,13 @@ 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( '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' );
                        }
@@ -636,7 +640,7 @@ class SpecialBlock extends SpecialPage {
                if( !$status ) {
                        # Show form unless the user is already aware of this...
                        if( !$data['Confirm'] || ( array_key_exists( 'PreviousTarget', $data )
-                               && $data['PreviousTarget'] !== htmlspecialchars( $block->getTarget() ) ) )
+                               && $data['PreviousTarget'] !== $block->getTarget() ) )
                        {
                                return array( array( 'ipb_already_blocked', $block->getTarget() ) );
                        # Otherwise, try to update the block...
@@ -715,6 +719,8 @@ class SpecialBlock extends SpecialPage {
         * Get an array of suggested block durations from MediaWiki:Ipboptions
         * @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
         */
        public static function getSuggestedDurations( $lang = null ){
@@ -731,9 +737,11 @@ class SpecialBlock extends SpecialPage {
                        if( strpos( $option, ':' ) === false ){
                                $option = "$option:$option";
                        }
+
                        list( $show, $value ) = explode( ':', $option );
                        $a[htmlspecialchars( $show )] = htmlspecialchars( $value );
                }
+
                return $a;
        }
 
@@ -748,15 +756,19 @@ class SpecialBlock extends SpecialPage {
                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;
        }
 
@@ -767,6 +779,7 @@ class SpecialBlock extends SpecialPage {
         */
        public static function canBlockEmail( $user ) {
                global $wgEnableUserEmail, $wgSysopEmailBans;
+
                return ( $wgEnableUserEmail && $wgSysopEmailBans && $user->isAllowed( 'blockemail' ) );
        }
 
@@ -775,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';
                                }
@@ -813,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( $wgBlockAllowsUTEdit && $data['DisableUTEdit'] ){
+                       // For grepping: message block-log-flags-nousertalk
                        $flags[] = 'nousertalk';
                }
 
                if( $data['HideUser'] ){
+                       // For grepping: message block-log-flags-hiddenname
                        $flags[] = 'hiddenname';
                }