Page MenuHomePhabricator

Support loading wiki pages through mediaWiki.loader.load()
Closed, DeclinedPublic

Description

According to http://www.mediawiki.org/wiki/ResourceLoader/JavaScript_Deprecations#wikibits.js importScript should be replaced by mediaWiki.loader.load in the future, but at the moment it's not a good substitute. If you want a script to be loaded both via http or https depending you have to replace

importScript('User:Foo/bar.js');

by

mediaWiki.loader.load(mediaWiki.config.get('wgServer') + mediaWiki.config.get('wgScript') + '?title=User:Foo/bar.js&action=raw&ctype=text/javascript');

Another user suggested the syntax

mediaWiki.loader.load('[[User:Foo/bar.js]]');

This has two advantages: It doesn't conflict with names of modules and it generates an entry in "What links here".


Version: 1.17.x
Severity: enhancement

Details

Reference
bz25845

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 21 2014, 11:14 PM
bzimport set Reference to bz25845.
bzimport added a subscriber: Unknown Object (MLST).

Created attachment 7897
Proposed patch

I added a patch by [[User:PerfektesChaos]].

Attached:

  • Bug 27475 has been marked as a duplicate of this bug. ***

(In reply to comment #0)

Another user suggested the syntax

mediaWiki.loader.load('[[User:Foo/bar.js]]');

This has two advantages: It doesn't conflict with names of modules and it
generates an entry in "What links here".

I would support this syntax, because of the second advantage.

(In reply to comment #0)

According to
http://www.mediawiki.org/wiki/ResourceLoader/JavaScript_Deprecations#wikibits.js
importScript should be replaced by mediaWiki.loader.load in the future, but at
the moment it's not a good substitute. If you want a script to be loaded both
via http or https depending you have to replace

importScript('User:Foo/bar.js');

by

mediaWiki.loader.load(mediaWiki.config.get('wgServer') +

mediaWiki.config.get('wgScript') +
'?title=User:Foo/bar.js&action=raw&ctype=text/javascript');

Another user suggested the syntax

mediaWiki.loader.load('[[User:Foo/bar.js]]');

This has two advantages: It doesn't conflict with names of modules and it
generates an entry in "What links here".

No brackets please. If there is any bracket, we may be in need of lines of script for eliminating brackets. I think distinguishment with former importScriptURI can be done by protocol. Best regards.

mdale wrote:

[[PageToImport]] could work or they could be namespaced somehow. The important part is they have a way to transition into normal named modules. The patch is a good start.

A followup patch should have the page assets loaded through the resource loader, so you can load multiple pages in a single request, they get minified when not in debug mode, and [[myPage.css]] has auto LTR css transforms / optimizations etc.

Also would be nice to support a simple interface or SpecialPage for defining what page resources ( and associated messages / dependencies for thous resources ) are globally used on a given wiki, this way their versions numbers can be included in the start-up module definitions. This would probably fit best within the "gadgets" extension and expand the gadgets concept to include "on-by-default" gadgets.

(In reply to comment #5)
[snip]

This would probably fit
best within the "gadgets" extension and expand the gadgets concept to include
"on-by-default" gadgets.

About this, see Bug 13742.

aokomoriuta wrote:

How about using different method name from "load"?
For example:

  • mw.loader.loadScript(page), same as importScript(page) does
  • mw.loader.loadStyleSheet(page) same as importStyleSheet(page) does

mdale wrote:

The problem with a different method is that you move the page loads out of the resource loader loading system. This may be fine for individual page loads and prototyping... For obvious reasons we should have user-space javascript loading through the resource loader.

aokomoriuta wrote:

(In reply to comment #8)

The problem with a different method is that you move the page loads out of the
resource loader loading system. This may be fine for individual page loads and
prototyping... For obvious reasons we should have user-space javascript loading
through the resource loader.

Forgive me, let me make comment #7 clear.

mw.loader.loadScript converts given pagename to URL and uses mw.loader.load internally.
It's just to make a helper method, not to rename.

Or must we use mw.loader.load directly otherwise the page loads get moved?

  • Bug 27535 has been marked as a duplicate of this bug. ***

(In reply to comment #10)

  • Bug 27535 has been marked as a duplicate of this bug. ***

And unduped. bug 27535 requests a broader feature where wiki pages are and/or user scripts are treated as fully-fledged modules.

mw.loader.load either loads modules (combined/minified/localized) or external links (seperate, unmodified).

I think adding support to load wiki-pages as external links is a bad idea and ineffecient as well (since they'd be all seperate and unminified). Instead they should be loaded as modules:

ie. mw.loader.load( ['gadget.Foobar', 'user.Foo.bar'] );

Support to allow normal wiki pages (other then MediaWiki:Common.js and User:name/vector.js etc.) to be part of a module is bug 27535.

Support to register them from JavaScript as well is bug 27561.

Proposing to close this bug as WONTFIX.

Reply to Comment #12:
Requiring the user to register user scripts as modules seems a bit onerous to me. I think we should keep this simple for end users. Otherwise, they are going to keep using importScript for the rest of time.

Couldn't we just detect for a parameter of type /(.*)\:(.*)\.(js|css)/i
and either convert it into an external link or construct a module on behalf of the user?

Then people could easily migrate...
importScript('User:Foo/bar.js');
...to...
mediaWiki.loader.load('User:Foo/bar.js');

(In reply to comment #13)

Reply to Comment #12:
Requiring the user to register user scripts as modules seems a bit onerous to
me. I think we should keep this simple for end users. Otherwise, they are going
to keep using importScript for the rest of time.

Couldn't we just detect for a parameter of type /(.*)\:(.*)\.(js|css)/i
and either convert it into an external link or construct a module on behalf of
the user?

Then people could easily migrate...
importScript('User:Foo/bar.js');
...to...
mediaWiki.loader.load('User:Foo/bar.js');

I couldn't agree more that such a short cut would be ideal, and in fact I believe it should be implemented. However in should not do what mw.legacy.importScript did (loading index.php&title=..&action=raw). Instead it would register/load the module on-demand.

ie. load.php?wikiscripts=User:Foo/bar.js&wikistyles=User:Foo/bar.css|MediaWiki:VectorAltenative.css (or just &wikipages=).

It doesn't even depend on bug bug 27561 being fixed. See also comments on bug 27535.

sumanah wrote:

Michael M., thanks for submitting the patch. Adding the need-review keyword since it seems to me the patch hasn't quite been reviewed -- if you feel you and the author have gotten sufficient feedback, feel free to replace "need-review" with "reviewed". Thanks.

There should be some way to import a minified version of [[MediaWiki:SomePage.js]] or [[User:AnotherPage.js]], and importScript currently doesn't do that. I don't know which bug needs to be fixed in order to have this functionality (bug 27561? bug 27535?).

I believe such a feature is necessary if we want to avoid loading the full code of a script in the case of certain conditions are met. For example, if we create a long script which we want to be loaded only on [[ThisPage]], it seems desirable to be able to create a loader script with something like this:

if ( mw.config.get( 'wgPageName') === 'ThisPage' ) {

importMinifiedScript( 'FromAnotherPage.js' );

}

Then, the JavaScript 'FromAnotherPage.js' wouldn't be loaded unless it is needed.

See also [[mw:Thread:Talk:ResourceLoader/Migration guide (users)/Minifying central gadgets]].

mw.loader.load has 2 purposes:

  • On-demand loading of a module registered in ResourceLoader
  • On-demand loading of a raw url

I think this is a solid system. And loading wiki pages is not a useful feature. Instead we should focus on implementing the ability for users to create their own complete ResourceLoader modules within their own space. Allowing raw loading of wiki pages (although minified) is barely any improvement over the current system and doesn't address any of the following issues, which have on the contrary been solved in ResourceLoader

  • Proper caching
  • Not loading the same content twice (using dependency lists of modules by a name instead of direct loading of a script page)
  • Concatenation
  • Translation
  • RTL-support
  • more..

The original request "Include the functionality of importScript into mediaWiki.loader.load" is in my opinion a WONTFIX. Until a proper solution exists, please use importScript() (or if you need a callback, use jQuery.getScript(url, callback).

The underlying need however is not a wontfix, but a valid need. Instead of addressing that by adding support for direct loading of a wiki page to mw.loader.load is bug 34958 (creation of ResourceLoader modules on a user level)

I'm reopening this bug since all the suggested alternative bugs have been closed as INVALID or WONTFIX (T29535, T29561, T29281) and no alternative solution has emerged in the 4 years since this bug was created (AFAIK).

There is also this request: T36958: User-level gadget repositories. It is still open, but we had no progress there in the last three years other than the indication that the formatting for defining user modules should be the same as in the future Gadgets 2.0 (T31272), i.e. JSON instead of wikitext.

Closing in favour of T36958.

For the moment, mw.loader.load() with action=raw remains the status quo for loading extra pages in a user script.

In addition, there is also the ability to load gadgets from other wikis with ResourceLoader (which was created 2 years ago as part of RL2). So for use cases of loading gadgets from other wikis, this is no longer needed.

I request this wontfix be reconsidered. I think I make some decent arguments in ticket T313156 that a dedicated function for this would improve readability, improve conciseness, and reduce bugs when trying to manually write these. It'd make user script files easier to read and write. It could be implemented as something like mw.loader.importScript(pageName);

Readability example:

mw.loader.importScript('User:Anne drew Andrew and Drew/SetupAutoArchive.js');
mw.loader.importScript('User:Þjarkur/Highlight homographs in title.js');
mw.loader.importScript('User:Þjarkur/NeverUseMobileVersion.js');
mw.loader.importScript('User:Ahecht/Scripts/pageswap.js');
mw.loader.importScript('User:Ajbura/anrfc-lister.js');
mw.loader.importScript('User:Alexis Jazz/LuckyRename.js');
mw.loader.importScript('User:Ale_jrb/Scripts/csdhelper.js');
mw.loader.importScript('User:Anomie/unsignedhelper.js');
mw.loader.importScript('User:Awesome Aasim/xfdvote.js');
mw.loader.importScript('User:Bellezzasolo/Scripts/arb.js');
mw.loader.importScript('User:Bradv/Scripts/WhatLinksHereSnippets.js');
mw.loader.importScript('User:BrandonXLF/CitationStyleMarker.js');
mw.loader.importScript('User:BrandonXLF/HotDefaultSort.js');
mw.loader.importScript('User:DannyS712/copyvio-check.js'); 
mw.loader.importScript('User:DannyS712/copyvio-check.js');
mw.loader.importScript('User:DannyS712/DiscussionCloser.js');
mw.loader.importScript('User:DannyS712/Draft no cat.js');
mw.loader.importScript('User:DannyS712/EFFPRH.js');
mw.loader.importScript('User:DannyS712/SATG.js');
mw.loader.importScript('User:Enterprisey/AFCRHS.js');
mw.loader.importScript('User:Enterprisey/cv-revdel.js');
mw.loader.importScript('User:Enterprisey/delsort.js');
mw.loader.importScript('User:Enterprisey/diff-permalink.js');
mw.loader.importScript('User:Enterprisey/easy-brfa.js');
mw.loader.importScript('User:Enterprisey/offset-history-link.js');
mw.loader.importScript('User:Enterprisey/rfa-count-toolbar.js');
mw.loader.importScript('User:Enterprisey/undo-last-edit.js');
mw.loader.importScript('User:Evad37/duplinks-alt.js');
mw.loader.importScript('User:Evad37/MoveToDraft.js');
mw.loader.importScript('User:Evad37/OneClickArchiver.js');
mw.loader.importScript('User:Evad37/rater.js');
mw.loader.importScript('User:GeneralNotability/mark-locked.js');
mw.loader.importScript('User:GorillaWarfare/script/curlies.js');
mw.loader.importScript('User:Jonesey95/refill.js');
mw.loader.importScript('User:JPxG/current-switcher.js');
mw.loader.importScript('User:Kaniivel/RefConsolidate_start.js');
mw.loader.importScript('User:Kephir/gadgets/table-editor.js');
mw.loader.importScript('User:Mr. Stradivarius/gadgets/ConfirmRollback.js');
mw.loader.importScript('User:Nardog/MoveHistory.js');
mw.loader.importScript('User:Novem Linguae/Scripts/AddTaxobox.js');
mw.loader.importScript('User:Novem Linguae/Scripts/CWWEditSummary.js');
mw.loader.importScript('User:Novem Linguae/Scripts/DetectG4G5.js');
mw.loader.importScript('User:Novem Linguae/Scripts/DetectPromo.js');
mw.loader.importScript('User:Novem Linguae/Scripts/GANReviewTool.js');
mw.loader.importScript('User:Novem Linguae/Scripts/TemplateTaxonomyAddCite.js');
mw.loader.importScript('User:Novem Linguae/Scripts/UserRightsDiff.js');
mw.loader.importScript('User:Ohconfucius/script/MOSNUM dates.js');
mw.loader.importScript('User:Pythoncoder/Scripts/voteSymbols.js');
mw.loader.importScript('user:Qwertyytrewqqwerty/DisamAssist.js');
mw.loader.importScript('User:SD0001/DYK-helper.js');
mw.loader.importScript('User:SD0001/GAN-helper.js');
mw.loader.importScript('User:SD0001/StubSorter.js');
mw.loader.importScript('User:Shubinator/DYKcheck.js');
mw.loader.importScript('User:Suffusion of Yellow/filter-highlighter.js');
mw.loader.importScript('User:Suffusion of Yellow/filterNotes.js');
mw.loader.importScript('User:Terasail/COI Request Tool.js');
mw.loader.importScript('User:Terasail/Edit Request Tool.js');
mw.loader.importScript('User:TheTVExpert/rmCloser.js');
mw.loader.importScript('User:Writ Keeper/Scripts/deletionFinder.js');

vs.

mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Anne_drew_Andrew_and_Drew/SetupAutoArchive.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Þjarkur/Highlight_homographs_in_title.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Þjarkur/NeverUseMobileVersion.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Ahecht/Scripts/pageswap.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Ajbura/anrfc-lister.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Alexis_Jazz/LuckyRename.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Ale_jrb/Scripts/csdhelper.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Anomie/unsignedhelper.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Awesome_Aasim/xfdvote.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Bellezzasolo/Scripts/arb.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Bradv/Scripts/WhatLinksHereSnippets.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:BrandonXLF/CitationStyleMarker.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:BrandonXLF/HotDefaultSort.js&action=raw&ctype=text/javascript');
importScript('User:DannyS712/copyvio-check.js');_
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:DannyS712/copyvio-check.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:DannyS712/DiscussionCloser.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:DannyS712/Draft_no_cat.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:DannyS712/EFFPRH.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:DannyS712/SATG.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Enterprisey/AFCRHS.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Enterprisey/cv-revdel.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Enterprisey/delsort.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Enterprisey/diff-permalink.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Enterprisey/easy-brfa.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Enterprisey/offset-history-link.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Enterprisey/rfa-count-toolbar.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Enterprisey/undo-last-edit.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Evad37/duplinks-alt.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Evad37/MoveToDraft.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Evad37/OneClickArchiver.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Evad37/rater.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:GeneralNotability/mark-locked.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:GorillaWarfare/script/curlies.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Jonesey95/refill.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:JPxG/current-switcher.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Kaniivel/RefConsolidate_start.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Kephir/gadgets/table-editor.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Mr._Stradivarius/gadgets/ConfirmRollback.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Nardog/MoveHistory.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Novem_Linguae/Scripts/AddTaxobox.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Novem_Linguae/Scripts/CWWEditSummary.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Novem_Linguae/Scripts/DetectG4G5.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Novem_Linguae/Scripts/DetectPromo.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Novem_Linguae/Scripts/GANReviewTool.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Novem_Linguae/Scripts/TemplateTaxonomyAddCite.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Novem_Linguae/Scripts/UserRightsDiff.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Ohconfucius/script/MOSNUM_dates.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Pythoncoder/Scripts/voteSymbols.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=user:Qwertyytrewqqwerty/DisamAssist.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:SD0001/DYK-helper.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:SD0001/GAN-helper.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:SD0001/StubSorter.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Shubinator/DYKcheck.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Suffusion_of_Yellow/filter-highlighter.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Suffusion_of_Yellow/filterNotes.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Terasail/COI_Request_Tool.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Terasail/Edit_Request_Tool.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:TheTVExpert/rmCloser.js&action=raw&ctype=text/javascript');
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Writ_Keeper/Scripts/deletionFinder.js&action=raw&ctype=text/javascript');

For local imports, call importScript(). This is not, and never was, deprecated. For cross-wiki and other less trivial use cases, call mw.loader.load().

For local imports, call importScript(). This is not, and never was, deprecated. For cross-wiki and other less trivial use cases, call mw.loader.load().

@Krinkle. Thanks for the reply. People speak of importScript being deprecated and us needing to change to mw.loader.load.

Perhaps there was a misunderstanding somewhere.

This may be the source of confusion;
https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/51155b0dd6e8f98860a10bda96b982b6839110dc

Looks like importScript used to be loaded in a deprecated code path that was never loaded on mobile and moved. Looks like this only changed this month.