r88276 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88275‎ | r88276 | r88277 >
Date:22:51, 16 May 2011
Author:krinkle
Status:reverted (Comments)
Tags:
Comment:
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)
Modified paths:
  • /trunk/phase3/resources/mediawiki.util/mediawiki.util.js (modified) (history)
  • /trunk/phase3/resources/test/index.html (modified) (history)
  • /trunk/phase3/resources/test/unit/mediawiki.util/mediawiki.util.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki.util/mediawiki.util.js
@@ -226,55 +226,58 @@
227227 },
228228
229229 /**
230 - * Try to find the wiki action for a given URL with actions paths support
 230+ * Try to find the wiki action for a given URL with actions paths support.
 231+ * Defaults to 'view'.
231232 *
232233 * @param url URL to search for a wiki action
 234+ * @return Action
233235 */
234236 'getActionFrom' : function( url ) {
235237 // attempt to get the action from the parameter [&?]action=
236238 var action = mw.util.getParamValue( 'action', url );
237 - if( action !== null ) {
 239+ if ( action !== null ) {
238240 return action;
239241 }
240242
241243 // now from the action paths
242244 var actionPaths = mw.config.get( 'wgActionPaths' );
243 - if( actionPaths.length == 0 ) {
244 - actionPaths['view'] = mw.config.get( 'wgArticlePath' );
 245+ if ( actionPaths.length === 0 ) {
 246+ actionPaths.view = mw.config.get( 'wgArticlePath' );
245247 }
246 - var action = '';
 248+ var actionRe;
247249 for ( action in actionPaths ) {
248 - var actionRe = new RegExp( actionPaths[action].replace( '$1', '.*?' ) );
249 - if( url.match( actionRe ) ) {
 250+ actionRe = new RegExp( actionPaths[action].replace( '$1', '.*?' ) );
 251+ if ( url.match( actionRe ) ) {
250252 return action;
251253 }
252254 }
253255
254 - return null;
 256+ return 'view';
255257 },
256258
257259 /**
258260 * Try to find the wiki title for a given URL with actions paths support
259261 *
260262 * @param url URL to search for a title
 263+ * @return Title or null.
261264 */
262265 'getTitleFrom' : function( url ) {
263266 // attempt to get the title from the parameter [&?]title=
264267 var title = mw.util.getParamValue( 'title', url );
265 - if( title !== null ) {
 268+ if ( title !== null ) {
266269 return title;
267270 }
268271
269272 // now from the action paths
270273 var actionPaths = mw.config.get( 'wgActionPaths' );
271 - if( actionPaths.length == 0 ) {
272 - actionPaths['view'] = mw.config.get( 'wgArticlePath' );
 274+ if ( actionPaths.length === 0 ) {
 275+ actionPaths.view = mw.config.get( 'wgArticlePath' );
273276 }
274 - var action = '';
 277+ var action, actionRe;
275278 for ( action in actionPaths ) {
276 - var actionRe = new RegExp( '.*' + actionPaths[action].replace( '$1', '([^&?#]+)' ) );
277 - var title = url.match( actionRe );
278 - if( title !== null ) {
 279+ actionRe = new RegExp( '.*' + actionPaths[action].replace( '$1', '([^&?#]+)' ) );
 280+ title = url.match( actionRe );
 281+ if ( title !== null ) {
279282 return title[1];
280283 }
281284 }
Index: trunk/phase3/resources/test/unit/mediawiki.util/mediawiki.util.js
@@ -53,11 +53,82 @@
5454
5555 test( 'getParamValue', function(){
5656
57 - equals( mw.util.getParamValue( 'foo', 'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad' ), 'right', 'Use latest one, ignore hash' );
58 - same( mw.util.getParamValue( 'bar', 'http://mediawiki.org/?foo=right' ), null, 'Return null when not found' );
 57+ var url = 'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad';
5958
 59+ equal( mw.util.getParamValue( 'foo', url ), 'right', 'Use latest one, ignore hash' );
 60+ deepEqual( mw.util.getParamValue( 'bar', url ), null, 'Return null when not found' );
 61+
6062 });
6163
 64+test( 'getActionFrom', function(){
 65+
 66+ // Example urls
 67+ var urlA = 'http://mediawiki.org/wiki/Article',
 68+ urlB = 'http://mediawiki.org/w/index.php?title=Article&action=edit',
 69+ urlC = 'http://mediawiki.org/edit/Article',
 70+ urlD = 'http://mediawiki.org/w/index.php/Article';
 71+
 72+ // Common settings
 73+ mw.config.set( {
 74+ 'wgActionPaths': [],
 75+ 'wgArticlePath': '/wiki/$1'
 76+ });
 77+
 78+ equal( mw.util.getActionFrom( urlA ), 'view', 'wgArticlePath (/wiki/$1) support' );
 79+ equal( mw.util.getActionFrom( urlB ), 'edit', 'action-parameter support' );
 80+
 81+ // Custom settings
 82+ mw.config.set( 'wgActionPaths', {
 83+ 'view': '/view/$1',
 84+ 'edit': '/edit/$1'
 85+ });
 86+
 87+ equal( mw.util.getActionFrom( urlC ), 'edit', 'wgActionPaths support' );
 88+
 89+ // Default settings
 90+ mw.config.set( {
 91+ 'wgActionPaths': [],
 92+ 'wgArticlePath': '/w/index.php/$1'
 93+ });
 94+ equal( mw.util.getActionFrom( urlD ), 'view', 'wgArticlePath (/index.php/$1) support' );
 95+
 96+});
 97+
 98+test( 'getTitleFrom', function(){
 99+
 100+ // Example urls
 101+ var urlA = 'http://mediawiki.org/wiki/Article',
 102+ urlB = 'http://mediawiki.org/w/index.php?title=Article&action=edit',
 103+ urlC = 'http://mediawiki.org/edit/Article',
 104+ urlD = 'http://mediawiki.org/w/index.php/Article';
 105+
 106+ // Common settings
 107+ mw.config.set( {
 108+ 'wgActionPaths': [],
 109+ 'wgArticlePath': '/wiki/$1'
 110+ });
 111+
 112+ equal( mw.util.getTitleFrom( urlA ), 'Article', 'wgArticlePath (/wiki/$1) support' );
 113+ equal( mw.util.getTitleFrom( urlB ), 'Article', 'action-parameter support' );
 114+
 115+ // Custom settings
 116+ mw.config.set( 'wgActionPaths', {
 117+ 'view': '/view/$1',
 118+ 'edit': '/edit/$1'
 119+ });
 120+
 121+ equal( mw.util.getTitleFrom( urlC ), 'Article', 'wgActionPaths support' );
 122+
 123+ // Default settings
 124+ mw.config.set( {
 125+ 'wgActionPaths': [],
 126+ 'wgArticlePath': '/w/index.php/$1'
 127+ });
 128+
 129+ equal( mw.util.getTitleFrom( urlD ), 'Article', 'wgArticlePath (/index.php/$1) support' );
 130+
 131+});
 132+
62133 test( 'tooltipAccessKey', function(){
63134
64135 equals( typeof mw.util.tooltipAccessKeyPrefix, 'string', 'mw.util.tooltipAccessKeyPrefix must be a string' );
Index: trunk/phase3/resources/test/index.html
@@ -29,7 +29,7 @@
3030
3131 <meta name="ResourceLoaderDynamicStyles" content="" />
3232
33 - <!-- QUnit dependancies and scripts -->
 33+ <!-- QUnit -->
3434 <link rel="stylesheet" href="../jquery/jquery.qunit.css" />
3535 <script src="../jquery/jquery.qunit.js"></script>
3636

Follow-up revisions

RevisionCommit summaryAuthorDate
r88510qunit: add some tests for getActionFrom / getTitleFrom...hashar09:27, 21 May 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r87964merge in prettyURL patch...hashar11:41, 13 May 2011

Comments

#Comment by Hashar (talk | contribs)   09:28, 21 May 2011

Thanks for the default settings. I have reviewed the tests :)

#Comment by Brion VIBBER (talk | contribs)   18:48, 23 May 2011

There seem to be some bogus changes in here, for starters:

  • getTitleFrom is now documented to return either a Title object or null, whereas it seems to return a *string* or null.
  • getActionFrom is now documented to return an Action object, but it also looks like it returns a string.
#Comment by Brion VIBBER (talk | contribs)   19:04, 23 May 2011

These functions were added in a revision that's now been reverted, so they no longer exist on trunk.

Status & tagging log