From 8d86a7ce452553953c903eef080525ae659d24d8 Mon Sep 17 00:00:00 2001 From: Krinkle Date: Mon, 16 May 2011 22:51:29 +0000 Subject: [PATCH] mw.util.getActionFrom incorrectly returns null * 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 | 33 ++++---- resources/test/index.html | 2 +- .../unit/mediawiki.util/mediawiki.util.js | 75 ++++++++++++++++++- 3 files changed, 92 insertions(+), 18 deletions(-) diff --git a/resources/mediawiki.util/mediawiki.util.js b/resources/mediawiki.util/mediawiki.util.js index 67b7dde393..f23f253ca5 100644 --- a/resources/mediawiki.util/mediawiki.util.js +++ b/resources/mediawiki.util/mediawiki.util.js @@ -226,55 +226,58 @@ }, /** - * 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]; } } diff --git a/resources/test/index.html b/resources/test/index.html index 0910c25522..2bf5d36748 100644 --- a/resources/test/index.html +++ b/resources/test/index.html @@ -29,7 +29,7 @@ - + diff --git a/resources/test/unit/mediawiki.util/mediawiki.util.js b/resources/test/unit/mediawiki.util/mediawiki.util.js index 826f78af16..4569559fe5 100644 --- a/resources/test/unit/mediawiki.util/mediawiki.util.js +++ b/resources/test/unit/mediawiki.util/mediawiki.util.js @@ -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' ); }); -- 2.20.1