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 Normal.Nov 21 2014, 11:14 PM
bzimport set Reference to bz25845.
bzimport added a subscriber: Unknown Object (MLST).
Schnark created this task.Nov 9 2010, 9:39 AM

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)

Rical added a subscriber: Rical.Apr 20 2015, 11:35 PM
kaldari reopened this task as Open.May 4 2015, 11:33 PM

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).

He7d3r added a comment.EditedMay 5 2015, 9:55 AM

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.

Ltrlg added a subscriber: Ltrlg.May 5 2015, 10:04 PM
Izno added a subscriber: Izno.Nov 3 2015, 6:36 PM
Restricted Application added a subscriber: Aklapper. · View Herald TranscriptNov 3 2015, 6:36 PM
Danny_B added a subscriber: Danny_B.Feb 7 2016, 7:27 PM
Krinkle closed this task as Declined.Nov 17 2016, 1:06 AM

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.

Rical removed a subscriber: Rical.Jul 21 2017, 9:52 AM