Merge "ResourceLoaderModule: cache file content hash"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Mon, 21 Sep 2015 12:55:32 +0000 (12:55 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 21 Sep 2015 12:55:32 +0000 (12:55 +0000)
includes/resourceloader/ResourceLoaderModule.php

index 1a4d1f1..1d3ffb5 100644 (file)
@@ -857,14 +857,35 @@ abstract class ResourceLoaderModule {
         * @return string Hash
         */
        protected static function safeFileHash( $filePath ) {
+               static $cache;
+
+               if ( !$cache ) {
+                       $cache = ObjectCache::newAccelerator( CACHE_NONE );
+               }
+
+               MediaWiki\suppressWarnings();
+               $mtime = filemtime( $filePath );
+               MediaWiki\restoreWarnings();
+               if ( !$mtime ) {
+                       return '';
+               }
+
+               $cacheKey = wfGlobalCacheKey( 'resourceloader', __METHOD__, $filePath );
+               $cachedHash = $cache->get( $cacheKey );
+               if ( isset( $cachedHash['mtime'] ) && $cachedHash['mtime'] === $mtime ) {
+                       return $cachedHash['hash'];
+               }
+
                MediaWiki\suppressWarnings();
                $contents = file_get_contents( $filePath );
                MediaWiki\restoreWarnings();
-               if ( $contents !== false ) {
-                       $hash = hash( 'md4', $contents );
-               } else {
-                       $hash = '';
+               if ( !$contents ) {
+                       return '';
                }
+
+               $hash = hash( 'md4', $contents );
+               $cache->set( $cacheKey, array( 'mtime' => $mtime, 'hash' => $hash ), 60 * 60 * 24 );
+
                return $hash;
        }
 }