},
/**
- * 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];
}
}
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' );
});