From 78da7f163744dec7b48240a019c2ccd4ec266c4e Mon Sep 17 00:00:00 2001 From: Alex Monk Date: Sun, 10 May 2015 15:05:03 +0100 Subject: [PATCH] API: Give block details along with errors Bug: T95072 Change-Id: I295d74d5f33e6dd1072a1e85710a02597a46e14d --- RELEASE-NOTES-1.26 | 2 ++ includes/api/ApiBlock.php | 8 ++++++- includes/api/ApiCreateAccount.php | 7 +++++- includes/api/ApiEditPage.php | 32 +++++++++++++++++++++++-- includes/api/ApiLogin.php | 8 +++++++ includes/api/ApiQueryUserInfo.php | 39 ++++++++++++++++++++----------- includes/api/ApiUnblock.php | 8 ++++++- includes/api/ApiUndelete.php | 7 +++++- 8 files changed, 92 insertions(+), 19 deletions(-) diff --git a/RELEASE-NOTES-1.26 b/RELEASE-NOTES-1.26 index db560c6745..419525b587 100644 --- a/RELEASE-NOTES-1.26 +++ b/RELEASE-NOTES-1.26 @@ -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 === diff --git a/includes/api/ApiBlock.php b/includes/api/ApiBlock.php index 26b5f0e56c..6adfc1a02a 100644 --- a/includes/api/ApiBlock.php +++ b/includes/api/ApiBlock.php @@ -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() ) ) + ); } } diff --git a/includes/api/ApiCreateAccount.php b/includes/api/ApiCreateAccount.php index 57f96c6178..b3a543afd5 100644 --- a/includes/api/ApiCreateAccount.php +++ b/includes/api/ApiCreateAccount.php @@ -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(); diff --git a/includes/api/ApiEditPage.php b/includes/api/ApiEditPage.php index 6189f68e07..e70b8f0289 100644 --- a/includes/api/ApiEditPage.php +++ b/includes/api/ApiEditPage.php @@ -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: diff --git a/includes/api/ApiLogin.php b/includes/api/ApiLogin.php index d8b390c430..c4e7022875 100644 --- a/includes/api/ApiLogin.php +++ b/includes/api/ApiLogin.php @@ -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: diff --git a/includes/api/ApiQueryUserInfo.php b/includes/api/ApiQueryUserInfo.php index 4302ef39f5..e003e31f85 100644 --- a/includes/api/ApiQueryUserInfo.php +++ b/includes/api/ApiQueryUserInfo.php @@ -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'] ) ) { diff --git a/includes/api/ApiUnblock.php b/includes/api/ApiUnblock.php index 1af83ba3f0..f6c24b7627 100644 --- a/includes/api/ApiUnblock.php +++ b/includes/api/ApiUnblock.php @@ -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() ) ) + ); } } diff --git a/includes/api/ApiUndelete.php b/includes/api/ApiUndelete.php index c23e9ff6a4..28702b1926 100644 --- a/includes/api/ApiUndelete.php +++ b/includes/api/ApiUndelete.php @@ -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'] ); -- 2.20.1