Rillke has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/131595

Change subject: UploadWizard: Don't throw JS errors; instead report them in UI
......................................................................

UploadWizard: Don't throw JS errors; instead report them in UI

Currently, Upload Wizard throws JavaScript errors towards the user's
JS-console when entering characters that are not valid in titles,
including square brackets, for instance.

After applying this patch, UploadWizard will properly report these
errors by leveraging the jQuery.validate plugin.

Further clean up:

- When constructing jQuery objects, either used valid HTML or a
  single tag as the string argument.
- Combined multiple `var` statements.
- Avoid executing RegExps unnecessarily by passing the title as
  arguments to subroutines.

Bug: 64908

Change-Id: Ib54bf84497e37f8fba614e5c4dfa11550029b87d
---
M UploadWizardHooks.php
M i18n/en.json
M i18n/qqq.json
M resources/mw.DestinationChecker.js
M resources/mw.UploadWizardDetails.js
5 files changed, 48 insertions(+), 33 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/UploadWizard 
refs/changes/95/131595/1

diff --git a/UploadWizardHooks.php b/UploadWizardHooks.php
index 764de17..f4ec2ab 100644
--- a/UploadWizardHooks.php
+++ b/UploadWizardHooks.php
@@ -401,6 +401,7 @@
                                'mwe-upwiz-category-remove',
                                'mwe-upwiz-thumbnail-failed',
                                'mwe-upwiz-unparseable-filename',
+                               'mwe-upwiz-unparseable-title',
                                'mwe-upwiz-image-preview',
                                'mwe-upwiz-subhead-bugs',
                                'mwe-upwiz-subhead-alt-upload',
diff --git a/i18n/en.json b/i18n/en.json
index 2c320a2..0cb0c8d 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -293,6 +293,7 @@
     "mwe-upwiz-category-remove": "Remove this category",
     "mwe-upwiz-thumbnail-failed": "The upload succeeded, but the server could 
not get a preview thumbnail.",
     "mwe-upwiz-unparseable-filename": "Could not understand the file name 
\"$1\".",
+    "mwe-upwiz-unparseable-title": "This title is invalid. Make sure to remove 
characters like square brackets, colons, comparison operators, pipes and curly 
brackets.",
     "mwe-upwiz-image-preview": "File preview",
     "mwe-upwiz-subhead-bugs": "[$1 Known issues]",
     "mwe-upwiz-subhead-alt-upload": "[$1 Back to the old form]",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 26f6ae7..34106bf 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -253,6 +253,7 @@
        "mwe-upwiz-category-will-be-added": "Used to let the user know that the 
category they entered will be created upon form submission.",
        "mwe-upwiz-category-remove": "Used as action link 
text.\n{{Identical|Remove category}}",
        "mwe-upwiz-unparseable-filename": "Used as error message. 
Parameters:\n* $1 - filename",
+       "mwe-upwiz-unparseable-title": "Used as error message. More information 
about page titles [[:mw:Manual:Page title|at MediaWiki wiki]].",
        "mwe-upwiz-image-preview": "Used as title for the preview of the image 
file.",
        "mwe-upwiz-subhead-bugs": "Unused at this time. Parameters:\n* $1 - 
full URL\n{{Identical|Known issue}}",
        "mwe-upwiz-subhead-alt-upload": "Used as a link in the sub-header. 
Parameters:\n* $1 - full URL",
diff --git a/resources/mw.DestinationChecker.js 
b/resources/mw.DestinationChecker.js
index cb9312f..ad6d4e9 100644
--- a/resources/mw.DestinationChecker.js
+++ b/resources/mw.DestinationChecker.js
@@ -122,8 +122,8 @@
                        checker.spinner( status.unique === null || 
status.blacklist === null );
                }
 
-               this.checkUnique( checkerStatus );
-               this.checkBlacklist( checkerStatus );
+               this.checkUnique( checkerStatus, title );
+               this.checkBlacklist( checkerStatus, title );
        },
 
        /**
@@ -138,7 +138,9 @@
         * Async check if a title is in the titleblacklist.
         * @param {Function} takes object, like { 'blacklist': result }
         */
-       checkBlacklist: function ( callback ) {
+       checkBlacklist: function ( callback, title ) {
+               var checker = this;
+
                function blacklistResultProcessor( blacklistResult ) {
                        var result;
 
@@ -157,8 +159,6 @@
                        callback( { 'blacklist': result } );
                }
 
-               var checker = this,
-                       title = this.getTitle();
 
                if ( title === '' ) {
                        return;
@@ -186,7 +186,10 @@
         * This is a more abstract version of 
AddMedia/UploadHandler.js::doDestCheck
         * @param {Function} takes object, like { 'unique': result }
         */
-       checkUnique: function( callback ) {
+       checkUnique: function( callback, title ) {
+               var params,
+                       checker = this;
+
                function ok( data ) {
                        var result, protection, pageId, ntitle, img;
 
@@ -267,20 +270,18 @@
                        mw.log( 'mw.DestinationChecker::checkUnique> Error in 
checkUnique result: ' + code );
                }
 
-               var checker = this,
-                       title = this.getTitle(),
 
-                       // Setup the request -- will return thumbnail data if 
it finds one
-                       // XXX do not use iiurlwidth as it will create a 
thumbnail
-                       params = {
-                               'titles': title,
-                               'prop':  'info|imageinfo',
-                               'inprop': 'protection',
-                               'iiprop': 'url|mime|size',
-                               'iiurlwidth': 150
-                       };
+               // Setup the request -- will return thumbnail data if it finds 
one
+               // XXX do not use iiurlwidth as it will create a thumbnail
+               params = {
+                       'titles':     title,
+                       'prop':       'info|imageinfo',
+                       'inprop':     'protection',
+                       'iiprop':     'url|mime|size',
+                       'iiurlwidth': 150
+               };
 
-               // if input is empty don't bother.
+               // if input is empty or invalid, don't bother.
                if ( title === '' ) {
                        return;
                }
diff --git a/resources/mw.UploadWizardDetails.js 
b/resources/mw.UploadWizardDetails.js
index 8aebf47..9d258f5 100644
--- a/resources/mw.UploadWizardDetails.js
+++ b/resources/mw.UploadWizardDetails.js
@@ -33,14 +33,14 @@
        // descriptions
        _this.descriptionsDiv = $( '<div 
class="mwe-upwiz-details-descriptions"></div>' );
 
-       _this.descriptionAdder = $( '<a class="mwe-upwiz-more-options"/>' )
+       _this.descriptionAdder = $( '<a class="mwe-upwiz-more-options"></a>' )
                                        .text( mw.message( 
'mwe-upwiz-desc-add-0' ).text() )
                                        .click( function( ) { 
_this.addDescription(); } );
 
        var descriptionAdderDiv =
-               $( '<div />' ).append(
-                       $( '<div class="mwe-upwiz-details-fieldname" />' ),
-                       $( '<div class="mwe-upwiz-details-descriptions-add" />' 
)
+               $( '<div>' ).append(
+                       $( '<div class="mwe-upwiz-details-fieldname"></div>' ),
+                       $( '<div 
class="mwe-upwiz-details-descriptions-add"></div>' )
                                        .append( _this.descriptionAdder )
                );
 
@@ -51,19 +51,24 @@
        _this.titleId = "title" + _this.upload.index;
        _this.titleInput = $( '<input type="text" id="' + _this.titleId + '" 
name="' + _this.titleId + '" class="mwe-title" maxlength="250"/>' )
                .keyup( function() {
-                       _this.setCleanTitle( $( _this.titleInput ).val() );
+                       var cleanTitle = _this.setCleanTitle( $( 
_this.titleInput ).val() );
                } )
                .destinationChecked( {
                        api: _this.upload.api,
                        spinner: function(bool) { 
_this.toggleDestinationBusy(bool); },
                        preprocess: function( name ) {
+                               var cleanTitle;
+
                                // First, clear out any existing errors, to 
prevent strange visual effects.
                                // Fixes bug 32469.
+                               // FixMe: There should be a method 
`.clearError()`
                                _this.$form.find( 'label[for=' + _this.titleId 
+ ']' ).empty();
+
                                if ( name !== '' ) {
-                                       // turn the contents of the input into 
a MediaWiki title ("File:foo_bar.jpg") to look up
+                                       // turn the contents of the input into 
a MediaWiki title ("File:foo bar.jpg") to look up
                                        // side effect -- also sets this as our 
current title
-                                       return _this.setCleanTitle( name 
).toString();
+                                       cleanTitle = _this.setCleanTitle( name 
);
+                                       return cleanTitle && 
cleanTitle.getMainText() || '';
                                } else {
                                        return name;
                                }
@@ -407,12 +412,14 @@
                                titleSenselessimagename: true,
                                titleThumbnail: true,
                                titleExtension: true,
+                               titleParsability: true,
                                messages: {
                                        required: mw.message( 
'mwe-upwiz-error-blank' ).escaped(),
                                        titleBadchars: mw.message( 
'mwe-upwiz-error-title-badchars' ).escaped(),
                                        titleSenselessimagename: mw.message( 
'mwe-upwiz-error-title-senselessimagename' ).escaped(),
                                        titleThumbnail: mw.message( 
'mwe-upwiz-error-title-thumbnail' ).escaped(),
-                                       titleExtension: mw.message( 
'mwe-upwiz-error-title-extension' ).escaped()
+                                       titleExtension: mw.message( 
'mwe-upwiz-error-title-extension' ).escaped(),
+                                       titleParsability: mw.message( 
'mwe-upwiz-unparseable-title' ).escaped()
                                }
                        } );
        }
@@ -1529,17 +1536,21 @@
        /**
         * Apply some special cleanups for titles before adding to model. These 
cleanups are not reflected in what the user sees in the title input field.
         * For example, we remove an extension in the title if it matches the 
extension we're going to add anyway. (bug #30676)
-        * @param {String} title in human-readable form, e.g. "Foo bar", rather 
than "File:Foo_bar.jpg"
-        * @return {String} cleaned title with prefix and extension, 
stringified.
+        * @param {string} title in human-readable form, e.g. "Foo bar", rather 
than "File:Foo_bar.jpg"
+        * @return {string} cleaned title with prefix and extension, 
stringified.
         */
        setCleanTitle: function( s ) {
-               var ext = this.upload.title.getExtension();
-               var re = new RegExp( '\\.' + this.upload.title.getExtension() + 
'$', 'i' );
-               var cleaned = $.trim( s.replace( re, '' ) );
-               this.upload.title = new mw.Title( cleaned + '.' + ext, fileNsId 
);
+               var ext = this.upload.title.getExtension(),
+                       re = new RegExp( '\\.' + 
this.upload.title.getExtension() + '$', 'i' ),
+                       cleaned = $.trim( s.replace( re, '' ) );
+
+               this.upload.title = mw.Title.newFromText( cleaned + '.' + ext, 
fileNsId ) || this.upload.title;
                return this.upload.title;
        }
-
 };
 
+$.validator.addMethod( 'titleParsability', function( s, elem ) {
+    return this.optional( elem ) || mw.Title.newFromText( $.trim( s ) );
+} );
+
 }) ( mediaWiki, jQuery );

-- 
To view, visit https://gerrit.wikimedia.org/r/131595
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib54bf84497e37f8fba614e5c4dfa11550029b87d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/UploadWizard
Gerrit-Branch: master
Gerrit-Owner: Rillke <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to