Page MenuHomePhabricator

Mechanism for loading site wide JavaScript libraries
Closed, DeclinedPublic

Description

Currently you cannot load anything before gadgets. This should load libraries and scripts that only use jquery, wikibits and other basic MW scripts.

I think the best mechanism would be gadgets based. By that I mean to load scripts defined on a special page like this:

  1. some_lib.js
  2. other-lib.js{*}
  3. Some AdvancedEditing Stuff.js {user.autoconfirmed,edit,ns[0]}
  4. ForSpecialOnSpecial.js{user.bureaucrat,ns.Special,user.sysop}

All scripts would be loaded in order given. The scripts would have a common prefix e.g. "MediaWiki:Lib-".

Parsing:
/[\r\n]# *([^|{}]+) *\{(.+)\}/

  • $1 - script page name without the prefix
  • $2 - options (CSV, order not important)

Options:

  • "*" - for all on all pages (default)
  • edit - only when editing
  • user.groupname - only for users with given
  • ns[name space number]
  • ns.CanonicalNamespaceName

Additionally a section for scripts to be loaded without parsing with RL would be nice. Something like:

Load == <-- optional section heading

#stdlib

Minification off == <-- case insensitive section name

  1. SomethingSpecialAndVerySpecific
  2. http://maybe.even.external/script.js?

Would be great to have built into RL.


Version: unspecified
Severity: enhancement
See Also:
https://bugzilla.wikimedia.org/show_bug.cgi?id=27488

Details

Reference
bz27771

Event Timeline

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

happy.melon.wiki wrote:

As I said on bug27488, this is *not* what is actually wanted. All of this implementation minutiae belongs in backend code, not frontend. The solution to modules loading out of order is not to return to each wiki painstakingly ordering its scripts, it's to fully leverage RL's ability to let you say "script X requires script Y; make it happen".

We do need a UI interface for adding user-defined resources to be loaded through ResourceLoader. But that should be a proper special page interface to a dedicated database table, not yet another parsing-an-interface-message-in-a-unique-way hack.

(In reply to comment #1)

As I said on bug27488, this is *not* what is actually wanted. All of this
implementation minutiae belongs in backend code, not frontend. The solution to
modules loading out of order is not to return to each wiki painstakingly
ordering its scripts, it's to fully leverage RL's ability to let you say
"script X requires script Y; make it happen".

We do need a UI interface for adding user-defined resources to be loaded
through ResourceLoader. But that should be a proper special page interface to
a dedicated database table, not yet another
parsing-an-interface-message-in-a-unique-way hack.

Please note that this is targeted primarily at Wikimedia sites where you don't have access to PHP and, to my knowledge, you don't have any way to say "script X requires script Y; make it happen" with RL.

happy.melon.wiki wrote:

(In reply to comment #2)

Please note that this is targeted primarily at Wikimedia sites where you don't
have access to PHP and, to my knowledge, you don't have any way to say "script
X requires script Y; make it happen" with RL.

Precisely, I'm saying that a solution to *that* missing functionality is what you *actually* need.

(In reply to comment #2)

Please note that this is targeted primarily at Wikimedia sites where you don't
have access to PHP and, to my knowledge, you don't have any way to say "script
X requires script Y; make it happen" with RL.

You can specify dependencies for gadgets like * Gadgetname [ResourceLoader|dependencies=foo.bar, bar.foo]|Gadgetname.js|Gadgetname|.css

Essentially, you're requesting a combination of bug 13742 (Allow for gadgets to be turned on by default) and bug 12211 (Show some gadgets only for admins) and so on. I've worked in this direction today, meanwhile I think this bug is a duplicate.

(In reply to comment #4)

(In reply to comment #2)

Please note that this is targeted primarily at Wikimedia sites where you don't
have access to PHP and, to my knowledge, you don't have any way to say "script
X requires script Y; make it happen" with RL.

You can specify dependencies for gadgets like * Gadgetname
[ResourceLoader|dependencies=foo.bar, bar.foo]|Gadgetname.js|Gadgetname|.css

Could you give a more specific example on how would I use that in JS? Let's say I have a script User:Name/script.js that needs MediaWiki:Library.js.

Essentially, you're requesting a combination of bug 13742 (Allow for gadgets to
be turned on by default) and bug 12211 (Show some gadgets only for admins) and
so on. I've worked in this direction today, meanwhile I think this bug is a
duplicate.

No, because this is not only for gadgets. Not all gadgets end up implemented as actual Gadgets.

If the thing you depend on is a ResourceLoader module, you can use:

mediaWiki.loader.using( 'modulename', function() { code here } );

Currently modules can't be defined on-wiki other than as gadgets, but that's something that we'll tackle soon in our RL 2.0 push.

content hidden as private in Bugzilla

Libs:

MediaWiki:Gadget-Libs-Foobar.js

window.Foobar { lib: true, myLib: 'awesome' };

Gadget dependancies (currently possible):

MediaWiki:Gadget-Gadgetname.js

GadgetName = {

init: function(){
 $( Foobar.buildCool ).appendTo( 'body' ).tabs();

}
}
mw.loader.using( ['ext.gadget.Libs-Foobar', 'jquery.ui.tabs'], GadgetName.init );

Gadget dependancies (soon will be or already deployed):

    1. MediaWiki:Gadgets-definitions
  • Gadgetname[ResourceLoader][dependancies:ext.gadget.Libs-Foobar|jquery.ui.tabs]|Gadgetname.js
    1. MediaWiki:Gadget-Gadgetname.js

GadgetName = {

init: function(){
 $( Foobar.buildCool ).appendTo( 'body' ).tabs();

}
}
$( GadgetName.init );

Load by default: pending (bug 13742)
Show only for certain user rights: [rights:block] fixed (bug 12211)
Dependancies: fixed (via mw.loader.using, or via [dependancies:] r76639 ) -

(In reply to comment #6)

If the thing you depend on is a ResourceLoader module, you can use:

mediaWiki.loader.using

Gadget's [dependencies:] is passed as second argument to
ResourceLoaderWikiModule (via extending GadgetResourceLoaderModule class), so
either way the modules must be registered in ResourceLoader, which makes sense.

(In reply to comment #8)

Load by default: pending (bug 13742)

I can do it in 15 minutes, however I need wider discussion.

Marking as WORKSFORME, this bug us obsolete as basically everything it is about is either invalid, obsolete or implemented through other tickets:

  • Loading by default: done (bug 13742)
  • Show only for certain user rights: (e.g. [rights:block]) done (bug 12211)
  • Dependencies ("script X needs Y, do it") done (ResourceLoader in 1.17 for core/extensions, Gadgets as of 1.18)

For user scripts: No, but as explained else where "user scripts" technically don't exist anywhere in the system.

  • Short-term solution ("now"): Use mw.loader.using( .., callback );
  • Long-term solution ("Gadgets 3.0"): Ability for users to create their own gadgets that do not require admin rights and are only visible to them.

For the record, some wikis are using the current version of the Gadgets extension to create "libraries" (such as [[commons:MediaWiki:Gadget-libAPI.js]] or [[pl:MediaWiki:Gadget-lib-toolbar.js]]), which can be added as dependencies to any gadget on its definition (or to "user script", via mw.loader.using).

For other examples, see the first few lines of any of these:

(those items marked with "rights=hidden")

Thanks, that's a good example.

In Gadgets 2.0 those can use [hidden] instead of [rights=hidden] (which uses the work-around that there is no user right literally named "hidden" and as such nobody has the right to see the gadget). But that's just a convenience. Using libraries and dependencies the way as described in comment 12 is exactly how it should be done :)