API: Give block details along with errors
authorAlex Monk <krenair@gmail.com>
Sun, 10 May 2015 14:05:03 +0000 (15:05 +0100)
committerAlex Monk <krenair@gmail.com>
Wed, 17 Jun 2015 19:40:14 +0000 (20:40 +0100)
Bug: T95072
Change-Id: I295d74d5f33e6dd1072a1e85710a02597a46e14d

RELEASE-NOTES-1.26
includes/api/ApiBlock.php
includes/api/ApiCreateAccount.php
includes/api/ApiEditPage.php
includes/api/ApiLogin.php
includes/api/ApiQueryUserInfo.php
includes/api/ApiUnblock.php
includes/api/ApiUndelete.php

index db560c6..419525b 100644 (file)
@@ -45,6 +45,8 @@ production.
   with formatversion=2.
 * Various other output from meta=siteinfo will now always be arrays instead of
   sometimes being numerically-indexed objects with formatversion=2.
+* When errors about users being blocked are returned, they now include
+  information about the relevant block.
 
 === Action API internal changes in 1.26 ===
 
index 26b5f0e..6adfc1a 100644 (file)
@@ -52,7 +52,13 @@ class ApiBlock extends ApiBase {
                if ( $user->isBlocked() ) {
                        $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
                        if ( $status !== true ) {
-                               $this->dieUsageMsg( array( $status ) );
+                               $msg = $this->parseMsg( $status );
+                               $this->dieUsage(
+                                       $msg['info'],
+                                       $msg['code'],
+                                       0,
+                                       array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) )
+                               );
                        }
                }
 
index 57f96c6..b3a543a 100644 (file)
@@ -48,7 +48,12 @@ class ApiCreateAccount extends ApiBase {
                        );
                }
                if ( $this->getUser()->isBlockedFromCreateAccount() ) {
-                       $this->dieUsage( 'You cannot create a new account because you are blocked', 'blocked' );
+                       $this->dieUsage(
+                               'You cannot create a new account because you are blocked',
+                               'blocked',
+                               0,
+                               array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $this->getUser()->getBlock() ) )
+                       );
                }
 
                $params = $this->extractRequestParams();
index 6189f68..e70b8f0 100644 (file)
@@ -130,7 +130,30 @@ class ApiEditPage extends ApiBase {
                        $errors = array_merge( $errors, $titleObj->getUserPermissionsErrors( 'create', $user ) );
                }
                if ( count( $errors ) ) {
-                       $this->dieUsageMsg( $errors[0] );
+                       if ( is_array( $errors[0] ) ) {
+                               switch ( $errors[0][0] ) {
+                                       case 'blockedtext':
+                                               $this->dieUsage(
+                                                       'You have been blocked from editing',
+                                                       'blocked',
+                                                       0,
+                                                       array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) )
+                                               );
+                                               break;
+                                       case 'autoblockedtext':
+                                               $this->dieUsage(
+                                                       'Your IP address has been blocked automatically, because it was used by a blocked user',
+                                                       'autoblocked',
+                                                       0,
+                                                       array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) )
+                                               );
+                                               break;
+                                       default:
+                                               $this->dieUsageMsg( $errors[0] );
+                               }
+                       } else {
+                               $this->dieUsageMsg( $errors[0] );
+                       }
                }
 
                $toMD5 = $params['text'];
@@ -450,7 +473,12 @@ class ApiEditPage extends ApiBase {
                                $this->dieUsageMsg( array( 'spamdetected', $result['spam'] ) );
 
                        case EditPage::AS_BLOCKED_PAGE_FOR_USER:
-                               $this->dieUsageMsg( 'blockedtext' );
+                               $this->dieUsage(
+                                       'You have been blocked from editing',
+                                       'blocked',
+                                       0,
+                                       array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) )
+                               );
 
                        case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED:
                        case EditPage::AS_CONTENT_TOO_BIG:
index d8b390c..c4e7022 100644 (file)
@@ -144,6 +144,10 @@ class ApiLogin extends ApiBase {
                        case LoginForm::CREATE_BLOCKED:
                                $result['result'] = 'CreateBlocked';
                                $result['details'] = 'Your IP address is blocked from account creation';
+                               $result = array_merge(
+                                       $result,
+                                       ApiQueryUserInfo::getBlockInfo( $context->getUser()->getBlock() )
+                               );
                                break;
 
                        case LoginForm::THROTTLED:
@@ -154,6 +158,10 @@ class ApiLogin extends ApiBase {
 
                        case LoginForm::USER_BLOCKED:
                                $result['result'] = 'Blocked';
+                               $result = array_merge(
+                                       $result,
+                                       ApiQueryUserInfo::getBlockInfo( User::newFromName( $params['name'] )->getBlock() )
+                               );
                                break;
 
                        case LoginForm::ABORTED:
index 4302ef3..e003e31 100644 (file)
@@ -51,9 +51,32 @@ class ApiQueryUserInfo extends ApiQueryBase {
                $result->addValue( 'query', $this->getModuleName(), $r );
        }
 
-       protected function getCurrentUserInfo() {
+       /**
+        * Get basic info about a given block
+        * @param Block $block
+        * @return array Array containing several keys:
+        *  - blockid - ID of the block
+        *  - blockedby - username of the blocker
+        *  - blockedbyid - user ID of the blocker
+        *  - blockreason - reason provided for the block
+        *  - blockedtimestamp - timestamp for when the block was placed/modified
+        *  - blockexpiry - expiry time of the block
+        */
+       public static function getBlockInfo( Block $block ) {
                global $wgContLang;
+               $vals = array();
+               $vals['blockid'] = $block->getId();
+               $vals['blockedby'] = $block->getByName();
+               $vals['blockedbyid'] = $block->getBy();
+               $vals['blockreason'] = $block->mReason;
+               $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $block->mTimestamp );
+               $vals['blockexpiry'] = $wgContLang->formatExpiry(
+                       $block->getExpiry(), TS_ISO_8601, 'infinite'
+               );
+               return $vals;
+       }
 
+       protected function getCurrentUserInfo() {
                $user = $this->getUser();
                $result = $this->getResult();
                $vals = array();
@@ -64,18 +87,8 @@ class ApiQueryUserInfo extends ApiQueryBase {
                        $vals['anon'] = true;
                }
 
-               if ( isset( $this->prop['blockinfo'] ) ) {
-                       if ( $user->isBlocked() ) {
-                               $block = $user->getBlock();
-                               $vals['blockid'] = $block->getId();
-                               $vals['blockedby'] = $block->getByName();
-                               $vals['blockedbyid'] = $block->getBy();
-                               $vals['blockreason'] = $user->blockedFor();
-                               $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $block->mTimestamp );
-                               $vals['blockexpiry'] = $wgContLang->formatExpiry(
-                                       $block->getExpiry(), TS_ISO_8601, 'infinite'
-                               );
-                       }
+               if ( isset( $this->prop['blockinfo'] ) && $user->isBlocked() ) {
+                       $vals = array_merge( $vals, self::getBlockInfo( $user->getBlock() ) );
                }
 
                if ( isset( $this->prop['hasmsg'] ) ) {
index 1af83ba..f6c24b7 100644 (file)
@@ -53,7 +53,13 @@ class ApiUnblock extends ApiBase {
                if ( $user->isBlocked() ) {
                        $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
                        if ( $status !== true ) {
-                               $this->dieUsageMsg( $status );
+                               $msg = $this->parseMsg( $status );
+                               $this->dieUsage(
+                                       $msg['info'],
+                                       $msg['code'],
+                                       0,
+                                       array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) )
+                               );
                        }
                }
 
index c23e9ff..28702b1 100644 (file)
@@ -37,7 +37,12 @@ class ApiUndelete extends ApiBase {
                }
 
                if ( $this->getUser()->isBlocked() ) {
-                       $this->dieUsageMsg( 'blockedtext' );
+                       $this->dieUsage(
+                               'You have been blocked from editing',
+                               'blocked',
+                               0,
+                               array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $this->getUser()->getBlock() ) )
+                       );
                }
 
                $titleObj = Title::newFromText( $params['title'] );