Merge "Pass in __METHOD__ to upsert() in ResourceLoaderModule::saveFileDependencies()"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.php
index f051cd4..418f532 100644 (file)
@@ -240,8 +240,6 @@ class ResourceLoader implements LoggerAwareInterface {
         * @param LoggerInterface|null $logger [optional]
         */
        public function __construct( Config $config = null, LoggerInterface $logger = null ) {
-               global $IP;
-
                $this->logger = $logger ?: new NullLogger();
 
                if ( !$config ) {
@@ -254,18 +252,8 @@ class ResourceLoader implements LoggerAwareInterface {
                // Add 'local' source first
                $this->addSource( 'local', $config->get( 'LoadScript' ) );
 
-               // Register core modules
-               $this->register( include "$IP/resources/Resources.php" );
-               // Register extension modules
-               $this->register( $config->get( 'ResourceModules' ) );
-
-               // Avoid PHP 7.1 warning from passing $this by reference
-               $rl = $this;
-               Hooks::run( 'ResourceLoaderRegisterModules', [ &$rl ] );
-
-               if ( $config->get( 'EnableJavaScriptTest' ) === true ) {
-                       $this->registerTestModules();
-               }
+               // Special module that always exists
+               $this->register( 'startup', [ 'class' => ResourceLoaderStartUpModule::class ] );
 
                $this->setMessageBlobStore( new MessageBlobStore( $this, $this->logger ) );
        }
@@ -395,6 +383,9 @@ class ResourceLoader implements LoggerAwareInterface {
                }
        }
 
+       /**
+        * @internal For use by ServiceWiring only
+        */
        public function registerTestModules() {
                global $IP;
 
@@ -1120,6 +1111,10 @@ MESSAGE;
 
                                if ( !$context->getDebug() ) {
                                        $strContent = self::filter( $filter, $strContent );
+                               } else {
+                                       // In debug mode, separate each response by a new line.
+                                       // For example, between 'mw.loader.implement();' statements.
+                                       $strContent = $this->ensureNewline( $strContent );
                                }
 
                                if ( $context->getOnly() === 'scripts' ) {
@@ -1173,7 +1168,7 @@ MESSAGE;
         */
        private function ensureNewline( $str ) {
                $end = substr( $str, -1 );
-               if ( $end === false || $end === "\n" ) {
+               if ( $end === false || $end === '' || $end === "\n" ) {
                        return $str;
                }
                return $str . "\n";
@@ -1487,7 +1482,7 @@ MESSAGE;
         */
        public static function makeLoaderConditionalScript( $script ) {
                // Adds a function to lazy-created RLQ
-               return '(window.RLQ=window.RLQ||[]).push(function(){' .
+               return '(RLQ=window.RLQ||[]).push(function(){' .
                        trim( $script ) . '});';
        }
 
@@ -1501,7 +1496,7 @@ MESSAGE;
         */
        public static function makeInlineCodeWithModule( $modules, $script ) {
                // Adds an array to lazy-created RLQ
-               return '(window.RLQ=window.RLQ||[]).push(['
+               return '(RLQ=window.RLQ||[]).push(['
                        . self::encodeJsonForScript( $modules ) . ','
                        . 'function(){' . trim( $script ) . '}'
                        . ']);';
@@ -1532,7 +1527,7 @@ MESSAGE;
 
                return new WrappedString(
                        Html::inlineScript( $js, $nonce ),
-                       "<script$escNonce>(window.RLQ=window.RLQ||[]).push(function(){",
+                       "<script$escNonce>(RLQ=window.RLQ||[]).push(function(){",
                        '});</script>'
                );
        }
@@ -1568,7 +1563,7 @@ MESSAGE;
         * For example, `[ 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ]`
         * becomes `'foo.bar,baz|bar.baz,quux'`.
         *
-        * This process is reversed by ResourceLoaderContext::expandModuleNames().
+        * This process is reversed by ResourceLoader::expandModuleNames().
         * See also mw.loader#buildModulesString() which is a port of this, used
         * on the client-side.
         *
@@ -1592,6 +1587,44 @@ MESSAGE;
                return implode( '|', $arr );
        }
 
+       /**
+        * Expand a string of the form `jquery.foo,bar|jquery.ui.baz,quux` to
+        * an array of module names like `[ 'jquery.foo', 'jquery.bar',
+        * 'jquery.ui.baz', 'jquery.ui.quux' ]`.
+        *
+        * This process is reversed by ResourceLoader::makePackedModulesString().
+        *
+        * @since 1.33
+        * @param string $modules Packed module name list
+        * @return array Array of module names
+        */
+       public static function expandModuleNames( $modules ) {
+               $retval = [];
+               $exploded = explode( '|', $modules );
+               foreach ( $exploded as $group ) {
+                       if ( strpos( $group, ',' ) === false ) {
+                               // This is not a set of modules in foo.bar,baz notation
+                               // but a single module
+                               $retval[] = $group;
+                       } else {
+                               // This is a set of modules in foo.bar,baz notation
+                               $pos = strrpos( $group, '.' );
+                               if ( $pos === false ) {
+                                       // Prefixless modules, i.e. without dots
+                                       $retval = array_merge( $retval, explode( ',', $group ) );
+                               } else {
+                                       // We have a prefix and a bunch of suffixes
+                                       $prefix = substr( $group, 0, $pos ); // 'foo'
+                                       $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // [ 'bar', 'baz' ]
+                                       foreach ( $suffixes as $suffix ) {
+                                               $retval[] = "$prefix.$suffix";
+                                       }
+                               }
+                       }
+               }
+               return $retval;
+       }
+
        /**
         * Determine whether debug mode was requested
         * Order of priority is 1) request param, 2) cookie, 3) $wg setting