TheDJ has uploaded a new change for review.

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

Change subject: Link dialog: do basic URI validation
......................................................................

Link dialog: do basic URI validation

This isn't very sophisticated, but it will handle the basics. Also
reworked the switch between internal and external links and moved all
state calculation into a single function instead of having it spread
troughout..

Bug: 28789
Change-Id: I243106901939b4baef6d9d04a4e0e428f2a6b79e
---
M WikiEditor.php
M i18n/en.json
M i18n/qqq.json
M modules/jquery.wikiEditor.dialogs.config.css
M modules/jquery.wikiEditor.dialogs.config.js
5 files changed, 35 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikiEditor 
refs/changes/67/141367/1

diff --git a/WikiEditor.php b/WikiEditor.php
index 723c4bf..848e7b0 100644
--- a/WikiEditor.php
+++ b/WikiEditor.php
@@ -99,6 +99,7 @@
                        'jquery.wikiEditor.toolbar.i18n',
                        'jquery.suggestions',
                        'mediawiki.Title',
+                       'mediawiki.Uri',
                        'mediawiki.jqueryMsg',
                ),
                'messages' => array(
@@ -181,6 +182,7 @@
                        'wikieditor-toolbar-tool-link-int-target-status-exists',
                        
'wikieditor-toolbar-tool-link-int-target-status-notexists',
                        
'wikieditor-toolbar-tool-link-int-target-status-invalid',
+                       
'wikieditor-toolbar-tool-link-int-target-status-invalidurl',
                        
'wikieditor-toolbar-tool-link-int-target-status-external',
                        
'wikieditor-toolbar-tool-link-int-target-status-loading',
                        
'wikieditor-toolbar-tool-link-int-target-status-disambig',
diff --git a/i18n/en.json b/i18n/en.json
index 5d4ae47..ecd4208 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -53,6 +53,7 @@
     "wikieditor-toolbar-tool-link-int-target-status-exists": "Page exists",
     "wikieditor-toolbar-tool-link-int-target-status-notexists": "Page does not 
exist",
     "wikieditor-toolbar-tool-link-int-target-status-invalid": "Invalid title",
+    "wikieditor-toolbar-tool-link-int-target-status-invalidurl": "Invalid URL",
     "wikieditor-toolbar-tool-link-int-target-status-external": "External link",
     "wikieditor-toolbar-tool-link-int-target-status-loading": "Checking page 
existence...",
     "wikieditor-toolbar-tool-link-int-target-status-disambig": "Disambiguation 
page",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 22bb04f..34a3ed9 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -60,6 +60,7 @@
        "wikieditor-toolbar-tool-link-int-target-status-exists": "Text shown 
when the title the user entered exists",
        "wikieditor-toolbar-tool-link-int-target-status-notexists": "Text shown 
when the title the user entered does not exist",
        "wikieditor-toolbar-tool-link-int-target-status-invalid": "Text shown 
when the title the user entered is invalid.\n{{Identical|Invalid title}}",
+       "wikieditor-toolbar-tool-link-int-target-status-invalid": "Text shown 
when the external URL the user entered is invalid.",
        "wikieditor-toolbar-tool-link-int-target-status-external": "Text shown 
when the link target the user entered points to an external web 
site\n{{Identical|External link}}",
        "wikieditor-toolbar-tool-link-int-target-status-loading": "alt text and 
title text for the image shown while the title the user entered is being 
checked for existence",
        "wikieditor-toolbar-tool-link-int-target-status-disambig": "Text shown 
when the link target the user entered points to a disambiguation 
page.\n{{Identical|Disambiguation page}}",
diff --git a/modules/jquery.wikiEditor.dialogs.config.css 
b/modules/jquery.wikiEditor.dialogs.config.css
index 1cc9f5a..a9fa7b1 100644
--- a/modules/jquery.wikiEditor.dialogs.config.css
+++ b/modules/jquery.wikiEditor.dialogs.config.css
@@ -55,6 +55,7 @@
 #wikieditor-toolbar-link-int-target-status-exists,
 #wikieditor-toolbar-link-int-target-status-notexists,
 #wikieditor-toolbar-link-int-target-status-invalid,
+#wikieditor-toolbar-link-int-target-status-invalidurl,
 #wikieditor-toolbar-link-int-target-status-external,
 #wikieditor-toolbar-link-int-target-status-disambig {
        padding-left: 30px;
@@ -71,7 +72,8 @@
        background-image: url(images/dialogs/insert-link-notexists.png);
        background-position: left;
 }
-#wikieditor-toolbar-link-int-target-status-invalid {
+#wikieditor-toolbar-link-int-target-status-invalid,
+#wikieditor-toolbar-link-int-target-status-invalidurl {
        /* @embed */
        background-image: url(images/dialogs/insert-link-invalid.png);
        background-position: left;
diff --git a/modules/jquery.wikiEditor.dialogs.config.js 
b/modules/jquery.wikiEditor.dialogs.config.js
index 3ec2641..75df58c 100644
--- a/modules/jquery.wikiEditor.dialogs.config.js
+++ b/modules/jquery.wikiEditor.dialogs.config.js
@@ -138,13 +138,13 @@
                                                $( 
'#wikieditor-toolbar-link-int-target-status' ).children().hide();
                                                $( 
'#wikieditor-toolbar-link-int-target' ).parent()
                                                        .removeClass(
-                                                               'status-invalid 
status-external status-notexists status-exists status-loading'
+                                                               'status-invalid 
status-invalidurl status-external status-notexists status-exists status-loading'
                                                        );
                                                if ( status ) {
                                                        $( 
'#wikieditor-toolbar-link-int-target-status-' + status ).show();
                                                        $( 
'#wikieditor-toolbar-link-int-target' ).parent().addClass( 'status-' + status );
                                                }
-                                               if ( status === 'invalid' ) {
+                                               if ( status === 'invalid' || 
status === 'invalidurl' ) {
                                                        $( '.ui-dialog:visible 
.ui-dialog-buttonpane button:first' )
                                                                .attr( 
'disabled', true )
                                                                .addClass( 
'disabled' );
@@ -156,12 +156,7 @@
                                        }
 
                                        // Updates the UI to show if the page 
title being inputed by the user exists or not
-                                       // accepts parameter internal for 
bypassing external link detection
-                                       function updateExistence( internal ) {
-                                               // ensure the internal 
parameter is a boolean
-                                               if ( internal !== true ) {
-                                                       internal = false;
-                                               }
+                                       function updateState() {
                                                // Abort previous request
                                                var request = $( 
'#wikieditor-toolbar-link-int-target-status' ).data( 'request' );
                                                if ( request ) {
@@ -169,17 +164,21 @@
                                                }
                                                var target = $( 
'#wikieditor-toolbar-link-int-target' ).val();
                                                var cache = $( 
'#wikieditor-toolbar-link-int-target-status' ).data( 'existencecache' );
-                                               if ( hasOwn.call( cache, target 
) ) {
-                                                       updateWidget( 
cache[target] );
-                                                       return;
-                                               }
                                                if ( target.replace( /^\s+$/,'' 
) === '' ) {
                                                        // Hide the widget when 
the textbox is empty
                                                        updateWidget( false );
                                                        return;
                                                }
-                                               // If the forced internal 
paremter was not true, check if the target is an external link
-                                               if ( !internal && 
isExternalLink( target ) ) {
+                                               if( $( 
'#wikieditor-toolbar-link-type-ext' ).prop( 'checked' ) ) {
+                                                       try {
+                                                               if ( 
target.indexOf( 'www.' ) === 0 ) {
+                                                                       target 
= 'http://' + target;
+                                                               }
+                                                               new mw.Uri( 
target );
+                                                       } catch ( e ) {
+                                                               updateWidget( 
'invalidurl' );
+                                                               return;
+                                                       }
                                                        updateWidget( 
'external' );
                                                        return;
                                                }
@@ -187,6 +186,10 @@
                                                        // Title contains | , 
which means it's invalid
                                                        // but confuses the 
API. Show invalid and bypass API
                                                        updateWidget( 'invalid' 
);
+                                                       return;
+                                               }
+                                               if ( hasOwn.call( cache, target 
) ) {
+                                                       updateWidget( 
cache[target] );
                                                        return;
                                                }
                                                // Show loading spinner while 
waiting for the API to respond
@@ -220,27 +223,21 @@
                                                                                
status = 'disambig';
                                                                        }
                                                                }
-                                                               // Cache the 
status of the link target if the force internal
-                                                               // parameter 
was not passed
-                                                               if ( !internal 
) {
-                                                                       
cache[target] = status;
-                                                               }
+                                                               cache[target] = 
status;
                                                                updateWidget( 
status );
                                                        } )
                                                );
                                        }
                                        $( '#wikieditor-toolbar-link-type-int, 
#wikieditor-toolbar-link-type-ext' ).click( function () {
+
                                                if ( $( 
'#wikieditor-toolbar-link-type-ext' ).prop( 'checked' ) ) {
                                                        // Abort previous 
request
                                                        var request = $( 
'#wikieditor-toolbar-link-int-target-status' ).data( 'request' );
                                                        if ( request ) {
                                                                request.abort();
                                                        }
-                                                       updateWidget( 
'external' );
                                                }
-                                               if ( $( 
'#wikieditor-toolbar-link-type-int' ).prop( 'checked' ) ) {
-                                                       updateExistence( true );
-                                               }
+                                               updateState();
                                        } );
                                        // Set labels of tabs based on rel 
values
                                        $( this ).find( '[rel]' ).each( 
function () {
@@ -295,11 +292,10 @@
                                                setTimeout( function () {
                                                        if ( isExternalLink( $( 
'#wikieditor-toolbar-link-int-target' ).val() ) ) {
                                                                $( 
'#wikieditor-toolbar-link-type-ext' ).prop( 'checked', true );
-                                                               updateWidget( 
'external' );
                                                        } else {
                                                                $( 
'#wikieditor-toolbar-link-type-int' ).prop( 'checked', true );
-                                                               
updateExistence();
                                                        }
+                                                       updateState();
                                                        /*jshint eqeqeq:false */
                                                        if ( $( 
'#wikieditor-toolbar-link-int-text' ).data( 'untouched' ) ) {
                                                                if ( $( 
'#wikieditor-toolbar-link-int-target' ).val() ==
@@ -331,6 +327,7 @@
                                        var existsMsg = mw.msg( 
'wikieditor-toolbar-tool-link-int-target-status-exists' );
                                        var notexistsMsg = mw.msg( 
'wikieditor-toolbar-tool-link-int-target-status-notexists' );
                                        var invalidMsg = mw.msg( 
'wikieditor-toolbar-tool-link-int-target-status-invalid' );
+                                       var invalidUrlMsg = mw.msg( 
'wikieditor-toolbar-tool-link-int-target-status-invalidurl' );
                                        var externalMsg = mw.msg( 
'wikieditor-toolbar-tool-link-int-target-status-external' );
                                        var loadingMsg = mw.msg( 
'wikieditor-toolbar-tool-link-int-target-status-loading' );
                                        var disambigMsg = mw.msg( 
'wikieditor-toolbar-tool-link-int-target-status-disambig' );
@@ -346,6 +343,10 @@
                                                .append( $( '<div>' )
                                                        .attr( 'id', 
'wikieditor-toolbar-link-int-target-status-invalid' )
                                                        .append( invalidMsg )
+                                               )
+                                               .append( $( '<div>' )
+                                                       .attr( 'id', 
'wikieditor-toolbar-link-int-target-status-invalidurl' )
+                                                       .append( invalidUrlMsg )
                                                )
                                                .append( $( '<div>' )
                                                        .attr( 'id', 
'wikieditor-toolbar-link-int-target-status-external' )
@@ -374,7 +375,7 @@
                                                        }
                                                        // Delay fetch for a 
while
                                                        // FIXME: Make 120 
configurable elsewhere
-                                                       var timerID = 
setTimeout( updateExistence, 120 );
+                                                       var timerID = 
setTimeout( updateState, 120 );
                                                        $( this ).data( 
'timerID', timerID );
                                                } )
                                                .change( function () {
@@ -383,7 +384,7 @@
                                                                clearTimeout( 
$( this ).data( 'timerID' ) );
                                                        }
                                                        // Fetch right now
-                                                       updateExistence();
+                                                       updateState();
                                                } );
 
                                        // Title suggestions

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I243106901939b4baef6d9d04a4e0e428f2a6b79e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikiEditor
Gerrit-Branch: master
Gerrit-Owner: TheDJ <hartman.w...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to