MediaWiki URL Parameters Without Values

The query a URL consists of key-value pairs separated by the & symbol and associated with = .

I always used the jQuery $.param() function to url the query strings because I found that my code is more readable and supported.

In the last couple of days, I find myself in the MediaWiki API, but when clearing my working prototype with hard-coded URLs to use $.param() I noticed that some MediaWiki APIs include query parameters with keys, but not values!

api.php ? action=query & titles=Main%20page & redirects

Pay attention to the &redirects part, which does not accept the value.

jQuery $.param() takes an object, and since objects consist only of key-value pairs, it is impossible to pass an object in which one element has a key but does not matter.

This is so that I assumed that I could just pass some value, like null or undefined or 0 , but it seems that they all work the same way. I find this unexpected, and I could not find anything in the MediaWiki API documentation about the reasons for this.

It’s good that in my case it’s easy to work by building the URL string manually. My question is: β€œIs this a quirk in the MediaWiki API? Or a quirk in the design of URL coding? Where should I read to understand the arguments of URL-encoded parameters that have no associated values?

+6
source share
3 answers

Just asking this question and getting some feedback from others, I also suggested digging further.

The Wikipedia Query String in the Web Forms section reads:

  • Each pair of field values ​​is separated by an equal sign. An equal sign may be omitted if the value is an empty string.

The query string is defined in section 3.4 of RFC 3986 , but in fact key-value pairs are not part of the standard, and are briefly mentioned:

However, since query components are often used to transfer identifying information in the form of a key-value pair and one frequently used value is a link to another URI, it is sometimes better to use convenience to avoid percent encoding of these characters.

As you can see, nothing indicates the presence or absence of values ​​for the keys.

As for jQuery, two error messages / feature requests about this behavior have been reported over the past 15 months:

Various suggestions have been made to convert param: null and param: undefined to param or param= .

At the end, a fix was included for the next version of jQuery, 1.8, which converts both null and undefined to param= - an empty string.

This makes some sense, of course, but for the MediaWiki case, which was not mentioned in the error reports / function requests, this does not help at all:

http://en.wikipedia.org/w/api.php?action=query&titles=Main%20page&redirects=

returns

 <?xml version="1.0"?> <api> <query> <redirects> <r from="Main page" to="Main Page" /> </redirects> <pages> <page pageid="15580374" ns="0" title="Main Page" /> </pages> </query> </api> 

Summarizing:

The standards do not indicate what should be done here, leaving it to implementations. The MediaWiki API did one thing: jQuery initially missed it, and then, when it was specified, did another. It seems that both sides do not know each other.

The gap in the specification led to incompatible interpretations ... but they are easy to get around.

+3
source

Most likely what they are doing is just checking that the parameter is defined. By adding redirects to the query string, this actually says the redirect variable is true. Thus, adding redirects=0 still defines this variable, and the MediaWiki API notes that it is defined (without worrying about what value matters).

Your jQuery code just needs to add this parameter (with any value or without value) or omit it if you do not want it to be defined.

+7
source

Well, if you make a small test web server in python or ruby ​​or something else and request a URL with a query string like ?something , you will usually see the parameters go in as something=nil (or similar).

MediaWiki is written in PHP, so it can be handled a little differently, but it seems to me that you should be safe just doing $.params( { redirects: null } ) , and it just does not check the value, since it is not needed. Or just create it yourself by adding the correct line.

0
source

Source: https://habr.com/ru/post/918389/


All Articles