/**
* Get the link to a page name (relative to `wgServer`),
*
- * @param {string|null} [str=wgPageName] Page name
+ * @param {string|null} [pageName=wgPageName] Page name
* @param {Object} [params] A mapping of query parameter names to values,
* e.g. `{ action: 'edit' }`
- * @return {string} Url of the page with name of `str`
+ * @return {string} Url of the page with name of `pageName`
*/
- getUrl: function ( str, params ) {
- var titleFragmentStart,
- url,
+ getUrl: function ( pageName, params ) {
+ var titleFragmentStart, url, query,
fragment = '',
- pageName = typeof str === 'string' ? str : mw.config.get( 'wgPageName' );
-
- // Find any fragment should one exist
- if ( typeof str === 'string' ) {
- titleFragmentStart = pageName.indexOf( '#' );
- if ( titleFragmentStart !== -1 ) {
- fragment = pageName.slice( titleFragmentStart + 1 );
- // Exclude the fragment from the page name
- pageName = pageName.slice( 0, titleFragmentStart );
- }
+ title = typeof pageName === 'string' ? pageName : mw.config.get( 'wgPageName' );
+
+ // Find any fragment
+ titleFragmentStart = title.indexOf( '#' );
+ if ( titleFragmentStart !== -1 ) {
+ fragment = title.slice( titleFragmentStart + 1 );
+ // Exclude the fragment from the page name
+ title = title.slice( 0, titleFragmentStart );
}
- url = mw.config.get( 'wgArticlePath' ).replace( '$1', util.wikiUrlencode( pageName ) );
-
- // Add query string if necessary
- if ( params && !$.isEmptyObject( params ) ) {
- url += ( url.indexOf( '?' ) !== -1 ? '&' : '?' ) + $.param( params );
+ // Produce query string
+ if ( params ) {
+ query = $.param( params );
+ }
+ if ( query ) {
+ url = title
+ ? util.wikiScript() + '?title=' + util.wikiUrlencode( title ) + '&' + query
+ : util.wikiScript() + '?' + query;
+ } else {
+ url = mw.config.get( 'wgArticlePath' ).replace( '$1', util.wikiUrlencode( title ) );
}
// Append the encoded fragment
- if ( fragment.length > 0 ) {
+ if ( fragment.length ) {
url += '#' + util.escapeId( fragment );
}
QUnit.test( 'getUrl', 4, function ( assert ) {
var title;
-
- // Config
- mw.config.set( 'wgArticlePath', '/wiki/$1' );
+ mw.config.set( {
+ wgScript: '/w/index.php',
+ wgArticlePath: '/wiki/$1'
+ } );
title = new mw.Title( 'Foobar' );
assert.equal( title.getUrl(), '/wiki/Foobar', 'Basic functionality, getUrl uses mw.util.getUrl' );
- assert.equal( title.getUrl( { action: 'edit' } ), '/wiki/Foobar?action=edit', 'Basic functionality, \'params\' parameter' );
+ assert.equal( title.getUrl( { action: 'edit' } ), '/w/index.php?title=Foobar&action=edit', 'Basic functionality, \'params\' parameter' );
title = new mw.Title( 'John Doe', 3 );
assert.equal( title.getUrl(), '/wiki/User_talk:John_Doe', 'Escaping in title and namespace for urls' );
title = new mw.Title( 'John Cena#And_His_Name_Is', 3 );
- assert.equal( title.getUrl( { meme: true } ), '/wiki/User_talk:John_Cena?meme=true#And_His_Name_Is', 'title with fragment and query parameter' );
+ assert.equal( title.getUrl( { meme: true } ), '/w/index.php?title=User_talk:John_Cena&meme=true#And_His_Name_Is', 'title with fragment and query parameter' );
} );
QUnit.test( 'newFromImg', 44, function ( assert ) {
} );
} );
- QUnit.test( 'getUrl', 12, function ( assert ) {
+ QUnit.test( 'getUrl', 13, function ( assert ) {
+ var href;
mw.config.set( {
+ wgScript: '/w/index.php',
wgArticlePath: '/wiki/$1',
wgPageName: 'Foobar'
} );
- var href = mw.util.getUrl( 'Sandbox' );
+ href = mw.util.getUrl( 'Sandbox' );
assert.equal( href, '/wiki/Sandbox', 'simple title' );
href = mw.util.getUrl( 'Foo:Sandbox? 5+5=10! (test)/sub ' );
- assert.equal( href, '/wiki/Foo:Sandbox%3F_5%2B5%3D10!_(test)/sub_', 'advanced title' );
+ assert.equal( href, '/wiki/Foo:Sandbox%3F_5%2B5%3D10!_(test)/sub_', 'complex title' );
href = mw.util.getUrl();
assert.equal( href, '/wiki/Foobar', 'default title' );
href = mw.util.getUrl( null, { action: 'edit' } );
- assert.equal( href, '/wiki/Foobar?action=edit', 'default title with query string' );
+ assert.equal( href, '/w/index.php?title=Foobar&action=edit', 'default title with query string' );
href = mw.util.getUrl( 'Sandbox', { action: 'edit' } );
- assert.equal( href, '/wiki/Sandbox?action=edit', 'simple title with query string' );
+ assert.equal( href, '/w/index.php?title=Sandbox&action=edit', 'simple title with query string' );
// Test fragments
href = mw.util.getUrl( 'Foo:Sandbox#Fragment', { action: 'edit' } );
- assert.equal( href, '/wiki/Foo:Sandbox?action=edit#Fragment', 'advanced title with query string and fragment' );
+ assert.equal( href, '/w/index.php?title=Foo:Sandbox&action=edit#Fragment', 'namespaced title with query string and fragment' );
- href = mw.util.getUrl( 'Foo:Sandbox#', { action: 'edit' } );
- assert.equal( href, '/wiki/Foo:Sandbox?action=edit', 'title with query string and empty fragment' );
+ href = mw.util.getUrl( 'Sandbox#', { action: 'edit' } );
+ assert.equal( href, '/w/index.php?title=Sandbox&action=edit', 'title with query string and empty fragment' );
+
+ href = mw.util.getUrl( 'Sandbox', {} );
+ assert.equal( href, '/wiki/Sandbox', 'title with empty query string' );
href = mw.util.getUrl( '#Fragment' );
- assert.equal( href, '/wiki/#Fragment', 'epmty title with fragment' );
+ assert.equal( href, '/wiki/#Fragment', 'empty title with fragment' );
href = mw.util.getUrl( '#Fragment', { action: 'edit' } );
- assert.equal( href, '/wiki/?action=edit#Fragment', 'epmty title with query string and fragment' );
+ assert.equal( href, '/w/index.php?action=edit#Fragment', 'epmty title with query string and fragment' );
href = mw.util.getUrl( 'Foo:Sandbox \xC4#Fragment \xC4', { action: 'edit' } );
- assert.equal( href, '/wiki/Foo:Sandbox_%C3%84?action=edit#Fragment_.C3.84', 'title with query string, fragment, and special characters' );
+ assert.equal( href, '/w/index.php?title=Foo:Sandbox_%C3%84&action=edit#Fragment_.C3.84', 'title with query string, fragment, and special characters' );
href = mw.util.getUrl( 'Foo:%23#Fragment', { action: 'edit' } );
- assert.equal( href, '/wiki/Foo:%2523?action=edit#Fragment', 'title containing %23 (#), fragment, and a query string' );
+ assert.equal( href, '/w/index.php?title=Foo:%2523&action=edit#Fragment', 'title containing %23 (#), fragment, and a query string' );
href = mw.util.getUrl( '#+&=:;@$-_.!*/[]<>\'ยง', { action: 'edit' } );
- assert.equal( href, '/wiki/?action=edit#.2B.26.3D:.3B.40.24-_..21.2A.2F.5B.5D.3C.3E.27.C2.A7', 'fragment with various characters' );
+ assert.equal( href, '/w/index.php?action=edit#.2B.26.3D:.3B.40.24-_..21.2A.2F.5B.5D.3C.3E.27.C2.A7', 'fragment with various characters' );
} );
QUnit.test( 'wikiScript', 4, function ( assert ) {