Merge "Update for API error i18n"

This commit is contained in:
jenkins-bot 2016-12-06 19:58:50 +00:00 committed by Gerrit Code Review
commit 99c537c6b2
9 changed files with 38 additions and 64 deletions

View file

@ -28,7 +28,7 @@ class ApiFlowThank extends ApiThank {
try {
$postId = UUID::create( $params['postid'] );
} catch ( FlowException $e ) {
$this->dieUsage( 'Post ID is invalid', 'invalidpostid' );
$this->dieWithError( 'thanks-error-invalidpostid', 'invalidpostid' );
}
$data = $this->getFlowData( $postId );
@ -83,11 +83,11 @@ class ApiFlowThank extends ApiThank {
try {
$data = $rootPostLoader->getWithRoot( $postId );
} catch ( FlowException $e ) {
$this->dieUsage( 'Post ID is invalid', 'invalidpostid' );
$this->dieWithError( 'thanks-error-invalidpostid', 'invalidpostid' );
}
if ( $data['post'] === null ) {
$this->dieUsage( 'Post ID is invalid', 'invalidpostid' );
$this->dieWithError( 'thanks-error-invalidpostid', 'invalidpostid' );
}
return $data;
}
@ -99,7 +99,7 @@ class ApiFlowThank extends ApiThank {
private function getRecipientFromPost( PostRevision $post ) {
$recipient = User::newFromId( $post->getCreatorId() );
if ( !$recipient->loadFromId() ) {
$this->dieUsage( 'Recipient is invalid', 'invalidrecipient' );
$this->dieWithError( 'thanks-error-invalidrecipient', 'invalidrecipient' );
}
return $recipient;
}

View file

@ -38,9 +38,9 @@ class ApiRevThank extends ApiThank {
// Revision ID 1 means an invalid argument was passed in.
if ( !$revision || $revision->getId() === 1 ) {
$this->dieUsage( 'Revision ID is not valid', 'invalidrevision' );
$this->dieWithError( 'thanks-error-invalidrevision', 'invalidrevision' );
} elseif ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
$this->dieUsage( 'Revision has been deleted', 'revdeleted' );
$this->dieWithError( 'thanks-error-revdeleted', 'revdeleted' );
}
return $revision;
}
@ -48,7 +48,7 @@ class ApiRevThank extends ApiThank {
private function getTitleFromRevision( Revision $revision ) {
$title = Title::newFromID( $revision->getPage() );
if ( !$title instanceof Title ) {
$this->dieUsage( 'Page title could not be retrieved', 'notitle' );
$this->dieWithError( 'thanks-error-notitle', 'notitle' );
}
return $title;
}
@ -67,7 +67,7 @@ class ApiRevThank extends ApiThank {
private function getUserFromRevision( Revision $revision ) {
$recipient = $revision->getUser();
if ( !$recipient ) {
$this->dieUsage( 'No valid recipient found', 'invalidrecipient' );
$this->dieWithError( 'thanks-error-invalidrecipient', 'invalidrecipient' );
}
return User::newFromId( $recipient );
}

View file

@ -8,17 +8,17 @@
abstract class ApiThank extends ApiBase {
protected function dieIfEchoNotInstalled() {
if ( !class_exists( 'EchoNotifier' ) ) {
$this->dieUsage( 'Echo is not installed on this wiki', 'echonotinstalled' );
$this->dieWithError( 'thanks-error-echonotinstalled', 'echonotinstalled' );
}
}
protected function dieOnBadUser( User $user ) {
if ( $user->isAnon() ) {
$this->dieUsage( 'Anonymous users cannot send thanks', 'notloggedin' );
$this->dieWithError( 'thanks-error-notloggedin', 'notloggedin' );
} elseif ( $user->pingLimiter( 'thanks-notification' ) ) {
$this->dieUsageMsg( [ 'actionthrottledtext' ] );
$this->dieWithError( [ 'thanks-error-ratelimited', $user->getName() ], 'ratelimited' );
} elseif ( $user->isBlocked() ) {
$this->dieUsageMsg( [ 'blockedtext' ] );
$this->dieBlocked( $user->getBlock() );
}
}
@ -26,9 +26,9 @@ abstract class ApiThank extends ApiBase {
global $wgThanksSendToBots;
if ( $user->getId() === $recipient->getId() ) {
$this->dieUsage( 'You cannot thank yourself', 'invalidrecipient' );
$this->dieWithError( 'thanks-error-invalidrecipient-self', 'invalidrecipient' );
} elseif ( !$wgThanksSendToBots && in_array( 'bot', $recipient->getGroups() ) ) {
$this->dieUsage( 'Bots cannot be thanked', 'invalidrecipient' );
$this->dieWithError( 'thanks-error-invalidrecipient-bot', 'invalidrecipient' );
}
}

View file

@ -146,42 +146,14 @@ class SpecialThanks extends FormSpecialPage {
try {
$api->execute();
} catch ( UsageException $e ) {
return $this->handleErrorCodes( $e->getCodeString() );
} catch ( ApiUsageException $e ) {
return Status::wrap( $e->getStatusValue() );
}
$this->result = $api->getResult()->getResultData( [ 'result' ] );
return Status::newGood();
}
/**
* Handle error codes returned by the API.
*
* @param $code
* @return Status
*/
protected function handleErrorCodes( $code ) {
$status = new Status;
switch ( $code ) {
case 'invalidrevision':
case 'invalidpostid':
case 'ratelimited':
// Message keys used here:
// * thanks-error-invalidrevision
// * thanks-error-invalidpostid
// * thanks-error-ratelimited
$status->fatal( 'thanks-error-' . $code );
break;
case 'notloggedin':
case 'blockedtext':
$status->fatal( $code );
break;
default:
$status->fatal( 'thanks-error-undefined' );
}
return $status;
}
/**
* Display a message to the user.
*/

View file

@ -11,6 +11,13 @@
"thanks-button-thanked": "{{GENDER:$1|{{GENDER:$2|Thanked}}}}",
"thanks-error-undefined": "Thank action failed (error code: $1). Please try again.",
"thanks-error-invalidrevision": "Revision ID is not valid.",
"thanks-error-revdeleted": "Revision has been deleted",
"thanks-error-notitle": "Page title could not be retrieved",
"thanks-error-invalidrecipient": "No valid recipient found",
"thanks-error-invalidrecipient-bot": "Bots cannot be thanked",
"thanks-error-invalidrecipient-self": "You cannot thank yourself",
"thanks-error-echonotinstalled": "Echo is not installed on this wiki",
"thanks-error-notloggedin": "Anonymous users cannot send thanks",
"thanks-error-ratelimited": "{{GENDER:$1|You}}'ve exceeded your rate limit. Please wait some time and try again.",
"thanks-thank-tooltip": "{{GENDER:$1|Send}} a thank you notification to this {{GENDER:$2|user}}",
"thanks-thank-tooltip-no": "{{GENDER:$1|Cancel}} the thank you notification",

View file

@ -21,6 +21,13 @@
"thanks-button-thanked": "This message immediately replaces the message {{msg-mw|Thanks-button-thank}} after it's pressed. It means that the thanking operation has been completed.\n\nSame as {{msg-mw|Thanks-thanked}}, but the context is in a button.\n\nParameters:\n* $1 - The user that is thanking, for gender\n* $2 - The user that is being thanked, for gender\n{{Identical|Thanked}}",
"thanks-error-undefined": "Error message that is displayed when the thank action fails. $1 is the error code returned by the API (an English string)",
"thanks-error-invalidrevision": "Error message that is displayed when the revision ID is not valid",
"thanks-error-echonotinstalled": "Error message that is displayed when Echo is not installed",
"thanks-error-invalidrecipient": "Error message that is displayed when no recipient is found",
"thanks-error-invalidrecipient-bot": "Error message that is displayed when the recipient is a bot",
"thanks-error-invalidrecipient-self": "Error message that is displayed when the recipient is the user doing the thanking",
"thanks-error-notitle": "Error message that is displayed when the title of the page cannot be determined",
"thanks-error-notloggedin": "Error message that is displayed when the user is not logged in",
"thanks-error-revdeleted": "Error message that is displayed when the revision has been deleted (RevDel)",
"thanks-error-ratelimited": "Error message that is displayed when user exceeds rate limit. Parameters:\n* $1 - gender",
"thanks-thank-tooltip": "Tooltip that appears when a user hovers over the \"thank\" link. Parameters:\n* $1 - The user sending the thanks. Can be used for GENDER support.\n* $2 - The user receiving the thanks. Can be used for GENDER support",
"thanks-thank-tooltip-no": "Tooltip that appears when a user hovers over the \"No\" confirmation link (which cancels the thank action). Parameters:\n* $1 - The user sending the thanks. Can be used for GENDER support.",

View file

@ -117,11 +117,7 @@ class ApiFlowThankTest extends ApiTestCase {
}
public function testRequestWithoutToken() {
if ( class_exists( 'ApiUsageException' ) ) {
$this->setExpectedException( 'ApiUsageException', 'The "token" parameter must be set.' );
} else {
$this->setExpectedException( 'UsageException', 'The token parameter must be set' );
}
$this->setExpectedException( 'ApiUsageException', 'The "token" parameter must be set.' );
$this->doApiRequest( [
'action' => 'flowthank',
'postid' => UUID::create( '42' )->getAlphadecimal(),
@ -129,11 +125,7 @@ class ApiFlowThankTest extends ApiTestCase {
}
public function testInvalidRequest() {
if ( class_exists( 'ApiUsageException' ) ) {
$this->setExpectedException( 'ApiUsageException', 'The "postid" parameter must be set.' );
} else {
$this->setExpectedException( 'UsageException', 'The postid parameter must be set' );
}
$this->setExpectedException( 'ApiUsageException', 'The "postid" parameter must be set.' );
$this->doApiRequestWithToken( [ 'action' => 'flowthank' ] );
}
@ -146,7 +138,7 @@ class ApiFlowThankTest extends ApiTestCase {
}
public function testRequestWithInvalidId() {
$this->setExpectedException( 'UsageException', 'Post ID is invalid' );
$this->setExpectedException( 'ApiUsageException', 'Post ID is not valid' );
list( $result,, ) = $this->doApiRequestWithToken( [
'action' => 'flowthank',
'postid' => UUID::create( '42' )->getAlphadecimal(),
@ -154,7 +146,7 @@ class ApiFlowThankTest extends ApiTestCase {
}
public function testRequestWithOwnId() {
$this->setExpectedException( 'UsageException', 'You cannot thank yourself' );
$this->setExpectedException( 'ApiUsageException', 'You cannot thank yourself' );
list( $result,, ) = $this->doApiRequestWithToken( [
'action' => 'flowthank',
'postid' => $this->postByMe->getPostId()->getAlphadecimal(),

View file

@ -36,11 +36,7 @@ class ApiRevThankIntegrationTest extends ApiTestCase {
}
public function testRequestWithoutToken() {
if ( class_exists( 'ApiUsageException' ) ) {
$this->setExpectedException( 'ApiUsageException', 'The "token" parameter must be set.' );
} else {
$this->setExpectedException( 'UsageException', 'The token parameter must be set' );
}
$this->setExpectedException( 'ApiUsageException', 'The "token" parameter must be set.' );
$this->doApiRequest( [
'action' => 'thank',
'source' => 'someSource',
@ -75,7 +71,7 @@ class ApiRevThankIntegrationTest extends ApiTestCase {
}
public function testInvalidRequest() {
$this->setExpectedException( 'UsageException' );
$this->setExpectedException( 'ApiUsageException' );
$this->doApiRequestWithToken( [ 'action' => 'thank' ] );
}

View file

@ -26,7 +26,7 @@ class ApiRevThankUnitTest extends MediaWikiTestCase {
$method->setAccessible( true );
if ( $expectedError ) {
$this->setExpectedException( 'UsageException', $expectedError );
$this->setExpectedException( 'ApiUsageException', $expectedError );
}
$method->invoke( $module, $user );
@ -67,7 +67,7 @@ class ApiRevThankUnitTest extends MediaWikiTestCase {
$mockUser->expects( $this->once() )
->method( 'isBlocked' )
->will( $this->returnValue( true ) );
$mockUser->expects( $this->any() )
$mockUser->expects( $this->once() )
->method( 'getBlock' )
->will( $this->returnValue( new Block( [
'address' => 'Test user',