(bug 30625) Return warnings, if they exist, despite ignorewarnings
authorMark Holmquist <mtraceur@member.fsf.org>
Sat, 26 May 2012 00:05:04 +0000 (17:05 -0700)
committerMark Holmquist <mtraceur@member.fsf.org>
Fri, 6 Jul 2012 17:26:31 +0000 (10:26 -0700)
The API currently doesn't return any indication of the warnings on
a successful upload where the API call had ignorewarnings=true.
This patch allows the client to handle the warnings gracefully.

Patchset 2: Document it properly.

Patchset 3 and 4: Rebase.

Patchset 5: Document it even more properly.

Patchset 6: I didn't understand doxygen properly.

Change-Id: I09c8f3dc13832df55a0ffa9e8b9380aaa4038cf1

includes/api/ApiUpload.php

index aaa4c85..d84e5d0 100644 (file)
@@ -117,25 +117,26 @@ class ApiUpload extends ApiBase {
         */
        private function getContextResult(){
                $warnings = $this->getApiWarnings();
-               if ( $warnings ) {
+               if ( $warnings && !$this->mParams['ignorewarnings'] ) {
                        // Get warnings formated in result array format
                        return $this->getWarningsResult( $warnings );
                } elseif ( $this->mParams['chunk'] ) {
                        // Add chunk, and get result
-                       return $this->getChunkResult();
+                       return $this->getChunkResult( $warnings );
                } elseif ( $this->mParams['stash'] ) {
                        // Stash the file and get stash result
-                       return $this->getStashResult();
+                       return $this->getStashResult( $warnings );
                }
                // This is the most common case -- a normal upload with no warnings
                // performUpload will return a formatted properly for the API with status
-               return $this->performUpload();
+               return $this->performUpload( $warnings );
        }
        /**
         * Get Stash Result, throws an expetion if the file could not be stashed.
+        * @param $warnings array Array of Api upload warnings
         * @return array
         */
-       private function getStashResult(){
+       private function getStashResult( $warnings ){
                $result = array ();
                // Some uploads can request they be stashed, so as not to publish them immediately.
                // In this case, a failure to stash ought to be fatal
@@ -143,6 +144,9 @@ class ApiUpload extends ApiBase {
                        $result['result'] = 'Success';
                        $result['filekey'] = $this->performStash();
                        $result['sessionkey'] = $result['filekey']; // backwards compatibility
+                       if ( $warnings && count( $warnings ) > 0 ) {
+                               $result['warnings'] = $warnings;
+                       }
                } catch ( MWException $e ) {
                        $this->dieUsage( $e->getMessage(), 'stashfailed' );
                }
@@ -150,7 +154,7 @@ class ApiUpload extends ApiBase {
        }
        /**
         * Get Warnings Result
-        * @param $warnings Array of Api upload warnings
+        * @param $warnings array Array of Api upload warnings
         * @return array
         */
        private function getWarningsResult( $warnings ){
@@ -169,12 +173,16 @@ class ApiUpload extends ApiBase {
        }
        /**
         * Get the result of a chunk upload.
+        * @param $warnings array Array of Api upload warnings
         * @return array
         */
-       private function getChunkResult(){
+       private function getChunkResult( $warnings ){
                $result = array();
 
                $result['result'] = 'Continue';
+               if ( $warnings && count( $warnings ) > 0 ) {
+                       $result['warnings'] = $warnings;
+               }
                $request = $this->getMain()->getRequest();
                $chunkPath = $request->getFileTempname( 'chunk' );
                $chunkSize = $request->getUpload( 'chunk' )->getSize();
@@ -444,7 +452,7 @@ class ApiUpload extends ApiBase {
 
 
        /**
-        * Check warnings if ignorewarnings is not set.
+        * Check warnings.
         * Returns a suitable array for inclusion into API results if there were warnings
         * Returns the empty array if there were no warnings
         *
@@ -453,9 +461,8 @@ class ApiUpload extends ApiBase {
        protected function getApiWarnings() {
                $warnings = array();
 
-               if ( !$this->mParams['ignorewarnings'] ) {
-                       $warnings = $this->mUpload->checkWarnings();
-               }
+               $warnings = $this->mUpload->checkWarnings();
+
                return $this->transformWarnings( $warnings );
        }
 
@@ -488,9 +495,10 @@ class ApiUpload extends ApiBase {
         * Perform the actual upload. Returns a suitable result array on success;
         * dies on failure.
         *
+        * @param $warnings array Array of Api upload warnings
         * @return array
         */
-       protected function performUpload() {
+       protected function performUpload( $warnings ) {
                // Use comment as initial page text by default
                if ( is_null( $this->mParams['text'] ) ) {
                        $this->mParams['text'] = $this->mParams['comment'];
@@ -529,6 +537,9 @@ class ApiUpload extends ApiBase {
 
                $result['result'] = 'Success';
                $result['filename'] = $file->getName();
+               if ( $warnings && count( $warnings ) > 0 ) {
+                       $result['warnings'] = $warnings;
+               }
 
                return $result;
        }