From 90500d15e769ec4f98a74d294bdc79b057c737df Mon Sep 17 00:00:00 2001 From: Andrew H Date: Mon, 21 Dec 2015 22:59:16 +0000 Subject: [PATCH] Add dieBlocked to APIBase and make use of it Moves a frequently used snippet of code into APIBase to throw a usage exception with block info. Change-Id: I9bd0b2804e9e246f6d53031b04af48f111c0814c --- includes/api/ApiBase.php | 27 +++++++++++++++++++++++++++ includes/api/ApiRevisionDelete.php | 20 +------------------- includes/api/ApiTag.php | 19 +------------------ includes/api/ApiUndelete.php | 13 ++++--------- 4 files changed, 33 insertions(+), 46 deletions(-) diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 8a98197754..cb74ae1bfd 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -1461,6 +1461,33 @@ abstract class ApiBase extends ContextSource { ); } + /** + * Throw a UsageException, which will (if uncaught) call the main module's + * error handler and die with an error message including block info. + * + * @since 1.27 + * @param Block $block The block used to generate the UsageException + * @throws UsageException always + */ + public function dieBlocked( Block $block ) { + // Die using the appropriate message depending on block type + if ( $block->getType() == Block::TYPE_AUTO ) { + $this->dieUsage( + 'Your IP address has been blocked automatically, because it was used by a blocked user', + 'autoblocked', + 0, + array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ) + ); + } else { + $this->dieUsage( + 'You have been blocked from editing', + 'blocked', + 0, + array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ) + ); + } + } + /** * Get error (as code, string) from a Status object. * diff --git a/includes/api/ApiRevisionDelete.php b/includes/api/ApiRevisionDelete.php index b70b536286..4db3ca1d89 100644 --- a/includes/api/ApiRevisionDelete.php +++ b/includes/api/ApiRevisionDelete.php @@ -36,30 +36,12 @@ class ApiRevisionDelete extends ApiBase { $params = $this->extractRequestParams(); $user = $this->getUser(); - if ( !$user->isAllowed( RevisionDeleter::getRestriction( $params['type'] ) ) ) { $this->dieUsageMsg( 'badaccess-group0' ); } if ( $user->isBlocked() ) { - $block = $user->getBlock(); - - // Die using the appropriate message depending on block type - if ( $block->getType() == TYPE_AUTO ) { - $this->dieUsage( - 'Your IP address has been blocked automatically, because it was used by a blocked user', - 'autoblocked', - 0, - array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ) - ); - } else { - $this->dieUsage( - 'You have been blocked from editing', - 'blocked', - 0, - array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ) - ); - } + $this->dieBlocked( $user->getBlock() ); } if ( !$params['ids'] ) { diff --git a/includes/api/ApiTag.php b/includes/api/ApiTag.php index 4157de0f37..4bf799ec90 100644 --- a/includes/api/ApiTag.php +++ b/includes/api/ApiTag.php @@ -40,24 +40,7 @@ class ApiTag extends ApiBase { } if ( $user->isBlocked() ) { - $block = $user->getBlock(); - - // Die using the appropriate message depending on block type - if ( $block->getType() == TYPE_AUTO ) { - $this->dieUsage( - 'Your IP address has been blocked automatically, because it was used by a blocked user', - 'autoblocked', - 0, - array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ) - ); - } else { - $this->dieUsage( - 'You have been blocked from editing', - 'blocked', - 0, - array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ) - ); - } + $this->dieBlocked( $user->getBlock() ); } // validate and process each revid, rcid and logid diff --git a/includes/api/ApiUndelete.php b/includes/api/ApiUndelete.php index cd50ee65c7..469fe7f7e3 100644 --- a/includes/api/ApiUndelete.php +++ b/includes/api/ApiUndelete.php @@ -33,18 +33,13 @@ class ApiUndelete extends ApiBase { $this->useTransactionalTimeLimit(); $params = $this->extractRequestParams(); - - if ( !$this->getUser()->isAllowed( 'undelete' ) ) { + $user = $this->getUser(); + if ( !$user->isAllowed( 'undelete' ) ) { $this->dieUsageMsg( 'permdenied-undelete' ); } - if ( $this->getUser()->isBlocked() ) { - $this->dieUsage( - 'You have been blocked from editing', - 'blocked', - 0, - array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $this->getUser()->getBlock() ) ) - ); + if ( $user->isBlocked() ) { + $this->dieBlocked( $user->getBlock() ); } $titleObj = Title::newFromText( $params['title'] ); -- 2.20.1