Merge "Fix sessionfailure i18n message during authentication"
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.upload.js
index 1a3f26c..9af7ab2 100644 (file)
@@ -10,7 +10,7 @@
 /* global Uint8Array */
 
 ( function ( mw, $ ) {
-       var uploadWarning, uploadLicense,
+       var uploadWarning, uploadTemplatePreview,
                ajaxUploadDestCheck = mw.config.get( 'wgAjaxUploadDestCheck' ),
                $license = $( '#wpLicense' );
 
@@ -61,7 +61,7 @@
 
                timeout: function () {
                        var $spinnerDestCheck, title;
-                       if ( !ajaxUploadDestCheck || this.nameToCheck === '' ) {
+                       if ( !ajaxUploadDestCheck || this.nameToCheck.trim() === '' ) {
                                return;
                        }
                        $spinnerDestCheck = $.createSpinner().insertAfter( '#wpDestFile' );
                                formatversion: 2,
                                action: 'query',
                                // If title is empty, user input is invalid, the API call will produce details about why
-                               titles: title ? title.getPrefixedText() : this.nameToCheck,
+                               titles: [ title ? title.getPrefixedText() : this.nameToCheck ],
                                prop: 'imageinfo',
-                               iiprop: 'uploadwarning'
+                               iiprop: 'uploadwarning',
+                               errorformat: 'html',
+                               errorlang: mw.config.get( 'wgUserLanguage' )
                        } ).done( function ( result ) {
                                var
                                        resultOut = '',
@@ -81,7 +83,7 @@
                                if ( page.imageinfo ) {
                                        resultOut = page.imageinfo[ 0 ].html;
                                } else if ( page.invalidreason ) {
-                                       resultOut = mw.html.escape( page.invalidreason );
+                                       resultOut = page.invalidreason.html;
                                }
                                uploadWarning.processResult( resultOut, uploadWarning.nameToCheck );
                        } ).always( function () {
                }
        };
 
-       uploadLicense = {
+       window.wgUploadTemplatePreviewObj = uploadTemplatePreview = {
 
                responseCache: { '': '' },
 
-               fetchPreview: function ( license ) {
-                       var $spinnerLicense;
-                       if ( !mw.config.get( 'wgAjaxLicensePreview' ) ) {
-                               return;
-                       }
-                       if ( this.responseCache.hasOwnProperty( license ) ) {
-                               this.showPreview( this.responseCache[ license ] );
+               /**
+                * @param {jQuery} $element The element whose .val() will be previewed
+                * @param {jQuery} $previewContainer The container to display the preview in
+                */
+               getPreview: function ( $element, $previewContainer ) {
+                       var template = $element.val(),
+                               $spinner;
+
+                       if ( this.responseCache.hasOwnProperty( template ) ) {
+                               this.showPreview( this.responseCache[ template ], $previewContainer );
                                return;
                        }
 
-                       $spinnerLicense = $.createSpinner().insertAfter( '#wpLicense' );
+                       $spinner = $.createSpinner().insertAfter( $element );
 
-                       ( new mw.Api() ).get( {
-                               formatversion: 2,
-                               action: 'parse',
-                               text: '{{' + license + '}}',
+                       ( new mw.Api() ).parse( '{{' + template + '}}', {
                                title: $( '#wpDestFile' ).val() || 'File:Sample.jpg',
                                prop: 'text',
                                pst: true,
                                uselang: mw.config.get( 'wgUserLanguage' )
                        } ).done( function ( result ) {
-                               uploadLicense.processResult( result, license );
+                               uploadTemplatePreview.processResult( result, template, $previewContainer );
                        } ).always( function () {
-                               $spinnerLicense.remove();
+                               $spinner.remove();
                        } );
                },
 
-               processResult: function ( result, license ) {
-                       this.responseCache[ license ] = result.parse.text;
-                       this.showPreview( this.responseCache[ license ] );
+               processResult: function ( result, template, $previewContainer ) {
+                       this.responseCache[ template ] = result;
+                       this.showPreview( this.responseCache[ template ], $previewContainer );
                },
 
-               showPreview: function ( preview ) {
-                       $( '#mw-license-preview' ).html( preview );
+               showPreview: function ( preview, $previewContainer ) {
+                       $previewContainer.html( preview );
                }
 
        };
                        // License selector check
                        $license.change( function () {
                                // We might show a preview
-                               uploadLicense.fetchPreview( $license.val() );
+                               uploadTemplatePreview.getPreview( $license, $( '#mw-license-preview' ) );
                        } );
 
                        // License selector table row
                }
 
                // fillDestFile setup
-               $.each( mw.config.get( 'wgUploadSourceIds' ), function ( index, sourceId ) {
+               mw.config.get( 'wgUploadSourceIds' ).forEach( function ( sourceId ) {
                        $( '#' + sourceId ).change( function () {
                                var path, slash, backslash, fname;
                                if ( !mw.config.get( 'wgUploadAutoFill' ) ) {