Page MenuHomePhabricator

Add support for relative paths to mw.loader.load
Closed, ResolvedPublic

Description

After I openned the page
https://pt.wikipedia.org/wiki/MediaWiki:Edittools.js
on Google Chrome (16.0.912.77 m) and typed the commands below I got the following results:

typeof EditTools
"undefined"
mw.loader.load(mw.config.get( 'wgScript' ) + '?title=MediaWiki:Edittools.js&action=raw&ctype=text/javascript&nocache=005');
true
typeof EditTools
"undefined"
importScriptURI(mw.config.get( 'wgScript' ) + '?title=MediaWiki:Edittools.js&action=raw&ctype=text/javascript&nocache=005');
<script src=​"/​w/​index.php?title=MediaWiki:​Edittools.js&action=raw&ctype=text/​javascript&nocache=005" type=​"text/​javascript">​</script>​
typeof EditTools
"object"

So, although mw.loader.load is supposed to be a replacement for importScriptURI, it doesn't woks in every case where importScriptURI worked.

For comparison, notice that it works if I use the full URL like this:

typeof EditTools
"undefined"
mw.loader.load('https://pt.wikipedia.org/w/index.php?title=MediaWiki:Edittools.js&action=raw&ctype=text/javascript&nocache=005')
true
typeof EditTools
"object"

or a protocol relative URL like this:

typeof EditTools
"undefined"
mw.loader.load('//pt.wikipedia.org/w/index.php?title=MediaWiki:Edittools.js&action=raw&ctype=text/javascript&nocache=005')
true
typeof EditTools
"object"

See also

Details

Reference
bz34036
ReferenceSource BranchDest BranchAuthorTitle
repos/sre/miscweb/design-style-guide!4archivemasterdaniArchive repo
Customize query in GitLab

Event Timeline

bzimport raised the priority of this task from to Low.Nov 22 2014, 12:09 AM
bzimport set Reference to bz34036.
bzimport added a subscriber: Unknown Object (MLST).

Created attachment 9924
Proposed change to resources/mediawiki/mediawiki.js

I believe a change like this would be enough.

Attached:

mw.loader.load, as documented takes one ore more modules or a full url.

Relative urls do not work, this is not a regression as is never worked.

importScriptURI is still an unvalidated, raw way of adding a <script> tag for whatever is passed in. That hasn't changed. If some script is using a relative path to index.php and using importScriptURI, then that script will continue to work exactly as is. No regression.

mw.loader.load can be used if you decide to migrate your script. In that case one should make sure that the url is complete, this is a requirement of mw.loader.loader because the path to which the url is relative to is dynamic because the script will be executed from load.php instead of where you write it.

So in the case of index.php, you'd prefix it with wgServer.

I don't think we should be adding exceptions for index.php. What about api.php, load.php, or extension files and resources ? There is need to add exceptions for everything, a full url will work.

(In reply to comment #3)

For the record, this behavior made (at least) two more victims since then:

No, this behaviour hasn't made any victims. No where does it nor has it ever said that mw.loader.load supports relative paths. Writing code that does will fail, and that is, with all due respect, a mistake by the writer.

If you expect a function to do something it doesn't, the function is most certainly not at fault unless the function previously met your expectations if documentation suggests so.

Please read the documentation and don't write code without testing that it works :-)

Krinkle changed the task status from Declined to Resolved.Mar 25 2016, 10:46 PM

Feature was added in b284645180bbe62d53a7d5494413deb5a847bd6e. Shipped in MediaWiki 1.26.0.