From 3e596a9185724793bb8ff4e47373a9d75c17914a Mon Sep 17 00:00:00 2001 From: Prateek Saxena Date: Fri, 2 Oct 2015 17:20:54 +0530 Subject: [PATCH] mw.Upload.BookletLayout: Show errors and try to recover from warnings Show useful errors and try and recover from warning by changing the state of the upload. Bug: T114130 Change-Id: I08704108704ffaa70245c5facfca4b063a61f01b --- languages/i18n/en.json | 1 + languages/i18n/qqq.json | 1 + resources/Resources.php | 7 ++ .../mediawiki.Upload.BookletLayout.js | 72 +++++++++++++++---- resources/src/mediawiki/mediawiki.Upload.js | 18 ++++- 5 files changed, 84 insertions(+), 15 deletions(-) diff --git a/languages/i18n/en.json b/languages/i18n/en.json index f73315d2b4..818edffd3e 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -1391,6 +1391,7 @@ "upload-options": "Upload options", "watchthisupload": "Watch this file", "filewasdeleted": "A file of this name has been previously uploaded and subsequently deleted.\nYou should check the $1 before proceeding to upload it again.", + "filename-thumb-name": "This looks like a thumbnail title. Please do not upload thumbnails back to the same wiki. Otherwise, please fix the filename so it is more meaningful, and does not have the thumbnail prefix.", "filename-bad-prefix": "The name of the file you are uploading begins with \"$1\", which is a non-descriptive name typically assigned automatically by digital cameras.\nPlease choose a more descriptive name for your file.", "filename-prefix-blacklist": " #
\n# Syntax is as follows:\n#   * Everything from a \"#\" character to the end of the line is a comment\n#   * Every non-blank line is a prefix for typical filenames assigned automatically by digital cameras\nCIMG # Casio\nDSC_ # Nikon\nDSCF # Fuji\nDSCN # Nikon\nDUW # some mobile phones\nIMG # generic\nJD # Jenoptik\nMGP # Pentax\nPICT # misc.\n #
", "upload-success-subj": "Successful upload", diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json index c03578d7db..3893577dc2 100644 --- a/languages/i18n/qqq.json +++ b/languages/i18n/qqq.json @@ -1564,6 +1564,7 @@ "upload-options": "Caption above a section of the [[Special:Upload]] page", "watchthisupload": "In [[Special:Upload]]", "filewasdeleted": "This warning is shown when trying to upload a file that does not exist, but has previously been deleted.\n\nParameters:\n* $1 - a link to the deletion log, with the text from {{msg-mw|deletionlog}}", + "filename-thumb-name": "Appears when thumbnail title is similar to \"22px-Example.jpg\" - the prefix is what MW adds when downloading pictures as thumbnails.", "filename-bad-prefix": "Used as warning in [[Special:Upload]]. Parameters:\n* $1 - prefix\nSee also:\n* {{msg-mw|fileexists}}\n* {{msg-mw|filepageexists}}\n* {{msg-mw|fileexists-extension}}\n* {{msg-mw|fileexists-thumbnail-yes}}\n* {{msg-mw|file-thumbnail-no}}\n* {{msg-mw|filewasdeleted}}", "filename-prefix-blacklist": "{{optional}}\nDo not translate the file name prefixes before the hash mark (#). Leave all the wiki markup, including the spaces, as is. You can translate the text, including 'Leave this line exactly as it is'. The first line of this messages has one (1) leading space.", "upload-success-subj": "Used as message subject which is posted on the user talk page.\n\nSee also:\n* {{msg-mw|upload-success-subj|subject}}\n* {{msg-mw|upload-success-msg|message}}\n* {{msg-mw|upload-failure-subj|subject}}\n* {{msg-mw|upload-failure-msg|message}}", diff --git a/resources/Resources.php b/resources/Resources.php index ccc3cd5971..9affe26f85 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -1220,6 +1220,7 @@ return array( 'dependencies' => array( 'oojs-ui', 'mediawiki.Upload', + 'mediawiki.jqueryMsg', ), 'messages' => array( 'upload-process-error', @@ -1230,6 +1231,12 @@ return array( 'upload-form-label-infoform-description', 'upload-form-label-usage-title', 'upload-form-label-usage-filename', + 'fileexists', + 'filepageexists', + 'filename-bad-prefix', + 'filename-thumb-name', + 'badfilename', + 'api-error-duplicate-archive', ), ), 'mediawiki.ForeignStructuredUpload.BookletLayout' => array( diff --git a/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js b/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js index d1e01b1908..b574a5d8fd 100644 --- a/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js +++ b/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js @@ -232,31 +232,75 @@ this.upload.setFilename( this.getFilename() ); this.upload.setText( this.getText() ); - this.uploadPromise.always( function () { - layout.upload.finishStashUpload().always( function () { + this.uploadPromise.then( function () { + layout.upload.finishStashUpload().then( function () { var name; + // Normalize page name and localise the 'File:' prefix + name = new mw.Title( 'File:' + layout.upload.getFilename() ).toString(); + layout.filenameUsageWidget.setValue( '[[' + name + ']]' ); + layout.setPage( 'insert' ); + + deferred.resolve(); + layout.emit( 'fileSaved' ); + }, function () { + var stateDetails = layout.upload.getStateDetails(); + if ( layout.upload.getState() === mw.Upload.State.ERROR ) { - deferred.reject( new OO.ui.Error( mw.msg( 'upload-process-error' ), { + deferred.reject( new OO.ui.Error( stateDetails, { recoverable: false } ) ); return false; } if ( layout.upload.getState() === mw.Upload.State.WARNING ) { - deferred.reject( new OO.ui.Error( mw.msg( 'upload-process-warning' ), { - recoverable: false - } ) ); + if ( stateDetails.exists !== undefined ) { + deferred.reject( new OO.ui.Error( + $( '

' ).html( + mw.message( 'filepageexists', stateDetails.exists ).parse() + ), + { recoverable: false } + ) ); + } else if ( stateDetails.duplicate !== undefined ) { + deferred.reject( new OO.ui.Error( + $( '

' ).html( + mw.message( 'fileexists', stateDetails.duplicate[ 0 ] ).parse() + ), + { recoverable: false } + ) ); + } else if ( stateDetails[ 'thumb-name' ] !== undefined ) { + deferred.reject( new OO.ui.Error( + $( '

' ).html( + mw.message( 'filename-thumb-name' ).parse() + ), + { recoverable: false } + ) ); + } else if ( stateDetails[ 'bad-prefix' ] !== undefined ) { + deferred.reject( new OO.ui.Error( + $( '

' ).html( + mw.message( 'filename-bad-prefix', stateDetails[ 'bad-prefix' ] ).parse() + ), + { recoverable: false } + ) ); + } else if ( stateDetails[ 'duplicate-archive' ] !== undefined ) { + deferred.reject( new OO.ui.Error( + $( '

' ).html( + mw.message( 'api-error-duplicate-archive', stateDetails[ 'duplicate-archive' ] ).parse() + ), + { recoverable: false } + ) ); + } else if ( stateDetails.badfilename !== undefined ) { + // Change the name if the current name isn't acceptable + layout.filenameWidget.setValue( stateDetails.badfilename ); + deferred.reject( new OO.ui.Error( + $( '

' ).html( + mw.message( 'badfilename', stateDetails.badfilename ).parse() + ) + ) ); + } + return false; } - - // Normalize page name and localise the 'File:' prefix - name = new mw.Title( 'File:' + layout.upload.getFilename() ).toString(); - layout.filenameUsageWidget.setValue( '[[' + name + ']]' ); - layout.setPage( 'insert' ); - - deferred.resolve(); - layout.emit( 'fileSaved' ); } ); } ); diff --git a/resources/src/mediawiki/mediawiki.Upload.js b/resources/src/mediawiki/mediawiki.Upload.js index 56f4f830d6..007c855495 100644 --- a/resources/src/mediawiki/mediawiki.Upload.js +++ b/resources/src/mediawiki/mediawiki.Upload.js @@ -189,6 +189,9 @@ /** * Sets the state and state details (if any) of the upload. + * + * @param {mw.Upload.State} state + * @param {string|Object} stateDetails */ UP.setState = function ( state, stateDetails ) { this.state = state; @@ -310,7 +313,20 @@ upload.setState( Upload.State.UPLOADED ); upload.imageinfo = result.upload.imageinfo; return result; - }, function () { + }, function ( result ) { + // Errors are strings that can be used to get error message + if ( typeof result === 'string' ) { + upload.setState( Upload.State.ERROR, mw.message( 'api-error-' + result ) ); + return; + } + + // Warnings come in the form of objects + if ( $.isPlainObject( result ) ) { + upload.setState( Upload.State.WARNING, result ); + return; + } + + // Throw an empty error if we can't figure it out upload.setState( Upload.State.ERROR ); } ); } ); -- 2.20.1