Merge "Exclude redirects from Special:Fewestrevisions"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.php
index 2ae6d74..671fe86 100644 (file)
@@ -118,11 +118,10 @@ class ResourceLoader implements LoggerAwareInterface {
                        return;
                }
                $dbr = wfGetDB( DB_REPLICA );
-               $skin = $context->getSkin();
                $lang = $context->getLanguage();
 
                // Batched version of ResourceLoaderModule::getFileDependencies
-               $vary = "$skin|$lang";
+               $vary = ResourceLoaderModule::getVary( $context );
                $res = $dbr->select( 'module_deps', [ 'md_module', 'md_deps' ], [
                                'md_module' => $moduleNames,
                                'md_skin' => $vary,
@@ -298,13 +297,13 @@ class ResourceLoader implements LoggerAwareInterface {
        /**
         * Register a module with the ResourceLoader system.
         *
-        * @param mixed $name Name of module as a string or List of name/object pairs as an array
-        * @param array|null $info Module info array. For backwards compatibility with 1.17alpha,
-        *   this may also be a ResourceLoaderModule object. Optional when using
-        *   multiple-registration calling style.
+        * @param string|array[] $name Module name as a string or, array of module info arrays
+        *  keyed by name.
+        * @param array|null $info Module info array. When using the first parameter to register
+        *  multiple modules at once, this parameter is optional.
         * @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
+        * @throws InvalidArgumentException If the module info is not an array
         */
        public function register( $name, $info = null ) {
                $moduleSkinStyles = $this->config->get( 'ResourceModuleSkinStyles' );
@@ -321,29 +320,21 @@ class ResourceLoader implements LoggerAwareInterface {
                                );
                        }
 
-                       // Check $name for validity
+                       // Check validity
                        if ( !self::isValidModuleName( $name ) ) {
                                throw new MWException( "ResourceLoader module name '$name' is invalid, "
                                        . "see ResourceLoader::isValidModuleName()" );
                        }
-
-                       // Attach module
-                       if ( $info instanceof ResourceLoaderModule ) {
-                               $this->moduleInfos[$name] = [ 'object' => $info ];
-                               $info->setName( $name );
-                               $this->modules[$name] = $info;
-                       } elseif ( is_array( $info ) ) {
-                               // New calling convention
-                               $this->moduleInfos[$name] = $info;
-                       } else {
-                               throw new MWException(
-                                       'ResourceLoader module info type error for module \'' . $name .
-                                       '\': expected ResourceLoaderModule or array (got: ' . gettype( $info ) . ')'
+                       if ( !is_array( $info ) ) {
+                               throw new InvalidArgumentException(
+                                       'Invalid module info for "' . $name . '": expected array, got ' . gettype( $info )
                                );
                        }
 
-                       // Last-minute changes
+                       // Attach module
+                       $this->moduleInfos[$name] = $info;
 
+                       // Last-minute changes
                        // Apply custom skin-defined styles to existing modules.
                        if ( $this->isFileModule( $name ) ) {
                                foreach ( $moduleSkinStyles as $skinName => $skinStyles ) {
@@ -529,23 +520,18 @@ class ResourceLoader implements LoggerAwareInterface {
                                // No such module
                                return null;
                        }
-                       // Construct the requested object
+                       // Construct the requested module object
                        $info = $this->moduleInfos[$name];
-                       /** @var ResourceLoaderModule $object */
-                       if ( isset( $info['object'] ) ) {
-                               // Object given in info array
-                               $object = $info['object'];
-                       } elseif ( isset( $info['factory'] ) ) {
+                       if ( isset( $info['factory'] ) ) {
+                               /** @var ResourceLoaderModule $object */
                                $object = call_user_func( $info['factory'], $info );
-                               $object->setConfig( $this->getConfig() );
-                               $object->setLogger( $this->logger );
                        } else {
                                $class = $info['class'] ?? ResourceLoaderFileModule::class;
                                /** @var ResourceLoaderModule $object */
                                $object = new $class( $info );
-                               $object->setConfig( $this->getConfig() );
-                               $object->setLogger( $this->logger );
                        }
+                       $object->setConfig( $this->getConfig() );
+                       $object->setLogger( $this->logger );
                        $object->setName( $name );
                        $this->modules[$name] = $object;
                }
@@ -564,10 +550,7 @@ class ResourceLoader implements LoggerAwareInterface {
                        return false;
                }
                $info = $this->moduleInfos[$name];
-               if ( isset( $info['object'] ) ) {
-                       return false;
-               }
-               return (
+               return !isset( $info['factory'] ) && (
                        // The implied default for 'class' is ResourceLoaderFileModule
                        !isset( $info['class'] ) ||
                        // Explicit default
@@ -1048,9 +1031,6 @@ MESSAGE;
                        $states[$name] = 'missing';
                }
 
-               // Generate output
-               $isRaw = false;
-
                $filter = $context->getOnly() === 'styles' ? 'minify-css' : 'minify-js';
 
                foreach ( $modules as $name => $module ) {
@@ -1129,12 +1109,11 @@ MESSAGE;
                                $states[$name] = 'error';
                                unset( $modules[$name] );
                        }
-                       $isRaw |= $module->isRaw();
                }
 
                // Update module states
-               if ( $context->shouldIncludeScripts() && !$context->getRaw() && !$isRaw ) {
-                       if ( count( $modules ) && $context->getOnly() === 'scripts' ) {
+               if ( $context->shouldIncludeScripts() && !$context->getRaw() ) {
+                       if ( $modules && $context->getOnly() === 'scripts' ) {
                                // Set the state of modules loaded as only scripts to ready as
                                // they don't have an mw.loader.implement wrapper that sets the state
                                foreach ( $modules as $name => $module ) {
@@ -1143,7 +1122,7 @@ MESSAGE;
                        }
 
                        // Set the state of modules we didn't respond to with mw.loader.implement
-                       if ( count( $states ) ) {
+                       if ( $states ) {
                                $stateScript = self::makeLoaderStateScript( $states );
                                if ( !$context->getDebug() ) {
                                        $stateScript = self::filter( 'minify-js', $stateScript );
@@ -1264,11 +1243,9 @@ MESSAGE;
         * @return string JavaScript code
         */
        public static function makeMessageSetScript( $messages ) {
-               return Xml::encodeJsCall(
-                       'mw.messages.set',
-                       [ (object)$messages ],
-                       self::inDebugMode()
-               );
+               return 'mw.messages.set('
+                       . self::encodeJsonForScript( (object)$messages )
+                       . ');';
        }
 
        /**
@@ -1352,11 +1329,9 @@ MESSAGE;
                if ( !is_array( $states ) ) {
                        $states = [ $states => $state ];
                }
-               return Xml::encodeJsCall(
-                       'mw.loader.state',
-                       [ $states ],
-                       self::inDebugMode()
-               );
+               return 'mw.loader.state('
+                       . self::encodeJsonForScript( $states )
+                       . ');';
        }
 
        private static function isEmptyObject( stdClass $obj ) {
@@ -1440,11 +1415,9 @@ MESSAGE;
 
                array_walk( $modules, [ self::class, 'trimArray' ] );
 
-               return Xml::encodeJsCall(
-                       'mw.loader.register',
-                       [ $modules ],
-                       self::inDebugMode()
-               );
+               return 'mw.loader.register('
+                       . self::encodeJsonForScript( $modules )
+                       . ');';
        }
 
        /**
@@ -1465,11 +1438,9 @@ MESSAGE;
                if ( !is_array( $sources ) ) {
                        $sources = [ $sources => $loadUrl ];
                }
-               return Xml::encodeJsCall(
-                       'mw.loader.addSource',
-                       [ $sources ],
-                       self::inDebugMode()
-               );
+               return 'mw.loader.addSource('
+                       . self::encodeJsonForScript( $sources )
+                       . ');';
        }
 
        /**
@@ -1721,10 +1692,10 @@ MESSAGE;
                // match the defaults assumed by ResourceLoaderContext.
                // Note: This relies on the defaults either being insignificant or forever constant,
                // as otherwise cached urls could change in meaning when the defaults change.
-               if ( $lang !== 'qqx' ) {
+               if ( $lang !== ResourceLoaderContext::DEFAULT_LANG ) {
                        $query['lang'] = $lang;
                }
-               if ( $skin !== 'fallback' ) {
+               if ( $skin !== ResourceLoaderContext::DEFAULT_SKIN ) {
                        $query['skin'] = $skin;
                }
                if ( $debug === true ) {