2010-09-04 04:00:09 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* 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-08-01 23:20:58 +00:00
|
|
|
protected static $filterCacheVersion = 5;
|
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();
|
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();
|
|
|
|
|
|
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
|
|
|
*
|
2010-11-05 18:33:50 +00:00
|
|
|
* @param $modules Array: List of module names to preload information for
|
|
|
|
|
* @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
|
|
|
*
|
2010-11-05 18:33:50 +00:00
|
|
|
* @param $filter String: Name of filter to run
|
|
|
|
|
* @param $data String: 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 ) ) {
|
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 {
|
|
|
|
|
switch ( $filter ) {
|
|
|
|
|
case 'minify-js':
|
2011-03-14 13:24:30 +00:00
|
|
|
$result = JavaScriptMinifier::minify( $data,
|
|
|
|
|
$wgResourceLoaderMinifierStatementsOnOwnLine,
|
|
|
|
|
$wgResourceLoaderMinifierMaxLineLength
|
|
|
|
|
);
|
2011-02-18 05:55:09 +00:00
|
|
|
$result .= "\n\n/* cache key: $key */\n";
|
2010-09-04 04:00:09 +00:00
|
|
|
break;
|
|
|
|
|
case 'minify-css':
|
|
|
|
|
$result = CSSMin::minify( $data );
|
2011-02-18 05:55:09 +00:00
|
|
|
$result .= "\n\n/* cache key: $key */\n";
|
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 );
|
2010-09-04 04:00:09 +00:00
|
|
|
} catch ( Exception $exception ) {
|
2010-12-23 20:14:18 +00:00
|
|
|
// Return exception as a comment
|
2011-01-09 12:29:02 +00:00
|
|
|
$result = "/*\n{$exception->__toString()}\n*/\n";
|
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() {
|
2011-07-26 21:10:34 +00:00
|
|
|
global $IP, $wgResourceModules, $wgResourceLoaderSources, $wgLoadScript;
|
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
|
|
|
|
|
$this->register( include( "$IP/resources/Resources.php" ) );
|
|
|
|
|
// 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
|
|
|
|
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
|
2011-06-17 16:05:05 +00:00
|
|
|
* @param $info Module info array. For backwards compatibility with 1.17alpha,
|
|
|
|
|
* 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
|
* 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 ( is_array( $name ) ) {
|
2010-09-04 04:00:09 +00:00
|
|
|
foreach ( $name as $key => $value ) {
|
2010-09-29 19:04:04 +00:00
|
|
|
$this->register( $key, $value );
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2011-02-08 22:01:34 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2010-09-04 04:00:09 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Disallow duplicate registrations
|
* 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] ) ) {
|
2010-09-04 04:00:09 +00:00
|
|
|
// A module has already been registered by this name
|
2010-10-20 00:22:25 +00:00
|
|
|
throw new MWException(
|
2011-06-17 16:05:05 +00:00
|
|
|
'ResourceLoader duplicate registration error. ' .
|
2010-11-03 07:58:03 +00:00
|
|
|
'Another module has already been registered as ' . $name
|
2010-10-20 00:22:25 +00:00
|
|
|
);
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2011-06-17 16:05:05 +00:00
|
|
|
|
2011-05-05 13:46:47 +00:00
|
|
|
// Check $name for illegal characters
|
2011-05-14 09:24:24 +00:00
|
|
|
if ( preg_match( '/[|,!]/', $name ) ) {
|
|
|
|
|
throw new MWException( "ResourceLoader module name '$name' is invalid. Names may not contain pipes (|), commas (,) or exclamation marks (!)" );
|
2011-05-05 13:46:47 +00:00
|
|
|
}
|
2010-10-20 00:22:25 +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
|
|
|
// Attach module
|
|
|
|
|
if ( is_object( $info ) ) {
|
|
|
|
|
// Old calling convention
|
|
|
|
|
// Validate the input
|
|
|
|
|
if ( !( $info instanceof ResourceLoaderModule ) ) {
|
2011-06-17 16:05:05 +00:00
|
|
|
throw new MWException( 'ResourceLoader invalid module error. ' .
|
* 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
|
|
|
'Instances of ResourceLoaderModule expected.' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->moduleInfos[$name] = array( 'object' => $info );
|
2010-11-30 01:40:22 +00:00
|
|
|
$info->setName( $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->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
|
|
|
|
2011-07-26 21:10:34 +00:00
|
|
|
/**
|
|
|
|
|
* Add a foreign source of modules.
|
|
|
|
|
*
|
|
|
|
|
* Source properties:
|
|
|
|
|
* 'loadScript': URL (either fully-qualified or protocol-relative) of load.php for this source
|
|
|
|
|
*
|
|
|
|
|
* @param $id Mixed: source ID (string), or array( id1 => props1, id2 => props2, ... )
|
|
|
|
|
* @param $properties Array: source properties
|
|
|
|
|
*/
|
|
|
|
|
public function addSource( $id, $properties = null) {
|
|
|
|
|
// 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
|
|
|
}
|
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
|
|
|
*
|
2010-11-05 18:33:50 +00:00
|
|
|
* @param $name String: 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];
|
|
|
|
|
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
|
|
|
|
|
*
|
|
|
|
|
* @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 ) {
|
2010-10-07 19:35:05 +00:00
|
|
|
global $wgResourceLoaderMaxage, $wgCacheEpoch;
|
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__ );
|
2011-01-09 12:29:02 +00:00
|
|
|
$exceptions = '';
|
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] ) ) {
|
|
|
|
|
$modules[$name] = $this->getModule( $name );
|
2010-09-04 04:00:09 +00:00
|
|
|
} else {
|
|
|
|
|
$missing[] = $name;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2011-06-17 16:05:05 +00:00
|
|
|
// If a version wasn't specified we need a shorter expiry time for updates
|
2010-11-03 07:58:03 +00:00
|
|
|
// to propagate to clients quickly
|
2010-09-13 23:19:05 +00:00
|
|
|
if ( is_null( $context->getVersion() ) ) {
|
2010-10-19 20:45:02 +00:00
|
|
|
$maxage = $wgResourceLoaderMaxage['unversioned']['client'];
|
2010-09-22 20:44:12 +00:00
|
|
|
$smaxage = $wgResourceLoaderMaxage['unversioned']['server'];
|
2010-09-13 23:19:05 +00:00
|
|
|
}
|
2011-06-17 16:05:05 +00:00
|
|
|
// If a version was specified we can use a longer expiry time since changing
|
2010-11-03 07:58:03 +00:00
|
|
|
// version numbers causes cache misses
|
2010-09-13 23:19:05 +00:00
|
|
|
else {
|
2010-10-19 20:45:02 +00:00
|
|
|
$maxage = $wgResourceLoaderMaxage['versioned']['client'];
|
2010-09-22 20:44:12 +00:00
|
|
|
$smaxage = $wgResourceLoaderMaxage['versioned']['server'];
|
2010-09-13 23:19:05 +00:00
|
|
|
}
|
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 );
|
|
|
|
|
} catch( Exception $e ) {
|
|
|
|
|
// Add exception to the output as a comment
|
2011-01-09 12:29:02 +00:00
|
|
|
$exceptions .= "/*\n{$e->__toString()}\n*/\n";
|
2010-12-23 20:14:18 +00:00
|
|
|
}
|
2010-09-23 21:23:51 +00:00
|
|
|
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileIn( __METHOD__.'-getModifiedTime' );
|
2010-10-20 00:22:25 +00:00
|
|
|
|
2011-01-19 19:31:14 +00:00
|
|
|
$private = false;
|
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 ) {
|
2010-12-23 20:14:18 +00:00
|
|
|
try {
|
2011-01-19 19:31:14 +00:00
|
|
|
// Bypass Squid and other shared caches if the request includes any private modules
|
2010-12-23 20:14:18 +00:00
|
|
|
if ( $module->getGroup() === 'private' ) {
|
2011-01-19 19:31:14 +00:00
|
|
|
$private = true;
|
2010-12-23 20:14:18 +00:00
|
|
|
}
|
|
|
|
|
// Calculate maximum modified time
|
|
|
|
|
$mtime = max( $mtime, $module->getModifiedTime( $context ) );
|
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
// Add exception to the output as a comment
|
2011-01-09 12:29:02 +00:00
|
|
|
$exceptions .= "/*\n{$e->__toString()}\n*/\n";
|
2010-09-24 22:10:25 +00:00
|
|
|
}
|
2010-09-04 08:38:45 +00:00
|
|
|
}
|
2010-10-20 00:22:25 +00:00
|
|
|
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__.'-getModifiedTime' );
|
2010-09-04 12:53:01 +00:00
|
|
|
|
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' );
|
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 {
|
2011-01-19 19:31:14 +00:00
|
|
|
if ( $private ) {
|
|
|
|
|
header( "Cache-Control: private, max-age=$maxage" );
|
|
|
|
|
$exp = $maxage;
|
|
|
|
|
} else {
|
|
|
|
|
header( "Cache-Control: public, max-age=$maxage, s-maxage=$smaxage" );
|
|
|
|
|
$exp = min( $maxage, $smaxage );
|
|
|
|
|
}
|
|
|
|
|
header( 'Expires: ' . wfTimestamp( TS_RFC2822, $exp + time() ) );
|
2010-11-01 20:48:30 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
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.
|
2011-01-08 15:41:55 +00:00
|
|
|
// On some setups, ob_get_level() doesn't seem to go down to zero
|
|
|
|
|
// no matter how often we call ob_get_clean(), so instead of doing
|
|
|
|
|
// the more intuitive while ( ob_get_level() > 0 ) ob_get_clean();
|
2011-09-22 21:27:04 +00:00
|
|
|
// we have to be safe here and avoid an infinite loop.
|
|
|
|
|
for ( $i = 0; $i < ob_get_level(); $i++ ) {
|
|
|
|
|
ob_end_clean();
|
2010-12-10 17:06:00 +00:00
|
|
|
}
|
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' );
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2011-06-17 16:05:05 +00:00
|
|
|
|
2010-10-20 00:22:25 +00:00
|
|
|
// Generate a response
|
2011-01-09 12:29:02 +00:00
|
|
|
$response = $this->makeModuleResponse( $context, $modules, $missing );
|
2011-06-17 16:05:05 +00:00
|
|
|
|
2011-01-09 12:29:02 +00:00
|
|
|
// Prepend comments indicating exceptions
|
|
|
|
|
$response = $exceptions . $response;
|
2010-10-20 00:22:25 +00:00
|
|
|
|
2010-12-10 17:06:00 +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.
|
2010-10-19 17:52:01 +00:00
|
|
|
if ( $context->getDebug() && strlen( $warnings = ob_get_contents() ) ) {
|
2011-01-09 12:29:02 +00:00
|
|
|
$response = "/*\n$warnings\n*/\n" . $response;
|
2010-10-19 17:52:01 +00:00
|
|
|
}
|
2010-10-20 00:22:25 +00:00
|
|
|
|
2010-12-10 17:06:00 +00:00
|
|
|
// Remove the output buffer and output the response
|
|
|
|
|
ob_end_clean();
|
2010-10-18 20:16:19 +00:00
|
|
|
echo $response;
|
2010-09-24 22:10:25 +00:00
|
|
|
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
* @param $modules Array: List of module objects keyed by module name
|
|
|
|
|
* @param $missing Array: List of unavailable modules (optional)
|
|
|
|
|
* @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,
|
|
|
|
|
array $modules, $missing = array() )
|
2010-11-03 07:58:03 +00:00
|
|
|
{
|
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 ) {
|
2010-12-23 20:14:18 +00:00
|
|
|
// Add exception to the output as a comment
|
2011-01-09 12:29:02 +00:00
|
|
|
$exceptions .= "/*\n{$e->__toString()}\n*/\n";
|
|
|
|
|
}
|
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
|
2010-09-24 22:10:25 +00:00
|
|
|
foreach ( $modules as $name => $module ) {
|
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 );
|
|
|
|
|
if ( is_string( $scripts ) ) {
|
|
|
|
|
// 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() ) {
|
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
|
|
|
|
|
// See comment near shouldIncludeScripts() for more details
|
|
|
|
|
if ( $context->getDebug() && !$context->getOnly() && $module->supportsURLLoading() ) {
|
|
|
|
|
$styles = $module->getStyleURLsForDebug( $context );
|
|
|
|
|
} else {
|
|
|
|
|
$styles = $module->getStyles( $context );
|
|
|
|
|
}
|
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':
|
|
|
|
|
$out .= self::makeCombinedStyles( $styles );
|
|
|
|
|
break;
|
|
|
|
|
case 'messages':
|
|
|
|
|
$out .= self::makeMessageSetScript( new XmlJsCode( $messagesBlob ) );
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2011-02-19 22:59:44 +00:00
|
|
|
// Minify CSS before embedding in mw.loader.implement call
|
2010-12-23 20:14:18 +00:00
|
|
|
// (unless in debug mode)
|
|
|
|
|
if ( !$context->getDebug() ) {
|
|
|
|
|
foreach ( $styles as $media => $style ) {
|
2011-05-14 12:15:58 +00:00
|
|
|
if ( is_string( $style ) ) {
|
|
|
|
|
$styles[$media] = $this->filter( 'minify-css', $style );
|
|
|
|
|
}
|
2010-12-23 20:14:18 +00:00
|
|
|
}
|
2010-09-10 20:18:24 +00:00
|
|
|
}
|
2010-12-23 20:14:18 +00:00
|
|
|
$out .= self::makeLoaderImplementScript( $name, $scripts, $styles,
|
|
|
|
|
new XmlJsCode( $messagesBlob ) );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
// Add exception to the output as a comment
|
2011-01-09 12:29:02 +00:00
|
|
|
$exceptions .= "/*\n{$e->__toString()}\n*/\n";
|
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] );
|
|
|
|
|
}
|
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
|
2010-09-04 04:00:09 +00:00
|
|
|
if ( $context->shouldIncludeScripts() ) {
|
2010-09-24 18:49:19 +00:00
|
|
|
// Set the state of modules loaded as only scripts to ready
|
2011-06-17 16:05:05 +00:00
|
|
|
if ( count( $modules ) && $context->getOnly() === 'scripts'
|
|
|
|
|
&& !isset( $modules['startup'] ) )
|
2010-11-03 07:58:03 +00:00
|
|
|
{
|
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.
|
|
|
|
|
*
|
|
|
|
|
* @param $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
|
|
|
|
|
* @param $styles Mixed: List of CSS strings keyed by media type, or list of lists of URLs to
|
|
|
|
|
* 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
|
|
|
*
|
|
|
|
|
* @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 ) ) {
|
|
|
|
|
$scripts = new XmlJsCode( "function( $ ) {{$scripts}}" );
|
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,
|
* 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
|
|
|
|
|
) );
|
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
|
* 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
|
|
|
* single stylesheet with @media blocks.
|
|
|
|
|
*
|
2010-11-05 18:33:50 +00:00
|
|
|
* @param $styles Array: List of CSS strings keyed by media type
|
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 makeCombinedStyles( array $styles ) {
|
|
|
|
|
$out = '';
|
|
|
|
|
foreach ( $styles as $media => $style ) {
|
2011-01-07 20:22:50 +00:00
|
|
|
// 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 );
|
2011-06-17 16:05:05 +00:00
|
|
|
|
2011-01-07 20:22:50 +00:00
|
|
|
if ( $media === null ) {
|
|
|
|
|
// Skip
|
2011-06-17 16:05:05 +00:00
|
|
|
} elseif ( $media === '' || $media == 'all' ) {
|
2011-01-07 20:22:50 +00:00
|
|
|
// Don't output invalid or frivolous @media statements
|
|
|
|
|
$out .= "$style\n";
|
|
|
|
|
} else {
|
|
|
|
|
$out .= "@media $media {\n" . str_replace( "\n", "\n\t", "\t" . $style ) . "\n}\n";
|
|
|
|
|
}
|
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
|
|
|
*
|
2010-11-05 18:33:50 +00:00
|
|
|
* @param $name String: Module name
|
|
|
|
|
* @param $version Integer: Module version number as a timestamp
|
|
|
|
|
* @param $dependencies Array: List of module names on which this module depends
|
|
|
|
|
* @param $group String: Group which the module is in.
|
2011-07-26 21:10:34 +00:00
|
|
|
* @param $source String: Source of the module, or 'local' if not foreign.
|
2010-11-05 18:33:50 +00:00
|
|
|
* @param $script String: 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(
|
2011-07-26 21:10:34 +00:00
|
|
|
"( function( name, version, dependencies, group, source ) {\n\t$script\n} )",
|
|
|
|
|
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.
|
|
|
|
|
*
|
2010-11-05 18:33:50 +00:00
|
|
|
* @param $name String: Module name
|
|
|
|
|
* @param $version Integer: Module version number as a timestamp
|
|
|
|
|
* @param $dependencies Array: List of module names on which this module depends
|
|
|
|
|
* @param $group String: group which the module is in.
|
2011-07-26 21:10:34 +00:00
|
|
|
* @param $source String: 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,
|
2011-07-26 21:10:34 +00:00
|
|
|
$dependencies = null, $group = null, $source = null )
|
2010-11-03 07:58:03 +00:00
|
|
|
{
|
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 {
|
|
|
|
|
$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:
|
|
|
|
|
*
|
|
|
|
|
* - ResourceLoader::makeLoaderSourcesScript( $id, $properties ):
|
|
|
|
|
* Register a single source
|
|
|
|
|
*
|
|
|
|
|
* - ResourceLoader::makeLoaderSourcesScript( array( $id1 => $props1, $id2 => $props2, ... ) );
|
|
|
|
|
* Register sources with the given IDs and properties.
|
|
|
|
|
*
|
|
|
|
|
* @param $id String: source ID
|
|
|
|
|
* @param $properties Array: source properties (see addSource())
|
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
|
*
|
2010-11-05 18:33:50 +00:00
|
|
|
* @param $script String: 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 ) {
|
|
|
|
|
$script = str_replace( "\n", "\n\t", trim( $script ) );
|
2011-09-28 22:47:05 +00:00
|
|
|
return "if(window.mw){\n\t$script\n}\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.
|
|
|
|
|
*
|
2010-11-05 18:33:50 +00:00
|
|
|
* @param $configuration Array: 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 ) {
|
2011-05-10 23:17:13 +00:00
|
|
|
return Xml::encodeJsCall( 'mw.config.set', array( $configuration ) );
|
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'
|
2011-05-05 13:46:47 +00:00
|
|
|
* @param $modules array of module names (strings)
|
|
|
|
|
* @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
|
|
|
|
|
* @param $modules array of module names (strings)
|
|
|
|
|
* @param $lang string Language code
|
|
|
|
|
* @param $skin string Skin name
|
|
|
|
|
* @param $user string|null User name. If null, the &user= parameter is omitted
|
|
|
|
|
* @param $version string|null Versioning timestamp
|
|
|
|
|
* @param $debug bool Whether the request should be in debug mode
|
|
|
|
|
* @param $only string|null &only= parameter
|
|
|
|
|
* @param $printable bool Printable mode
|
|
|
|
|
* @param $handheld bool Handheld mode
|
|
|
|
|
* @param $extraQuery array Extra query parameters to add
|
|
|
|
|
* @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() ) {
|
|
|
|
|
global $wgLoadScript;
|
|
|
|
|
$query = self::makeLoaderQuery( $modules, $lang, $skin, $user, $version, $debug,
|
|
|
|
|
$only, $printable, $handheld, $extraQuery
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Prevent the IE6 extension check from being triggered (bug 28840)
|
|
|
|
|
// by appending a character that's invalid in Windows extensions ('*')
|
2011-09-30 10:14:39 +00:00
|
|
|
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().
|
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
|
|
// Make queries uniform in order
|
|
|
|
|
ksort( $query );
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
2010-09-17 11:45:49 +00:00
|
|
|
}
|