From 702bce40d0e611961c5d14e6f9321355d5b34793 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Fri, 12 May 2017 19:03:12 +0100 Subject: [PATCH] resourceloader: Simplify validateScriptFile() with getWithSetCallback * Simplify by using early return and getWithSetCallback. * Add TTL (previously indefinite, now 1 week). Bug: T52919 Change-Id: Ic95ba392cdb3bcc8081c77d2c2a3240548bed366 --- .../resourceloader/ResourceLoaderModule.php | 46 +++++++++---------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/includes/resourceloader/ResourceLoaderModule.php b/includes/resourceloader/ResourceLoaderModule.php index 5404e0fb9b..97f9891eeb 100644 --- a/includes/resourceloader/ResourceLoaderModule.php +++ b/includes/resourceloader/ResourceLoaderModule.php @@ -931,36 +931,32 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface { * @return string JS with the original, or a replacement error */ protected function validateScriptFile( $fileName, $contents ) { - if ( $this->getConfig()->get( 'ResourceLoaderValidateJS' ) ) { - // Try for cache hit - $cache = ObjectCache::getMainWANInstance(); - $key = $cache->makeKey( + if ( !$this->getConfig()->get( 'ResourceLoaderValidateJS' ) ) { + return $contents; + } + $cache = ObjectCache::getMainWANInstance(); + return $cache->getWithSetCallback( + $cache->makeKey( 'resourceloader', 'jsparse', self::$parseCacheVersion, md5( $contents ) - ); - $cacheEntry = $cache->get( $key ); - if ( is_string( $cacheEntry ) ) { - return $cacheEntry; - } - - $parser = self::javaScriptParser(); - try { - $parser->parse( $contents, $fileName, 1 ); - $result = $contents; - } catch ( Exception $e ) { - // We'll save this to cache to avoid having to validate broken JS over and over... - $err = $e->getMessage(); - $result = "mw.log.error(" . - Xml::encodeJsVar( "JavaScript parse error: $err" ) . ");"; + ), + $cache::TTL_WEEK, + function () use ( $contents, $fileName ) { + $parser = self::javaScriptParser(); + try { + $parser->parse( $contents, $fileName, 1 ); + $result = $contents; + } catch ( Exception $e ) { + // We'll save this to cache to avoid having to re-validate broken JS + $err = $e->getMessage(); + $result = "mw.log.error(" . + Xml::encodeJsVar( "JavaScript parse error: $err" ) . ");"; + } + return $result; } - - $cache->set( $key, $result ); - return $result; - } else { - return $contents; - } + ); } /** -- 2.20.1