mediawiki.special.upload: Don't fail when trying to thumbnail invalid file
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.upload.js
index 677d26d..377b1f7 100644 (file)
                },
 
                timeout: function () {
-                       var $spinnerDestCheck;
+                       var $spinnerDestCheck, title;
                        if ( !ajaxUploadDestCheck || this.nameToCheck === '' ) {
                                return;
                        }
                        $spinnerDestCheck = $.createSpinner().insertAfter( '#wpDestFile' );
+                       title = mw.Title.newFromText( this.nameToCheck, mw.config.get( 'wgNamespaceIds' ).file );
 
                        ( new mw.Api() ).get( {
                                action: 'query',
-                               titles: ( new mw.Title( this.nameToCheck, mw.config.get( 'wgNamespaceIds' ).file ) ).getPrefixedText(),
+                               // If title is empty, user input is invalid, the API call will produce details about why
+                               titles: title ? title.getPrefixedText() : this.nameToCheck,
                                prop: 'imageinfo',
                                iiprop: 'uploadwarning',
-                               indexpageids: ''
+                               indexpageids: true
                        } ).done( function ( result ) {
-                               var resultOut = '';
-                               if ( result.query ) {
-                                       resultOut = result.query.pages[ result.query.pageids[ 0 ] ].imageinfo[ 0 ];
+                               var
+                                       resultOut = '',
+                                       pageId = result.query.pageids[ 0 ];
+                               if ( result.query.pages[ pageId ].imageinfo ) {
+                                       resultOut = result.query.pages[ pageId ].imageinfo[ 0 ].html;
+                               } else if ( result.query.pages[ pageId ].invalidreason ) {
+                                       resultOut = mw.html.escape( result.query.pages[ pageId ].invalidreason );
                                }
                                $spinnerDestCheck.remove();
                                uploadWarning.processResult( resultOut, uploadWarning.nameToCheck );
                },
 
                processResult: function ( result, fileName ) {
-                       this.setWarning( result.html );
-                       this.responseCache[ fileName ] = result.html;
+                       this.setWarning( result );
+                       this.responseCache[ fileName ] = result;
                },
 
                setWarning: function ( warning ) {
-                       $( '#wpDestFile-warning' ).html( warning );
+                       var $warning = $( $.parseHTML( warning ) );
+                       mw.hook( 'wikipage.content' ).fire( $warning );
+                       $( '#wpDestFile-warning' ).empty().append( $warning );
 
                        // Set a value in the form indicating that the warning is acknowledged and
                        // doesn't need to be redisplayed post-upload
                                text: '{{' + license + '}}',
                                title: $( '#wpDestFile' ).val() || 'File:Sample.jpg',
                                prop: 'text',
-                               pst: ''
+                               pst: true
                        } ).done( function ( result ) {
                                $spinnerLicense.remove();
                                uploadLicense.processResult( result, license );
                 *
                 * TODO: Is there a way we can ask the browser what's supported in `<img>`s?
                 *
-                * TODO: Put SVG back after working around Firefox 7 bug <https://bugzilla.wikimedia.org/show_bug.cgi?id=31643>
+                * TODO: Put SVG back after working around Firefox 7 bug <https://phabricator.wikimedia.org/T33643>
                 *
                 * @param {File} file
                 * @return {boolean}
 
                                        $( '#mw-upload-thumbnail .fileinfo' ).text( info );
                                };
+                               img.onerror = function () {
+                                       // Can happen for example for invalid SVG files
+                                       clearPreview();
+                               };
                                img.src = dataURL;
                        }, mw.config.get( 'wgFileCanRotate' ) ? function ( data ) {
                                try {
                        var reader = new FileReader();
                        if ( callbackBinary && 'readAsBinaryString' in reader ) {
                                // To fetch JPEG metadata we need a binary string; start there.
-                               // todo:
+                               // TODO
                                reader.onload = function () {
                                        callbackBinary( reader.result );