Merge "TableDiffFormatter: Don't repeatedly call array_shift()"
[lhc/web/wiklou.git] / resources / src / mediawiki / api / upload.js
index 437ddec..c2da10e 100644 (file)
@@ -17,9 +17,9 @@
                };
 
        /**
-        * @private
         * Get nonce for iframe IDs on the page.
         *
+        * @private
         * @return {number}
         */
        function getNonce() {
@@ -27,9 +27,9 @@
        }
 
        /**
-        * @private
         * Given a non-empty object, return one of its keys.
         *
+        * @private
         * @param {Object} obj
         * @return {string}
         */
@@ -42,9 +42,9 @@
        }
 
        /**
-        * @private
         * Get new iframe object for an upload.
         *
+        * @private
         * @return {HTMLIframeElement}
         */
        function getNewIframe( id ) {
@@ -55,9 +55,9 @@
        }
 
        /**
-        * @private
         * Shortcut for getting hidden inputs
         *
+        * @private
         * @return {jQuery}
         */
        function getHiddenInput( name, val ) {
                                                deferred.reject( 'ok-but-empty', 'No response from API on upload attempt.' );
                                        } else if ( result.error ) {
                                                if ( result.error.code === 'badtoken' ) {
-                                                       api.badToken( 'edit' );
+                                                       api.badToken( 'csrf' );
                                                }
 
                                                deferred.reject( result.error.code, result );
                        }
 
                        function finishUpload( moreData ) {
-                               data = $.extend( data, moreData );
-                               data.filekey = filekey;
-                               data.action = 'upload';
-                               data.format = 'json';
-
-                               if ( !data.filename ) {
-                                       throw new Error( 'Filename not included in file data.' );
-                               }
-
-                               return api.postWithEditToken( data ).then( function ( result ) {
-                                       if ( result.upload && result.upload.warnings ) {
-                                               return $.Deferred().reject( getFirstKey( result.upload.warnings ), result ).promise();
-                                       }
-                                       return result;
-                               } );
+                               return api.uploadFromStash( filekey, $.extend( data, moreData ) );
                        }
 
                        return this.upload( file, { stash: true, filename: data.filename } ).then(
                        );
                },
 
+               /**
+                * Finish an upload in the stash.
+                *
+                * @param {string} filekey
+                * @param {Object} data
+                */
+               uploadFromStash: function ( filekey, data ) {
+                       data.filekey = filekey;
+                       data.action = 'upload';
+                       data.format = 'json';
+
+                       if ( !data.filename ) {
+                               throw new Error( 'Filename not included in file data.' );
+                       }
+
+                       return this.postWithEditToken( data ).then( function ( result ) {
+                               if ( result.upload && result.upload.warnings ) {
+                                       return $.Deferred().reject( getFirstKey( result.upload.warnings ), result ).promise();
+                               }
+                               return result;
+                       } );
+               },
+
                needToken: function () {
                        return true;
                }