MediaWiki URL parameters without values

删除回忆录丶 提交于 2019-12-01 04:44:28

Most likely what they are doing is just checking that the parameter is defined. By adding redirects to the query string, that effectively is saying "redirects variable is true". So, adding redirects=0 is still defining that variable, and the MediaWiki API is noting that it is defined (not caring what the value is).

Your jQuery code will simply need to append that parameter (with any value, or no value) or omit it if you do not want it to be defined.

Just asking this question and getting some feedback from others prompted me to dig further too.

Wikipedia's "Query string" page in the section "Web forms" says:

  • The field-value pairs are each separated by an equals sign. The equals 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 the key-value pairs are not part of the standard, and are only briefly mentioned:

However, as query components are often used to carry identifying information in the form of "key=value" pairs and one frequently used value is a reference to another URI, it is sometimes better for usability to avoid percent- encoding those characters.

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

As for jQuery, it turns out that two bug reports / feature requests have been filed about this behaviour in the past 15 months:

Various proposals were made covering whether to convert param: null and param: undefined to param or param=.

In the end a fix has been included for the next release of jQuery, 1.8, which converts both null and undefined to param= - the empty string.

This makes some kind of sense of course, but for the case of MediaWiki, which was not mentioned in the bug reports / feature 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>

To sum up:

The standards don't define what should be done here, leaving it up to the implementations. The MediaWiki API did one thing, jQuery initially overlooked it, then when it was pointed out, did a different thing. The two parties don't seem to be aware of each other.

A gap in the spec has led to incompatible interpretations ... but they're not hard to work around.

Well, if you make a little test webserver in python or ruby or something and request a url with query string like ?something, generally you will see the params come in as something=nil (or similar).

MediaWiki is written in PHP, so it might be handled a little differently, but it seems to me you should be safe just doing $.params( { redirects: null } ) and it will simply not check the value since it doesn't need one. Alternatively just construct it yourself by appending the correct string.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!