resourceloader: Change getLessCompiler() to not be static
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.php
index 414f8e2..74ee6ab 100644 (file)
@@ -41,7 +41,7 @@ class ResourceLoader implements LoggerAwareInterface {
        protected static $debugMode = null;
 
        /** @var array */
-       private static $lessVars = null;
+       private $lessVars = null;
 
        /**
         * Module name/ResourceLoaderModule object pairs
@@ -101,11 +101,11 @@ class ResourceLoader implements LoggerAwareInterface {
         * requests its own information. This sacrifice of modularity yields a substantial
         * performance improvement.
         *
-        * @param array $modules List of module names to preload information for
+        * @param array $moduleNames List of module names to preload information for
         * @param ResourceLoaderContext $context Context to load the information within
         */
-       public function preloadModuleInfo( array $modules, ResourceLoaderContext $context ) {
-               if ( !count( $modules ) ) {
+       public function preloadModuleInfo( array $moduleNames, ResourceLoaderContext $context ) {
+               if ( !$moduleNames ) {
                        // Or else Database*::select() will explode, plus it's cheaper!
                        return;
                }
@@ -116,11 +116,12 @@ class ResourceLoader implements LoggerAwareInterface {
                // Batched version of ResourceLoaderModule::getFileDependencies
                $vary = "$skin|$lang";
                $res = $dbr->select( 'module_deps', array( 'md_module', 'md_deps' ), array(
-                               'md_module' => $modules,
+                               'md_module' => $moduleNames,
                                'md_skin' => $vary,
                        ), __METHOD__
                );
-               // Prime in-object cache values for each module
+
+               // Prime in-object cache for file dependencies
                $modulesWithDeps = array();
                foreach ( $res as $row ) {
                        $module = $this->getModule( $row->md_module );
@@ -132,41 +133,25 @@ class ResourceLoader implements LoggerAwareInterface {
                        }
                }
                // Register the absence of a dependency row too
-               foreach ( array_diff( $modules, $modulesWithDeps ) as $name ) {
+               foreach ( array_diff( $moduleNames, $modulesWithDeps ) as $name ) {
                        $module = $this->getModule( $name );
                        if ( $module ) {
                                $this->getModule( $name )->setFileDependencies( $context, array() );
                        }
                }
 
-               // Get message blob mtimes. Only do this for modules with messages
-               $modulesWithMessages = array();
-               foreach ( $modules as $name ) {
+               // Prime in-object cache for message blobs for modules with messages
+               $modules = array();
+               foreach ( $moduleNames as $name ) {
                        $module = $this->getModule( $name );
-                       if ( $module && count( $module->getMessages() ) ) {
-                               $modulesWithMessages[] = $name;
-                       }
-               }
-               $modulesWithoutMessages = array_flip( $modules ); // Will be trimmed down by the loop below
-               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 ) {
-                               $module = $this->getModule( $row->mr_resource );
-                               if ( $module ) {
-                                       $module->setMsgBlobMtime( $lang, wfTimestamp( TS_UNIX, $row->mr_timestamp ) );
-                                       unset( $modulesWithoutMessages[$row->mr_resource] );
-                               }
+                       if ( $module && $module->getMessages() ) {
+                               $modules[$name] = $module;
                        }
                }
-               foreach ( array_keys( $modulesWithoutMessages ) as $name ) {
-                       $module = $this->getModule( $name );
-                       if ( $module ) {
-                               $module->setMsgBlobMtime( $lang, 1 );
-                       }
+               $store = $this->getMessageBlobStore();
+               $blobs = $store->getBlobs( $modules, $lang );
+               foreach ( $blobs as $name => $blob ) {
+                       $modules[$name]->setMessageBlob( $blob, $lang );
                }
        }
 
@@ -241,15 +226,13 @@ class ResourceLoader implements LoggerAwareInterface {
 
        /**
         * Register core modules and runs registration hooks.
-        * @param Config|null $config
+        * @param Config $config [optional]
+        * @param LoggerInterface $logger [optional]
         */
        public function __construct( Config $config = null, LoggerInterface $logger = null ) {
                global $IP;
 
-               if ( !$logger ) {
-                       $logger = new NullLogger();
-               }
-               $this->setLogger( $logger );
+               $this->logger = $logger ?: new NullLogger();
 
                if ( !$config ) {
                        $this->logger->debug( __METHOD__ . ' was called without providing a Config instance' );
@@ -274,7 +257,7 @@ class ResourceLoader implements LoggerAwareInterface {
                        $this->registerTestModules();
                }
 
-               $this->setMessageBlobStore( new MessageBlobStore( $this ) );
+               $this->setMessageBlobStore( new MessageBlobStore( $this, $this->logger ) );
        }
 
        /**
@@ -284,10 +267,22 @@ class ResourceLoader implements LoggerAwareInterface {
                return $this->config;
        }
 
+       /**
+        * @since 1.26
+        * @param LoggerInterface $logger
+        */
        public function setLogger( LoggerInterface $logger ) {
                $this->logger = $logger;
        }
 
+       /**
+        * @since 1.27
+        * @return LoggerInterface
+        */
+       public function getLogger() {
+               return $this->logger;
+       }
+
        /**
         * @since 1.26
         * @return MessageBlobStore
@@ -548,6 +543,7 @@ class ResourceLoader implements LoggerAwareInterface {
                                /** @var ResourceLoaderModule $object */
                                $object = new $class( $info );
                                $object->setConfig( $this->getConfig() );
+                               $object->setLogger( $this->logger );
                        }
                        $object->setName( $name );
                        $this->modules[$name] = $object;
@@ -666,7 +662,7 @@ class ResourceLoader implements LoggerAwareInterface {
                }
 
                try {
-                       // Preload for getCombinedVersion()
+                       // Preload for getCombinedVersion() and for batch makeModuleResponse()
                        $this->preloadModuleInfo( array_keys( $modules ), $context );
                } catch ( Exception $e ) {
                        MWExceptionHandler::logException( $e );
@@ -966,19 +962,6 @@ MESSAGE;
                        return $data;
                }
 
-               // Pre-fetch blobs
-               if ( $context->shouldIncludeMessages() ) {
-                       try {
-                               $this->blobStore->get( $this, $modules, $context->getLanguage() );
-                       } catch ( Exception $e ) {
-                               MWExceptionHandler::logException( $e );
-                               $this->logger->warning( 'Prefetching MessageBlobStore failed: {exception}', array(
-                                       'exception' => $e
-                               ) );
-                               $this->errors[] = self::formatExceptionNoComment( $e );
-                       }
-               }
-
                foreach ( $missing as $name ) {
                        $states[$name] = 'missing';
                }
@@ -1606,15 +1589,13 @@ MESSAGE;
        /**
         * Returns LESS compiler set up for use with MediaWiki
         *
-        * @since 1.22
-        * @since 1.27 added $extraVars parameter
-        * @param Config $config
+        * @since 1.27
         * @param array $extraVars Associative array of extra (i.e., other than the
         *   globally-configured ones) that should be used for compilation.
         * @throws MWException
         * @return Less_Parser
         */
-       public static function getLessCompiler( Config $config, $extraVars = array() ) {
+       public function getLessCompiler( $extraVars = array() ) {
                // When called from the installer, it is possible that a required PHP extension
                // is missing (at least for now; see bug 47564). If this is the case, throw an
                // exception (caught by the installer) to prevent a fatal error later on.
@@ -1623,10 +1604,12 @@ MESSAGE;
                }
 
                $parser = new Less_Parser;
-               $parser->ModifyVars( array_merge( self::getLessVars( $config ), $extraVars ) );
-               $parser->SetImportDirs( array_fill_keys( $config->get( 'ResourceLoaderLESSImportPaths' ), '' ) );
+               $parser->ModifyVars( array_merge( $this->getLessVars(), $extraVars ) );
+               $parser->SetImportDirs(
+                       array_fill_keys( $this->config->get( 'ResourceLoaderLESSImportPaths' ), '' )
+               );
                $parser->SetOption( 'relativeUrls', false );
-               $parser->SetCacheDir( $config->get( 'CacheDirectory' ) ?: wfTempDir() );
+               $parser->SetCacheDir( $this->config->get( 'CacheDirectory' ) ?: wfTempDir() );
 
                return $parser;
        }
@@ -1634,16 +1617,15 @@ MESSAGE;
        /**
         * Get global LESS variables.
         *
-        * @param Config $config
-        * @since 1.22
+        * @since 1.27
         * @return array Map of variable names to string CSS values.
         */
-       public static function getLessVars( Config $config ) {
-               if ( !self::$lessVars ) {
-                       $lessVars = $config->get( 'ResourceLoaderLESSVars' );
+       public function getLessVars() {
+               if ( !$this->lessVars ) {
+                       $lessVars = $this->config->get( 'ResourceLoaderLESSVars' );
                        Hooks::run( 'ResourceLoaderGetLessVars', array( &$lessVars ) );
-                       self::$lessVars = $lessVars;
+                       $this->lessVars = $lessVars;
                }
-               return self::$lessVars;
+               return $this->lessVars;
        }
 }