Merge "Handle missing namespace prefix in XML dumps more gracefully"
[lhc/web/wiklou.git] / includes / api / ApiBlock.php
index a4ea385..8577ad2 100644 (file)
@@ -39,14 +39,14 @@ class ApiBlock extends ApiBase {
         * of success. If it fails, the result will specify the nature of the error.
         */
        public function execute() {
-               global $wgContLang;
-
                $this->checkUserRightsAny( 'block' );
 
                $user = $this->getUser();
                $params = $this->extractRequestParams();
 
-               # bug 15810: blocked admins should have limited access here
+               $this->requireOnlyOneParameter( $params, 'user', 'userid' );
+
+               # T17810: blocked admins should have limited access here
                if ( $user->isBlocked() ) {
                        $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
                        if ( $status !== true ) {
@@ -58,13 +58,31 @@ class ApiBlock extends ApiBase {
                        }
                }
 
-               $target = User::newFromName( $params['user'] );
-               // Bug 38633 - if the target is a user (not an IP address), but it
-               // doesn't exist or is unusable, error.
-               if ( $target instanceof User &&
-                       ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) )
-               ) {
-                       $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
+               if ( $params['userid'] !== null ) {
+                       $username = User::whoIs( $params['userid'] );
+
+                       if ( $username === false ) {
+                               $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
+                       } else {
+                               $params['user'] = $username;
+                       }
+               } else {
+                       $target = User::newFromName( $params['user'] );
+
+                       // T40633 - if the target is a user (not an IP address), but it
+                       // doesn't exist or is unusable, error.
+                       if ( $target instanceof User &&
+                               ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) )
+                       ) {
+                               $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
+                       }
+               }
+
+               if ( $params['tags'] ) {
+                       $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
+                       if ( !$ableToTag->isOK() ) {
+                               $this->dieStatus( $ableToTag );
+                       }
                }
 
                if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
@@ -92,6 +110,7 @@ class ApiBlock extends ApiBase {
                        'Reblock' => $params['reblock'],
                        'Watch' => $params['watchuser'],
                        'Confirm' => true,
+                       'Tags' => $params['tags'],
                ];
 
                $retval = SpecialBlock::processForm( $data, $this->getContext() );
@@ -105,7 +124,7 @@ class ApiBlock extends ApiBase {
 
                $block = Block::newFromTarget( $target, null, true );
                if ( $block instanceof Block ) {
-                       $res['expiry'] = $wgContLang->formatExpiry( $block->mExpiry, TS_ISO_8601, 'infinite' );
+                       $res['expiry'] = ApiResult::formatExpiry( $block->mExpiry, 'infinite' );
                        $res['id'] = $block->getId();
                } else {
                        # should be unreachable
@@ -137,7 +156,9 @@ class ApiBlock extends ApiBase {
                return [
                        'user' => [
                                ApiBase::PARAM_TYPE => 'user',
-                               ApiBase::PARAM_REQUIRED => true
+                       ],
+                       'userid' => [
+                               ApiBase::PARAM_TYPE => 'integer',
                        ],
                        'expiry' => 'never',
                        'reason' => '',
@@ -149,6 +170,10 @@ class ApiBlock extends ApiBase {
                        'allowusertalk' => false,
                        'reblock' => false,
                        'watchuser' => false,
+                       'tags' => [
+                               ApiBase::PARAM_TYPE => 'tags',
+                               ApiBase::PARAM_ISMULTI => true,
+                       ],
                ];
        }