Merge "MessagesGom_deva: Correct syntax in namespace alias"
[lhc/web/wiklou.git] / includes / api / ApiUpload.php
index 15c1e39..95659e5 100644 (file)
@@ -350,9 +350,10 @@ class ApiUpload extends ApiBase {
         * @param array|string|MessageSpecifier $error Error suitable for passing to dieUsageMsg()
         * @param string $parameter Parameter that needs revising
         * @param array $data Optional extra data to pass to the user
+        * @param string $code Error code to use if the error is unknown
         * @throws UsageException
         */
-       private function dieRecoverableError( $error, $parameter, $data = [] ) {
+       private function dieRecoverableError( $error, $parameter, $data = [], $code = 'unknownerror' ) {
                try {
                        $data['filekey'] = $this->performStash();
                        $data['sessionkey'] = $data['filekey'];
@@ -365,6 +366,9 @@ class ApiUpload extends ApiBase {
                if ( isset( $parsed['data'] ) ) {
                        $data = array_merge( $data, $parsed['data'] );
                }
+               if ( $parsed['code'] === 'unknownerror' ) {
+                       $parsed['code'] = $code;
+               }
 
                $this->dieUsage( $parsed['info'], $parsed['code'], 0, $data );
        }
@@ -563,12 +567,28 @@ class ApiUpload extends ApiBase {
                                $this->dieUsage( $msg, 'filetype-banned', 0, $extradata );
                                break;
                        case UploadBase::VERIFICATION_ERROR:
-                               $params = $verification['details'];
-                               $key = array_shift( $params );
-                               $msg = $this->msg( $key, $params )->inLanguage( 'en' )->useDatabase( false )->text();
-                               ApiResult::setIndexedTagName( $verification['details'], 'detail' );
-                               $this->dieUsage( "This file did not pass file verification: $msg", 'verification-error',
-                                       0, [ 'details' => $verification['details'] ] );
+                               $parsed = $this->parseMsg( $verification['details'] );
+                               $info = "This file did not pass file verification: {$parsed['info']}";
+                               if ( $verification['details'][0] instanceof IApiMessage ) {
+                                       $code = $parsed['code'];
+                               } else {
+                                       // For backwards-compatibility, all of the errors from UploadBase::verifyFile() are
+                                       // reported as 'verification-error', and the real error code is reported in 'details'.
+                                       $code = 'verification-error';
+                               }
+                               if ( $verification['details'][0] instanceof IApiMessage ) {
+                                       $msg = $verification['details'][0];
+                                       $details = array_merge( [ $msg->getKey() ], $msg->getParams() );
+                               } else {
+                                       $details = $verification['details'];
+                               }
+                               ApiResult::setIndexedTagName( $details, 'detail' );
+                               $data = [ 'details' => $details ];
+                               if ( isset( $parsed['data'] ) ) {
+                                       $data = array_merge( $data, $parsed['data'] );
+                               }
+
+                               $this->dieUsage( $info, $code, 0, $data );
                                break;
                        case UploadBase::HOOK_ABORTED:
                                if ( is_array( $verification['error'] ) ) {
@@ -754,9 +774,14 @@ class ApiUpload extends ApiBase {
                                $this->mParams['text'], $watch, $this->getUser(), $this->mParams['tags'] );
 
                        if ( !$status->isGood() ) {
-                               $error = $status->getErrorsArray();
-                               ApiResult::setIndexedTagName( $error, 'error' );
-                               $this->dieUsage( 'An internal error occurred', 'internal-error', 0, $error );
+                               // Is there really no better way to do this?
+                               $errors = $status->getErrorsByType( 'error' );
+                               $msg = array_merge( [ $errors[0]['message'] ], $errors[0]['params'] );
+                               $data = $status->getErrorsArray();
+                               ApiResult::setIndexedTagName( $data, 'error' );
+                               // For backwards-compatibility, we use the 'internal-error' fallback key and merge $data
+                               // into the root of the response (rather than something sane like [ 'details' => $data ]).
+                               $this->dieRecoverableError( $msg, null, $data, 'internal-error' );
                        }
                        $result['result'] = 'Success';
                }