Followup r95753 per CR: prevent extensions from making isMovable() return true for...
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.php
index 3d09b26..57c179b 100644 (file)
 class ResourceLoader {
 
        /* Protected Static Members */
+       protected static $filterCacheVersion = 5;
+       protected static $requiredSourceProperties = array( 'loadScript' );
 
        /** Array: List of module name/ResourceLoaderModule object pairs */
        protected $modules = array();
+
        /** Associative array mapping module name to info associative array */
        protected $moduleInfos = array();
 
+       /** array( 'source-id' => array( 'loadScript' => 'http://.../load.php' ) ) **/
+       protected $sources = array();
+
        /* Protected Methods */
 
        /**
         * Loads information stored in the database about modules.
-        * 
-        * This method grabs modules dependencies from the database and updates modules 
+        *
+        * This method grabs modules dependencies from the database and updates modules
         * objects.
-        * 
-        * 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 
+        *
+        * 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
         * requests its own information. This sacrifice of modularity yields a substantial
         * performance improvement.
-        * 
+        *
         * @param $modules Array: List of module names to preload information for
         * @param $context ResourceLoaderContext: Context to load the information within
         */
@@ -58,11 +64,11 @@ class ResourceLoader {
                $dbr = wfGetDB( DB_SLAVE );
                $skin = $context->getSkin();
                $lang = $context->getLanguage();
-               
+
                // Get file dependency information
                $res = $dbr->select( 'module_deps', array( 'md_module', 'md_deps' ), array(
                                'md_module' => $modules,
-                               'md_skin' => $context->getSkin()
+                               'md_skin' => $skin
                        ), __METHOD__
                );
 
@@ -79,7 +85,7 @@ class ResourceLoader {
                foreach ( array_diff( $modules, $modulesWithDeps ) as $name ) {
                        $this->getModule( $name )->setFileDependencies( $skin, array() );
                }
-               
+
                // Get message blob mtimes. Only do this for modules with messages
                $modulesWithMessages = array();
                foreach ( $modules as $name ) {
@@ -95,11 +101,11 @@ class ResourceLoader {
                                ), __METHOD__
                        );
                        foreach ( $res as $row ) {
-                               $this->getModule( $row->mr_resource )->setMsgBlobMtime( $lang, 
+                               $this->getModule( $row->mr_resource )->setMsgBlobMtime( $lang,
                                        wfTimestamp( TS_UNIX, $row->mr_timestamp ) );
                                unset( $modulesWithoutMessages[$row->mr_resource] );
                        }
-               } 
+               }
                foreach ( array_keys( $modulesWithoutMessages ) as $name ) {
                        $this->getModule( $name )->setMsgBlobMtime( $lang, 0 );
                }
@@ -107,27 +113,26 @@ class ResourceLoader {
 
        /**
         * Runs JavaScript or CSS data through a filter, caching the filtered result for future calls.
-        * 
+        *
         * Available filters are:
-        *  - minify-js \see JavaScriptDistiller::stripWhiteSpace
+        *  - minify-js \see JavaScriptMinifier::minify
         *  - minify-css \see CSSMin::minify
-        * 
-        * If $data is empty, only contains whitespace or the filter was unknown, 
+        *
+        * If $data is empty, only contains whitespace or the filter was unknown,
         * $data is returned unmodified.
-        * 
+        *
         * @param $filter String: Name of filter to run
         * @param $data String: Text to filter, such as JavaScript or CSS text
         * @return String: Filtered data, or a comment containing an error message
         */
        protected function filter( $filter, $data ) {
-               global $wgResourceLoaderMinifyJSVerticalSpace;
-
+               global $wgResourceLoaderMinifierStatementsOnOwnLine, $wgResourceLoaderMinifierMaxLineLength;
                wfProfileIn( __METHOD__ );
 
-               // For empty/whitespace-only data or for unknown filters, don't perform 
+               // For empty/whitespace-only data or for unknown filters, don't perform
                // any caching or processing
-               if ( trim( $data ) === '' 
-                       || !in_array( $filter, array( 'minify-js', 'minify-css' ) ) ) 
+               if ( trim( $data ) === ''
+                       || !in_array( $filter, array( 'minify-js', 'minify-css' ) ) )
                {
                        wfProfileOut( __METHOD__ );
                        return $data;
@@ -135,7 +140,7 @@ class ResourceLoader {
 
                // Try for cache hit
                // Use CACHE_ANYTHING since filtering is very slow compared to DB queries
-               $key = wfMemcKey( 'resourceloader', 'filter', $filter, md5( $data ) );
+               $key = wfMemcKey( 'resourceloader', 'filter', $filter, self::$filterCacheVersion, md5( $data ) );
                $cache = wfGetCache( CACHE_ANYTHING );
                $cacheEntry = $cache->get( $key );
                if ( is_string( $cacheEntry ) ) {
@@ -148,12 +153,15 @@ class ResourceLoader {
                try {
                        switch ( $filter ) {
                                case 'minify-js':
-                                       $result = JavaScriptDistiller::stripWhiteSpace(
-                                               $data, $wgResourceLoaderMinifyJSVerticalSpace
+                                       $result = JavaScriptMinifier::minify( $data,
+                                               $wgResourceLoaderMinifierStatementsOnOwnLine,
+                                               $wgResourceLoaderMinifierMaxLineLength
                                        );
+                                       $result .= "\n\n/* cache key: $key */\n";
                                        break;
                                case 'minify-css':
                                        $result = CSSMin::minify( $data );
+                                       $result .= "\n\n/* cache key: $key */\n";
                                        break;
                        }
 
@@ -165,7 +173,7 @@ class ResourceLoader {
                }
 
                wfProfileOut( __METHOD__ );
-               
+
                return $result;
        }
 
@@ -175,27 +183,34 @@ class ResourceLoader {
         * Registers core modules and runs registration hooks.
         */
        public function __construct() {
-               global $IP, $wgResourceModules;
-               
+               global $IP, $wgResourceModules, $wgResourceLoaderSources, $wgLoadScript;
+
                wfProfileIn( __METHOD__ );
-               
+
+               // Add 'local' source first
+               $this->addSource( 'local', array( 'loadScript' => $wgLoadScript ) );
+
+               // Add other sources
+               $this->addSource( $wgResourceLoaderSources );
+
                // Register core modules
                $this->register( include( "$IP/resources/Resources.php" ) );
                // Register extension modules
                wfRunHooks( 'ResourceLoaderRegisterModules', array( &$this ) );
                $this->register( $wgResourceModules );
-               
+
                wfProfileOut( __METHOD__ );
        }
 
        /**
         * Registers a module with the ResourceLoader system.
-        * 
+        *
         * @param $name Mixed: Name of module as a string or List of name/object pairs as an array
-        * @param $info Module info array. For backwards compatibility with 1.17alpha, 
-        *   this may also be a ResourceLoaderModule object. Optional when using 
+        * @param $info Module info array. For backwards compatibility with 1.17alpha,
+        *   this may also be a ResourceLoaderModule object. Optional when using
         *   multiple-registration calling style.
         * @throws MWException: If a duplicate module registration is attempted
+        * @throws MWException: If a module name contains illegal characters (pipes or commas)
         * @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
@@ -216,17 +231,22 @@ class ResourceLoader {
                if ( isset( $this->moduleInfos[$name] ) ) {
                        // A module has already been registered by this name
                        throw new MWException(
-                               'ResourceLoader duplicate registration error. ' . 
+                               'ResourceLoader duplicate registration error. ' .
                                'Another module has already been registered as ' . $name
                        );
                }
 
+               // Check $name for illegal characters
+               if ( preg_match( '/[|,!]/', $name ) ) {
+                       throw new MWException( "ResourceLoader module name '$name' is invalid. Names may not contain pipes (|), commas (,) or exclamation marks (!)" );
+               }
+
                // Attach module
                if ( is_object( $info ) ) {
                        // Old calling convention
                        // Validate the input
                        if ( !( $info instanceof ResourceLoaderModule ) ) {
-                               throw new MWException( 'ResourceLoader invalid module error. ' . 
+                               throw new MWException( 'ResourceLoader invalid module error. ' .
                                        'Instances of ResourceLoaderModule expected.' );
                        }
 
@@ -241,7 +261,43 @@ class ResourceLoader {
                wfProfileOut( __METHOD__ );
        }
 
-       /**
+       /**
+        * 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;
+       }
+
+       /**
         * Get a list of module names
         *
         * @return Array: List of module names
@@ -282,6 +338,15 @@ class ResourceLoader {
                return $this->modules[$name];
        }
 
+       /**
+        * Get the list of sources
+        * 
+        * @return Array: array( id => array of properties, .. )
+        */
+       public function getSources() {
+               return $this->sources;
+       }
+
        /**
         * Outputs a response to a resource load-request, including a content-type header.
         *
@@ -289,7 +354,7 @@ class ResourceLoader {
         */
        public function respond( ResourceLoaderContext $context ) {
                global $wgResourceLoaderMaxage, $wgCacheEpoch;
-               
+
                // 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
@@ -313,13 +378,13 @@ class ResourceLoader {
                        }
                }
 
-               // If a version wasn't specified we need a shorter expiry time for updates 
+               // If a version wasn't specified we need a shorter expiry time for updates
                // to propagate to clients quickly
                if ( is_null( $context->getVersion() ) ) {
                        $maxage  = $wgResourceLoaderMaxage['unversioned']['client'];
                        $smaxage = $wgResourceLoaderMaxage['unversioned']['server'];
                }
-               // If a version was specified we can use a longer expiry time since changing 
+               // If a version was specified we can use a longer expiry time since changing
                // version numbers causes cache misses
                else {
                        $maxage  = $wgResourceLoaderMaxage['versioned']['client'];
@@ -337,7 +402,7 @@ class ResourceLoader {
                wfProfileIn( __METHOD__.'-getModifiedTime' );
 
                $private = false;
-               // To send Last-Modified and support If-Modified-Since, we need to detect 
+               // To send Last-Modified and support If-Modified-Since, we need to detect
                // the last modified time
                $mtime = wfTimestamp( TS_UNIX, $wgCacheEpoch );
                foreach ( $modules as $module ) {
@@ -357,9 +422,9 @@ class ResourceLoader {
                wfProfileOut( __METHOD__.'-getModifiedTime' );
 
                if ( $context->getOnly() === 'styles' ) {
-                       header( 'Content-Type: text/css' );
+                       header( 'Content-Type: text/css; charset=utf-8' );
                } else {
-                       header( 'Content-Type: text/javascript' );
+                       header( 'Content-Type: text/javascript; charset=utf-8' );
                }
                header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $mtime ) );
                if ( $context->getDebug() ) {
@@ -381,7 +446,8 @@ class ResourceLoader {
                // Some clients send "timestamp;length=123". Strip the part after the first ';'
                // so we get a valid timestamp.
                $ims = $context->getRequest()->getHeader( 'If-Modified-Since' );
-               if ( $ims !== false ) {
+               // Never send 304s in debug mode
+               if ( $ims !== false && !$context->getDebug() ) {
                        $imsTS = strtok( $ims, ';' );
                        if ( $mtime <= wfTimestamp( TS_UNIX, $imsTS ) ) {
                                // There's another bug in ob_gzhandler (see also the comment at
@@ -400,17 +466,17 @@ class ResourceLoader {
                                for ( $i = 0; $i < ob_get_level(); $i++ ) {
                                        ob_end_clean();
                                }
-                               
+
                                header( 'HTTP/1.0 304 Not Modified' );
                                header( 'Status: 304 Not Modified' );
                                wfProfileOut( __METHOD__ );
                                return;
                        }
                }
-               
+
                // Generate a response
                $response = $this->makeModuleResponse( $context, $modules, $missing );
-               
+
                // Prepend comments indicating exceptions
                $response = $exceptions . $response;
 
@@ -429,21 +495,21 @@ class ResourceLoader {
 
        /**
         * Generates code for a response
-        * 
+        *
         * @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
         */
-       public function makeModuleResponse( ResourceLoaderContext $context, 
-               array $modules, $missing = array() ) 
+       public function makeModuleResponse( ResourceLoaderContext $context,
+               array $modules, $missing = array() )
        {
                $out = '';
                $exceptions = '';
                if ( $modules === array() && $missing === array() ) {
                        return '/* No modules requested. Max made me put this here */';
                }
-               
+
                wfProfileIn( __METHOD__ );
                // Pre-fetch blobs
                if ( $context->shouldIncludeMessages() ) {
@@ -461,12 +527,15 @@ class ResourceLoader {
                foreach ( $modules as $name => $module ) {
                        wfProfileIn( __METHOD__ . '-' . $name );
                        try {
-                               // Scripts
                                $scripts = '';
                                if ( $context->shouldIncludeScripts() ) {
-                                       $scripts .= $module->getScript( $context ) . "\n";
+                                       $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";
+                                       }
                                }
-
                                // Styles
                                $styles = array();
                                if ( $context->shouldIncludeStyles() ) {
@@ -479,7 +548,13 @@ class ResourceLoader {
                                // Append output
                                switch ( $context->getOnly() ) {
                                        case 'scripts':
-                                               $out .= $scripts;
+                                               if ( is_string( $scripts ) ) {
+                                                       // Load scripts raw...
+                                                       $out .= $scripts;
+                                               } elseif ( is_array( $scripts ) ) {
+                                                       // ...except when $scripts is an array of URLs
+                                                       $out .= self::makeLoaderImplementScript( $name, $scripts, array(), array() );
+                                               }
                                                break;
                                        case 'styles':
                                                $out .= self::makeCombinedStyles( $styles );
@@ -488,11 +563,13 @@ class ResourceLoader {
                                                $out .= self::makeMessageSetScript( new XmlJsCode( $messagesBlob ) );
                                                break;
                                        default:
-                                               // Minify CSS before embedding in mediaWiki.loader.implement call
+                                               // Minify CSS before embedding in mw.loader.implement call
                                                // (unless in debug mode)
                                                if ( !$context->getDebug() ) {
                                                        foreach ( $styles as $media => $style ) {
-                                                               $styles[$media] = $this->filter( 'minify-css', $style );
+                                                               if ( is_string( $style ) ) {
+                                                                       $styles[$media] = $this->filter( 'minify-css', $style );
+                                                               }
                                                        }
                                                }
                                                $out .= self::makeLoaderImplementScript( $name, $scripts, $styles,
@@ -513,10 +590,10 @@ class ResourceLoader {
                // Update module states
                if ( $context->shouldIncludeScripts() ) {
                        // Set the state of modules loaded as only scripts to ready
-                       if ( count( $modules ) && $context->getOnly() === 'scripts' 
-                               && !isset( $modules['startup'] ) ) 
+                       if ( count( $modules ) && $context->getOnly() === 'scripts'
+                               && !isset( $modules['startup'] ) )
                        {
-                               $out .= self::makeLoaderStateScript( 
+                               $out .= self::makeLoaderStateScript(
                                        array_fill_keys( array_keys( $modules ), 'ready' ) );
                        }
                        // Set the state of modules which were requested but unavailable as missing
@@ -532,7 +609,7 @@ class ResourceLoader {
                                $out = $this->filter( 'minify-js', $out );
                        }
                }
-               
+
                wfProfileOut( __METHOD__ );
                return $exceptions . $out;
        }
@@ -540,26 +617,30 @@ class ResourceLoader {
        /* Static Methods */
 
        /**
-        * Returns JS code to call to mediaWiki.loader.implement for a module with 
+        * Returns JS code to call to mw.loader.implement for a module with
         * given properties.
         *
         * @param $name Module name
-        * @param $scripts Array: List of JavaScript code snippets to be executed after the 
-        *     module is loaded
-        * @param $styles Array: List of CSS strings keyed by media type
-        * @param $messages Mixed: List of messages associated with this module. May either be an 
+        * @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
+        * @param $messages Mixed: List of messages associated with this module. May either be an
         *     associative array mapping message key to value, or a JSON-encoded message blob containing
         *     the same data, wrapped in an XmlJsCode object.
+        *
+        * @return string
         */
        public static function makeLoaderImplementScript( $name, $scripts, $styles, $messages ) {
-               if ( is_array( $scripts ) ) {
-                       $scripts = implode( $scripts, "\n" );
+               if ( is_string( $scripts ) ) {
+                       $scripts = new XmlJsCode( "function( $ ) {{$scripts}}" );
+               } elseif ( !is_array( $scripts ) ) {
+                       throw new MWException( 'Invalid scripts error. Array of URLs or string of code expected.' );
                }
-               return Xml::encodeJsCall( 
-                       'mediaWiki.loader.implement', 
+               return Xml::encodeJsCall(
+                       'mw.loader.implement',
                        array(
                                $name,
-                               new XmlJsCode( "function( $, mw ) {{$scripts}}" ),
+                               $scripts,
                                (object)$styles,
                                (object)$messages
                        ) );
@@ -570,16 +651,20 @@ class ResourceLoader {
         *
         * @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.
+        *
+        * @return string
         */
        public static function makeMessageSetScript( $messages ) {
-               return Xml::encodeJsCall( 'mediaWiki.messages.set', array( (object)$messages ) );
+               return Xml::encodeJsCall( 'mw.messages.set', array( (object)$messages ) );
        }
 
        /**
-        * Combines an associative array mapping media type to CSS into a 
+        * Combines an associative array mapping media type to CSS into a
         * single stylesheet with @media blocks.
         *
         * @param $styles Array: List of CSS strings keyed by media type
+        *
+        * @return string
         */
        public static function makeCombinedStyles( array $styles ) {
                $out = '';
@@ -587,10 +672,10 @@ class ResourceLoader {
                        // Transform the media type based on request params and config
                        // The way that this relies on $wgRequest to propagate request params is slightly evil
                        $media = OutputPage::transformCssMedia( $media );
-                       
+
                        if ( $media === null ) {
                                // Skip
-                       } else if ( $media === '' || $media == 'all' ) {
+                       } elseif ( $media === '' || $media == 'all' ) {
                                // Don't output invalid or frivolous @media statements
                                $out .= "$style\n";
                        } else {
@@ -601,7 +686,7 @@ class ResourceLoader {
        }
 
        /**
-        * Returns a JS call to mediaWiki.loader.state, which sets the state of a 
+        * Returns a JS call to mw.loader.state, which sets the state of a
         * module or modules to a given value. Has two calling conventions:
         *
         *    - ResourceLoader::makeLoaderStateScript( $name, $state ):
@@ -609,47 +694,55 @@ class ResourceLoader {
         *
         *    - ResourceLoader::makeLoaderStateScript( array( $name => $state, ... ) ):
         *         Set the state of modules with the given names to the given states
+        *
+        * @param $name string
+        * @param $state
+        *
+        * @return string
         */
        public static function makeLoaderStateScript( $name, $state = null ) {
                if ( is_array( $name ) ) {
-                       return Xml::encodeJsCall( 'mediaWiki.loader.state', array( $name ) );
+                       return Xml::encodeJsCall( 'mw.loader.state', array( $name ) );
                } else {
-                       return Xml::encodeJsCall( 'mediaWiki.loader.state', array( $name, $state ) );
+                       return Xml::encodeJsCall( 'mw.loader.state', array( $name, $state ) );
                }
        }
 
        /**
         * Returns JS code which calls the script given by $script. The script will
-        * be called with local variables name, version, dependencies and group, 
-        * which will have values corresponding to $name, $version, $dependencies 
-        * and $group as supplied. 
+        * be called with local variables name, version, dependencies and group,
+        * which will have values corresponding to $name, $version, $dependencies
+        * and $group as supplied.
         *
         * @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.
+        * @param $source String: Source of the module, or 'local' if not foreign.
         * @param $script String: JavaScript code
+        *
+        * @return string
         */
-       public static function makeCustomLoaderScript( $name, $version, $dependencies, $group, $script ) {
+       public static function makeCustomLoaderScript( $name, $version, $dependencies, $group, $source, $script ) {
                $script = str_replace( "\n", "\n\t", trim( $script ) );
-               return Xml::encodeJsCall( 
-                       "( function( name, version, dependencies, group ) {\n\t$script\n} )",
-                       array( $name, $version, $dependencies, $group ) );
+               return Xml::encodeJsCall(
+                       "( function( name, version, dependencies, group, source ) {\n\t$script\n} )",
+                       array( $name, $version, $dependencies, $group, $source ) );
        }
 
        /**
-        * Returns JS code which calls mediaWiki.loader.register with the given 
+        * Returns JS code which calls mw.loader.register with the given
         * parameters. Has three calling conventions:
         *
-        *   - ResourceLoader::makeLoaderRegisterScript( $name, $version, $dependencies, $group ):
+        *   - ResourceLoader::makeLoaderRegisterScript( $name, $version, $dependencies, $group, $source ):
         *       Register a single module.
         *
         *   - ResourceLoader::makeLoaderRegisterScript( array( $name1, $name2 ) ):
         *       Register modules with the given names.
         *
         *   - ResourceLoader::makeLoaderRegisterScript( array(
-        *        array( $name1, $version1, $dependencies1, $group1 ),
-        *        array( $name2, $version2, $dependencies1, $group2 ),
+        *        array( $name1, $version1, $dependencies1, $group1, $source1 ),
+        *        array( $name2, $version2, $dependencies1, $group2, $source2 ),
         *        ...
         *     ) ):
         *        Registers modules with the given names and parameters.
@@ -658,24 +751,50 @@ class ResourceLoader {
         * @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.
+        * @param $source String: source of the module, or 'local' if not foreign
+        *
+        * @return string
         */
-       public static function makeLoaderRegisterScript( $name, $version = null, 
-               $dependencies = null, $group = null ) 
+       public static function makeLoaderRegisterScript( $name, $version = null,
+               $dependencies = null, $group = null, $source = null )
        {
                if ( is_array( $name ) ) {
-                       return Xml::encodeJsCall( 'mediaWiki.loader.register', array( $name ) );
+                       return Xml::encodeJsCall( 'mw.loader.register', array( $name ) );
                } else {
                        $version = (int) $version > 1 ? (int) $version : 1;
-                       return Xml::encodeJsCall( 'mediaWiki.loader.register', 
-                               array( $name, $version, $dependencies, $group ) );
+                       return Xml::encodeJsCall( 'mw.loader.register',
+                               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 ) );
                }
        }
 
        /**
-        * Returns JS code which runs given JS code if the client-side framework is 
+        * Returns JS code which runs given JS code if the client-side framework is
         * present.
         *
         * @param $script String: JavaScript code
+        *
+        * @return string
         */
        public static function makeLoaderConditionalScript( $script ) {
                $script = str_replace( "\n", "\n\t", trim( $script ) );
@@ -683,15 +802,43 @@ class ResourceLoader {
        }
 
        /**
-        * Returns JS code which will set the MediaWiki configuration array to 
+        * Returns JS code which will set the MediaWiki configuration array to
         * the given value.
         *
         * @param $configuration Array: List of configuration values keyed by variable name
+        *
+        * @return string
         */
        public static function makeConfigSetScript( array $configuration ) {
-               return Xml::encodeJsCall( 'mediaWiki.config.set', array( $configuration ) );
+               return Xml::encodeJsCall( 'mw.config.set', array( $configuration ) );
        }
-       
+
+       /**
+        * Convert an array of module names to a packed query string.
+        *
+        * For example, array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' )
+        * becomes 'foo.bar,baz|bar.baz,quux'
+        * @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;
+               }
+
+               $arr = array();
+               foreach ( $groups as $prefix => $suffixes ) {
+                       $p = $prefix === '' ? '' : $prefix . '.';
+                       $arr[] = $p . implode( ',', $suffixes );
+               }
+               $str = implode( '|', $arr );
+               return $str;
+       }
+
        /**
         * Determine whether debug mode was requested
         * Order of priority is 1) request param, 2) cookie, 3) $wg setting
@@ -700,8 +847,9 @@ class ResourceLoader {
        public static function inDebugMode() {
                global $wgRequest, $wgResourceLoaderDebug;
                static $retval = null;
-               if ( !is_null( $retval ) )
+               if ( !is_null( $retval ) ) {
                        return $retval;
+               }
                return $retval = $wgRequest->getFuzzyBool( 'debug',
                        $wgRequest->getCookie( 'resourceLoaderDebug', '', $wgResourceLoaderDebug ) );
        }