Albert221 has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/394859 )
Change subject: [WIP]Un-i18n of exception messages ...................................................................... [WIP]Un-i18n of exception messages Bug: T59181 Change-Id: I763fdf290f3ee5a7f9281ff14ad6c2db7163f770 --- M api/MWOAuthSessionProvider.php M backend/MWOAuthDataStore.php M backend/MWOAuthServer.php M frontend/specialpages/SpecialMWOAuth.php M i18n/en.json 5 files changed, 110 insertions(+), 56 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/OAuth refs/changes/59/394859/1 diff --git a/api/MWOAuthSessionProvider.php b/api/MWOAuthSessionProvider.php index f5cb4bf..2a785c2 100644 --- a/api/MWOAuthSessionProvider.php +++ b/api/MWOAuthSessionProvider.php @@ -41,32 +41,46 @@ * @returns SessionInfo */ private function makeException( $key /*, ... */ ) { - global $wgHooks; - // First, schedule the throwing of the exception for later when the API // is ready to catch it $params = func_get_args(); array_shift( $params ); $msg = wfMessage( $key, $params ); - if ( class_exists( \ApiUsageException::class ) ) { - $exception = \ApiUsageException::newWithMessage( null, $msg ); - } else { - $msg = $msg->inLanguage( 'en' )->useDatabase( false )->plain(); - $exception = new \UsageException( $msg, $key ); - } - $wgHooks['ApiBeforeMain'][] = function () use ( $exception ) { - throw $exception; - }; - // Then return an appropriate SessionInfo - $id = $this->hashToSessionId( 'bogus' ); - return new SessionInfo( SessionInfo::MAX_PRIORITY, [ - 'provider' => $this, - 'id' => $id, - 'userInfo' => UserInfo::newAnonymous(), - 'persisted' => false, - ] ); + if ( !class_exists( \ApiUsageException::class ) ) { + $msg = $msg->inLanguage( 'en' )->useDatabase( false )->plain(); + + return $this->makeRawException($msg, $key); + } + + return $this->makeRawException($msg); } + + private function makeRawException( $msg, $key = '' ) { + global $wgHooks; + + if ( class_exists( \ApiUsageException::class ) ) { + $exception = \ApiUsageException::newWithMessage( null, $msg ); + } else { + if ( !is_null($key) && $key !== '' ) { + $exception = new \UsageException ( $msg, $key ); + } else { + $exception = new \UsageException('', $msg); + } + } + $wgHooks['ApiBeforeMain'][] = function () use ( $exception ) { + throw $exception; + }; + + // Then return an appropriate SessionInfo + $id = $this->hashToSessionId( 'bogus' ); + return new SessionInfo( SessionInfo::MAX_PRIORITY, [ + 'provider' => $this, + 'id' => $id, + 'userInfo' => UserInfo::newAnonymous(), + 'persisted' => false, + ] ); + } public function provideSessionInfo( WebRequest $request ) { // For some reason MWOAuth is restricted to be API-only. @@ -98,7 +112,7 @@ // There exists a local user $localUser = MWOAuthUtils::getLocalUserFromCentralId( $access->get( 'userId' ) ); if ( !$localUser || !$localUser->isLoggedIn() ) { - return $this->makeException( 'mwoauth-invalid-authorization-invalid-user' ); + return $this->makeRawException( 'The authorization headers in your request are for a user that does not exist here' ); } if ( $localUser->isLocked() || ( $this->config->get( 'BlockDisablesLogin' ) && $localUser->isBlocked() ) diff --git a/backend/MWOAuthDataStore.php b/backend/MWOAuthDataStore.php index 02fbe00..5185b84 100644 --- a/backend/MWOAuthDataStore.php +++ b/backend/MWOAuthDataStore.php @@ -49,7 +49,7 @@ * @param OAuthConsumer|MWOAuthConsumer $consumer * @param string $token_type * @param string $token String the token - * @throws MWOAuthException + * @throws OAuthException * @return MWOAuthToken */ public function lookup_token( $consumer, $token_type, $token ) { @@ -66,7 +66,12 @@ throw new MWOAuthException( 'mwoauthdatastore-request-token-already-used' ); } if ( $token === null || !( $returnToken instanceof MWOAuthToken ) ) { - throw new MWOAuthException( 'mwoauthdatastore-request-token-not-found' ); + throw new OAuthException( + 'Sorry, something went wrong connecting this application.\n' . + 'Go back and try to connect your account again, or contact the application author.\n\n' . + '<span class=\"plainlinks mw-mwoautherror-details\">OAuth token not found, ' . + '[https://www.mediawiki.org/wiki/Help:OAuth/Errors#E004 E004]</span>' + ); } } elseif ( $token_type === 'access' ) { $cmra = MWOAuthConsumerAcceptance::newFromToken( $this->centralSlave, $token ); diff --git a/backend/MWOAuthServer.php b/backend/MWOAuthServer.php index 4905e29..e30ef6b 100644 --- a/backend/MWOAuthServer.php +++ b/backend/MWOAuthServer.php @@ -270,27 +270,39 @@ * @param \User $mwUser user authorizing the request (local user) * @param bool $update update the grants/wiki to those requested by consumer * @return String the callback URL to redirect the user - * @throws MWOAuthException + * @throws OAuthException */ public function authorize( $consumerKey, $requestTokenKey, \User $mwUser, $update ) { global $wgBlockDisablesLogin; // Check that user and consumer are in good standing if ( $mwUser->isLocked() || $wgBlockDisablesLogin && $mwUser->isBlocked() ) { - throw new MWOAuthException( 'mwoauthserver-insufficient-rights' ); + throw new OAuthException( + 'Your account is not allowed to use Connected Apps,' . + 'contact your site administrator to find out why.\n\n' . + '<span class=\"plainlinks mw-mwoautherror-details\">Insufficient OAuth user rights,' . + '[https://www.mediawiki.org/wiki/Help:OAuth/Errors#E007 E007]</span>' + ); } $consumer = $this->data_store->lookup_consumer( $consumerKey ); if ( !$consumer || $consumer->get( 'deleted' ) ) { - throw new MWOAuthException( 'mwoauthserver-bad-consumer-key' ); + throw new OAuthException( + 'Sorry, something went wrong connecting this application.\n\n' . + '<span class=\"plainlinks mw-mwoautherror-details\">Unknown OAuth key, ' . + '[https://www.mediawiki.org/wiki/Help:OAuth/Errors#E006 E006]</span>' + ); } elseif ( !$consumer->isUsableBy( $mwUser ) ) { $owner = MWOAuthUtils::getCentralUserNameFromId( $consumer->get( 'userId' ), $mwUser ); - throw new MWOAuthException( - 'mwoauthserver-bad-consumer', - [ $consumer->get( 'name' ), MWOAuthUtils::getCentralUserTalk( $owner ) ] - ); + throw new OAuthException( sprintf( + '"%s" is not approved as a Connected App. [%s Contact] the application author for help.\n\n' . + '<span class=\"plainlinks mw-mwoautherror-details\">Connected OAuth app not approved, ' . + '[https://www.mediawiki.org/wiki/Help:OAuth/Errors#E005 E005]</span>', + $consumer->get( 'name' ), + MWOauthUtils::getCentralUserTalk( $owner ) + ) ); } elseif ( $consumer->get( 'ownerOnly' ) ) { throw new MWOAuthException( 'mwoauthserver-consumer-owner-only', [ $consumer->get( 'name' ), @@ -316,8 +328,13 @@ // CentralAuth may abort here if there is no global account for this user $centralUserId = MWOAuthUtils::getCentralIdFromLocalUser( $mwUser ); if ( !$centralUserId ) { - $userMsg = MWOAuthUtils::getSiteMessage( 'mwoauthserver-invalid-user' ); - throw new MWOAuthException( $userMsg, [ $consumer->get( 'name' ) ] ); + throw new OAuthException( sprintf( + 'To use Connected Apps on this site, you must have an account across all projects.' . + 'When you have an account on all projects, you can try to connect "%s" again.\n\n' . + '<span class=\"plainlinks mw-mwoautherror-details\">Unified login needed, ' . + '[https://www.mediawiki.org/wiki/Help:OAuth/Errors#E008 E008]</span>', + $consumer->get( 'name' ) + ) ); } // Authorization Token @@ -381,7 +398,7 @@ * @param \User $mwUser (local wiki user) User who may or may not have authorizations * @param MWOAuthConsumer $consumer * @param string $wikiId - * @throws MWOAuthException + * @throws OAuthException * @return MWOAuthConsumerAcceptance */ public function getCurrentAuthorization( \User $mwUser, $consumer, $wikiId ) { @@ -389,8 +406,13 @@ $centralUserId = MWOAuthUtils::getCentralIdFromLocalUser( $mwUser ); if ( !$centralUserId ) { - $userMsg = MWOAuthUtils::getSiteMessage( 'mwoauthserver-invalid-user' ); - throw new MWOAuthException( $userMsg, [ $consumer->get( 'name' ) ] ); + throw new OAuthException( sprintf( + 'To use Connected Apps on this site, you must have an account across all projects.' . + 'When you have an account on all projects, you can try to connect "%s" again.\n\n' . + '<span class=\"plainlinks mw-mwoautherror-details\">Unified login needed, ' . + '[https://www.mediawiki.org/wiki/Help:OAuth/Errors#E008 E008]</span>', + $consumer->get( 'name' ) + ) ); } $checkWiki = $consumer->get( 'wiki' ) !== '*' ? $consumer->get( 'wiki' ) : $wikiId; diff --git a/frontend/specialpages/SpecialMWOAuth.php b/frontend/specialpages/SpecialMWOAuth.php index e36708f..329f675 100644 --- a/frontend/specialpages/SpecialMWOAuth.php +++ b/frontend/specialpages/SpecialMWOAuth.php @@ -162,7 +162,7 @@ $access = MWOAuthConsumerAcceptance::newFromToken( $dbr, $token->key ); $localUser = MWOAuthUtils::getLocalUserFromCentralId( $access->get( 'userId' ) ); if ( !$localUser || !$localUser->isLoggedIn() ) { - throw new MWOAuthException( 'mwoauth-invalid-authorization-invalid-user' ); + throw new OAuthException( 'The authorization headers in your request are for a user that does not exist here' ); } elseif ( $localUser->isLocked() || $wgBlockDisablesLogin && $localUser->isBlocked() ) { @@ -202,9 +202,12 @@ $owner = MWOAuthUtils::getCentralUserNameFromId( $cmr->get( 'userId' ), $this->getUser() ); $this->showError( - wfMessage( 'mwoauth-bad-request-invalid-action-contact', - MWOAuthUtils::getCentralUserTalk( $owner ) - ), + sprintf( + 'Sorry, something went wrong. You\'ll need to [%s contact] the application ' . + 'author for help with this.\n\n<span class=\"plainlinks mw-mwoautherror-details\">' . + 'Unknown URL, [https://www.mediawiki.org/wiki/Help:OAuth/Errors#E003 E003]</span>', + MWOAuthUtils::getCentralUserTalk( $owner ) + ), $format ); } @@ -318,7 +321,11 @@ $this->getContext() ); if ( !$cmr ) { - throw new MWOAuthException( 'mwoauthserver-bad-consumer-key' ); + throw new OAuthException( + 'Sorry, something went wrong connecting this application.\n\n' . + '<span class=\"plainlinks mw-mwoautherror-details\">Unknown OAuth key, ' . + '[https://www.mediawiki.org/wiki/Help:OAuth/Errors#E006 E006]</span>' + ); } elseif ( !$cmr->getDAO()->isUsableBy( $user ) ) { throw new MWOAuthException( 'mwoauthserver-bad-consumer', @@ -449,18 +456,30 @@ } } - /** - * @param \Message $message to return to the user - * @param string $format the format of the response: html, raw, or json - */ - private function showError( $message, $format ) { + /** + * @param \Message|string $message to return to the user + * @param string $format the format of the response: html, raw, or json + * @param bool $isRawMessage + */ + private function showError( $message, $format, $isRawMessage = false ) { if ( $format == 'raw' ) { - $this->showResponse( 'Error: ' .$message->escaped(), 'raw' ); - } elseif ( $format == 'json' ) { - $error = \FormatJson::encode( [ - 'error' => $message->getKey(), - 'message' => $message->text(), - ] ); + if (!$isRawMessage) { + $message = $message->escaped(); + } + + $this->showResponse('Error: ' . $message, 'raw'); + } elseif ( $format == 'json' ) { + if ($isRawMessage) { + $error = \FormatJson::encode( [ + 'message' => $message + ] ); + } else { + $error = \FormatJson::encode( [ + 'error' => $message->getKey(), + 'message' => $message->text(), + ] ); + } + $this->showResponse( $error, 'json' ); } elseif ( $format == 'html' ) { $this->getOutput()->showErrorPage( 'mwoauth-error', $message ); diff --git a/i18n/en.json b/i18n/en.json index d8ead7e..17e36c6 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -197,7 +197,6 @@ "mwoauth-bad-request-invalid-action": "Sorry, something went wrong, you'll need to contact the application author for help with this.\n\n<span class=\"plainlinks mw-mwoautherror-details\">Unknown URL, [https://www.mediawiki.org/wiki/Help:OAuth/Errors#E002 E002]</span>", "mwoauth-bad-request-invalid-action-contact": "Sorry, something went wrong. You'll need to [$1 contact] the application author for help with this.\n\n<span class=\"plainlinks mw-mwoautherror-details\">Unknown URL, [https://www.mediawiki.org/wiki/Help:OAuth/Errors#E003 E003]</span>", "mwoauthdatastore-access-token-not-found": "No approved grant was found for that authorization token.", - "mwoauthdatastore-request-token-not-found": "Sorry, something went wrong connecting this application.\nGo back and try to connect your account again, or contact the application author.\n\n<span class=\"plainlinks mw-mwoautherror-details\">OAuth token not found, [https://www.mediawiki.org/wiki/Help:OAuth/Errors#E004 E004]</span>", "mwoauthdatastore-callback-not-found": "OAuth callback URL not found in cache. This is probably an error in how the application makes requests to the server.", "mwoauthdatastore-request-token-already-used": "This request has already been completed and cannot be resubmitted.\nGo back to the application and try to connect your account again, or contact the application author.\n\n<span class=\"plainlinks mw-mwoautherror-details\">OAuth token already used, [https://www.mediawiki.org/wiki/Help:OAuth/Errors#E009 E009]</span>", "mwoauthdatastore-bad-token": "No token was found matching your request.", @@ -205,17 +204,12 @@ "mwoauthdatastore-bad-verifier": "The verification code provided was not valid.", "mwoauthdatastore-invalid-token-type": "The requested token type is invalid.", "mwoauthgrants-general-error": "There was an error in your OAuth request.", - "mwoauthserver-bad-consumer": "\"$1\" is not approved as a Connected App. [$2 Contact] the application author for help.\n\n<span class=\"plainlinks mw-mwoautherror-details\">Connected OAuth app not approved, [https://www.mediawiki.org/wiki/Help:OAuth/Errors#E005 E005]</span>", - "mwoauthserver-bad-consumer-key": "Sorry, something went wrong connecting this application.\n\n<span class=\"plainlinks mw-mwoautherror-details\">Unknown OAuth key, [https://www.mediawiki.org/wiki/Help:OAuth/Errors#E006 E006]</span>", - "mwoauthserver-insufficient-rights": "Your account is not allowed to use Connected Apps, contact your site administrator to find out why.\n\n<span class=\"plainlinks mw-mwoautherror-details\">Insufficient OAuth user rights, [https://www.mediawiki.org/wiki/Help:OAuth/Errors#E007 E007]</span>", "mwoauthserver-invalid-request-token": "Invalid token in your request.", - "mwoauthserver-invalid-user": "To use Connected Apps on this site, you must have an account across all projects. When you have an account on all projects, you can try to connect \"$1\" again.\n\n<span class=\"plainlinks mw-mwoautherror-details\">Unified login needed, [https://www.mediawiki.org/wiki/Help:OAuth/Errors#E008 E008]</span>", "mwoauthserver-consumer-no-secret": "Sorry, something went wrong connecting this application.\n\n<span class=\"plainlinks mw-mwoautherror-details\">Consumer has no secret key, [https://www.mediawiki.org/wiki/Help:OAuth/Errors#E009 E009]</span>", "mwoauthserver-consumer-owner-only": "\"$1\" is an owner-only Connected App. To fetch the access token, see [[$2]].\n\n<span class=\"plainlinks mw-mwoautherror-details\">Consumer is owner-only, [https://www.mediawiki.org/wiki/Help:OAuth/Errors#E010 E010]</span>", "mwoauth-invalid-authorization-title": "OAuth authorization error", "mwoauth-invalid-authorization": "The authorization headers in your request are not valid: $1", "mwoauth-invalid-authorization-wrong-wiki": "The authorization headers in your request are not valid for $1", - "mwoauth-invalid-authorization-invalid-user": "The authorization headers in your request are for a user that does not exist here", "mwoauth-invalid-authorization-wrong-user": "The authorization headers in your request are for a different user", "mwoauth-invalid-authorization-not-approved": "The app that you are trying to connect seems to be set up incorrectly. Contact the author of \"$1\" for help.", "mwoauth-invalid-authorization-blocked-user": "The authorization headers in your request are for a user who is blocked", -- To view, visit https://gerrit.wikimedia.org/r/394859 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I763fdf290f3ee5a7f9281ff14ad6c2db7163f770 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/OAuth Gerrit-Branch: master Gerrit-Owner: Albert221 <w.albert...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits