Last modified: 2013-10-31 11:52:18 UTC

Wikimedia Bugzilla is closed!

Wikimedia migrated from Bugzilla to Phabricator. Bug reports are handled in Wikimedia Phabricator.
This static website is read-only and for historical purposes. It is not possible to log in and except for displaying bug reports and their history, links might be broken. See T32803, the corresponding Phabricator task for complete and up-to-date bug report information.
Bug 30803 - mw.util.wikiGetlink should allow query parameter passing
mw.util.wikiGetlink should allow query parameter passing
Status: RESOLVED FIXED
Product: MediaWiki
Classification: Unclassified
JavaScript (Other open bugs)
unspecified
All All
: Normal enhancement (vote)
: 1.22.0 release
Assigned To: Krinkle
: patch, patch-reviewed
: 34743 (view as bug list)
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-09-07 17:32 UTC by darklama
Modified: 2013-10-31 11:52 UTC (History)
6 users (show)

See Also:
Web browser: ---
Mobile Platform: ---
Assignee Huggle Beta Tester: ---


Attachments
patch for passing query string (18.72 KB, patch)
2011-09-07 17:32 UTC, darklama
Details
patch to allow passing of query string (1.21 KB, patch)
2011-09-07 17:40 UTC, darklama
Details
clone params, use wgActionPaths, mark page as optional, and decode titles (2.54 KB, patch)
2011-09-09 17:39 UTC, darklama
Details

Description darklama 2011-09-07 17:32:27 UTC
Created attachment 9023 [details]
patch for passing query string

mw.util.wikiGetlink should allow query parameters to be passed
and thus effect the relative URL created.

Examples:

mw.util.wikiGetlink({
  title: 'Main Page',
  action: 'edit'
});

mw.util.wikiGetlink( "Main Page", {
  action:
})

Both return: /w/index.php?title=Main_Page&action=edit

While:

mw.util.wikiGetlink( "Main Page" );
mw.util.wikiGetlink({ title: "Main Page" })

Both return "/wiki/Main_Page"

Patch included.
Comment 1 darklama 2011-09-07 17:35:39 UTC
Comment on attachment 9023 [details]
patch for passing query string

--- mediawiki.util.js	2011-09-07 11:10:17.000000000 -0500
+++ mediawiki-new.util.js	2011-09-07 12:04:26.000000000 -0500
@@ -124,12 +124,24 @@
 		/**
 		 * Get the link to a page name (relative to wgServer)
 		 *
-		 * @param str string Page name to get the link for.
-		 * @return string Location for a page with name of 'str' or boolean false on error.
+		 * @param page {String} Page name to get the link for.
+		 * @param [params] {Object} Parameters to pass with page.
+		 * @return {String} Relative URL for page.
 		 */
-		'wikiGetlink' : function( str ) {
-			return mw.config.get( 'wgArticlePath' ).replace( '$1',
-				this.wikiUrlencode( str || mw.config.get( 'wgPageName' ) ) );
+		'wikiGetlink' : function( page, params ) {
+			if ( $.isPlainObject( page ) ) {
+				params = page;
+				page = params.title || mw.config.get( 'wgPageName' );
+			} else if ( !page ) {
+				page = mw.config.get( 'wgPageName' );
+			}
+			if ( 'title' in params ) {
+				delete params.title;
+			}
+			if ( $.isEmptyObject( params ) ) {
+				return mw.config.get( 'wgArticlePath' ).replace( '$1', this.wikiUrlencode( page ) );
+			}
+			return mw.config.get( 'wgScript' ) + '?title=' + this.wikiUrlencode( page ) + $.param( params );
 		},
 
 		/**
Comment 2 darklama 2011-09-07 17:40:09 UTC
Created attachment 9024 [details]
patch to allow passing of query string

accidentally included whole file rather than patch first time, and patch edit added a comment instead of what I expected.
Comment 3 Brion Vibber 2011-09-08 23:11:23 UTC
Avoid modifying the given params map directly; the caller might be expecting to reuse it on multiple calls etc.

Looks like the code makes the page parameter optional, but only params is marked as such -- probably should list [page] in the doc comments too.

If allowing parameters such as other actions, $wgActionPaths may apply to canonical URL generation, which would not be picked up by the current code.
Comment 4 darklama 2011-09-09 17:39:31 UTC
Created attachment 9039 [details]
clone params, use wgActionPaths, mark page as optional, and decode titles

Marked page as optional. Cloned params so changes doesn't
effect the original. Used wgActionPaths for URL generation
when possible. Added rawurldecode and wikiUrldecode for use
in URL generation.
Comment 5 Krinkle 2011-09-14 10:22:28 UTC
Assigning to myself to review and commit later.

Note to self:
* Base could have a ? already (see wfAppendQuery)
* Add tests for cases where base has ?, as well as for action paths on combination with only passing title, only action, only extra query, and combinations.
* Perhaps possible without needing a decode function
Comment 6 darklama 2011-09-18 17:39:56 UTC
(In reply to comment #5)
> Assigning to myself to review and commit later.
> 
> Note to self:
> * Base could have a ? already (see wfAppendQuery)
> * Add tests for cases where base has ?, as well as for action paths on
> combination with only passing title, only action, only extra query, and
> combinations.
> * Perhaps possible without needing a decode function

I think assuming "?" at the end of page means a query string is an error.
I believe wikiGetlink is intended to expand the page parameter in basically
the same way as [[Page]] does, which includes encoding "?". If that is not
done than wikiGetlink won't be able to be used for pages which include a
"?" in their name.

Though come to think of it, I didn't check for section anchors. Those need
to be encoded differently. A check for the case where an anchor without
a title is used is needed too.
Comment 7 Sumana Harihareswara 2011-11-10 02:11:31 UTC
darklama, thanks for the patch.  Adding "need-review" so that other developers know this patch needs review, in case they have time before Krinkle does.  And darklama, if you have time to revise and update your patch in response to the critique Krinkle has already given, we would welcome that as well.  Thanks!
Comment 8 Sumana Harihareswara 2012-05-25 02:56:54 UTC
darklama, thanks again for the patch.  After you revise your patch, are you interested in using developer access to directly suggest it into our Git source control system?  That might get more code review and faster.

https://www.mediawiki.org/wiki/Developer_access

https://www.mediawiki.org/wiki/Git/Workflow#How_to_submit_a_patch
Comment 9 Krinkle 2012-05-25 03:26:34 UTC
Looking at this patch again, I think this is becoming too complex for a simple utility function that gets the url of a page name.

There is handling for all of this already in the mw.Uri module and also integrated with the mw.Title module. mw.util is a arbitrary/simple utilities, of which most really belong in a separate module. Adding features like this means re-inventing wheels that have already been polished in other modules.

The Uri and Title modules aren't all that big but do their job really well (including handling for hash fragments, which you mentioned), so if you need something like this in your code, I'd say just use those modules directly and be done with it.

I see your patch also contains handling for action paths, which is nice. I'm not sure if mw.Title / mw.Uri support that right now. If it makes sense to have there, that would be great to have!
Comment 10 darklama 2012-05-26 17:18:52 UTC
(In reply to comment #9)
> There is handling for all of this already in the mw.Uri module and also
> integrated with the mw.Title module. mw.util is a arbitrary/simple utilities,
> of which most really belong in a separate module. Adding features like this
> means re-inventing wheels that have already been polished in other modules.
>
> The Uri and Title modules aren't all that big but do their job
> really well (including handling for hash fragments, which you
> mentioned), so if you need something like this in your code, I'd
> say just use those modules directly and be done with it.

The mw.Uri module handles parsing and proper encoding of URIs that is
compliant with the URI spec. However Titles are allowed to include "?"
which must be encoded to avoid interpretation as a query string when
converted into URI. Titles are not allowed to contain some character
which are valid in a URI, like "[" and "|". Hashes seem to be encoded
differently from the URI spec as well. mw.Uri and mw.Title operate in
isolation without dependency or understanding of the needs of the other.
As a consequence of this isolation, none of these things are handled by
mw.Uri or mw.Title. mw.Title depends on mw.util.wikiGetlink for proper
conversion from Title or URI, but there are bugs and missing features.

> I see your patch also contains handling for action paths, which is nice.
> I'm not sure if mw.Title / mw.Uri support that right now. If it makes
> sense to have there, that would be great to have!

mw.Title and mw.Uri does not support that right now, just as they do not
support conversation between each other.
Comment 11 Krinkle 2012-05-26 21:15:24 UTC
The relevance of the question mark presence is indeed not related to the page name itself. However it is related to the url formatting. Because wgArticlePath could look like this:
"/w/index.php?title=$1"

Then an additional query string has to start with `&` instead of `?`. Any question mark in the title will be url encoded so a string search for that shouldn't be match the page name.

(In reply to comment #10)
> mw.Title and mw.Uri does not support that right now, just as they do not
> support conversation between each other.

That sounds like a good place to start. There is something to be said about keeping mw.Uri somewhat re-usable, so the special casing for 'title' and 'action' should ideally be handled through some kind of hook. ("paramHooks" callbacks?) so that it is easy to extend or disable in the future.

Then from there we can make mw.Title use mw.Uri for url construction (returning an mw.Uri object, which users of mw.Title can optionally convert to String or then call a method on to add query parameters.
Comment 12 Krinkle 2012-12-31 18:29:20 UTC
(Reviewing attachment 9039 [details])
>--- mediawiki.util.js
>+++ mediawiki.util.js
>@@ -110,7 +110,17 @@
> 		},
> 
> 		/**
>-		 * Encode page titles for use in a URL
>+		 * Decode the string like PHP's rawurldecode
>+		 *
>+		 * @param str {String} String to be decoded
>+		 */
>+		'rawurldecode' : function( str ) {
>+			str = ( str + '' ).toString();
>+			return decodeURIComponent( str );


This doesn't make sense. This is triple conversion to string:
* + ''
* toString
* [internal] decodeURIComponent casts value `str = String(value)`

Suggest to use String(str) (not `new String`) instead of '' and/or toString
So just `return decodeURIComponent( String( str ) );`

>+		},
>+
>+		/**
> 		 * Get the link to a page name (relative to wgServer)
> 		 */
>-		'wikiGetlink' : function( str ) {
>-			return mw.config.get( 'wgArticlePath' ).replace( '$1',
>-				this.wikiUrlencode( str || mw.config.get( 'wgPageName' ) ) );
>+		'wikiGetlink' : function( page, params ) {
>+			var url, query;
>+			if ( $.isPlainObject( page ) ) {
>+				params = page;
>+				page = params.title || mw.config.get( 'wgPageName' );
>+			} else if ( !page ) {
>+				page = mw.config.get( 'wgPageName' );
>+			}
>+			query = $.extend( params || {} , {} );
>+			if ( 'title' in query ) {
>+				delete query.title;
>+			}
>+			if ( $.isEmptyObject( query ) ) {
>+				return mw.config.get( 'wgArticlePath' ).replace( '$1', this.wikiUrldecode( page ) );

Why is this decoding the page parameter? This is a url, it should be encoded.

>+			}
>+			if ( 'action' in query ) {
>+				url = mw.config.get( 'wgActionPaths', {} )[ query.action ];
>+				if ( url ) {
>+					url = url.replace( '$1', this.wikiUrldecode( page ) ) + '?';

Why is this decoding the page parameter? It should be encoded instead of decoded.

>+					delete query.action;
>+				}
>+			}
>+			if ( !url ) {
>+				url = mw.config.get( 'wgScript' ) + '?title=' + this.wikiUrlencode( page ) + '&';
>+			}
>+			return url + $.param( query );
> 		},
> 
> 		/**

* The wikiUrldecode method appears to be unneeded.
* Needs unit tests.
Comment 13 Krinkle 2013-10-31 11:51:38 UTC
Change-Id: I5224a0910a822f1c3b1b34f505dbcdf879052b39
Comment 14 Krinkle 2013-10-31 11:52:18 UTC
*** Bug 34743 has been marked as a duplicate of this bug. ***

Note You need to log in before you can comment on or make changes to this bug.


Navigation
Links