2010-09-04 04:00:09 +00:00
< ? php
/**
2012-04-30 07:16:10 +00:00
* Base class for resource loading system .
*
2010-09-04 04:00:09 +00:00
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License along
* with this program ; if not , write to the Free Software Foundation , Inc . ,
* 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
* http :// www . gnu . org / copyleft / gpl . html
*
2010-09-05 13:31:34 +00:00
* @ file
2010-09-04 04:00:09 +00:00
* @ author Roan Kattouw
* @ author Trevor Parscal
*/
2010-09-05 13:31:34 +00:00
/**
2010-10-19 20:45:02 +00:00
* Dynamic JavaScript and CSS resource loading system .
*
2010-10-20 00:22:25 +00:00
* Most of the documention is on the MediaWiki documentation wiki starting at :
2010-10-19 20:45:02 +00:00
* http :// www . mediawiki . org / wiki / ResourceLoader
2010-09-04 04:00:09 +00:00
*/
class ResourceLoader {
2010-09-13 23:19:05 +00:00
2010-09-04 04:00:09 +00:00
/* Protected Static Members */
2011-11-23 00:22:46 +00:00
protected static $filterCacheVersion = 7 ;
2011-07-26 21:10:34 +00:00
protected static $requiredSourceProperties = array ( 'loadScript' );
2010-09-04 12:53:01 +00:00
2010-11-05 18:33:50 +00:00
/** Array: List of module name/ResourceLoaderModule object pairs */
2010-09-29 19:04:04 +00:00
protected $modules = array ();
2011-07-26 21:10:34 +00:00
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
/** Associative array mapping module name to info associative array */
protected $moduleInfos = array ();
2012-10-19 20:03:05 +00:00
2012-01-03 18:33:26 +00:00
/** Associative array mapping framework ids to a list of names of test suite modules */
/** like array( 'qunit' => array( 'mediawiki.tests.qunit.suites', 'ext.foo.tests', .. ), .. ) */
protected $testModuleNames = array ();
2010-09-04 12:53:01 +00:00
2011-07-26 21:10:34 +00:00
/** array( 'source-id' => array( 'loadScript' => 'http://.../load.php' ) ) **/
protected $sources = array ();
2013-07-01 18:01:11 +00:00
/** @var bool */
protected $hasErrors = false ;
2010-09-29 19:04:04 +00:00
/* Protected Methods */
2010-10-20 00:22:25 +00:00
2010-09-29 19:04:04 +00:00
/**
2010-10-20 00:22:25 +00:00
* Loads information stored in the database about modules .
2011-06-17 16:05:05 +00:00
*
* This method grabs modules dependencies from the database and updates modules
2010-11-03 07:58:03 +00:00
* objects .
2011-06-17 16:05:05 +00:00
*
* This is not inside the module code because it is much faster to
* request all of the information at once than it is to have each module
2010-11-19 06:52:38 +00:00
* requests its own information . This sacrifice of modularity yields a substantial
2010-10-20 00:22:25 +00:00
* performance improvement .
2011-06-17 16:05:05 +00:00
*
2013-03-11 17:15:01 +00:00
* @ param array $modules List of module names to preload information for
2010-11-05 18:33:50 +00:00
* @ param $context ResourceLoaderContext : Context to load the information within
2010-09-24 18:49:19 +00:00
*/
2010-12-23 21:01:54 +00:00
public function preloadModuleInfo ( array $modules , ResourceLoaderContext $context ) {
2010-10-02 10:18:48 +00:00
if ( ! count ( $modules ) ) {
2010-10-20 00:22:25 +00:00
return ; // or else Database*::select() will explode, plus it's cheaper!
2010-10-02 10:18:48 +00:00
}
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
$dbr = wfGetDB ( DB_SLAVE );
2010-09-23 21:23:51 +00:00
$skin = $context -> getSkin ();
$lang = $context -> getLanguage ();
2011-06-17 16:05:05 +00:00
2010-09-23 21:23:51 +00:00
// Get file dependency information
$res = $dbr -> select ( 'module_deps' , array ( 'md_module' , 'md_deps' ), array (
'md_module' => $modules ,
2011-05-04 20:58:14 +00:00
'md_skin' => $skin
2010-09-23 21:23:51 +00:00
), __METHOD__
);
2010-10-19 20:45:02 +00:00
2010-12-23 20:14:18 +00:00
// Set modules' dependencies
2010-09-23 21:23:51 +00:00
$modulesWithDeps = array ();
foreach ( $res as $row ) {
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
$this -> getModule ( $row -> md_module ) -> setFileDependencies ( $skin ,
2010-09-23 21:23:51 +00:00
FormatJson :: decode ( $row -> md_deps , true )
);
$modulesWithDeps [] = $row -> md_module ;
}
2010-10-20 00:22:25 +00:00
2010-10-19 20:45:02 +00:00
// Register the absence of a dependency row too
2010-09-23 21:23:51 +00:00
foreach ( array_diff ( $modules , $modulesWithDeps ) as $name ) {
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
$this -> getModule ( $name ) -> setFileDependencies ( $skin , array () );
2010-09-23 21:23:51 +00:00
}
2011-06-17 16:05:05 +00:00
2010-09-23 21:23:51 +00:00
// Get message blob mtimes. Only do this for modules with messages
$modulesWithMessages = array ();
foreach ( $modules as $name ) {
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
if ( count ( $this -> getModule ( $name ) -> getMessages () ) ) {
2010-09-23 21:23:51 +00:00
$modulesWithMessages [] = $name ;
}
}
2010-12-23 21:01:54 +00:00
$modulesWithoutMessages = array_flip ( $modules ); // Will be trimmed down by the loop below
2010-09-23 21:23:51 +00:00
if ( count ( $modulesWithMessages ) ) {
$res = $dbr -> select ( 'msg_resource' , array ( 'mr_resource' , 'mr_timestamp' ), array (
'mr_resource' => $modulesWithMessages ,
'mr_lang' => $lang
), __METHOD__
);
foreach ( $res as $row ) {
2011-06-17 16:05:05 +00:00
$this -> getModule ( $row -> mr_resource ) -> setMsgBlobMtime ( $lang ,
2011-02-08 05:33:17 +00:00
wfTimestamp ( TS_UNIX , $row -> mr_timestamp ) );
2010-12-23 21:01:54 +00:00
unset ( $modulesWithoutMessages [ $row -> mr_resource ] );
2010-09-23 21:23:51 +00:00
}
2011-06-17 16:05:05 +00:00
}
2010-12-23 21:01:54 +00:00
foreach ( array_keys ( $modulesWithoutMessages ) as $name ) {
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
$this -> getModule ( $name ) -> setMsgBlobMtime ( $lang , 0 );
2010-09-23 21:23:51 +00:00
}
}
2010-09-15 18:27:47 +00:00
2010-09-04 04:00:09 +00:00
/**
2010-10-20 00:22:25 +00:00
* Runs JavaScript or CSS data through a filter , caching the filtered result for future calls .
2011-06-17 16:05:05 +00:00
*
2010-10-20 14:58:35 +00:00
* Available filters are :
2011-03-14 11:44:33 +00:00
* - minify - js \see JavaScriptMinifier :: minify
2010-10-19 20:45:02 +00:00
* - minify - css \see CSSMin :: minify
2011-06-17 16:05:05 +00:00
*
* If $data is empty , only contains whitespace or the filter was unknown ,
2010-11-03 07:58:03 +00:00
* $data is returned unmodified .
2011-06-17 16:05:05 +00:00
*
2013-03-11 17:15:01 +00:00
* @ param string $filter Name of filter to run
* @ param string $data Text to filter , such as JavaScript or CSS text
2010-12-23 20:14:18 +00:00
* @ return String : Filtered data , or a comment containing an error message
2010-09-04 04:00:09 +00:00
*/
2010-09-29 19:04:04 +00:00
protected function filter ( $filter , $data ) {
2011-03-14 13:24:30 +00:00
global $wgResourceLoaderMinifierStatementsOnOwnLine , $wgResourceLoaderMinifierMaxLineLength ;
2010-09-17 11:45:49 +00:00
wfProfileIn ( __METHOD__ );
2010-09-04 12:53:01 +00:00
2011-06-17 16:05:05 +00:00
// For empty/whitespace-only data or for unknown filters, don't perform
2010-11-03 07:58:03 +00:00
// any caching or processing
2011-06-17 16:05:05 +00:00
if ( trim ( $data ) === ''
|| ! in_array ( $filter , array ( 'minify-js' , 'minify-css' ) ) )
2010-11-03 07:58:03 +00:00
{
2010-09-17 11:45:49 +00:00
wfProfileOut ( __METHOD__ );
2010-09-04 04:00:09 +00:00
return $data ;
}
2010-09-04 12:53:01 +00:00
2010-11-19 01:40:00 +00:00
// Try for cache hit
// Use CACHE_ANYTHING since filtering is very slow compared to DB queries
2011-02-17 22:13:52 +00:00
$key = wfMemcKey ( 'resourceloader' , 'filter' , $filter , self :: $filterCacheVersion , md5 ( $data ) );
2010-11-19 01:40:00 +00:00
$cache = wfGetCache ( CACHE_ANYTHING );
$cacheEntry = $cache -> get ( $key );
2010-11-03 07:58:03 +00:00
if ( is_string ( $cacheEntry ) ) {
2013-09-19 23:02:59 +00:00
wfIncrStats ( " rl- $filter -cache-hits " );
2010-09-17 11:45:49 +00:00
wfProfileOut ( __METHOD__ );
2010-11-03 07:58:03 +00:00
return $cacheEntry ;
2010-09-04 04:00:09 +00:00
}
2010-09-04 12:53:01 +00:00
2011-02-08 23:09:22 +00:00
$result = '' ;
2010-10-20 00:22:25 +00:00
// Run the filter - we've already verified one of these will work
2010-09-04 04:00:09 +00:00
try {
2013-09-19 23:02:59 +00:00
wfIncrStats ( " rl- $filter -cache-misses " );
2010-09-04 04:00:09 +00:00
switch ( $filter ) {
case 'minify-js' :
2011-03-14 13:24:30 +00:00
$result = JavaScriptMinifier :: minify ( $data ,
$wgResourceLoaderMinifierStatementsOnOwnLine ,
$wgResourceLoaderMinifierMaxLineLength
);
ResourceLoader: Refactor style loading
Fixes:
* bug 31676: Work around IE stylesheet limit.
* bug 35562: @import styles broken in modules that combine
multiple stylesheets.
* bug 40498: Don't output empty "@media print { }" blocks.
* bug 40500: Don't ignore media-type for urls in debug mode.
Approach:
* Re-use the same <style> tag so that we stay under the 31
stylesheet limit in IE. Unless the to-be-added css text from
the being-loaded module contains @import, in which case we do
create a new <style> tag and then re-use that one from that
point on (bug 31676).
* Return stylesheets as arrays, instead of a concatenated string.
This fixes bug 35562, because @import only works when at the
top of a stylesheet. By not unconditionally concatenating files
within a module on the server side already, @import will work
in e.g. module 'site' that contains 2 wiki pages.
This is normalized in ResourceLoader::makeCombinedStyles(),
so far only ResourceLoaderWikiModule makes use of this.
Misc. clean up and bug fixes:
* Reducing usage of jQuery() and mw.html.element() where
native DOM would be very simple and faster. Aside from
simplicity and speed, this is also working towards a more
stand-alone ResourceLoader.
* Trim server output a little bit more
- Redundant new line after minify-css (it is now an array, so
no need to keep space afterwards)
- Redundant semi-colon after minify-js if it ends in a colon
* Allow space in styleTest.css.php
* Clean up and extend unit tests to cover for these features
and bug fixes.
* Don't set styleEl.rel = 'stylesheet'; that has no business
on a <style> tag.
* Fix bug in mw.loader's addStyleTag(). It turns out IE6
has an odd security measure that does not allow manipulation
of elements (at least style tags) that are created by a
different script (even if that script was served from the same
domain/origin etc.). We didn't ran into this before because
we only created new style tags, never appended to them. Now
that we do, this came up. Took a while to figure out because
it was created by mediawiki.js but it calls jQuery which did
the actual dom insertion. Odd thing is, we load jquery.js and
mediawiki.js in the same request even...
Without this all css-url related mw.loader tests would fail
in IE6.
* mediawiki.js and mediawiki.test.js now pass jshint again.
Tested (and passing qunit/?module=mediawiki; 123 of 123):
* Chrome 14, 21
* Firefox 3.0, 3.6, 4, 7, 14, 15, 16beta
* IE 6, 7, 8, 9
* Safari 4.0, 5.0, 5.1
* Opera 10.0, 11.1, 11.5, 11.6, 12.0, 12.5beta
* iPhone 3GS / iOS 3.0 / Mobile Safari 4.0
iPhone 4 / iOS 4.0.1 / Mobile Safari 4.0.5
iPhone 4S / iOS 6.0 Beta / Mobile Safari 6.0
Change-Id: I3e8227ddb87fd9441071ca935439fc6467751dab
2012-07-25 21:20:21 +00:00
$result .= " \n /* cache key: $key */ " ;
2010-09-04 04:00:09 +00:00
break ;
case 'minify-css' :
$result = CSSMin :: minify ( $data );
ResourceLoader: Refactor style loading
Fixes:
* bug 31676: Work around IE stylesheet limit.
* bug 35562: @import styles broken in modules that combine
multiple stylesheets.
* bug 40498: Don't output empty "@media print { }" blocks.
* bug 40500: Don't ignore media-type for urls in debug mode.
Approach:
* Re-use the same <style> tag so that we stay under the 31
stylesheet limit in IE. Unless the to-be-added css text from
the being-loaded module contains @import, in which case we do
create a new <style> tag and then re-use that one from that
point on (bug 31676).
* Return stylesheets as arrays, instead of a concatenated string.
This fixes bug 35562, because @import only works when at the
top of a stylesheet. By not unconditionally concatenating files
within a module on the server side already, @import will work
in e.g. module 'site' that contains 2 wiki pages.
This is normalized in ResourceLoader::makeCombinedStyles(),
so far only ResourceLoaderWikiModule makes use of this.
Misc. clean up and bug fixes:
* Reducing usage of jQuery() and mw.html.element() where
native DOM would be very simple and faster. Aside from
simplicity and speed, this is also working towards a more
stand-alone ResourceLoader.
* Trim server output a little bit more
- Redundant new line after minify-css (it is now an array, so
no need to keep space afterwards)
- Redundant semi-colon after minify-js if it ends in a colon
* Allow space in styleTest.css.php
* Clean up and extend unit tests to cover for these features
and bug fixes.
* Don't set styleEl.rel = 'stylesheet'; that has no business
on a <style> tag.
* Fix bug in mw.loader's addStyleTag(). It turns out IE6
has an odd security measure that does not allow manipulation
of elements (at least style tags) that are created by a
different script (even if that script was served from the same
domain/origin etc.). We didn't ran into this before because
we only created new style tags, never appended to them. Now
that we do, this came up. Took a while to figure out because
it was created by mediawiki.js but it calls jQuery which did
the actual dom insertion. Odd thing is, we load jquery.js and
mediawiki.js in the same request even...
Without this all css-url related mw.loader tests would fail
in IE6.
* mediawiki.js and mediawiki.test.js now pass jshint again.
Tested (and passing qunit/?module=mediawiki; 123 of 123):
* Chrome 14, 21
* Firefox 3.0, 3.6, 4, 7, 14, 15, 16beta
* IE 6, 7, 8, 9
* Safari 4.0, 5.0, 5.1
* Opera 10.0, 11.1, 11.5, 11.6, 12.0, 12.5beta
* iPhone 3GS / iOS 3.0 / Mobile Safari 4.0
iPhone 4 / iOS 4.0.1 / Mobile Safari 4.0.5
iPhone 4S / iOS 6.0 Beta / Mobile Safari 6.0
Change-Id: I3e8227ddb87fd9441071ca935439fc6467751dab
2012-07-25 21:20:21 +00:00
$result .= " \n /* cache key: $key */ " ;
2010-09-04 04:00:09 +00:00
break ;
}
2010-12-23 20:14:18 +00:00
// Save filtered text to Memcached
$cache -> set ( $key , $result );
2013-10-09 00:20:41 +00:00
} catch ( Exception $e ) {
MWExceptionHandler :: logException ( $e );
wfDebugLog ( 'resourceloader' , __METHOD__ . " : minification failed: $e " );
2013-05-02 01:27:58 +00:00
$this -> hasErrors = true ;
2010-12-23 20:14:18 +00:00
// Return exception as a comment
2013-10-09 00:20:41 +00:00
$result = self :: formatException ( $e );
2010-09-04 04:00:09 +00:00
}
2010-09-04 12:53:01 +00:00
2010-09-17 11:45:49 +00:00
wfProfileOut ( __METHOD__ );
2011-06-17 16:05:05 +00:00
2010-09-04 04:00:09 +00:00
return $result ;
}
2010-09-04 12:53:01 +00:00
2010-09-29 19:04:04 +00:00
/* Methods */
/**
2010-10-20 00:22:25 +00:00
* Registers core modules and runs registration hooks .
2010-09-29 19:04:04 +00:00
*/
public function __construct () {
2012-01-03 18:33:26 +00:00
global $IP , $wgResourceModules , $wgResourceLoaderSources , $wgLoadScript , $wgEnableJavaScriptTest ;
2011-06-17 16:05:05 +00:00
2010-09-29 19:04:04 +00:00
wfProfileIn ( __METHOD__ );
2011-06-17 16:05:05 +00:00
2011-07-26 21:10:34 +00:00
// Add 'local' source first
2011-09-15 15:40:40 +00:00
$this -> addSource ( 'local' , array ( 'loadScript' => $wgLoadScript , 'apiScript' => wfScript ( 'api' ) ) );
2011-07-26 21:10:34 +00:00
// Add other sources
$this -> addSource ( $wgResourceLoaderSources );
2010-09-29 19:04:04 +00:00
// Register core modules
2013-05-17 00:16:59 +00:00
$this -> register ( include " $IP /resources/Resources.php " );
2010-09-29 19:04:04 +00:00
// Register extension modules
wfRunHooks ( 'ResourceLoaderRegisterModules' , array ( & $this ) );
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
$this -> register ( $wgResourceModules );
2011-06-17 16:05:05 +00:00
2012-01-03 18:33:26 +00:00
if ( $wgEnableJavaScriptTest === true ) {
$this -> registerTestModules ();
}
2010-09-29 19:04:04 +00:00
wfProfileOut ( __METHOD__ );
}
2010-09-04 12:53:01 +00:00
2010-09-04 04:00:09 +00:00
/**
* Registers a module with the ResourceLoader system .
2011-06-17 16:05:05 +00:00
*
2010-11-05 18:33:50 +00:00
* @ param $name Mixed : Name of module as a string or List of name / object pairs as an array
2013-03-11 17:15:01 +00:00
* @ param array $info Module info array . For backwards compatibility with 1.17 alpha ,
2011-06-17 16:05:05 +00:00
* this may also be a ResourceLoaderModule object . Optional when using
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
* multiple - registration calling style .
2010-11-05 18:33:50 +00:00
* @ throws MWException : If a duplicate module registration is attempted
2011-05-05 13:46:47 +00:00
* @ throws MWException : If a module name contains illegal characters ( pipes or commas )
2010-11-05 18:33:50 +00:00
* @ throws MWException : If something other than a ResourceLoaderModule is being registered
* @ return Boolean : False if there were any errors , in which case one or more modules were not
* registered
2010-09-04 04:00:09 +00:00
*/
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
public function register ( $name , $info = null ) {
2010-09-17 11:45:49 +00:00
wfProfileIn ( __METHOD__ );
2010-10-20 00:22:25 +00:00
2010-09-04 04:00:09 +00:00
// Allow multiple modules to be registered in one call
2011-11-19 16:02:26 +00:00
$registrations = is_array ( $name ) ? $name : array ( $name => $info );
foreach ( $registrations as $name => $info ) {
// Disallow duplicate registrations
if ( isset ( $this -> moduleInfos [ $name ] ) ) {
2013-04-07 16:34:58 +00:00
wfProfileOut ( __METHOD__ );
2011-11-19 16:02:26 +00:00
// A module has already been registered by this name
throw new MWException (
'ResourceLoader duplicate registration error. ' .
'Another module has already been registered as ' . $name
);
2010-09-04 04:00:09 +00:00
}
2011-06-17 16:05:05 +00:00
2012-05-09 21:10:33 +00:00
// Check $name for validity
if ( ! self :: isValidModuleName ( $name ) ) {
2013-04-07 16:34:58 +00:00
wfProfileOut ( __METHOD__ );
2012-05-09 21:10:33 +00:00
throw new MWException ( " ResourceLoader module name ' $name ' is invalid, see ResourceLoader::isValidModuleName() " );
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
}
2011-11-19 16:02:26 +00:00
// Attach module
if ( is_object ( $info ) ) {
// Old calling convention
// Validate the input
if ( ! ( $info instanceof ResourceLoaderModule ) ) {
2013-04-07 16:34:58 +00:00
wfProfileOut ( __METHOD__ );
2011-11-19 16:02:26 +00:00
throw new MWException ( 'ResourceLoader invalid module error. ' .
'Instances of ResourceLoaderModule expected.' );
}
$this -> moduleInfos [ $name ] = array ( 'object' => $info );
$info -> setName ( $name );
$this -> modules [ $name ] = $info ;
} else {
// New calling convention
$this -> moduleInfos [ $name ] = $info ;
}
2010-09-29 23:57:53 +00:00
}
2010-10-20 00:22:25 +00:00
2010-09-17 11:45:49 +00:00
wfProfileOut ( __METHOD__ );
2010-09-04 04:00:09 +00:00
}
2010-09-04 12:53:01 +00:00
2012-01-03 18:33:26 +00:00
/**
*/
public function registerTestModules () {
global $IP , $wgEnableJavaScriptTest ;
if ( $wgEnableJavaScriptTest !== true ) {
2013-07-01 18:01:11 +00:00
throw new MWException ( 'Attempt to register JavaScript test modules but <code>$wgEnableJavaScriptTest</code> is false. Edit your <code>LocalSettings.php</code> to enable it.' );
2012-01-03 18:33:26 +00:00
}
wfProfileIn ( __METHOD__ );
// Get core test suites
$testModules = array ();
2013-05-07 23:00:15 +00:00
$testModules [ 'qunit' ] = include " $IP /tests/qunit/QUnitTestResources.php " ;
2012-01-03 18:33:26 +00:00
// Get other test suites (e.g. from extensions)
wfRunHooks ( 'ResourceLoaderTestModules' , array ( & $testModules , & $this ) );
// Add the testrunner (which configures QUnit) to the dependencies.
// Since it must be ready before any of the test suites are executed.
2013-04-17 14:52:47 +00:00
foreach ( $testModules [ 'qunit' ] as & $module ) {
2013-04-02 17:59:07 +00:00
// Make sure all test modules are top-loading so that when QUnit starts
// on document-ready, it will run once and finish. If some tests arrive
// later (possibly after QUnit has already finished) they will be ignored.
2013-04-02 20:31:20 +00:00
$module [ 'position' ] = 'top' ;
$module [ 'dependencies' ][] = 'mediawiki.tests.qunit.testrunner' ;
2012-01-03 18:33:26 +00:00
}
2013-04-17 14:52:47 +00:00
foreach ( $testModules as $id => $names ) {
2012-01-03 18:33:26 +00:00
// Register test modules
$this -> register ( $testModules [ $id ] );
// Keep track of their names so that they can be loaded together
$this -> testModuleNames [ $id ] = array_keys ( $testModules [ $id ] );
}
wfProfileOut ( __METHOD__ );
}
2011-07-26 21:10:34 +00:00
/**
* Add a foreign source of modules .
2011-10-14 08:06:54 +00:00
*
2011-07-26 21:10:34 +00:00
* Source properties :
* 'loadScript' : URL ( either fully - qualified or protocol - relative ) of load . php for this source
2011-10-14 08:06:54 +00:00
*
2011-07-26 21:10:34 +00:00
* @ param $id Mixed : source ID ( string ), or array ( id1 => props1 , id2 => props2 , ... )
2013-03-11 17:15:01 +00:00
* @ param array $properties source properties
2012-10-07 23:35:26 +00:00
* @ throws MWException
2011-07-26 21:10:34 +00:00
*/
2013-04-27 12:02:08 +00:00
public function addSource ( $id , $properties = null ) {
2011-07-26 21:10:34 +00:00
// Allow multiple sources to be registered in one call
if ( is_array ( $id ) ) {
foreach ( $id as $key => $value ) {
$this -> addSource ( $key , $value );
}
return ;
}
// Disallow duplicates
if ( isset ( $this -> sources [ $id ] ) ) {
throw new MWException (
'ResourceLoader duplicate source addition error. ' .
'Another source has already been registered as ' . $id
);
}
// Validate properties
foreach ( self :: $requiredSourceProperties as $prop ) {
if ( ! isset ( $properties [ $prop ] ) ) {
throw new MWException ( " Required property $prop missing from source ID $id " );
}
}
$this -> sources [ $id ] = $properties ;
}
/**
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
* Get a list of module names
2010-09-04 04:00:09 +00:00
*
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
* @ return Array : List of module names
2010-09-04 04:00:09 +00:00
*/
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
public function getModuleNames () {
return array_keys ( $this -> moduleInfos );
2010-09-04 04:00:09 +00:00
}
2012-10-19 20:03:05 +00:00
2012-10-26 15:42:13 +00:00
/**
2012-01-03 18:33:26 +00:00
* Get a list of test module names for one ( or all ) frameworks .
* If the given framework id is unknkown , or if the in - object variable is not an array ,
* then it will return an empty array .
*
2013-03-11 17:15:01 +00:00
* @ param string $framework Optional . Get only the test module names for one
2012-01-03 18:33:26 +00:00
* particular framework .
* @ return Array
*/
public function getTestModuleNames ( $framework = 'all' ) {
2013-05-15 01:12:35 +00:00
/// @todo api siteinfo prop testmodulenames modulenames
2012-01-03 18:33:26 +00:00
if ( $framework == 'all' ) {
return $this -> testModuleNames ;
} elseif ( isset ( $this -> testModuleNames [ $framework ] ) && is_array ( $this -> testModuleNames [ $framework ] ) ) {
return $this -> testModuleNames [ $framework ];
} else {
return array ();
}
}
2010-09-04 12:53:01 +00:00
2010-09-04 04:00:09 +00:00
/**
2010-10-20 00:22:25 +00:00
* Get the ResourceLoaderModule object for a given module name .
2010-09-05 13:31:34 +00:00
*
2013-03-11 17:15:01 +00:00
* @ param string $name Module name
2011-02-08 23:09:22 +00:00
* @ return ResourceLoaderModule if module has been registered , null otherwise
2010-09-04 04:00:09 +00:00
*/
2010-09-29 19:04:04 +00:00
public function getModule ( $name ) {
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
if ( ! isset ( $this -> modules [ $name ] ) ) {
if ( ! isset ( $this -> moduleInfos [ $name ] ) ) {
// No such module
return null ;
}
// Construct the requested object
$info = $this -> moduleInfos [ $name ];
2013-07-01 18:01:11 +00:00
/** @var ResourceLoaderModule $object */
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
if ( isset ( $info [ 'object' ] ) ) {
// Object given in info array
$object = $info [ 'object' ];
} else {
if ( ! isset ( $info [ 'class' ] ) ) {
$class = 'ResourceLoaderFileModule' ;
} else {
$class = $info [ 'class' ];
}
$object = new $class ( $info );
}
$object -> setName ( $name );
$this -> modules [ $name ] = $object ;
}
return $this -> modules [ $name ];
2010-09-04 04:00:09 +00:00
}
2010-09-04 12:53:01 +00:00
2011-07-26 21:10:34 +00:00
/**
* Get the list of sources
2011-10-14 08:06:54 +00:00
*
2011-07-26 21:10:34 +00:00
* @ return Array : array ( id => array of properties , .. )
*/
public function getSources () {
return $this -> sources ;
}
2010-09-05 13:31:34 +00:00
/**
2010-10-20 00:22:25 +00:00
* Outputs a response to a resource load - request , including a content - type header .
2010-09-04 04:00:09 +00:00
*
2010-11-05 18:33:50 +00:00
* @ param $context ResourceLoaderContext : Context in which a response should be formed
2010-09-04 04:00:09 +00:00
*/
2010-09-29 19:04:04 +00:00
public function respond ( ResourceLoaderContext $context ) {
2011-10-02 17:53:33 +00:00
global $wgCacheEpoch , $wgUseFileCache ;
// Use file cache if enabled and available...
if ( $wgUseFileCache ) {
$fileCache = ResourceFileCache :: newFromContext ( $context );
if ( $this -> tryRespondFromFileCache ( $fileCache , $context ) ) {
return ; // output handled
}
}
2011-06-17 16:05:05 +00:00
2010-12-10 17:06:00 +00:00
// Buffer output to catch warnings. Normally we'd use ob_clean() on the
// top-level output buffer to clear warnings, but that breaks when ob_gzhandler
// is used: ob_clean() will clear the GZIP header in that case and it won't come
// back for subsequent output, resulting in invalid GZIP. So we have to wrap
// the whole thing in our own output buffer to be sure the active buffer
// doesn't use ob_gzhandler.
// See http://bugs.php.net/bug.php?id=36514
ob_start ();
2010-09-13 23:19:05 +00:00
2010-09-17 11:45:49 +00:00
wfProfileIn ( __METHOD__ );
2012-03-22 19:52:37 +00:00
$errors = '' ;
2010-10-20 00:22:25 +00:00
2010-09-04 04:00:09 +00:00
// Split requested modules into two groups, modules and missing
$modules = array ();
$missing = array ();
foreach ( $context -> getModules () as $name ) {
* Made Resources.php return a pure-data array instead of an ugly mix of data and code. This allows the class code to be lazy-loaded with the autoloader, for a performance advantage especially on non-APC installs. And using the convention where if the class is omitted, ResourceLoaderFileModule is assumed, the registration code becomes shorter and simpler.
* Modified ResourceLoader to lazy-initialise module objects, for a further performance advantage.
* Deleted ResourceLoader::getModules(), provided getModuleNames() instead. Although the startup module needs this functionality, it's slow to generate, so to avoid misuse, it's better to provide a foolproof fast interface and let the startup module do the slow thing itself.
* Modified ResourceLoader::register() to optionally accept an info array instead of an object.
* Added $wgResourceModules, allowing extensions to efficiently define their own resource loader modules. The trouble with hooks is that they contain code, and code is slow. We've been through all this before with i18n. Hooks are useful as a performance tool only if you call them very rarely.
* Moved ResourceLoader settings to their own section in DefaultSettings.php
* Added options to ResourceLoaderFileModule equivalent to the $localBasePath and $remoteBasePath parameters, to allow it to be instantiated via the new array style. Also added remoteExtPath, which allows modules to be registered before $wgExtensionAssetsPath is known.
* Added OutputPage::getResourceLoader(), mostly for debugging.
* The time saving at the moment is about 5ms per request with no extensions, which is significant already with 6 load.php requests for a cold cache page view. This is a much more scalable interface; the relative saving will grow as more extensions are added which use this interface, especially for non-APC installs.
Although the interface is backwards compatible, extension updates will follow in a subsequent commit.
2010-11-19 10:41:06 +00:00
if ( isset ( $this -> moduleInfos [ $name ] ) ) {
2012-03-22 19:52:37 +00:00
$module = $this -> getModule ( $name );
// Do not allow private modules to be loaded from the web.
// This is a security issue, see bug 34907.
if ( $module -> getGroup () === 'private' ) {
2013-07-01 18:01:11 +00:00
wfDebugLog ( 'resourceloader' , __METHOD__ . " : request for private module ' $name ' denied " );
2013-01-15 23:32:10 +00:00
$this -> hasErrors = true ;
2013-05-02 01:27:58 +00:00
// Add exception to the output as a comment
2013-08-11 07:11:17 +00:00
$errors .= self :: makeComment ( " Cannot show private module \" $name\ " " );
2013-05-02 01:27:58 +00:00
2012-03-22 19:52:37 +00:00
continue ;
}
2013-03-06 00:24:57 +00:00
$modules [ $name ] = $module ;
2010-09-04 04:00:09 +00:00
} else {
$missing [] = $name ;
}
}
2010-09-04 12:53:01 +00:00
2010-09-23 21:23:51 +00:00
// Preload information needed to the mtime calculation below
2010-12-23 20:14:18 +00:00
try {
$this -> preloadModuleInfo ( array_keys ( $modules ), $context );
2013-04-27 12:02:08 +00:00
} catch ( Exception $e ) {
2013-10-09 00:20:41 +00:00
MWExceptionHandler :: logException ( $e );
2013-05-02 01:27:58 +00:00
wfDebugLog ( 'resourceloader' , __METHOD__ . " : preloading module info failed: $e " );
$this -> hasErrors = true ;
2010-12-23 20:14:18 +00:00
// Add exception to the output as a comment
2013-08-16 23:20:26 +00:00
$errors .= self :: formatException ( $e );
2010-12-23 20:14:18 +00:00
}
2010-09-23 21:23:51 +00:00
2013-04-13 11:36:24 +00:00
wfProfileIn ( __METHOD__ . '-getModifiedTime' );
2010-10-20 00:22:25 +00:00
2011-06-17 16:05:05 +00:00
// To send Last-Modified and support If-Modified-Since, we need to detect
2010-11-03 07:58:03 +00:00
// the last modified time
2010-10-08 10:25:23 +00:00
$mtime = wfTimestamp ( TS_UNIX , $wgCacheEpoch );
2010-09-24 22:10:25 +00:00
foreach ( $modules as $module ) {
2011-10-14 08:06:54 +00:00
/**
* @ var $module ResourceLoaderModule
*/
2010-12-23 20:14:18 +00:00
try {
// Calculate maximum modified time
$mtime = max ( $mtime , $module -> getModifiedTime ( $context ) );
} catch ( Exception $e ) {
2013-10-09 00:20:41 +00:00
MWExceptionHandler :: logException ( $e );
2013-05-02 01:27:58 +00:00
wfDebugLog ( 'resourceloader' , __METHOD__ . " : calculating maximum modified time failed: $e " );
$this -> hasErrors = true ;
2010-12-23 20:14:18 +00:00
// Add exception to the output as a comment
2013-08-16 23:20:26 +00:00
$errors .= self :: formatException ( $e );
2010-09-24 22:10:25 +00:00
}
2010-09-04 08:38:45 +00:00
}
2010-10-20 00:22:25 +00:00
2013-04-13 11:36:24 +00:00
wfProfileOut ( __METHOD__ . '-getModifiedTime' );
2010-09-04 12:53:01 +00:00
2011-10-01 04:15:07 +00:00
// If there's an If-Modified-Since header, respond with a 304 appropriately
if ( $this -> tryRespondLastModified ( $context , $mtime ) ) {
2011-10-27 20:50:07 +00:00
wfProfileOut ( __METHOD__ );
2011-10-01 04:15:07 +00:00
return ; // output handled (buffers cleared)
}
// Generate a response
$response = $this -> makeModuleResponse ( $context , $modules , $missing );
// Prepend comments indicating exceptions
2012-03-22 19:52:37 +00:00
$response = $errors . $response ;
2011-10-01 04:15:07 +00:00
// Capture any PHP warnings from the output buffer and append them to the
// response in a comment if we're in debug mode.
if ( $context -> getDebug () && strlen ( $warnings = ob_get_contents () ) ) {
2013-08-11 07:11:17 +00:00
$response = self :: makeComment ( $warnings ) . $response ;
2013-01-15 23:32:10 +00:00
$this -> hasErrors = true ;
2011-10-01 04:15:07 +00:00
}
2012-03-22 19:52:37 +00:00
// Save response to file cache unless there are errors
if ( isset ( $fileCache ) && ! $errors && ! $missing ) {
2011-10-02 17:53:33 +00:00
// Cache single modules...and other requests if there are enough hits
if ( ResourceFileCache :: useFileCache ( $context ) ) {
if ( $fileCache -> isCacheWorthy () ) {
$fileCache -> saveText ( $response );
} else {
$fileCache -> incrMissesRecent ( $context -> getRequest () );
}
}
}
2013-01-15 23:32:10 +00:00
// Send content type and cache related headers
$this -> sendResponseHeaders ( $context , $mtime , $this -> hasErrors );
2012-04-16 19:17:02 +00:00
// Remove the output buffer and output the response
ob_end_clean ();
echo $response ;
2011-10-01 04:15:07 +00:00
wfProfileOut ( __METHOD__ );
}
/**
* Send content type and last modified headers to the client .
* @ param $context ResourceLoaderContext
2013-03-11 17:15:01 +00:00
* @ param string $mtime TS_MW timestamp to use for last - modified
2013-07-01 18:01:11 +00:00
* @ param bool $errors Whether there are commented - out errors in the response
2012-01-12 19:41:18 +00:00
* @ return void
2011-10-01 04:15:07 +00:00
*/
2013-01-15 23:32:10 +00:00
protected function sendResponseHeaders ( ResourceLoaderContext $context , $mtime , $errors ) {
2011-10-01 04:15:07 +00:00
global $wgResourceLoaderMaxage ;
// If a version wasn't specified we need a shorter expiry time for updates
// to propagate to clients quickly
2013-01-15 23:32:10 +00:00
// If there were errors, we also need a shorter expiry time so we can recover quickly
if ( is_null ( $context -> getVersion () ) || $errors ) {
2013-03-07 16:50:43 +00:00
$maxage = $wgResourceLoaderMaxage [ 'unversioned' ][ 'client' ];
2011-10-01 04:15:07 +00:00
$smaxage = $wgResourceLoaderMaxage [ 'unversioned' ][ 'server' ];
// If a version was specified we can use a longer expiry time since changing
// version numbers causes cache misses
} else {
2013-03-07 16:50:43 +00:00
$maxage = $wgResourceLoaderMaxage [ 'versioned' ][ 'client' ];
2011-10-01 04:15:07 +00:00
$smaxage = $wgResourceLoaderMaxage [ 'versioned' ][ 'server' ];
}
2010-11-03 07:58:03 +00:00
if ( $context -> getOnly () === 'styles' ) {
2011-04-01 20:48:50 +00:00
header ( 'Content-Type: text/css; charset=utf-8' );
2013-04-21 14:38:23 +00:00
header ( 'Access-Control-Allow-Origin: *' );
2010-11-03 07:58:03 +00:00
} else {
2011-04-01 20:48:50 +00:00
header ( 'Content-Type: text/javascript; charset=utf-8' );
2010-11-03 07:58:03 +00:00
}
2010-09-04 04:00:09 +00:00
header ( 'Last-Modified: ' . wfTimestamp ( TS_RFC2822 , $mtime ) );
2010-11-01 20:48:30 +00:00
if ( $context -> getDebug () ) {
2011-01-19 19:31:14 +00:00
// Do not cache debug responses
header ( 'Cache-Control: private, no-cache, must-revalidate' );
header ( 'Pragma: no-cache' );
2010-11-01 20:48:30 +00:00
} else {
2012-03-22 19:52:37 +00:00
header ( " Cache-Control: public, max-age= $maxage , s-maxage= $smaxage " );
$exp = min ( $maxage , $smaxage );
2011-01-19 19:31:14 +00:00
header ( 'Expires: ' . wfTimestamp ( TS_RFC2822 , $exp + time () ) );
2010-11-01 20:48:30 +00:00
}
2011-10-01 04:15:07 +00:00
}
2010-09-04 12:53:01 +00:00
2011-10-01 04:15:07 +00:00
/**
* If there ' s an If - Modified - Since header , respond with a 304 appropriately
* and clear out the output buffer . If the client cache is too old then do nothing .
* @ param $context ResourceLoaderContext
2013-03-11 17:15:01 +00:00
* @ param string $mtime The TS_MW timestamp to check the header against
2011-10-01 04:15:07 +00:00
* @ return bool True iff 304 header sent and output handled
*/
protected function tryRespondLastModified ( ResourceLoaderContext $context , $mtime ) {
2010-09-13 23:19:05 +00:00
// If there's an If-Modified-Since header, respond with a 304 appropriately
2010-12-02 16:54:06 +00:00
// Some clients send "timestamp;length=123". Strip the part after the first ';'
// so we get a valid timestamp.
2010-09-04 04:00:09 +00:00
$ims = $context -> getRequest () -> getHeader ( 'If-Modified-Since' );
2011-02-12 23:41:28 +00:00
// Never send 304s in debug mode
if ( $ims !== false && ! $context -> getDebug () ) {
2010-12-02 16:54:06 +00:00
$imsTS = strtok ( $ims , ';' );
if ( $mtime <= wfTimestamp ( TS_UNIX , $imsTS ) ) {
2010-12-10 17:06:00 +00:00
// There's another bug in ob_gzhandler (see also the comment at
// the top of this function) that causes it to gzip even empty
// responses, meaning it's impossible to produce a truly empty
// response (because the gzip header is always there). This is
// a problem because 304 responses have to be completely empty
// per the HTTP spec, and Firefox behaves buggily when they're not.
// See also http://bugs.php.net/bug.php?id=51579
// To work around this, we tear down all output buffering before
// sending the 304.
2013-07-19 01:21:38 +00:00
wfResetOutputBuffers ( /* $resetGzipEncoding = */ true );
2011-06-17 16:05:05 +00:00
2010-12-02 16:54:06 +00:00
header ( 'HTTP/1.0 304 Not Modified' );
header ( 'Status: 304 Not Modified' );
2011-10-01 04:15:07 +00:00
return true ;
2010-12-02 16:54:06 +00:00
}
2010-09-04 04:00:09 +00:00
}
2011-10-01 04:15:07 +00:00
return false ;
2010-09-24 22:10:25 +00:00
}
2011-10-02 17:53:33 +00:00
/**
* Send out code for a response from file cache if possible
*
2012-05-08 15:53:59 +00:00
* @ param $fileCache ResourceFileCache : Cache object for this request URL
2011-10-02 17:53:33 +00:00
* @ param $context ResourceLoaderContext : Context in which to generate a response
* @ return bool If this found a cache file and handled the response
*/
protected function tryRespondFromFileCache (
ResourceFileCache $fileCache , ResourceLoaderContext $context
) {
global $wgResourceLoaderMaxage ;
// Buffer output to catch warnings.
ob_start ();
// Get the maximum age the cache can be
$maxage = is_null ( $context -> getVersion () )
? $wgResourceLoaderMaxage [ 'unversioned' ][ 'server' ]
: $wgResourceLoaderMaxage [ 'versioned' ][ 'server' ];
// Minimum timestamp the cache file must have
$good = $fileCache -> isCacheGood ( wfTimestamp ( TS_MW , time () - $maxage ) );
if ( ! $good ) {
try { // RL always hits the DB on file cache miss...
wfGetDB ( DB_SLAVE );
2013-04-27 12:02:08 +00:00
} catch ( DBConnectionError $e ) { // ...check if we need to fallback to cache
2011-10-02 17:53:33 +00:00
$good = $fileCache -> isCacheGood (); // cache existence check
}
}
if ( $good ) {
$ts = $fileCache -> cacheTimestamp ();
// Send content type and cache headers
$this -> sendResponseHeaders ( $context , $ts , false );
// If there's an If-Modified-Since header, respond with a 304 appropriately
if ( $this -> tryRespondLastModified ( $context , $ts ) ) {
2011-10-14 08:06:54 +00:00
return false ; // output handled (buffers cleared)
2011-10-02 17:53:33 +00:00
}
$response = $fileCache -> fetchText ();
2011-12-17 05:02:15 +00:00
// Capture any PHP warnings from the output buffer and append them to the
// response in a comment if we're in debug mode.
if ( $context -> getDebug () && strlen ( $warnings = ob_get_contents () ) ) {
$response = " /* \n $warnings\n */ \n " . $response ;
}
2011-10-02 17:53:33 +00:00
// Remove the output buffer and output the response
ob_end_clean ();
echo $response . " \n /* Cached { $ts } */ " ;
return true ; // cache hit
}
// Clear buffer
ob_end_clean ();
return false ; // cache miss
}
2013-08-11 07:11:17 +00:00
/**
2013-08-16 23:20:26 +00:00
* Generate a CSS or JS comment block . Only use this for public data ,
* not error message details .
2013-08-11 07:11:17 +00:00
*
* @ param $text string
* @ return string
*/
public static function makeComment ( $text ) {
2012-03-22 19:52:37 +00:00
$encText = str_replace ( '*/' , '* /' , $text );
return " /* \n $encText\n */ \n " ;
}
2013-08-16 23:20:26 +00:00
/**
* Handle exception display
*
* @ param Exception $e to be shown to the user
* @ return string sanitized text that can be returned to the user
*/
public static function formatException ( $e ) {
global $wgShowExceptionDetails ;
if ( $wgShowExceptionDetails ) {
return self :: makeComment ( $e -> __toString () );
} else {
return self :: makeComment ( wfMessage ( 'internalerror' ) -> text () );
}
}
2010-10-19 20:45:02 +00:00
/**
2010-10-20 00:22:25 +00:00
* Generates code for a response
2011-06-17 16:05:05 +00:00
*
2010-11-05 18:33:50 +00:00
* @ param $context ResourceLoaderContext : Context in which to generate a response
2013-03-11 17:15:01 +00:00
* @ param array $modules List of module objects keyed by module name
* @ param array $missing List of unavailable modules ( optional )
2010-11-05 18:33:50 +00:00
* @ return String : Response data
2010-10-19 20:45:02 +00:00
*/
2011-06-17 16:05:05 +00:00
public function makeModuleResponse ( ResourceLoaderContext $context ,
2013-04-02 20:28:40 +00:00
array $modules , $missing = array ()
) {
2010-12-23 20:14:18 +00:00
$out = '' ;
2011-01-09 12:29:02 +00:00
$exceptions = '' ;
2010-12-10 17:21:09 +00:00
if ( $modules === array () && $missing === array () ) {
return '/* No modules requested. Max made me put this here */' ;
}
2011-06-17 16:05:05 +00:00
2011-02-10 16:44:57 +00:00
wfProfileIn ( __METHOD__ );
2010-09-04 04:00:09 +00:00
// Pre-fetch blobs
2010-11-03 07:58:03 +00:00
if ( $context -> shouldIncludeMessages () ) {
2011-01-09 12:29:02 +00:00
try {
2010-12-23 20:14:18 +00:00
$blobs = MessageBlobStore :: get ( $this , $modules , $context -> getLanguage () );
2011-01-09 12:29:02 +00:00
} catch ( Exception $e ) {
2013-10-09 00:20:41 +00:00
MWExceptionHandler :: logException ( $e );
2013-05-02 01:27:58 +00:00
wfDebugLog ( 'resourceloader' , __METHOD__ . " : pre-fetching blobs from MessageBlobStore failed: $e " );
$this -> hasErrors = true ;
2010-12-23 20:14:18 +00:00
// Add exception to the output as a comment
2013-08-16 23:20:26 +00:00
$exceptions .= self :: formatException ( $e );
2011-01-09 12:29:02 +00:00
}
2010-11-03 07:58:03 +00:00
} else {
$blobs = array ();
}
2010-09-04 12:53:01 +00:00
2010-09-04 04:00:09 +00:00
// Generate output
2012-05-11 19:16:29 +00:00
$isRaw = false ;
2010-09-24 22:10:25 +00:00
foreach ( $modules as $name => $module ) {
2011-10-14 08:06:54 +00:00
/**
* @ var $module ResourceLoaderModule
*/
2010-09-17 11:45:49 +00:00
wfProfileIn ( __METHOD__ . '-' . $name );
2010-12-23 20:14:18 +00:00
try {
$scripts = '' ;
if ( $context -> shouldIncludeScripts () ) {
Fix the fixme on r88053: dependency handling was broken in debug mode in certain cases. More specifically, if A is a file module that depends on B, B is a wiki module that depends on C and C is a file module, the loading order is CBA (correct) in production mode but was BCA (wrong) in debug mode. Fixed this by URL-ifying scripts and styles for those modules in debug mode, as I said to on CR. What this means is that the initial debug=true request for a module will now always return arrays of URLs, never the JS or CSS itself. This was already the case for file modules (which returned arrays of URLs to the raw files), but not for other modules (which returned the JS and CSS itself). So for non-file modules, load.php?modules=foo&debug=true now returns some JS that instructs the loader to fetch the module's JS from load.php?modules=foo&debug=true&only=scripts and the CSS from ...&only=styles .
* Removed the magic behavior where ResourceLoaderModule::getScripts() and getStyles() could return an array of URLs where the documentation said they should return a JS/CSS string. Because I didn't restructure the calling code too much, the old magical behavior should still work.
* Instead, move this behavior to getScriptURLsForDebug() and getStyleURLsForDebug(). The default implementation constructs a single URL for a load.php request for the module with debug=true&only=scripts (or styles). The URL building code duplicates some things from OutputPage::makeResourceLoaderLink(), I'll clean that up later. ResourceLoaderFileModule overrides this method to return URLs to the raw files, using code that I removed from getScripts()/getStyles()
* Add ResourceLoaderModule::supportsURLLoading(), which returns true by default but may return false to indicate that a module does not support loading via a URL. This is needed to respect $this->debugRaw in ResourceLoaderFileModule (set to true for jquery and mediawiki), and obviously for the startup module as well, because we get bootstrapping problems otherwise (can't call mw.loader.implement() when the code for mw.loader isn't loaded yet)
2011-09-13 17:13:53 +00:00
// If we are in debug mode, we'll want to return an array of URLs if possible
// However, we can't do this if the module doesn't support it
// We also can't do this if there is an only= parameter, because we have to give
// the module a way to return a load.php URL without causing an infinite loop
if ( $context -> getDebug () && ! $context -> getOnly () && $module -> supportsURLLoading () ) {
$scripts = $module -> getScriptURLsForDebug ( $context );
} else {
$scripts = $module -> getScript ( $context );
ResourceLoader: Refactor style loading
Fixes:
* bug 31676: Work around IE stylesheet limit.
* bug 35562: @import styles broken in modules that combine
multiple stylesheets.
* bug 40498: Don't output empty "@media print { }" blocks.
* bug 40500: Don't ignore media-type for urls in debug mode.
Approach:
* Re-use the same <style> tag so that we stay under the 31
stylesheet limit in IE. Unless the to-be-added css text from
the being-loaded module contains @import, in which case we do
create a new <style> tag and then re-use that one from that
point on (bug 31676).
* Return stylesheets as arrays, instead of a concatenated string.
This fixes bug 35562, because @import only works when at the
top of a stylesheet. By not unconditionally concatenating files
within a module on the server side already, @import will work
in e.g. module 'site' that contains 2 wiki pages.
This is normalized in ResourceLoader::makeCombinedStyles(),
so far only ResourceLoaderWikiModule makes use of this.
Misc. clean up and bug fixes:
* Reducing usage of jQuery() and mw.html.element() where
native DOM would be very simple and faster. Aside from
simplicity and speed, this is also working towards a more
stand-alone ResourceLoader.
* Trim server output a little bit more
- Redundant new line after minify-css (it is now an array, so
no need to keep space afterwards)
- Redundant semi-colon after minify-js if it ends in a colon
* Allow space in styleTest.css.php
* Clean up and extend unit tests to cover for these features
and bug fixes.
* Don't set styleEl.rel = 'stylesheet'; that has no business
on a <style> tag.
* Fix bug in mw.loader's addStyleTag(). It turns out IE6
has an odd security measure that does not allow manipulation
of elements (at least style tags) that are created by a
different script (even if that script was served from the same
domain/origin etc.). We didn't ran into this before because
we only created new style tags, never appended to them. Now
that we do, this came up. Took a while to figure out because
it was created by mediawiki.js but it calls jQuery which did
the actual dom insertion. Odd thing is, we load jquery.js and
mediawiki.js in the same request even...
Without this all css-url related mw.loader tests would fail
in IE6.
* mediawiki.js and mediawiki.test.js now pass jshint again.
Tested (and passing qunit/?module=mediawiki; 123 of 123):
* Chrome 14, 21
* Firefox 3.0, 3.6, 4, 7, 14, 15, 16beta
* IE 6, 7, 8, 9
* Safari 4.0, 5.0, 5.1
* Opera 10.0, 11.1, 11.5, 11.6, 12.0, 12.5beta
* iPhone 3GS / iOS 3.0 / Mobile Safari 4.0
iPhone 4 / iOS 4.0.1 / Mobile Safari 4.0.5
iPhone 4S / iOS 6.0 Beta / Mobile Safari 6.0
Change-Id: I3e8227ddb87fd9441071ca935439fc6467751dab
2012-07-25 21:20:21 +00:00
if ( is_string ( $scripts ) && strlen ( $scripts ) && substr ( $scripts , - 1 ) !== ';' ) {
Fix the fixme on r88053: dependency handling was broken in debug mode in certain cases. More specifically, if A is a file module that depends on B, B is a wiki module that depends on C and C is a file module, the loading order is CBA (correct) in production mode but was BCA (wrong) in debug mode. Fixed this by URL-ifying scripts and styles for those modules in debug mode, as I said to on CR. What this means is that the initial debug=true request for a module will now always return arrays of URLs, never the JS or CSS itself. This was already the case for file modules (which returned arrays of URLs to the raw files), but not for other modules (which returned the JS and CSS itself). So for non-file modules, load.php?modules=foo&debug=true now returns some JS that instructs the loader to fetch the module's JS from load.php?modules=foo&debug=true&only=scripts and the CSS from ...&only=styles .
* Removed the magic behavior where ResourceLoaderModule::getScripts() and getStyles() could return an array of URLs where the documentation said they should return a JS/CSS string. Because I didn't restructure the calling code too much, the old magical behavior should still work.
* Instead, move this behavior to getScriptURLsForDebug() and getStyleURLsForDebug(). The default implementation constructs a single URL for a load.php request for the module with debug=true&only=scripts (or styles). The URL building code duplicates some things from OutputPage::makeResourceLoaderLink(), I'll clean that up later. ResourceLoaderFileModule overrides this method to return URLs to the raw files, using code that I removed from getScripts()/getStyles()
* Add ResourceLoaderModule::supportsURLLoading(), which returns true by default but may return false to indicate that a module does not support loading via a URL. This is needed to respect $this->debugRaw in ResourceLoaderFileModule (set to true for jquery and mediawiki), and obviously for the startup module as well, because we get bootstrapping problems otherwise (can't call mw.loader.implement() when the code for mw.loader isn't loaded yet)
2011-09-13 17:13:53 +00:00
// bug 27054: Append semicolon to prevent weird bugs
// caused by files not terminating their statements right
$scripts .= " ; \n " ;
}
2011-05-14 12:15:58 +00:00
}
2010-12-23 20:14:18 +00:00
}
// Styles
$styles = array ();
if ( $context -> shouldIncludeStyles () ) {
2012-06-28 13:10:39 +00:00
// Don't create empty stylesheets like array( '' => '' ) for modules
// that don't *have* any stylesheets (bug 38024).
$stylePairs = $module -> getStyles ( $context );
if ( count ( $stylePairs ) ) {
// If we are in debug mode without &only= set, we'll want to return an array of URLs
// See comment near shouldIncludeScripts() for more details
if ( $context -> getDebug () && ! $context -> getOnly () && $module -> supportsURLLoading () ) {
ResourceLoader: Refactor style loading
Fixes:
* bug 31676: Work around IE stylesheet limit.
* bug 35562: @import styles broken in modules that combine
multiple stylesheets.
* bug 40498: Don't output empty "@media print { }" blocks.
* bug 40500: Don't ignore media-type for urls in debug mode.
Approach:
* Re-use the same <style> tag so that we stay under the 31
stylesheet limit in IE. Unless the to-be-added css text from
the being-loaded module contains @import, in which case we do
create a new <style> tag and then re-use that one from that
point on (bug 31676).
* Return stylesheets as arrays, instead of a concatenated string.
This fixes bug 35562, because @import only works when at the
top of a stylesheet. By not unconditionally concatenating files
within a module on the server side already, @import will work
in e.g. module 'site' that contains 2 wiki pages.
This is normalized in ResourceLoader::makeCombinedStyles(),
so far only ResourceLoaderWikiModule makes use of this.
Misc. clean up and bug fixes:
* Reducing usage of jQuery() and mw.html.element() where
native DOM would be very simple and faster. Aside from
simplicity and speed, this is also working towards a more
stand-alone ResourceLoader.
* Trim server output a little bit more
- Redundant new line after minify-css (it is now an array, so
no need to keep space afterwards)
- Redundant semi-colon after minify-js if it ends in a colon
* Allow space in styleTest.css.php
* Clean up and extend unit tests to cover for these features
and bug fixes.
* Don't set styleEl.rel = 'stylesheet'; that has no business
on a <style> tag.
* Fix bug in mw.loader's addStyleTag(). It turns out IE6
has an odd security measure that does not allow manipulation
of elements (at least style tags) that are created by a
different script (even if that script was served from the same
domain/origin etc.). We didn't ran into this before because
we only created new style tags, never appended to them. Now
that we do, this came up. Took a while to figure out because
it was created by mediawiki.js but it calls jQuery which did
the actual dom insertion. Odd thing is, we load jquery.js and
mediawiki.js in the same request even...
Without this all css-url related mw.loader tests would fail
in IE6.
* mediawiki.js and mediawiki.test.js now pass jshint again.
Tested (and passing qunit/?module=mediawiki; 123 of 123):
* Chrome 14, 21
* Firefox 3.0, 3.6, 4, 7, 14, 15, 16beta
* IE 6, 7, 8, 9
* Safari 4.0, 5.0, 5.1
* Opera 10.0, 11.1, 11.5, 11.6, 12.0, 12.5beta
* iPhone 3GS / iOS 3.0 / Mobile Safari 4.0
iPhone 4 / iOS 4.0.1 / Mobile Safari 4.0.5
iPhone 4S / iOS 6.0 Beta / Mobile Safari 6.0
Change-Id: I3e8227ddb87fd9441071ca935439fc6467751dab
2012-07-25 21:20:21 +00:00
$styles = array (
'url' => $module -> getStyleURLsForDebug ( $context )
);
2012-06-28 13:10:39 +00:00
} else {
// Minify CSS before embedding in mw.loader.implement call
// (unless in debug mode)
if ( ! $context -> getDebug () ) {
foreach ( $stylePairs as $media => $style ) {
ResourceLoader: Refactor style loading
Fixes:
* bug 31676: Work around IE stylesheet limit.
* bug 35562: @import styles broken in modules that combine
multiple stylesheets.
* bug 40498: Don't output empty "@media print { }" blocks.
* bug 40500: Don't ignore media-type for urls in debug mode.
Approach:
* Re-use the same <style> tag so that we stay under the 31
stylesheet limit in IE. Unless the to-be-added css text from
the being-loaded module contains @import, in which case we do
create a new <style> tag and then re-use that one from that
point on (bug 31676).
* Return stylesheets as arrays, instead of a concatenated string.
This fixes bug 35562, because @import only works when at the
top of a stylesheet. By not unconditionally concatenating files
within a module on the server side already, @import will work
in e.g. module 'site' that contains 2 wiki pages.
This is normalized in ResourceLoader::makeCombinedStyles(),
so far only ResourceLoaderWikiModule makes use of this.
Misc. clean up and bug fixes:
* Reducing usage of jQuery() and mw.html.element() where
native DOM would be very simple and faster. Aside from
simplicity and speed, this is also working towards a more
stand-alone ResourceLoader.
* Trim server output a little bit more
- Redundant new line after minify-css (it is now an array, so
no need to keep space afterwards)
- Redundant semi-colon after minify-js if it ends in a colon
* Allow space in styleTest.css.php
* Clean up and extend unit tests to cover for these features
and bug fixes.
* Don't set styleEl.rel = 'stylesheet'; that has no business
on a <style> tag.
* Fix bug in mw.loader's addStyleTag(). It turns out IE6
has an odd security measure that does not allow manipulation
of elements (at least style tags) that are created by a
different script (even if that script was served from the same
domain/origin etc.). We didn't ran into this before because
we only created new style tags, never appended to them. Now
that we do, this came up. Took a while to figure out because
it was created by mediawiki.js but it calls jQuery which did
the actual dom insertion. Odd thing is, we load jquery.js and
mediawiki.js in the same request even...
Without this all css-url related mw.loader tests would fail
in IE6.
* mediawiki.js and mediawiki.test.js now pass jshint again.
Tested (and passing qunit/?module=mediawiki; 123 of 123):
* Chrome 14, 21
* Firefox 3.0, 3.6, 4, 7, 14, 15, 16beta
* IE 6, 7, 8, 9
* Safari 4.0, 5.0, 5.1
* Opera 10.0, 11.1, 11.5, 11.6, 12.0, 12.5beta
* iPhone 3GS / iOS 3.0 / Mobile Safari 4.0
iPhone 4 / iOS 4.0.1 / Mobile Safari 4.0.5
iPhone 4S / iOS 6.0 Beta / Mobile Safari 6.0
Change-Id: I3e8227ddb87fd9441071ca935439fc6467751dab
2012-07-25 21:20:21 +00:00
// Can be either a string or an array of strings.
if ( is_array ( $style ) ) {
$stylePairs [ $media ] = array ();
foreach ( $style as $cssText ) {
if ( is_string ( $cssText ) ) {
$stylePairs [ $media ][] = $this -> filter ( 'minify-css' , $cssText );
}
}
} elseif ( is_string ( $style ) ) {
2012-06-28 13:10:39 +00:00
$stylePairs [ $media ] = $this -> filter ( 'minify-css' , $style );
}
2012-06-14 00:46:39 +00:00
}
}
ResourceLoader: Refactor style loading
Fixes:
* bug 31676: Work around IE stylesheet limit.
* bug 35562: @import styles broken in modules that combine
multiple stylesheets.
* bug 40498: Don't output empty "@media print { }" blocks.
* bug 40500: Don't ignore media-type for urls in debug mode.
Approach:
* Re-use the same <style> tag so that we stay under the 31
stylesheet limit in IE. Unless the to-be-added css text from
the being-loaded module contains @import, in which case we do
create a new <style> tag and then re-use that one from that
point on (bug 31676).
* Return stylesheets as arrays, instead of a concatenated string.
This fixes bug 35562, because @import only works when at the
top of a stylesheet. By not unconditionally concatenating files
within a module on the server side already, @import will work
in e.g. module 'site' that contains 2 wiki pages.
This is normalized in ResourceLoader::makeCombinedStyles(),
so far only ResourceLoaderWikiModule makes use of this.
Misc. clean up and bug fixes:
* Reducing usage of jQuery() and mw.html.element() where
native DOM would be very simple and faster. Aside from
simplicity and speed, this is also working towards a more
stand-alone ResourceLoader.
* Trim server output a little bit more
- Redundant new line after minify-css (it is now an array, so
no need to keep space afterwards)
- Redundant semi-colon after minify-js if it ends in a colon
* Allow space in styleTest.css.php
* Clean up and extend unit tests to cover for these features
and bug fixes.
* Don't set styleEl.rel = 'stylesheet'; that has no business
on a <style> tag.
* Fix bug in mw.loader's addStyleTag(). It turns out IE6
has an odd security measure that does not allow manipulation
of elements (at least style tags) that are created by a
different script (even if that script was served from the same
domain/origin etc.). We didn't ran into this before because
we only created new style tags, never appended to them. Now
that we do, this came up. Took a while to figure out because
it was created by mediawiki.js but it calls jQuery which did
the actual dom insertion. Odd thing is, we load jquery.js and
mediawiki.js in the same request even...
Without this all css-url related mw.loader tests would fail
in IE6.
* mediawiki.js and mediawiki.test.js now pass jshint again.
Tested (and passing qunit/?module=mediawiki; 123 of 123):
* Chrome 14, 21
* Firefox 3.0, 3.6, 4, 7, 14, 15, 16beta
* IE 6, 7, 8, 9
* Safari 4.0, 5.0, 5.1
* Opera 10.0, 11.1, 11.5, 11.6, 12.0, 12.5beta
* iPhone 3GS / iOS 3.0 / Mobile Safari 4.0
iPhone 4 / iOS 4.0.1 / Mobile Safari 4.0.5
iPhone 4S / iOS 6.0 Beta / Mobile Safari 6.0
Change-Id: I3e8227ddb87fd9441071ca935439fc6467751dab
2012-07-25 21:20:21 +00:00
// Wrap styles into @media groups as needed and flatten into a numerical array
$styles = array (
'css' => self :: makeCombinedStyles ( $stylePairs )
);
2012-06-14 00:46:39 +00:00
}
Fix the fixme on r88053: dependency handling was broken in debug mode in certain cases. More specifically, if A is a file module that depends on B, B is a wiki module that depends on C and C is a file module, the loading order is CBA (correct) in production mode but was BCA (wrong) in debug mode. Fixed this by URL-ifying scripts and styles for those modules in debug mode, as I said to on CR. What this means is that the initial debug=true request for a module will now always return arrays of URLs, never the JS or CSS itself. This was already the case for file modules (which returned arrays of URLs to the raw files), but not for other modules (which returned the JS and CSS itself). So for non-file modules, load.php?modules=foo&debug=true now returns some JS that instructs the loader to fetch the module's JS from load.php?modules=foo&debug=true&only=scripts and the CSS from ...&only=styles .
* Removed the magic behavior where ResourceLoaderModule::getScripts() and getStyles() could return an array of URLs where the documentation said they should return a JS/CSS string. Because I didn't restructure the calling code too much, the old magical behavior should still work.
* Instead, move this behavior to getScriptURLsForDebug() and getStyleURLsForDebug(). The default implementation constructs a single URL for a load.php request for the module with debug=true&only=scripts (or styles). The URL building code duplicates some things from OutputPage::makeResourceLoaderLink(), I'll clean that up later. ResourceLoaderFileModule overrides this method to return URLs to the raw files, using code that I removed from getScripts()/getStyles()
* Add ResourceLoaderModule::supportsURLLoading(), which returns true by default but may return false to indicate that a module does not support loading via a URL. This is needed to respect $this->debugRaw in ResourceLoaderFileModule (set to true for jquery and mediawiki), and obviously for the startup module as well, because we get bootstrapping problems otherwise (can't call mw.loader.implement() when the code for mw.loader isn't loaded yet)
2011-09-13 17:13:53 +00:00
}
2010-12-23 20:14:18 +00:00
}
2010-09-04 12:53:01 +00:00
2010-12-23 20:14:18 +00:00
// Messages
$messagesBlob = isset ( $blobs [ $name ] ) ? $blobs [ $name ] : '{}' ;
// Append output
switch ( $context -> getOnly () ) {
case 'scripts' :
2011-05-14 12:15:58 +00:00
if ( is_string ( $scripts ) ) {
2011-07-28 05:48:57 +00:00
// Load scripts raw...
2011-05-14 12:15:58 +00:00
$out .= $scripts ;
2011-06-17 16:05:05 +00:00
} elseif ( is_array ( $scripts ) ) {
2011-07-28 05:48:57 +00:00
// ...except when $scripts is an array of URLs
2011-05-14 12:15:58 +00:00
$out .= self :: makeLoaderImplementScript ( $name , $scripts , array (), array () );
}
2010-12-23 20:14:18 +00:00
break ;
case 'styles' :
ResourceLoader: Refactor style loading
Fixes:
* bug 31676: Work around IE stylesheet limit.
* bug 35562: @import styles broken in modules that combine
multiple stylesheets.
* bug 40498: Don't output empty "@media print { }" blocks.
* bug 40500: Don't ignore media-type for urls in debug mode.
Approach:
* Re-use the same <style> tag so that we stay under the 31
stylesheet limit in IE. Unless the to-be-added css text from
the being-loaded module contains @import, in which case we do
create a new <style> tag and then re-use that one from that
point on (bug 31676).
* Return stylesheets as arrays, instead of a concatenated string.
This fixes bug 35562, because @import only works when at the
top of a stylesheet. By not unconditionally concatenating files
within a module on the server side already, @import will work
in e.g. module 'site' that contains 2 wiki pages.
This is normalized in ResourceLoader::makeCombinedStyles(),
so far only ResourceLoaderWikiModule makes use of this.
Misc. clean up and bug fixes:
* Reducing usage of jQuery() and mw.html.element() where
native DOM would be very simple and faster. Aside from
simplicity and speed, this is also working towards a more
stand-alone ResourceLoader.
* Trim server output a little bit more
- Redundant new line after minify-css (it is now an array, so
no need to keep space afterwards)
- Redundant semi-colon after minify-js if it ends in a colon
* Allow space in styleTest.css.php
* Clean up and extend unit tests to cover for these features
and bug fixes.
* Don't set styleEl.rel = 'stylesheet'; that has no business
on a <style> tag.
* Fix bug in mw.loader's addStyleTag(). It turns out IE6
has an odd security measure that does not allow manipulation
of elements (at least style tags) that are created by a
different script (even if that script was served from the same
domain/origin etc.). We didn't ran into this before because
we only created new style tags, never appended to them. Now
that we do, this came up. Took a while to figure out because
it was created by mediawiki.js but it calls jQuery which did
the actual dom insertion. Odd thing is, we load jquery.js and
mediawiki.js in the same request even...
Without this all css-url related mw.loader tests would fail
in IE6.
* mediawiki.js and mediawiki.test.js now pass jshint again.
Tested (and passing qunit/?module=mediawiki; 123 of 123):
* Chrome 14, 21
* Firefox 3.0, 3.6, 4, 7, 14, 15, 16beta
* IE 6, 7, 8, 9
* Safari 4.0, 5.0, 5.1
* Opera 10.0, 11.1, 11.5, 11.6, 12.0, 12.5beta
* iPhone 3GS / iOS 3.0 / Mobile Safari 4.0
iPhone 4 / iOS 4.0.1 / Mobile Safari 4.0.5
iPhone 4S / iOS 6.0 Beta / Mobile Safari 6.0
Change-Id: I3e8227ddb87fd9441071ca935439fc6467751dab
2012-07-25 21:20:21 +00:00
// We no longer seperate into media, they are all combined now with
// custom media type groups into @media .. {} sections as part of the css string.
// Module returns either an empty array or a numerical array with css strings.
$out .= isset ( $styles [ 'css' ] ) ? implode ( '' , $styles [ 'css' ] ) : '' ;
2010-12-23 20:14:18 +00:00
break ;
case 'messages' :
$out .= self :: makeMessageSetScript ( new XmlJsCode ( $messagesBlob ) );
break ;
default :
2012-06-13 23:50:13 +00:00
$out .= self :: makeLoaderImplementScript (
$name ,
$scripts ,
2012-06-14 00:46:39 +00:00
$styles ,
2012-06-13 23:50:13 +00:00
new XmlJsCode ( $messagesBlob )
);
2010-12-23 20:14:18 +00:00
break ;
}
} catch ( Exception $e ) {
2013-10-09 00:20:41 +00:00
MWExceptionHandler :: logException ( $e );
2013-05-02 01:27:58 +00:00
wfDebugLog ( 'resourceloader' , __METHOD__ . " : generating module package failed: $e " );
$this -> hasErrors = true ;
2010-12-23 20:14:18 +00:00
// Add exception to the output as a comment
2013-08-16 23:20:26 +00:00
$exceptions .= self :: formatException ( $e );
2010-10-20 00:22:25 +00:00
2010-12-23 20:14:18 +00:00
// Register module as missing
$missing [] = $name ;
unset ( $modules [ $name ] );
}
2012-05-11 19:16:29 +00:00
$isRaw |= $module -> isRaw ();
2010-09-17 11:45:49 +00:00
wfProfileOut ( __METHOD__ . '-' . $name );
2010-09-04 04:00:09 +00:00
}
2010-09-04 12:53:01 +00:00
2010-09-24 18:49:19 +00:00
// Update module states
2012-05-11 19:16:29 +00:00
if ( $context -> shouldIncludeScripts () && ! $context -> getRaw () && ! $isRaw ) {
2010-09-24 18:49:19 +00:00
// Set the state of modules loaded as only scripts to ready
2012-05-11 19:16:29 +00:00
if ( count ( $modules ) && $context -> getOnly () === 'scripts' ) {
2011-06-17 16:05:05 +00:00
$out .= self :: makeLoaderStateScript (
2010-11-03 07:58:03 +00:00
array_fill_keys ( array_keys ( $modules ), 'ready' ) );
2010-09-24 18:49:19 +00:00
}
// Set the state of modules which were requested but unavailable as missing
2010-09-24 22:10:25 +00:00
if ( is_array ( $missing ) && count ( $missing ) ) {
2010-09-24 18:49:19 +00:00
$out .= self :: makeLoaderStateScript ( array_fill_keys ( $missing , 'missing' ) );
2010-09-04 04:00:09 +00:00
}
}
2010-09-04 12:53:01 +00:00
2011-02-10 16:44:57 +00:00
if ( ! $context -> getDebug () ) {
2010-09-24 18:49:19 +00:00
if ( $context -> getOnly () === 'styles' ) {
2011-02-10 16:44:57 +00:00
$out = $this -> filter ( 'minify-css' , $out );
2010-09-04 04:00:09 +00:00
} else {
2011-02-10 16:44:57 +00:00
$out = $this -> filter ( 'minify-js' , $out );
2010-09-04 04:00:09 +00:00
}
}
2011-06-17 16:05:05 +00:00
2011-02-10 16:44:57 +00:00
wfProfileOut ( __METHOD__ );
return $exceptions . $out ;
2010-09-04 04:00:09 +00:00
}
2010-10-20 00:22:25 +00:00
2010-09-29 19:04:04 +00:00
/* Static Methods */
2010-10-20 00:22:25 +00:00
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
/**
2011-06-17 16:05:05 +00:00
* Returns JS code to call to mw . loader . implement for a module with
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
* given properties .
*
2013-03-11 17:15:01 +00:00
* @ param string $name Module name
2011-05-14 12:15:58 +00:00
* @ param $scripts Mixed : List of URLs to JavaScript files or String of JavaScript code
2012-02-13 15:17:15 +00:00
* @ param $styles Mixed : Array of CSS strings keyed by media type , or an array of lists of URLs to
2011-05-14 12:15:58 +00:00
* CSS files keyed by media type
2011-06-17 16:05:05 +00:00
* @ param $messages Mixed : List of messages associated with this module . May either be an
2010-11-05 18:33:50 +00:00
* associative array mapping message key to value , or a JSON - encoded message blob containing
* the same data , wrapped in an XmlJsCode object .
2011-05-21 17:45:20 +00:00
*
2012-10-07 23:35:26 +00:00
* @ throws MWException
2011-05-21 17:45:20 +00:00
* @ return string
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
*/
2010-09-24 18:49:19 +00:00
public static function makeLoaderImplementScript ( $name , $scripts , $styles , $messages ) {
2011-05-14 12:15:58 +00:00
if ( is_string ( $scripts ) ) {
2012-02-13 15:17:15 +00:00
$scripts = new XmlJsCode ( " function () { \n { $scripts } \n } " );
2011-06-17 16:05:05 +00:00
} elseif ( ! is_array ( $scripts ) ) {
2011-05-15 12:36:21 +00:00
throw new MWException ( 'Invalid scripts error. Array of URLs or string of code expected.' );
2010-09-24 18:49:19 +00:00
}
2011-06-17 16:05:05 +00:00
return Xml :: encodeJsCall (
'mw.loader.implement' ,
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
array (
$name ,
2011-05-14 12:15:58 +00:00
$scripts ,
2012-02-13 15:17:15 +00:00
// Force objects. mw.loader.implement requires them to be javascript objects.
// Although these variables are associative arrays, which become javascript
// objects through json_encode. In many cases they will be empty arrays, and
// PHP/json_encode() consider empty arrays to be numerical arrays and
// output javascript "[]" instead of "{}". This fixes that.
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
( object ) $styles ,
( object ) $messages
2013-02-21 02:30:45 +00:00
),
ResourceLoader :: inDebugMode ()
);
2010-09-24 18:49:19 +00:00
}
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
/**
* Returns JS code which , when called , will register a given list of messages .
*
2010-11-05 18:33:50 +00:00
* @ param $messages Mixed : Either an associative array mapping message key to value , or a
* JSON - encoded message blob containing the same data , wrapped in an XmlJsCode object .
2011-05-21 17:45:20 +00:00
*
* @ return string
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
*/
2010-09-24 18:49:19 +00:00
public static function makeMessageSetScript ( $messages ) {
2011-02-19 22:59:44 +00:00
return Xml :: encodeJsCall ( 'mw.messages.set' , array ( ( object ) $messages ) );
2010-09-24 18:49:19 +00:00
}
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
/**
2011-06-17 16:05:05 +00:00
* Combines an associative array mapping media type to CSS into a
2012-07-10 12:48:06 +00:00
* single stylesheet with " @media " blocks .
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
*
2013-03-11 17:15:01 +00:00
* @ param array $stylePairs Array keyed by media type containing ( arrays of ) CSS strings .
2011-05-21 17:45:20 +00:00
*
ResourceLoader: Refactor style loading
Fixes:
* bug 31676: Work around IE stylesheet limit.
* bug 35562: @import styles broken in modules that combine
multiple stylesheets.
* bug 40498: Don't output empty "@media print { }" blocks.
* bug 40500: Don't ignore media-type for urls in debug mode.
Approach:
* Re-use the same <style> tag so that we stay under the 31
stylesheet limit in IE. Unless the to-be-added css text from
the being-loaded module contains @import, in which case we do
create a new <style> tag and then re-use that one from that
point on (bug 31676).
* Return stylesheets as arrays, instead of a concatenated string.
This fixes bug 35562, because @import only works when at the
top of a stylesheet. By not unconditionally concatenating files
within a module on the server side already, @import will work
in e.g. module 'site' that contains 2 wiki pages.
This is normalized in ResourceLoader::makeCombinedStyles(),
so far only ResourceLoaderWikiModule makes use of this.
Misc. clean up and bug fixes:
* Reducing usage of jQuery() and mw.html.element() where
native DOM would be very simple and faster. Aside from
simplicity and speed, this is also working towards a more
stand-alone ResourceLoader.
* Trim server output a little bit more
- Redundant new line after minify-css (it is now an array, so
no need to keep space afterwards)
- Redundant semi-colon after minify-js if it ends in a colon
* Allow space in styleTest.css.php
* Clean up and extend unit tests to cover for these features
and bug fixes.
* Don't set styleEl.rel = 'stylesheet'; that has no business
on a <style> tag.
* Fix bug in mw.loader's addStyleTag(). It turns out IE6
has an odd security measure that does not allow manipulation
of elements (at least style tags) that are created by a
different script (even if that script was served from the same
domain/origin etc.). We didn't ran into this before because
we only created new style tags, never appended to them. Now
that we do, this came up. Took a while to figure out because
it was created by mediawiki.js but it calls jQuery which did
the actual dom insertion. Odd thing is, we load jquery.js and
mediawiki.js in the same request even...
Without this all css-url related mw.loader tests would fail
in IE6.
* mediawiki.js and mediawiki.test.js now pass jshint again.
Tested (and passing qunit/?module=mediawiki; 123 of 123):
* Chrome 14, 21
* Firefox 3.0, 3.6, 4, 7, 14, 15, 16beta
* IE 6, 7, 8, 9
* Safari 4.0, 5.0, 5.1
* Opera 10.0, 11.1, 11.5, 11.6, 12.0, 12.5beta
* iPhone 3GS / iOS 3.0 / Mobile Safari 4.0
iPhone 4 / iOS 4.0.1 / Mobile Safari 4.0.5
iPhone 4S / iOS 6.0 Beta / Mobile Safari 6.0
Change-Id: I3e8227ddb87fd9441071ca935439fc6467751dab
2012-07-25 21:20:21 +00:00
* @ return Array
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
*/
ResourceLoader: Refactor style loading
Fixes:
* bug 31676: Work around IE stylesheet limit.
* bug 35562: @import styles broken in modules that combine
multiple stylesheets.
* bug 40498: Don't output empty "@media print { }" blocks.
* bug 40500: Don't ignore media-type for urls in debug mode.
Approach:
* Re-use the same <style> tag so that we stay under the 31
stylesheet limit in IE. Unless the to-be-added css text from
the being-loaded module contains @import, in which case we do
create a new <style> tag and then re-use that one from that
point on (bug 31676).
* Return stylesheets as arrays, instead of a concatenated string.
This fixes bug 35562, because @import only works when at the
top of a stylesheet. By not unconditionally concatenating files
within a module on the server side already, @import will work
in e.g. module 'site' that contains 2 wiki pages.
This is normalized in ResourceLoader::makeCombinedStyles(),
so far only ResourceLoaderWikiModule makes use of this.
Misc. clean up and bug fixes:
* Reducing usage of jQuery() and mw.html.element() where
native DOM would be very simple and faster. Aside from
simplicity and speed, this is also working towards a more
stand-alone ResourceLoader.
* Trim server output a little bit more
- Redundant new line after minify-css (it is now an array, so
no need to keep space afterwards)
- Redundant semi-colon after minify-js if it ends in a colon
* Allow space in styleTest.css.php
* Clean up and extend unit tests to cover for these features
and bug fixes.
* Don't set styleEl.rel = 'stylesheet'; that has no business
on a <style> tag.
* Fix bug in mw.loader's addStyleTag(). It turns out IE6
has an odd security measure that does not allow manipulation
of elements (at least style tags) that are created by a
different script (even if that script was served from the same
domain/origin etc.). We didn't ran into this before because
we only created new style tags, never appended to them. Now
that we do, this came up. Took a while to figure out because
it was created by mediawiki.js but it calls jQuery which did
the actual dom insertion. Odd thing is, we load jquery.js and
mediawiki.js in the same request even...
Without this all css-url related mw.loader tests would fail
in IE6.
* mediawiki.js and mediawiki.test.js now pass jshint again.
Tested (and passing qunit/?module=mediawiki; 123 of 123):
* Chrome 14, 21
* Firefox 3.0, 3.6, 4, 7, 14, 15, 16beta
* IE 6, 7, 8, 9
* Safari 4.0, 5.0, 5.1
* Opera 10.0, 11.1, 11.5, 11.6, 12.0, 12.5beta
* iPhone 3GS / iOS 3.0 / Mobile Safari 4.0
iPhone 4 / iOS 4.0.1 / Mobile Safari 4.0.5
iPhone 4S / iOS 6.0 Beta / Mobile Safari 6.0
Change-Id: I3e8227ddb87fd9441071ca935439fc6467751dab
2012-07-25 21:20:21 +00:00
private static function makeCombinedStyles ( array $stylePairs ) {
$out = array ();
foreach ( $stylePairs as $media => $styles ) {
// ResourceLoaderFileModule::getStyle can return the styles
// as a string or an array of strings. This is to allow separation in
// the front-end.
2013-04-02 20:28:40 +00:00
$styles = ( array ) $styles ;
ResourceLoader: Refactor style loading
Fixes:
* bug 31676: Work around IE stylesheet limit.
* bug 35562: @import styles broken in modules that combine
multiple stylesheets.
* bug 40498: Don't output empty "@media print { }" blocks.
* bug 40500: Don't ignore media-type for urls in debug mode.
Approach:
* Re-use the same <style> tag so that we stay under the 31
stylesheet limit in IE. Unless the to-be-added css text from
the being-loaded module contains @import, in which case we do
create a new <style> tag and then re-use that one from that
point on (bug 31676).
* Return stylesheets as arrays, instead of a concatenated string.
This fixes bug 35562, because @import only works when at the
top of a stylesheet. By not unconditionally concatenating files
within a module on the server side already, @import will work
in e.g. module 'site' that contains 2 wiki pages.
This is normalized in ResourceLoader::makeCombinedStyles(),
so far only ResourceLoaderWikiModule makes use of this.
Misc. clean up and bug fixes:
* Reducing usage of jQuery() and mw.html.element() where
native DOM would be very simple and faster. Aside from
simplicity and speed, this is also working towards a more
stand-alone ResourceLoader.
* Trim server output a little bit more
- Redundant new line after minify-css (it is now an array, so
no need to keep space afterwards)
- Redundant semi-colon after minify-js if it ends in a colon
* Allow space in styleTest.css.php
* Clean up and extend unit tests to cover for these features
and bug fixes.
* Don't set styleEl.rel = 'stylesheet'; that has no business
on a <style> tag.
* Fix bug in mw.loader's addStyleTag(). It turns out IE6
has an odd security measure that does not allow manipulation
of elements (at least style tags) that are created by a
different script (even if that script was served from the same
domain/origin etc.). We didn't ran into this before because
we only created new style tags, never appended to them. Now
that we do, this came up. Took a while to figure out because
it was created by mediawiki.js but it calls jQuery which did
the actual dom insertion. Odd thing is, we load jquery.js and
mediawiki.js in the same request even...
Without this all css-url related mw.loader tests would fail
in IE6.
* mediawiki.js and mediawiki.test.js now pass jshint again.
Tested (and passing qunit/?module=mediawiki; 123 of 123):
* Chrome 14, 21
* Firefox 3.0, 3.6, 4, 7, 14, 15, 16beta
* IE 6, 7, 8, 9
* Safari 4.0, 5.0, 5.1
* Opera 10.0, 11.1, 11.5, 11.6, 12.0, 12.5beta
* iPhone 3GS / iOS 3.0 / Mobile Safari 4.0
iPhone 4 / iOS 4.0.1 / Mobile Safari 4.0.5
iPhone 4S / iOS 6.0 Beta / Mobile Safari 6.0
Change-Id: I3e8227ddb87fd9441071ca935439fc6467751dab
2012-07-25 21:20:21 +00:00
foreach ( $styles as $style ) {
$style = trim ( $style );
// Don't output an empty "@media print { }" block (bug 40498)
if ( $style !== '' ) {
// Transform the media type based on request params and config
// The way that this relies on $wgRequest to propagate request params is slightly evil
$media = OutputPage :: transformCssMedia ( $media );
if ( $media === '' || $media == 'all' ) {
$out [] = $style ;
2013-04-17 14:52:47 +00:00
} elseif ( is_string ( $media ) ) {
ResourceLoader: Refactor style loading
Fixes:
* bug 31676: Work around IE stylesheet limit.
* bug 35562: @import styles broken in modules that combine
multiple stylesheets.
* bug 40498: Don't output empty "@media print { }" blocks.
* bug 40500: Don't ignore media-type for urls in debug mode.
Approach:
* Re-use the same <style> tag so that we stay under the 31
stylesheet limit in IE. Unless the to-be-added css text from
the being-loaded module contains @import, in which case we do
create a new <style> tag and then re-use that one from that
point on (bug 31676).
* Return stylesheets as arrays, instead of a concatenated string.
This fixes bug 35562, because @import only works when at the
top of a stylesheet. By not unconditionally concatenating files
within a module on the server side already, @import will work
in e.g. module 'site' that contains 2 wiki pages.
This is normalized in ResourceLoader::makeCombinedStyles(),
so far only ResourceLoaderWikiModule makes use of this.
Misc. clean up and bug fixes:
* Reducing usage of jQuery() and mw.html.element() where
native DOM would be very simple and faster. Aside from
simplicity and speed, this is also working towards a more
stand-alone ResourceLoader.
* Trim server output a little bit more
- Redundant new line after minify-css (it is now an array, so
no need to keep space afterwards)
- Redundant semi-colon after minify-js if it ends in a colon
* Allow space in styleTest.css.php
* Clean up and extend unit tests to cover for these features
and bug fixes.
* Don't set styleEl.rel = 'stylesheet'; that has no business
on a <style> tag.
* Fix bug in mw.loader's addStyleTag(). It turns out IE6
has an odd security measure that does not allow manipulation
of elements (at least style tags) that are created by a
different script (even if that script was served from the same
domain/origin etc.). We didn't ran into this before because
we only created new style tags, never appended to them. Now
that we do, this came up. Took a while to figure out because
it was created by mediawiki.js but it calls jQuery which did
the actual dom insertion. Odd thing is, we load jquery.js and
mediawiki.js in the same request even...
Without this all css-url related mw.loader tests would fail
in IE6.
* mediawiki.js and mediawiki.test.js now pass jshint again.
Tested (and passing qunit/?module=mediawiki; 123 of 123):
* Chrome 14, 21
* Firefox 3.0, 3.6, 4, 7, 14, 15, 16beta
* IE 6, 7, 8, 9
* Safari 4.0, 5.0, 5.1
* Opera 10.0, 11.1, 11.5, 11.6, 12.0, 12.5beta
* iPhone 3GS / iOS 3.0 / Mobile Safari 4.0
iPhone 4 / iOS 4.0.1 / Mobile Safari 4.0.5
iPhone 4S / iOS 6.0 Beta / Mobile Safari 6.0
Change-Id: I3e8227ddb87fd9441071ca935439fc6467751dab
2012-07-25 21:20:21 +00:00
$out [] = " @media $media { \n " . str_replace ( " \n " , " \n \t " , " \t " . $style ) . " } " ;
}
// else: skip
}
2011-01-07 20:22:50 +00:00
}
2010-09-24 18:49:19 +00:00
}
return $out ;
}
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
/**
2011-06-17 16:05:05 +00:00
* Returns a JS call to mw . loader . state , which sets the state of a
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
* module or modules to a given value . Has two calling conventions :
*
* - ResourceLoader :: makeLoaderStateScript ( $name , $state ) :
* Set the state of a single module called $name to $state
*
* - ResourceLoader :: makeLoaderStateScript ( array ( $name => $state , ... ) ) :
* Set the state of modules with the given names to the given states
2011-05-21 17:45:20 +00:00
*
* @ param $name string
* @ param $state
*
* @ return string
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
*/
2010-09-24 18:49:19 +00:00
public static function makeLoaderStateScript ( $name , $state = null ) {
if ( is_array ( $name ) ) {
2011-02-19 22:59:44 +00:00
return Xml :: encodeJsCall ( 'mw.loader.state' , array ( $name ) );
2010-09-24 18:49:19 +00:00
} else {
2011-02-19 22:59:44 +00:00
return Xml :: encodeJsCall ( 'mw.loader.state' , array ( $name , $state ) );
2010-09-24 18:49:19 +00:00
}
}
2010-09-24 22:10:25 +00:00
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
/**
* Returns JS code which calls the script given by $script . The script will
2011-06-17 16:05:05 +00:00
* be called with local variables name , version , dependencies and group ,
* which will have values corresponding to $name , $version , $dependencies
* and $group as supplied .
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
*
2013-03-11 17:15:01 +00:00
* @ param string $name Module name
2010-11-05 18:33:50 +00:00
* @ param $version Integer : Module version number as a timestamp
2013-03-11 17:15:01 +00:00
* @ param array $dependencies List of module names on which this module depends
* @ param string $group Group which the module is in .
* @ param string $source Source of the module , or 'local' if not foreign .
* @ param string $script JavaScript code
2011-05-21 17:45:20 +00:00
*
* @ return string
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
*/
2011-07-26 21:10:34 +00:00
public static function makeCustomLoaderScript ( $name , $version , $dependencies , $group , $source , $script ) {
2010-09-24 21:03:29 +00:00
$script = str_replace ( " \n " , " \n \t " , trim ( $script ) );
2011-06-17 16:05:05 +00:00
return Xml :: encodeJsCall (
2012-02-13 15:17:15 +00:00
" ( function ( name, version, dependencies, group, source ) { \n \t $script\n } ) " ,
2011-07-26 21:10:34 +00:00
array ( $name , $version , $dependencies , $group , $source ) );
2010-09-24 21:03:29 +00:00
}
2010-09-24 22:10:25 +00:00
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
/**
2011-06-17 16:05:05 +00:00
* Returns JS code which calls mw . loader . register with the given
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
* parameters . Has three calling conventions :
*
2011-07-26 21:10:34 +00:00
* - ResourceLoader :: makeLoaderRegisterScript ( $name , $version , $dependencies , $group , $source ) :
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
* Register a single module .
*
* - ResourceLoader :: makeLoaderRegisterScript ( array ( $name1 , $name2 ) ) :
* Register modules with the given names .
*
* - ResourceLoader :: makeLoaderRegisterScript ( array (
2011-07-26 21:10:34 +00:00
* array ( $name1 , $version1 , $dependencies1 , $group1 , $source1 ),
* array ( $name2 , $version2 , $dependencies1 , $group2 , $source2 ),
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
* ...
* ) ) :
* Registers modules with the given names and parameters .
*
2013-03-11 17:15:01 +00:00
* @ param string $name Module name
2010-11-05 18:33:50 +00:00
* @ param $version Integer : Module version number as a timestamp
2013-03-11 17:15:01 +00:00
* @ param array $dependencies List of module names on which this module depends
* @ param string $group group which the module is in .
* @ param string $source source of the module , or 'local' if not foreign
2011-05-21 17:45:20 +00:00
*
* @ return string
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
*/
2011-06-17 16:05:05 +00:00
public static function makeLoaderRegisterScript ( $name , $version = null ,
2013-04-02 20:28:40 +00:00
$dependencies = null , $group = null , $source = null
) {
2010-09-24 21:03:29 +00:00
if ( is_array ( $name ) ) {
2011-05-10 23:17:13 +00:00
return Xml :: encodeJsCall ( 'mw.loader.register' , array ( $name ) );
2010-09-24 21:03:29 +00:00
} else {
2013-04-02 20:28:40 +00:00
$version = ( int ) $version > 1 ? ( int ) $version : 1 ;
2011-06-17 16:05:05 +00:00
return Xml :: encodeJsCall ( 'mw.loader.register' ,
2011-07-26 21:10:34 +00:00
array ( $name , $version , $dependencies , $group , $source ) );
}
}
/**
* Returns JS code which calls mw . loader . addSource () with the given
* parameters . Has two calling conventions :
2011-10-14 08:06:54 +00:00
*
2011-07-26 21:10:34 +00:00
* - ResourceLoader :: makeLoaderSourcesScript ( $id , $properties ) :
* Register a single source
2011-10-14 08:06:54 +00:00
*
2011-07-26 21:10:34 +00:00
* - ResourceLoader :: makeLoaderSourcesScript ( array ( $id1 => $props1 , $id2 => $props2 , ... ) );
* Register sources with the given IDs and properties .
2011-10-14 08:06:54 +00:00
*
2013-03-11 17:15:01 +00:00
* @ param string $id source ID
* @ param array $properties source properties ( see addSource ())
2011-10-01 04:15:07 +00:00
*
* @ return string
2011-07-26 21:10:34 +00:00
*/
public static function makeLoaderSourcesScript ( $id , $properties = null ) {
if ( is_array ( $id ) ) {
return Xml :: encodeJsCall ( 'mw.loader.addSource' , array ( $id ) );
} else {
return Xml :: encodeJsCall ( 'mw.loader.addSource' , array ( $id , $properties ) );
2010-09-24 21:03:29 +00:00
}
}
2010-09-24 22:10:25 +00:00
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
/**
2011-06-17 16:05:05 +00:00
* Returns JS code which runs given JS code if the client - side framework is
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
* present .
*
2013-03-11 17:15:01 +00:00
* @ param string $script JavaScript code
2011-05-21 17:45:20 +00:00
*
* @ return string
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
*/
2010-09-24 22:10:25 +00:00
public static function makeLoaderConditionalScript ( $script ) {
2012-03-04 22:56:43 +00:00
return " if(window.mw) { \n " . trim ( $script ) . " \n } " ;
2010-09-24 22:10:25 +00:00
}
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
/**
2011-06-17 16:05:05 +00:00
* Returns JS code which will set the MediaWiki configuration array to
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
* the given value .
*
2013-03-11 17:15:01 +00:00
* @ param array $configuration List of configuration values keyed by variable name
2011-05-21 17:45:20 +00:00
*
* @ return string
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
*/
2010-09-24 22:10:25 +00:00
public static function makeConfigSetScript ( array $configuration ) {
2013-02-21 02:30:45 +00:00
return Xml :: encodeJsCall ( 'mw.config.set' , array ( $configuration ), ResourceLoader :: inDebugMode () );
2010-09-24 22:10:25 +00:00
}
2011-05-21 17:45:20 +00:00
2011-05-05 13:46:47 +00:00
/**
* Convert an array of module names to a packed query string .
2011-06-17 16:05:05 +00:00
*
2011-05-05 13:46:47 +00:00
* For example , array ( 'foo.bar' , 'foo.baz' , 'bar.baz' , 'bar.quux' )
2011-05-26 09:49:45 +00:00
* becomes 'foo.bar,baz|bar.baz,quux'
2013-03-11 17:15:01 +00:00
* @ param array $modules of module names ( strings )
2011-05-05 13:46:47 +00:00
* @ return string Packed query string
*/
public static function makePackedModulesString ( $modules ) {
$groups = array (); // array( prefix => array( suffixes ) )
foreach ( $modules as $module ) {
$pos = strrpos ( $module , '.' );
$prefix = $pos === false ? '' : substr ( $module , 0 , $pos );
$suffix = $pos === false ? $module : substr ( $module , $pos + 1 );
$groups [ $prefix ][] = $suffix ;
}
2011-05-21 17:45:20 +00:00
2011-05-05 13:46:47 +00:00
$arr = array ();
foreach ( $groups as $prefix => $suffixes ) {
$p = $prefix === '' ? '' : $prefix . '.' ;
$arr [] = $p . implode ( ',' , $suffixes );
}
2011-05-09 13:10:06 +00:00
$str = implode ( '|' , $arr );
2011-05-26 09:49:45 +00:00
return $str ;
2011-05-05 13:46:47 +00:00
}
2011-05-21 17:45:20 +00:00
2010-11-05 20:36:13 +00:00
/**
* Determine whether debug mode was requested
* Order of priority is 1 ) request param , 2 ) cookie , 3 ) $wg setting
* @ return bool
*/
public static function inDebugMode () {
global $wgRequest , $wgResourceLoaderDebug ;
static $retval = null ;
2011-05-21 17:45:20 +00:00
if ( ! is_null ( $retval ) ) {
2010-11-05 20:36:13 +00:00
return $retval ;
2011-05-21 17:45:20 +00:00
}
2010-11-05 20:36:13 +00:00
return $retval = $wgRequest -> getFuzzyBool ( 'debug' ,
$wgRequest -> getCookie ( 'resourceLoaderDebug' , '' , $wgResourceLoaderDebug ) );
}
2011-09-13 20:36:24 +00:00
/**
* Build a load . php URL
2013-03-11 17:15:01 +00:00
* @ param array $modules of module names ( strings )
* @ param string $lang Language code
* @ param string $skin Skin name
* @ param string | null $user User name . If null , the & user = parameter is omitted
* @ param string | null $version Versioning timestamp
* @ param bool $debug Whether the request should be in debug mode
* @ param string | null $only & only = parameter
* @ param bool $printable Printable mode
* @ param bool $handheld Handheld mode
* @ param array $extraQuery Extra query parameters to add
2011-09-13 20:36:24 +00:00
* @ return string URL to load . php . May be protocol - relative ( if $wgLoadScript is procol - relative )
*/
public static function makeLoaderURL ( $modules , $lang , $skin , $user = null , $version = null , $debug = false , $only = null ,
$printable = false , $handheld = false , $extraQuery = array () ) {
2011-10-01 05:03:10 +00:00
global $wgLoadScript ;
2011-09-13 20:36:24 +00:00
$query = self :: makeLoaderQuery ( $modules , $lang , $skin , $user , $version , $debug ,
$only , $printable , $handheld , $extraQuery
);
2011-10-14 08:06:54 +00:00
2011-09-13 20:36:24 +00:00
// Prevent the IE6 extension check from being triggered (bug 28840)
2011-10-01 05:03:10 +00:00
// by appending a character that's invalid in Windows extensions ('*')
return wfExpandUrl ( wfAppendQuery ( $wgLoadScript , $query ) . '&*' , PROTO_RELATIVE );
2011-09-13 20:36:24 +00:00
}
/**
* Build a query array ( array representation of query string ) for load . php . Helper
* function for makeLoaderURL () .
2013-07-01 18:01:11 +00:00
*
* @ param array $modules
* @ param string $lang
* @ param string $skin
* @ param string $user
* @ param string $version
* @ param bool $debug
* @ param string $only
* @ param bool $printable
* @ param bool $handheld
* @ param array $extraQuery
*
2011-09-13 20:36:24 +00:00
* @ return array
*/
public static function makeLoaderQuery ( $modules , $lang , $skin , $user = null , $version = null , $debug = false , $only = null ,
$printable = false , $handheld = false , $extraQuery = array () ) {
$query = array (
'modules' => self :: makePackedModulesString ( $modules ),
'lang' => $lang ,
'skin' => $skin ,
'debug' => $debug ? 'true' : 'false' ,
);
if ( $user !== null ) {
$query [ 'user' ] = $user ;
}
if ( $version !== null ) {
$query [ 'version' ] = $version ;
}
if ( $only !== null ) {
$query [ 'only' ] = $only ;
}
if ( $printable ) {
$query [ 'printable' ] = 1 ;
}
if ( $handheld ) {
$query [ 'handheld' ] = 1 ;
}
$query += $extraQuery ;
2011-10-14 08:06:54 +00:00
2011-09-13 20:36:24 +00:00
// Make queries uniform in order
ksort ( $query );
return $query ;
}
2012-05-09 21:10:33 +00:00
/**
* Check a module name for validity .
*
* Module names may not contain pipes ( | ), commas (,) or exclamation marks ( ! ) and can be
* at most 255 bytes .
*
2013-03-11 17:15:01 +00:00
* @ param string $moduleName Module name to check
2012-05-09 21:10:33 +00:00
* @ return bool Whether $moduleName is a valid module name
*/
public static function isValidModuleName ( $moduleName ) {
return ! preg_match ( '/[|,!]/' , $moduleName ) && strlen ( $moduleName ) <= 255 ;
}
2013-09-28 07:40:03 +00:00
/**
* Returns LESS compiler set up for use with MediaWiki
*
* @ since 1.22
* @ return lessc
*/
public static function getLessCompiler () {
global $wgResourceLoaderLESSFunctions , $wgResourceLoaderLESSImportPaths ;
$less = new lessc ();
$less -> setPreserveComments ( true );
$less -> setVariables ( self :: getLESSVars () );
$less -> setImportDir ( $wgResourceLoaderLESSImportPaths );
foreach ( $wgResourceLoaderLESSFunctions as $name => $func ) {
$less -> registerFunction ( $name , $func );
}
return $less ;
}
/**
* Get global LESS variables .
*
* $since 1.22
* @ return array : Map of variable names to string CSS values .
*/
public static function getLESSVars () {
global $wgResourceLoaderLESSVars ;
static $lessVars = null ;
if ( $lessVars === null ) {
$lessVars = $wgResourceLoaderLESSVars ;
// Sort by key to ensure consistent hashing for cache lookups.
ksort ( $lessVars );
}
return $lessVars ;
}
2010-09-17 11:45:49 +00:00
}