Merge "Avoid multiple writes to changetags table in recentchanges_save hook"
[lhc/web/wiklou.git] / resources / src / mediawiki / api / upload.js
1 /**
2 * Provides an interface for uploading files to MediaWiki.
3 *
4 * @class mw.Api.plugin.upload
5 * @singleton
6 */
7 ( function ( mw, $ ) {
8 var nonce = 0,
9 fieldsAllowed = {
10 stash: true,
11 filekey: true,
12 filename: true,
13 comment: true,
14 text: true,
15 watchlist: true,
16 ignorewarnings: true
17 };
18
19 /**
20 * Get nonce for iframe IDs on the page.
21 *
22 * @private
23 * @return {number}
24 */
25 function getNonce() {
26 return nonce++;
27 }
28
29 /**
30 * Given a non-empty object, return one of its keys.
31 *
32 * @private
33 * @param {Object} obj
34 * @return {string}
35 */
36 function getFirstKey( obj ) {
37 for ( var key in obj ) {
38 if ( obj.hasOwnProperty( key ) ) {
39 return key;
40 }
41 }
42 }
43
44 /**
45 * Get new iframe object for an upload.
46 *
47 * @private
48 * @return {HTMLIframeElement}
49 */
50 function getNewIframe( id ) {
51 var frame = document.createElement( 'iframe' );
52 frame.id = id;
53 frame.name = id;
54 return frame;
55 }
56
57 /**
58 * Shortcut for getting hidden inputs
59 *
60 * @private
61 * @return {jQuery}
62 */
63 function getHiddenInput( name, val ) {
64 return $( '<input>' ).attr( 'type', 'hidden' )
65 .attr( 'name', name )
66 .val( val );
67 }
68
69 /**
70 * Process the result of the form submission, returned to an iframe.
71 * This is the iframe's onload event.
72 *
73 * @param {HTMLIframeElement} iframe Iframe to extract result from
74 * @return {Object} Response from the server. The return value may or may
75 * not be an XMLDocument, this code was copied from elsewhere, so if you
76 * see an unexpected return type, please file a bug.
77 */
78 function processIframeResult( iframe ) {
79 var json,
80 doc = iframe.contentDocument || frames[ iframe.id ].document;
81
82 if ( doc.XMLDocument ) {
83 // The response is a document property in IE
84 return doc.XMLDocument;
85 }
86
87 if ( doc.body ) {
88 // Get the json string
89 // We're actually searching through an HTML doc here --
90 // according to mdale we need to do this
91 // because IE does not load JSON properly in an iframe
92 json = $( doc.body ).find( 'pre' ).text();
93
94 return JSON.parse( json );
95 }
96
97 // Response is a xml document
98 return doc;
99 }
100
101 function formDataAvailable() {
102 return window.FormData !== undefined &&
103 window.File !== undefined &&
104 window.File.prototype.slice !== undefined;
105 }
106
107 $.extend( mw.Api.prototype, {
108 /**
109 * Upload a file to MediaWiki.
110 *
111 * The file will be uploaded using AJAX and FormData, if the browser supports it, or via an
112 * iframe if it doesn't.
113 *
114 * Caveats of iframe upload:
115 * - The returned jQuery.Promise will not receive `progress` notifications during the upload
116 * - It is incompatible with uploads to a foreign wiki using mw.ForeignApi
117 * - You must pass a HTMLInputElement and not a File for it to be possible
118 *
119 * @param {HTMLInputElement|File|Blob} file HTML input type=file element with a file already inside
120 * of it, or a File object.
121 * @param {Object} data Other upload options, see action=upload API docs for more
122 * @return {jQuery.Promise}
123 */
124 upload: function ( file, data ) {
125 var isFileInput, canUseFormData;
126
127 isFileInput = file && file.nodeType === Node.ELEMENT_NODE;
128
129 if ( formDataAvailable() && isFileInput && file.files ) {
130 file = file.files[ 0 ];
131 }
132
133 if ( !file ) {
134 throw new Error( 'No file' );
135 }
136
137 // Blobs are allowed in formdata uploads, it turns out
138 canUseFormData = formDataAvailable() && ( file instanceof window.File || file instanceof window.Blob );
139
140 if ( !isFileInput && !canUseFormData ) {
141 throw new Error( 'Unsupported argument type passed to mw.Api.upload' );
142 }
143
144 if ( canUseFormData ) {
145 return this.uploadWithFormData( file, data );
146 }
147
148 return this.uploadWithIframe( file, data );
149 },
150
151 /**
152 * Upload a file to MediaWiki with an iframe and a form.
153 *
154 * This method is necessary for browsers without the File/FormData
155 * APIs, and continues to work in browsers with those APIs.
156 *
157 * The rough sketch of how this method works is as follows:
158 * 1. An iframe is loaded with no content.
159 * 2. A form is submitted with the passed-in file input and some extras.
160 * 3. The MediaWiki API receives that form data, and sends back a response.
161 * 4. The response is sent to the iframe, because we set target=(iframe id)
162 * 5. The response is parsed out of the iframe's document, and passed back
163 * through the promise.
164 *
165 * @private
166 * @param {HTMLInputElement} file The file input with a file in it.
167 * @param {Object} data Other upload options, see action=upload API docs for more
168 * @return {jQuery.Promise}
169 */
170 uploadWithIframe: function ( file, data ) {
171 var key,
172 tokenPromise = $.Deferred(),
173 api = this,
174 deferred = $.Deferred(),
175 nonce = getNonce(),
176 id = 'uploadframe-' + nonce,
177 $form = $( '<form>' ),
178 iframe = getNewIframe( id ),
179 $iframe = $( iframe );
180
181 for ( key in data ) {
182 if ( !fieldsAllowed[ key ] ) {
183 delete data[ key ];
184 }
185 }
186
187 data = $.extend( {}, this.defaults.parameters, { action: 'upload' }, data );
188 $form.addClass( 'mw-api-upload-form' );
189
190 $form.css( 'display', 'none' )
191 .attr( {
192 action: this.defaults.ajax.url,
193 method: 'POST',
194 target: id,
195 enctype: 'multipart/form-data'
196 } );
197
198 $iframe.one( 'load', function () {
199 $iframe.one( 'load', function () {
200 var result = processIframeResult( iframe );
201 deferred.notify( 1 );
202
203 if ( !result ) {
204 deferred.reject( 'ok-but-empty', 'No response from API on upload attempt.' );
205 } else if ( result.error ) {
206 if ( result.error.code === 'badtoken' ) {
207 api.badToken( 'csrf' );
208 }
209
210 deferred.reject( result.error.code, result );
211 } else if ( result.upload && result.upload.warnings ) {
212 deferred.reject( getFirstKey( result.upload.warnings ), result );
213 } else {
214 deferred.resolve( result );
215 }
216 } );
217 tokenPromise.done( function () {
218 $form.submit();
219 } );
220 } );
221
222 $iframe.on( 'error', function ( error ) {
223 deferred.reject( 'http', error );
224 } );
225
226 $iframe.prop( 'src', 'about:blank' ).hide();
227
228 file.name = 'file';
229
230 $.each( data, function ( key, val ) {
231 $form.append( getHiddenInput( key, val ) );
232 } );
233
234 if ( !data.filename && !data.stash ) {
235 throw new Error( 'Filename not included in file data.' );
236 }
237
238 if ( this.needToken() ) {
239 this.getEditToken().then( function ( token ) {
240 $form.append( getHiddenInput( 'token', token ) );
241 tokenPromise.resolve();
242 }, tokenPromise.reject );
243 } else {
244 tokenPromise.resolve();
245 }
246
247 $( 'body' ).append( $form, $iframe );
248
249 deferred.always( function () {
250 $form.remove();
251 $iframe.remove();
252 } );
253
254 return deferred.promise();
255 },
256
257 /**
258 * Uploads a file using the FormData API.
259 *
260 * @private
261 * @param {File} file
262 * @param {Object} data Other upload options, see action=upload API docs for more
263 * @return {jQuery.Promise}
264 */
265 uploadWithFormData: function ( file, data ) {
266 var key,
267 deferred = $.Deferred();
268
269 for ( key in data ) {
270 if ( !fieldsAllowed[ key ] ) {
271 delete data[ key ];
272 }
273 }
274
275 data = $.extend( {}, this.defaults.parameters, { action: 'upload' }, data );
276 data.file = file;
277
278 if ( !data.filename && !data.stash ) {
279 throw new Error( 'Filename not included in file data.' );
280 }
281
282 // Use this.postWithEditToken() or this.post()
283 this[ this.needToken() ? 'postWithEditToken' : 'post' ]( data, {
284 // Use FormData (if we got here, we know that it's available)
285 contentType: 'multipart/form-data',
286 // No timeout (default from mw.Api is 30 seconds)
287 timeout: 0,
288 // Provide upload progress notifications
289 xhr: function () {
290 var xhr = $.ajaxSettings.xhr();
291 if ( xhr.upload ) {
292 // need to bind this event before we open the connection (see note at
293 // https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest#Monitoring_progress)
294 xhr.upload.addEventListener( 'progress', function ( ev ) {
295 if ( ev.lengthComputable ) {
296 deferred.notify( ev.loaded / ev.total );
297 }
298 } );
299 }
300 return xhr;
301 }
302 } )
303 .done( function ( result ) {
304 deferred.notify( 1 );
305 if ( result.upload && result.upload.warnings ) {
306 deferred.reject( getFirstKey( result.upload.warnings ), result );
307 } else {
308 deferred.resolve( result );
309 }
310 } )
311 .fail( function ( errorCode, result ) {
312 deferred.notify( 1 );
313 deferred.reject( errorCode, result );
314 } );
315
316 return deferred.promise();
317 },
318
319 /**
320 * Upload a file to the stash.
321 *
322 * This function will return a promise, which when resolved, will pass back a function
323 * to finish the stash upload. You can call that function with an argument containing
324 * more, or conflicting, data to pass to the server. For example:
325 *
326 * // upload a file to the stash with a placeholder filename
327 * api.uploadToStash( file, { filename: 'testing.png' } ).done( function ( finish ) {
328 * // finish is now the function we can use to finalize the upload
329 * // pass it a new filename from user input to override the initial value
330 * finish( { filename: getFilenameFromUser() } ).done( function ( data ) {
331 * // the upload is complete, data holds the API response
332 * } );
333 * } );
334 *
335 * @param {File|HTMLInputElement} file
336 * @param {Object} [data]
337 * @return {jQuery.Promise}
338 * @return {Function} return.finishStashUpload Call this function to finish the upload.
339 * @return {Object} return.finishStashUpload.data Additional data for the upload.
340 * @return {jQuery.Promise} return.finishStashUpload.return API promise for the final upload
341 * @return {Object} return.finishStashUpload.return.data API return value for the final upload
342 */
343 uploadToStash: function ( file, data ) {
344 var filekey,
345 api = this;
346
347 if ( !data.filename ) {
348 throw new Error( 'Filename not included in file data.' );
349 }
350
351 function finishUpload( moreData ) {
352 return api.uploadFromStash( filekey, $.extend( data, moreData ) );
353 }
354
355 return this.upload( file, { stash: true, filename: data.filename } ).then(
356 function ( result ) {
357 filekey = result.upload.filekey;
358 return finishUpload;
359 },
360 function ( errorCode, result ) {
361 if ( result && result.upload && result.upload.filekey ) {
362 // Ignore any warnings if 'filekey' was returned, that's all we care about
363 filekey = result.upload.filekey;
364 return $.Deferred().resolve( finishUpload );
365 }
366 return $.Deferred().reject( errorCode, result );
367 }
368 );
369 },
370
371 /**
372 * Finish an upload in the stash.
373 *
374 * @param {string} filekey
375 * @param {Object} data
376 */
377 uploadFromStash: function ( filekey, data ) {
378 data.filekey = filekey;
379 data.action = 'upload';
380 data.format = 'json';
381
382 if ( !data.filename ) {
383 throw new Error( 'Filename not included in file data.' );
384 }
385
386 return this.postWithEditToken( data ).then( function ( result ) {
387 if ( result.upload && result.upload.warnings ) {
388 return $.Deferred().reject( getFirstKey( result.upload.warnings ), result ).promise();
389 }
390 return result;
391 } );
392 },
393
394 needToken: function () {
395 return true;
396 }
397 } );
398
399 /**
400 * @class mw.Api
401 * @mixins mw.Api.plugin.upload
402 */
403 }( mediaWiki, jQuery ) );