resourceloader: Include global LESS variables in LESS cache key
authorAlexia E. Smith <washuu@gmail.com>
Tue, 10 Apr 2018 22:35:49 +0000 (17:35 -0500)
committerJforrester <jforrester@wikimedia.org>
Fri, 25 May 2018 18:13:10 +0000 (18:13 +0000)
This prevents cache churn when the wiki-global LESS variables vary
between wikis because the cache key is used as a "global" instead of
db-local. This is good for the common case, but should still explicitly
vary if the vars differ between wikis.

Bug: T191937
Change-Id: If12fd07a7062792205384150d6f5fd9a83f996cc

includes/resourceloader/ResourceLoader.php
includes/resourceloader/ResourceLoaderFileModule.php
tests/phpunit/includes/resourceloader/ResourceLoaderTest.php

index 2274793..1cbd878 100644 (file)
@@ -1704,12 +1704,13 @@ MESSAGE;
         * Returns LESS compiler set up for use with MediaWiki
         *
         * @since 1.27
-        * @param array $extraVars Associative array of extra (i.e., other than the
-        *   globally-configured ones) that should be used for compilation.
+        * @param array $vars Associative array of variables that should be used
+        *  for compilation. Since 1.32, this method no longer automatically includes
+        *  global LESS vars from ResourceLoader::getLessVars (T191937).
         * @throws MWException
         * @return Less_Parser
         */
-       public function getLessCompiler( $extraVars = [] ) {
+       public function getLessCompiler( $vars = [] ) {
                // When called from the installer, it is possible that a required PHP extension
                // is missing (at least for now; see T49564). If this is the case, throw an
                // exception (caught by the installer) to prevent a fatal error later on.
@@ -1718,7 +1719,7 @@ MESSAGE;
                }
 
                $parser = new Less_Parser;
-               $parser->ModifyVars( array_merge( $this->getLessVars(), $extraVars ) );
+               $parser->ModifyVars( $vars );
                $parser->SetImportDirs(
                        array_fill_keys( $this->config->get( 'ResourceLoaderLESSImportPaths' ), '' )
                );
index f2f3383..7f8c7f5 100644 (file)
@@ -959,9 +959,12 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        $cache = ObjectCache::getLocalServerInstance( CACHE_ANYTHING );
                }
 
-               // Construct a cache key from the LESS file name and a hash digest
+               $vars = array_merge(
+                       $context->getResourceLoader()->getLessVars(),
+                       $this->getLessVars( $context )
+               );
+               // Construct a cache key from the LESS file name, and a hash digest
                // of the LESS variables used for compilation.
-               $vars = $this->getLessVars( $context );
                ksort( $vars );
                $varsHash = hash( 'md4', serialize( $vars ) );
                $cacheKey = $cache->makeGlobalKey( 'LESS', $fileName, $varsHash );
index 95006aa..7c2476f 100644 (file)
@@ -267,7 +267,7 @@ class ResourceLoaderTest extends ResourceLoaderTestCase {
         */
        public function testLessImportDirs() {
                $rl = new EmptyResourceLoader();
-               $lc = $rl->getLessCompiler();
+               $lc = $rl->getLessCompiler( $rl->getLessVars() );
                $basePath = dirname( dirname( __DIR__ ) ) . '/data/less';
                $lc->SetImportDirs( [
                         "$basePath/common" => '',