Adding support for query string params to mw.util.wikiGetlink
authorkaldari <rkaldari@wikimedia.org>
Wed, 11 Sep 2013 17:52:14 +0000 (10:52 -0700)
committerkaldari <rkaldari@wikimedia.org>
Wed, 11 Sep 2013 20:48:52 +0000 (13:48 -0700)
This will enable us to easily migrate MobileFrontend from
using M.pageApi.getPageUrl (mobile custom version) to
mw.util.wikiGetlink.

Includes a unit test.

Change-Id: I5224a0910a822f1c3b1b34f505dbcdf879052b39

resources/mediawiki/mediawiki.util.js
tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js

index 071a52b..acd2f07 100644 (file)
                 * Get the link to a page name (relative to `wgServer`),
                 *
                 * @param {string} str Page name to get the link for.
+                * @param {Object} params A mapping of query parameter names to values,
+                *     e.g. { action: 'edit' }. Optional.
                 * @return {string} Location for a page with name of `str` or boolean false on error.
                 */
-               wikiGetlink: function ( str ) {
-                       return mw.config.get( 'wgArticlePath' ).replace( '$1',
+               wikiGetlink: function ( str, params ) {
+                       var url = mw.config.get( 'wgArticlePath' ).replace( '$1',
                                util.wikiUrlencode( typeof str === 'string' ? str : mw.config.get( 'wgPageName' ) ) );
+                       if ( params && !$.isEmptyObject( params ) ) {
+                               url += url.indexOf( '?' ) !== -1 ? '&' : '?';
+                               url += $.param( params );
+                       }
+                       return url;
                },
 
                /**
index e867369..f2676d7 100644 (file)
@@ -17,7 +17,7 @@
                assert.equal( mw.util.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
        } );
 
-       QUnit.test( 'wikiGetlink', 3, function ( assert ) {
+       QUnit.test( 'wikiGetlink', 4, function ( assert ) {
                // Not part of startUp module
                mw.config.set( 'wgArticlePath', '/wiki/$1' );
                mw.config.set( 'wgPageName', 'Foobar' );
 
                href = mw.util.wikiGetlink();
                assert.equal( href, '/wiki/Foobar', 'Default title; Get link for current page ("Foobar")' );
+
+               href = mw.util.wikiGetlink( 'Sandbox', { action: 'edit' } );
+               assert.equal( href, '/wiki/Sandbox?action=edit',
+                       'Simple title with query string; Get link for "Sandbox" with action=edit' );
        } );
 
        QUnit.test( 'wikiScript', 4, function ( assert ) {