mw.util.getActionFrom incorrectly returns null
authorKrinkle <krinkle@users.mediawiki.org>
Mon, 16 May 2011 22:51:29 +0000 (22:51 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Mon, 16 May 2011 22:51:29 +0000 (22:51 +0000)
* Return 'view' for urls like:
 - /w/index.php?title=Foobar
* Anything else, fallback to 'view' (just like PHP does)
* JSHint
 - ['view'] is better written in dot notation.
 - 'action' was already defined on line 235.
 - 'actionRe' is defined multiple times (loop).
 - Use '===' to compare with '0'.
 - 'title' is already defined.
 >> The code check passed 100%!
* Adding QUnit test for getActionFrom and getTitleFrom

(Follow-up r87964)

resources/mediawiki.util/mediawiki.util.js
resources/test/index.html
resources/test/unit/mediawiki.util/mediawiki.util.js

index 67b7dde..f23f253 100644 (file)
                },
 
                /** 
-                * Try to find the wiki action for a given URL with actions paths support
+                * Try to find the wiki action for a given URL with actions paths support.
+                * Defaults to 'view'.
                 *
                 * @param url URL to search for a wiki action
+                * @return Action
                 */
                'getActionFrom' : function( url ) {
                        // attempt to get the action from the parameter [&?]action=
                        var action = mw.util.getParamValue( 'action', url );
-                       if( action !== null ) {
+                       if ( action !== null ) {
                                return action;
                        }
 
                        // now from the action paths
                        var actionPaths = mw.config.get( 'wgActionPaths' );
-                       if( actionPaths.length == 0 ) {
-                               actionPaths['view'] = mw.config.get( 'wgArticlePath' );
+                       if ( actionPaths.length === 0 ) {
+                               actionPaths.view = mw.config.get( 'wgArticlePath' );
                        }
-                       var action = '';
+                       var actionRe;
                        for ( action in actionPaths ) {
-                               var actionRe = new RegExp( actionPaths[action].replace( '$1', '.*?' ) );
-                               if( url.match( actionRe ) ) {
+                               actionRe = new RegExp( actionPaths[action].replace( '$1', '.*?' ) );
+                               if ( url.match( actionRe ) ) {
                                        return action;
                                }
                        }
 
-                       return null;
+                       return 'view';
                },
 
                /**
                 * Try to find the wiki title for a given URL with actions paths support
                 *
                 * @param url URL to search for a title
+                * @return Title or null.
                 */
                'getTitleFrom' : function( url ) {
                        // attempt to get the title from the parameter [&?]title=
                        var title = mw.util.getParamValue( 'title', url );
-                       if( title !== null ) {
+                       if ( title !== null ) {
                                return title;
                        }
 
                        // now from the action paths    
                        var actionPaths = mw.config.get( 'wgActionPaths' );
-                       if( actionPaths.length == 0 ) {
-                               actionPaths['view'] = mw.config.get( 'wgArticlePath' );
+                       if ( actionPaths.length === 0 ) {
+                               actionPaths.view = mw.config.get( 'wgArticlePath' );
                        }
-                       var action = '';
+                       var action, actionRe;
                        for ( action in actionPaths ) {
-                               var actionRe = new RegExp( '.*' + actionPaths[action].replace( '$1', '([^&?#]+)' ) );
-                               var title = url.match( actionRe );
-                               if( title !== null ) {
+                               actionRe = new RegExp( '.*' + actionPaths[action].replace( '$1', '([^&?#]+)' ) );
+                               title = url.match( actionRe );
+                               if ( title !== null ) {
                                        return title[1];
                                }
                        }
index 0910c25..2bf5d36 100644 (file)
@@ -29,7 +29,7 @@
 
        <meta name="ResourceLoaderDynamicStyles" content="" /> 
 
-       <!-- QUnit dependancies and scripts -->
+       <!-- QUnit -->
        <link rel="stylesheet" href="../jquery/jquery.qunit.css" />
        <script src="../jquery/jquery.qunit.js"></script>
 
index 826f78a..4569559 100644 (file)
@@ -53,8 +53,79 @@ test( 'wikiGetlink', function(){
 
 test( 'getParamValue', function(){
 
-       equals( mw.util.getParamValue( 'foo', 'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad' ), 'right', 'Use latest one, ignore hash' );
-       same( mw.util.getParamValue( 'bar', 'http://mediawiki.org/?foo=right' ), null, 'Return null when not found' );
+       var url = 'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad';
+
+       equal( mw.util.getParamValue( 'foo', url ), 'right', 'Use latest one, ignore hash' );
+       deepEqual( mw.util.getParamValue( 'bar', url ), null, 'Return null when not found' );
+
+});
+
+test( 'getActionFrom', function(){
+
+       // Example urls
+       var     urlA = 'http://mediawiki.org/wiki/Article',
+               urlB = 'http://mediawiki.org/w/index.php?title=Article&action=edit',
+               urlC = 'http://mediawiki.org/edit/Article',
+               urlD = 'http://mediawiki.org/w/index.php/Article';
+
+       // Common settings
+       mw.config.set( {
+               'wgActionPaths': [],
+               'wgArticlePath': '/wiki/$1'
+       });
+
+       equal( mw.util.getActionFrom( urlA ), 'view', 'wgArticlePath (/wiki/$1) support' );
+       equal( mw.util.getActionFrom( urlB ), 'edit', 'action-parameter support' );
+
+       // Custom settings
+       mw.config.set( 'wgActionPaths', {
+               'view': '/view/$1',
+               'edit': '/edit/$1'
+       });
+
+       equal( mw.util.getActionFrom( urlC ), 'edit', 'wgActionPaths support' );
+
+       // Default settings
+       mw.config.set( {
+               'wgActionPaths': [],
+               'wgArticlePath': '/w/index.php/$1' 
+       });
+       equal( mw.util.getActionFrom( urlD ), 'view', 'wgArticlePath (/index.php/$1) support' );
+
+});
+
+test( 'getTitleFrom', function(){
+
+       // Example urls
+       var     urlA = 'http://mediawiki.org/wiki/Article',
+               urlB = 'http://mediawiki.org/w/index.php?title=Article&action=edit',
+               urlC = 'http://mediawiki.org/edit/Article',
+               urlD = 'http://mediawiki.org/w/index.php/Article';
+
+       // Common settings
+       mw.config.set( {
+               'wgActionPaths': [],
+               'wgArticlePath': '/wiki/$1'
+       });
+
+       equal( mw.util.getTitleFrom( urlA ), 'Article', 'wgArticlePath (/wiki/$1) support' );
+       equal( mw.util.getTitleFrom( urlB ), 'Article', 'action-parameter support' );
+
+       // Custom settings
+       mw.config.set( 'wgActionPaths', {
+               'view': '/view/$1',
+               'edit': '/edit/$1'
+       });
+
+       equal( mw.util.getTitleFrom( urlC ), 'Article', 'wgActionPaths support' );
+
+       // Default settings
+       mw.config.set( {
+               'wgActionPaths': [],
+               'wgArticlePath': '/w/index.php/$1' 
+       });
+
+       equal( mw.util.getTitleFrom( urlD ), 'Article', 'wgArticlePath (/index.php/$1) support' );
 
 });