Last modified: 2012-06-20 12:32:39 UTC

Wikimedia Bugzilla is closed!

Wikimedia migrated from Bugzilla to Phabricator. Bug reports are handled in Wikimedia Phabricator.
This static website is read-only and for historical purposes. It is not possible to log in and except for displaying bug reports and their history, links might be broken. See T29771, the corresponding Phabricator task for complete and up-to-date bug report information.
Bug 27771 - Mechanism for loading site wide JavaScript libraries
Mechanism for loading site wide JavaScript libraries
Status: RESOLVED WORKSFORME
Product: MediaWiki
Classification: Unclassified
ResourceLoader (Other open bugs)
unspecified
All All
: Normal enhancement (vote)
: ---
Assigned To: Nobody - You can work on this!
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-02-27 23:49 UTC by Nux
Modified: 2012-06-20 12:32 UTC (History)
5 users (show)

See Also:
Web browser: ---
Mobile Platform: ---
Assignee Huggle Beta Tester: ---


Attachments

Description Nux 2011-02-27 23:49:31 UTC
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:
# some_lib.js
# other-lib.js{*}
# Some AdvancedEditing Stuff.js {user.autoconfirmed,edit,ns[0]}
# 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
# SomethingSpecialAndVerySpecific
# http://maybe.even.external/script.js?

Would be great to have built into RL.
Comment 1 Happy-melon 2011-04-02 17:34:53 UTC
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.
Comment 2 Nux 2011-04-02 17:56:31 UTC
(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.
Comment 3 Happy-melon 2011-04-02 20:50:57 UTC
(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.
Comment 4 Max Semenik 2011-04-02 21:03:51 UTC
(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.
Comment 5 Nux 2011-04-02 21:29:37 UTC
(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.
Comment 6 Roan Kattouw 2011-04-03 12:49:10 UTC
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.
Comment 8 Krinkle 2011-04-04 12:11:10 UTC
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):
## MediaWiki:Gadgets-definitions
* Gadgetname[ResourceLoader][dependancies:ext.gadget.Libs-Foobar|jquery.ui.tabs]|Gadgetname.js
## 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.
Comment 9 Max Semenik 2011-04-04 12:27:02 UTC
(In reply to comment #8)

> Load by default: pending (bug 13742)

I can do it in 15 minutes, however I need wider discussion.
Comment 10 Bugmeister Bot 2011-08-19 19:12:58 UTC
Unassigning default assignments. http://article.gmane.org/gmane.science.linguistics.wikipedia.technical/54734
Comment 11 Krinkle 2012-06-20 08:25:19 UTC
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.
Comment 12 Helder 2012-06-20 12:28:25 UTC
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:
* https://commons.wikimedia.org/wiki/MediaWiki:Gadgets-definition?oldid=72288149
* http://pl.wikipedia.org/wiki/MediaWiki:Gadgets-definition?oldid=31305058&uselang=en
(those items marked with "rights=hidden")
Comment 13 Krinkle 2012-06-20 12:32:39 UTC
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 :)

Note You need to log in before you can comment on or make changes to this bug.


Navigation
Links