resourceloader: Condition-wrap the HTML tag instead of JS response
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.php
index e30047a..f6a9d84 100644 (file)
@@ -32,9 +32,6 @@ class ResourceLoader {
        /** @var int */
        protected static $filterCacheVersion = 7;
 
-       /** @var array */
-       protected static $requiredSourceProperties = array( 'loadScript' );
-
        /** @var bool */
        protected static $debugMode = null;
 
@@ -53,7 +50,7 @@ class ResourceLoader {
         */
        protected $testModuleNames = array();
 
-       /** @var array E.g. array( 'source-id' => array( 'loadScript' => 'http://.../load.php' ) ) */
+       /** @var array E.g. array( 'source-id' => 'http://.../load.php' ) */
        protected $sources = array();
 
        /** @var bool */
@@ -216,6 +213,7 @@ class ResourceLoader {
 
        /**
         * Register core modules and runs registration hooks.
+        * @param Config|null $config
         */
        public function __construct( Config $config = null ) {
                global $IP;
@@ -230,10 +228,7 @@ class ResourceLoader {
                $this->config = $config;
 
                // Add 'local' source first
-               $this->addSource(
-                       'local',
-                       array( 'loadScript' => wfScript( 'load' ), 'apiScript' => wfScript( 'api' ) )
-               );
+               $this->addSource( 'local', wfScript( 'load' ) );
 
                // Add other sources
                $this->addSource( $config->get( 'ResourceLoaderSources' ) );
@@ -400,14 +395,12 @@ class ResourceLoader {
        /**
         * Add a foreign source of modules.
         *
-        * Source properties:
-        * 'loadScript': URL (either fully-qualified or protocol-relative) of load.php for this source
-        *
-        * @param mixed $id Source ID (string), or array( id1 => props1, id2 => props2, ... )
-        * @param array $properties Source properties
+        * @param array|string $id Source ID (string), or array( id1 => loadUrl, id2 => loadUrl, ... )
+        * @param string|array $loadUrl load.php url (string), or array with loadUrl key for
+        *  backwards-compatability.
         * @throws MWException
         */
-       public function addSource( $id, $properties = null ) {
+       public function addSource( $id, $loadUrl = null ) {
                // Allow multiple sources to be registered in one call
                if ( is_array( $id ) ) {
                        foreach ( $id as $key => $value ) {
@@ -424,14 +417,18 @@ class ResourceLoader {
                        );
                }
 
-               // Validate properties
-               foreach ( self::$requiredSourceProperties as $prop ) {
-                       if ( !isset( $properties[$prop] ) ) {
-                               throw new MWException( "Required property $prop missing from source ID $id" );
+               // Pre 1.24 backwards-compatability
+               if ( is_array( $loadUrl ) ) {
+                       if ( !isset( $loadUrl['loadScript'] ) ) {
+                               throw new MWException(
+                                       __METHOD__ . ' was passed an array with no "loadScript" key.'
+                               );
                        }
+
+                       $loadUrl = $loadUrl['loadScript'];
                }
 
-               $this->sources[$id] = $properties;
+               $this->sources[$id] = $loadUrl;
        }
 
        /**
@@ -526,7 +523,7 @@ class ResourceLoader {
        /**
         * Get the list of sources.
         *
-        * @return array Like array( id => array of properties, .. )
+        * @return array Like array( id => load.php url, .. )
         */
        public function getSources() {
                return $this->sources;
@@ -545,7 +542,7 @@ class ResourceLoader {
                if ( !isset( $this->sources[$source] ) ) {
                        throw new MWException( "The $source source was never registered in ResourceLoader." );
                }
-               return $this->sources[$source]['loadScript'];
+               return $this->sources[$source];
        }
 
        /**
@@ -854,7 +851,7 @@ class ResourceLoader {
                // Pre-fetch blobs
                if ( $context->shouldIncludeMessages() ) {
                        try {
-                               $blobs = MessageBlobStore::get( $this, $modules, $context->getLanguage() );
+                               $blobs = MessageBlobStore::getInstance()->get( $this, $modules, $context->getLanguage() );
                        } catch ( Exception $e ) {
                                MWExceptionHandler::logException( $e );
                                wfDebugLog(
@@ -1005,13 +1002,6 @@ class ResourceLoader {
                        if ( count( $states ) ) {
                                $out .= self::makeLoaderStateScript( $states );
                        }
-
-                       // In only=script requests for modules that are not raw (e.g. not the startup module)
-                       // ensure the execution is conditional to avoid situations where browsers with an
-                       // unsupported environment do unconditionally execute a module's scripts. Otherwise users
-                       // will get things like "ReferenceError: mw is undefined" or "jQuery is undefined" from
-                       // legacy scripts loaded with only=scripts (such as the 'site' module).
-                       $out = self::makeLoaderConditionalScript( $out );
                } else {
                        if ( count( $states ) ) {
                                $exceptions .= self::makeComment(
@@ -1178,11 +1168,13 @@ class ResourceLoader {
         * Returns JS code which calls mw.loader.register with the given
         * parameters. Has three calling conventions:
         *
-        *   - ResourceLoader::makeLoaderRegisterScript( $name, $version, $dependencies, $group, $source, $skip ):
-        *       Register a single module.
+        *   - ResourceLoader::makeLoaderRegisterScript( $name, $version,
+        *        $dependencies, $group, $source, $skip
+        *     ):
+        *        Register a single module.
         *
         *   - ResourceLoader::makeLoaderRegisterScript( array( $name1, $name2 ) ):
-        *       Register modules with the given names.
+        *        Register modules with the given names.
         *
         *   - ResourceLoader::makeLoaderRegisterScript( array(
         *        array( $name1, $version1, $dependencies1, $group1, $source1, $skip1 ),
@@ -1225,7 +1217,7 @@ class ResourceLoader {
         *   - ResourceLoader::makeLoaderSourcesScript( $id, $properties ):
         *       Register a single source
         *
-        *   - ResourceLoader::makeLoaderSourcesScript( array( $id1 => $props1, $id2 => $props2, ... ) );
+        *   - ResourceLoader::makeLoaderSourcesScript( array( $id1 => $loadUrl, $id2 => $loadUrl, ... ) );
         *       Register sources with the given IDs and properties.
         *
         * @param string $id Source ID
@@ -1480,7 +1472,7 @@ class ResourceLoader {
 
                $less = new lessc();
                $less->setPreserveComments( true );
-               $less->setVariables( self::getLESSVars( $config ) );
+               $less->setVariables( self::getLessVars( $config ) );
                $less->setImportDir( $config->get( 'ResourceLoaderLESSImportPaths' ) );
                foreach ( $config->get( 'ResourceLoaderLESSFunctions' ) as $name => $func ) {
                        $less->registerFunction( $name, $func );
@@ -1495,7 +1487,7 @@ class ResourceLoader {
         * @since 1.22
         * @return array Map of variable names to string CSS values.
         */
-       public static function getLESSVars( Config $config ) {
+       public static function getLessVars( Config $config ) {
                $lessVars = $config->get( 'ResourceLoaderLESSVars' );
                // Sort by key to ensure consistent hashing for cache lookups.
                ksort( $lessVars );