From d33b721c4eb0337a73baf0715f9b38a5d1120242 Mon Sep 17 00:00:00 2001 From: Matthias Mullie Date: Mon, 16 Oct 2017 17:29:37 +0200 Subject: [PATCH] i18n UploadStash exception messages In some cases, the error messages have changed slightly, mostly because of capitalisation & punctuation. In a few other cases (mainly UploadStashNotLoggedInException), the content has also slightly changed (removed mention of the __METHOD__ it occurred in) Bug: T178291 Change-Id: I184067f2d7fe0a0a2df1114d2525fd9ab95b6c86 --- includes/specials/SpecialUploadStash.php | 52 +++++++++++----- includes/upload/UploadStash.php | 75 ++++++++++++++++-------- languages/i18n/en.json | 19 ++++++ languages/i18n/qqq.json | 19 ++++++ 4 files changed, 125 insertions(+), 40 deletions(-) diff --git a/includes/specials/SpecialUploadStash.php b/includes/specials/SpecialUploadStash.php index b0bb595e85..ce8b5a09a6 100644 --- a/includes/specials/SpecialUploadStash.php +++ b/includes/specials/SpecialUploadStash.php @@ -106,8 +106,7 @@ class SpecialUploadStash extends UnlistedSpecialPage { $message = $e->getMessage(); } catch ( SpecialUploadStashTooLargeException $e ) { $code = 500; - $message = 'Cannot serve a file larger than ' . self::MAX_SERVE_BYTES . - ' bytes. ' . $e->getMessage(); + $message = $e->getMessage(); } catch ( Exception $e ) { $code = 500; $message = $e->getMessage(); @@ -129,7 +128,9 @@ class SpecialUploadStash extends UnlistedSpecialPage { $type = strtok( $key, '/' ); if ( $type !== 'file' && $type !== 'thumb' ) { - throw new UploadStashBadPathException( "Unknown type '$type'" ); + throw new UploadStashBadPathException( + wfMessage( 'uploadstash-bad-path-unknown-type', $type ) + ); } $fileName = strtok( '/' ); $thumbPart = strtok( '/' ); @@ -137,7 +138,9 @@ class SpecialUploadStash extends UnlistedSpecialPage { if ( $type === 'thumb' ) { $srcNamePos = strrpos( $thumbPart, $fileName ); if ( $srcNamePos === false || $srcNamePos < 1 ) { - throw new UploadStashBadPathException( 'Unrecognized thumb name' ); + throw new UploadStashBadPathException( + wfMessage( 'uploadstash-bad-path-unrecognized-thumb-name' ) + ); } $paramString = substr( $thumbPart, 0, $srcNamePos - 1 ); @@ -147,8 +150,9 @@ class SpecialUploadStash extends UnlistedSpecialPage { return [ 'file' => $file, 'type' => $type, 'params' => $params ]; } else { - throw new UploadStashBadPathException( 'No handler found for ' . - "mime {$file->getMimeType()} of file {$file->getPath()}" ); + throw new UploadStashBadPathException( + wfMessage( 'uploadstash-bad-path-no-handler', $file->getMimeType(), $file->getPath() ) + ); } } @@ -194,12 +198,16 @@ class SpecialUploadStash extends UnlistedSpecialPage { $thumbnailImage = $file->transform( $params, $flags ); if ( !$thumbnailImage ) { - throw new MWException( 'Could not obtain thumbnail' ); + throw new UploadStashFileNotFoundException( + wfMessage( 'uploadstash-file-not-found-no-thumb' ) + ); } // we should have just generated it locally if ( !$thumbnailImage->getStoragePath() ) { - throw new UploadStashFileNotFoundException( "no local path for scaled item" ); + throw new UploadStashFileNotFoundException( + wfMessage( 'uploadstash-file-not-found-no-local-path' ) + ); } // now we should construct a File, so we can get MIME and other such info in a standard way @@ -207,7 +215,9 @@ class SpecialUploadStash extends UnlistedSpecialPage { $thumbFile = new UnregisteredLocalFile( false, $this->stash->repo, $thumbnailImage->getStoragePath(), false ); if ( !$thumbFile ) { - throw new UploadStashFileNotFoundException( "couldn't create local file object for thumbnail" ); + throw new UploadStashFileNotFoundException( + wfMessage( 'uploadstash-file-not-found-no-object' ) + ); } return $this->outputLocalFile( $thumbFile ); @@ -261,13 +271,19 @@ class SpecialUploadStash extends UnlistedSpecialPage { $status = $req->execute(); if ( !$status->isOK() ) { $errors = $status->getErrorsArray(); - $errorStr = "Fetching thumbnail failed: " . print_r( $errors, 1 ); - $errorStr .= "\nurl = $scalerThumbUrl\n"; - throw new MWException( $errorStr ); + throw new UploadStashFileNotFoundException( + wfMessage( + 'uploadstash-file-not-found-no-remote-thumb', + print_r( $errors, 1 ), + $scalerThumbUrl + ) + ); } $contentType = $req->getResponseHeader( "content-type" ); if ( !$contentType ) { - throw new MWException( "Missing content-type header" ); + throw new UploadStashFileNotFoundException( + wfMessage( 'uploadstash-file-not-found-missing-content-type' ) + ); } return $this->outputContents( $req->getContent(), $contentType ); @@ -284,7 +300,9 @@ class SpecialUploadStash extends UnlistedSpecialPage { */ private function outputLocalFile( File $file ) { if ( $file->getSize() > self::MAX_SERVE_BYTES ) { - throw new SpecialUploadStashTooLargeException(); + throw new SpecialUploadStashTooLargeException( + wfMessage( 'uploadstash-file-too-large', self::MAX_SERVE_BYTES ) + ); } return $file->getRepo()->streamFile( $file->getPath(), @@ -304,7 +322,9 @@ class SpecialUploadStash extends UnlistedSpecialPage { private function outputContents( $content, $contentType ) { $size = strlen( $content ); if ( $size > self::MAX_SERVE_BYTES ) { - throw new SpecialUploadStashTooLargeException(); + throw new SpecialUploadStashTooLargeException( + wfMessage( 'uploadstash-file-too-large', self::MAX_SERVE_BYTES ) + ); } // Cancel output buffering and gzipping if set wfResetOutputBuffers(); @@ -427,5 +447,5 @@ class SpecialUploadStash extends UnlistedSpecialPage { } } -class SpecialUploadStashTooLargeException extends MWException { +class SpecialUploadStashTooLargeException extends UploadStashException { } diff --git a/includes/upload/UploadStash.php b/includes/upload/UploadStash.php index c3d2e4dfcc..c2ab899789 100644 --- a/includes/upload/UploadStash.php +++ b/includes/upload/UploadStash.php @@ -118,12 +118,15 @@ class UploadStash { */ public function getFile( $key, $noAuth = false ) { if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) { - throw new UploadStashBadPathException( "key '$key' is not in a proper format" ); + throw new UploadStashBadPathException( + wfMessage( 'uploadstash-bad-path-bad-format', $key ) + ); } if ( !$noAuth && !$this->isLoggedIn ) { - throw new UploadStashNotLoggedInException( __METHOD__ . - ' No user is logged in, files must belong to users' ); + throw new UploadStashNotLoggedInException( + wfMessage( 'uploadstash-not-logged-in' ) + ); } if ( !isset( $this->fileMetadata[$key] ) ) { @@ -134,7 +137,9 @@ class UploadStash { } if ( !isset( $this->fileMetadata[$key] ) ) { - throw new UploadStashFileNotFoundException( "key '$key' not found in stash" ); + throw new UploadStashFileNotFoundException( + wfMessage( 'uploadstash-file-not-found', $key ) + ); } // create $this->files[$key] @@ -153,13 +158,16 @@ class UploadStash { if ( !$this->files[$key]->exists() ) { wfDebug( __METHOD__ . " tried to get file at $key, but it doesn't exist\n" ); // @todo Is this not an UploadStashFileNotFoundException case? - throw new UploadStashBadPathException( "path doesn't exist" ); + throw new UploadStashBadPathException( + wfMessage( 'uploadstash-bad-path' ) + ); } if ( !$noAuth ) { if ( $this->fileMetadata[$key]['us_user'] != $this->userId ) { - throw new UploadStashWrongOwnerException( "This file ($key) doesn't " - . "belong to the current user." ); + throw new UploadStashWrongOwnerException( + wfMessage( 'uploadstash-wrong-owner', $key ) + ); } } @@ -205,7 +213,9 @@ class UploadStash { public function stashFile( $path, $sourceType = null ) { if ( !is_file( $path ) ) { wfDebug( __METHOD__ . " tried to stash file at '$path', but it doesn't exist\n" ); - throw new UploadStashBadPathException( "path doesn't exist" ); + throw new UploadStashBadPathException( + wfMessage( 'uploadstash-bad-path' ) + ); } $mwProps = new MWFileProps( MimeMagic::singleton() ); @@ -236,7 +246,9 @@ class UploadStash { $this->fileProps[$key] = $fileProps; if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) { - throw new UploadStashBadPathException( "key '$key' is not in a proper format" ); + throw new UploadStashBadPathException( + wfMessage( 'uploadstash-bad-path-bad-format', $key ) + ); } wfDebug( __METHOD__ . " key for '$path': $key\n" ); @@ -271,8 +283,9 @@ class UploadStash { // fetch the current user ID if ( !$this->isLoggedIn ) { - throw new UploadStashNotLoggedInException( __METHOD__ - . ' No user is logged in, files must belong to users' ); + throw new UploadStashNotLoggedInException( + wfMessage( 'uploadstash-not-logged-in' ) + ); } // insert the file metadata into the db. @@ -331,8 +344,9 @@ class UploadStash { */ public function clear() { if ( !$this->isLoggedIn ) { - throw new UploadStashNotLoggedInException( __METHOD__ - . ' No user is logged in, files must belong to users' ); + throw new UploadStashNotLoggedInException( + wfMessage( 'uploadstash-not-logged-in' ) + ); } wfDebug( __METHOD__ . ' clearing all rows for user ' . $this->userId . "\n" ); @@ -360,8 +374,9 @@ class UploadStash { */ public function removeFile( $key ) { if ( !$this->isLoggedIn ) { - throw new UploadStashNotLoggedInException( __METHOD__ - . ' No user is logged in, files must belong to users' ); + throw new UploadStashNotLoggedInException( + wfMessage( 'uploadstash-not-logged-in' ) + ); } $dbw = $this->repo->getMasterDB(); @@ -376,12 +391,15 @@ class UploadStash { ); if ( !$row ) { - throw new UploadStashNoSuchKeyException( "No such key ($key), cannot remove" ); + throw new UploadStashNoSuchKeyException( + wfMessage( 'uploadstash-no-such-key', $key ) + ); } if ( $row->us_user != $this->userId ) { - throw new UploadStashWrongOwnerException( "Can't delete: " - . "the file ($key) doesn't belong to this user." ); + throw new UploadStashWrongOwnerException( + wfMessage( 'uploadstash-wrong-owner', $key ) + ); } return $this->removeFileNoAuth( $key ); @@ -426,8 +444,9 @@ class UploadStash { */ public function listFiles() { if ( !$this->isLoggedIn ) { - throw new UploadStashNotLoggedInException( __METHOD__ - . ' No user is logged in, files must belong to users' ); + throw new UploadStashNotLoggedInException( + wfMessage( 'uploadstash-not-logged-in' ) + ); } $dbr = $this->repo->getReplicaDB(); @@ -480,7 +499,9 @@ class UploadStash { } if ( is_null( $extension ) ) { - throw new UploadStashFileException( "extension is null" ); + throw new UploadStashFileException( + wfMessage( 'uploadstash-no-extension' ) + ); } $extension = File::normalizeExtension( $extension ); @@ -545,7 +566,9 @@ class UploadStash { protected function initFile( $key ) { $file = new UploadStashFile( $this->repo, $this->fileMetadata[$key]['us_path'], $key ); if ( $file->getSize() === 0 ) { - throw new UploadStashZeroLengthFileException( "File is zero length" ); + throw new UploadStashZeroLengthFileException( + wfMessage( 'uploadstash-zero-length' ) + ); } $this->files[$key] = $file; @@ -585,14 +608,18 @@ class UploadStashFile extends UnregisteredLocalFile { ) { wfDebug( "UploadStash: tried to construct an UploadStashFile " . "from a file that should already exist at '$path', but path is not valid\n" ); - throw new UploadStashBadPathException( 'path is not valid' ); + throw new UploadStashBadPathException( + wfMessage( 'uploadstash-bad-path-invalid' ) + ); } // check if path exists! and is a plain file. if ( !$repo->fileExists( $path ) ) { wfDebug( "UploadStash: tried to construct an UploadStashFile from " . "a file that should already exist at '$path', but path is not found\n" ); - throw new UploadStashFileNotFoundException( 'cannot find path, or not a plain file' ); + throw new UploadStashFileNotFoundException( + wfMessage( 'uploadstash-file-not-found-not-exists' ) + ); } } diff --git a/languages/i18n/en.json b/languages/i18n/en.json index 27bc813eb9..b93a5d3cb3 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -1722,6 +1722,25 @@ "uploadstash-refresh": "Refresh the list of files", "uploadstash-thumbnail": "view thumbnail", "uploadstash-exception": "Could not store upload in the stash ($1): \"$2\".", + "uploadstash-bad-path": "Path doesn't exist.", + "uploadstash-bad-path-invalid": "Path is not valid.", + "uploadstash-bad-path-unknown-type": "Unknown type \"$1\".", + "uploadstash-bad-path-unrecognized-thumb-name": "Unrecognized thumb name.", + "uploadstash-bad-path-no-handler": "No handler found for mime $1 of file $2.", + "uploadstash-bad-path-bad-format": "Key \"$1\" is not in a proper format.", + "uploadstash-file-not-found": "Key \"$1\" not found in stash.", + "uploadstash-file-not-found-no-thumb": "Could not obtain thumbnail.", + "uploadstash-file-not-found-no-local-path": "No local path for scaled item.", + "uploadstash-file-not-found-no-object": "Couldn't create local file object for thumbnail.", + "uploadstash-file-not-found-no-remote-thumb": "Fetching thumbnail failed: $1\nurl = $2\n.", + "uploadstash-file-not-found-missing-content-type": "Missing content-type header.", + "uploadstash-file-not-found-not-exists": "Cannot find path, or not a plain file.", + "uploadstash-file-too-large": "Cannot serve a file larger than $1 bytes.", + "uploadstash-not-logged-in": "No user is logged in, files must belong to users.", + "uploadstash-wrong-owner": "This file ($1) doesn't belong to the current user.", + "uploadstash-no-such-key": "No such key ($1), cannot remove.", + "uploadstash-no-extension": "Extension is null.", + "uploadstash-zero-length": "File is zero length.", "invalid-chunk-offset": "Invalid chunk offset", "img-auth-accessdenied": "Access denied", "img-auth-nopathinfo": "Missing PATH_INFO.\nYour server is not set up to pass this information.\nIt may be CGI-based and cannot support img_auth.\nSee https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Image_Authorization.", diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json index b4a8d247f7..d71509197e 100644 --- a/languages/i18n/qqq.json +++ b/languages/i18n/qqq.json @@ -1916,6 +1916,25 @@ "uploadstash-refresh": "Used as link text in [[Special:UploadStash]].", "uploadstash-thumbnail": "Used as link text in [[Special:UploadStash]].", "uploadstash-exception": "Error message shown when an action related to the upload stash fails unexpectedly.\n\nParameters:\n* $1 - exception name, e.g. 'UploadStashFileNotFoundException'\n* $2 - exceptions details (always in English), e.g. 'cannot find path, or not a plain file'", + "uploadstash-bad-path": "Error message when the upload stash path doesn't exist.", + "uploadstash-bad-path-invalid": "Error message when the upload stash path is invalid.", + "uploadstash-bad-path-unknown-type": "Error message when the upload stash key is of an invalid type.\n\nParameters:\n* $1 - the type", + "uploadstash-bad-path-unrecognized-thumb-name": "Error message when the upload stash key is of an unrecognized thumbnail", + "uploadstash-bad-path-no-handler": "Error message when no handler can be found for the given upload stash key.\n\nParameters:\n* $1 - the mine type\n* $2 - the file path", + "uploadstash-bad-path-bad-format": "Error message when the upload stash key if formatted incorrectly.\n\nParameters:\n* $1 - the stash key", + "uploadstash-file-not-found": "Error message when a stashed file can't be found.\n\nParameters:\n* $1 - the stash key", + "uploadstash-file-not-found-no-thumb": "Error message when no thumbnail could be generated.", + "uploadstash-file-not-found-no-local-path": "Error message when no locally generated thumbnail can be found.", + "uploadstash-file-not-found-no-object": "Error message when no local thumbnail object can be created.", + "uploadstash-file-not-found-no-remote-thumb": "Error message when a thumbnail could not be retrieved from a remote server.\n\nParameters:\n*1 - error details\n* $2 - remote url", + "uploadstash-file-not-found-missing-content-type": "Error message when a thumbnail from a remote server is missing the content-type header.", + "uploadstash-file-not-found-not-exists": "Error message when a stashed file doesn't exist.", + "uploadstash-file-too-large": "Error message when a too large thumbnail is requested.\n\nParameters:\n* $1 - The maximum amount of bytes", + "uploadstash-not-logged-in": "Error message when a logged out user tries to access a stashed upload.", + "uploadstash-wrong-owner": "Error message when a wrong user tries to access a stashed upload.\n\nParameters:\n* $1 - the stash key", + "uploadstash-no-such-key": "Error message when a stash key couldn't be found.\n\nParameters:\n* $1 - the stash key", + "uploadstash-no-extension": "Error message when a stashed file has no extension.", + "uploadstash-zero-length": "Error message when a file has no length (file size is zero).", "invalid-chunk-offset": "Error that can happen if chunks get uploaded out of order.\nAs a result of this error, clients can continue from an offset provided or restart the upload.\nUsed on [[Special:UploadWizard]].", "img-auth-accessdenied": "[[mw:Manual:Image Authorization|Manual:Image Authorization]]: Access Denied\n{{Identical|Access denied}}", "img-auth-nopathinfo": "[[mw:Manual:Image Authorization|Manual:Image Authorization]]: Missing PATH_INFO - see english description\n{{Doc-important|This is plain text. Do not use any wiki syntax.}}", -- 2.20.1