Merge "ResourcesTest: Detect missing files in url(...) expressions"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderFileModule.php
index b734def..112ebc0 100644 (file)
@@ -152,6 +152,12 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         */
        protected $localFileRefs = array();
 
+       /**
+        * @var array Place where readStyleFile() tracks file dependencies for non-existent files.
+        * Used in tests to detect missing dependencies.
+        */
+       protected $missingLocalFileRefs = array();
+
        /* Methods */
 
        /**
@@ -526,19 +532,16 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        }
 
        /**
-        * Helper method to gather file mtimes for getDefinitionSummary.
+        * Helper method to gather file hashes for getDefinitionSummary.
         *
-        * Last modified timestamps are calculated from the highest last modified
-        * timestamp of this module's constituent files as well as the files it
-        * depends on. This function is context-sensitive, only performing
-        * calculations on files relevant to the given language, skin and debug
-        * mode.
+        * This function is context-sensitive, only computing hashes of files relevant to the
+        * given language, skin, etc.
         *
         * @see ResourceLoaderModule::getFileDependencies
         * @param ResourceLoaderContext $context
         * @return array
         */
-       protected function getFileMtimes( ResourceLoaderContext $context ) {
+       protected function getFileHashes( ResourceLoaderContext $context ) {
                $files = array();
 
                // Flatten style files into $files
@@ -577,13 +580,10 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                // entry point Less file we already know about.
                $files = array_values( array_unique( $files ) );
 
-               // Don't max() because older files are significant.
-               // While the associated file names are significant, that is already taken care of by the
-               // definition summary. Avoid creating an array keyed by file path here because those are
-               // absolute file paths. Including that would needlessly cause global cache invalidation
-               // when the MediaWiki installation path changes (which is quite common in cases like
-               // Wikimedia where the installation path reflects the MediaWiki branch name).
-               return array_map( array( __CLASS__, 'safeFilemtime' ), $files );
+               // Don't include keys or file paths here, only the hashes. Including that would needlessly
+               // cause global cache invalidation when files move or if e.g. the MediaWiki path changes.
+               // Any significant ordering is already detected by the definition summary.
+               return array_map( array( __CLASS__, 'safeFileHash' ), $files );
        }
 
        /**
@@ -597,6 +597,14 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
 
                $options = array();
                foreach ( array(
+                       // The following properties are omitted because they don't affect the module reponse:
+                       // - localBasePath (Per T104950; Changes when absolute directory name changes. If
+                       //    this affects 'scripts' and other file paths, getFileHashes accounts for that.)
+                       // - remoteBasePath (Per T104950)
+                       // - dependencies (provided via startup module)
+                       // - targets
+                       // - group (provided via startup module)
+                       // - position (only used by OutputPage)
                        'scripts',
                        'debugScripts',
                        'loaderScripts',
@@ -604,17 +612,9 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        'languageScripts',
                        'skinScripts',
                        'skinStyles',
-                       'dependencies',
                        'messages',
-                       'targets',
                        'templates',
-                       'group',
-                       'position',
                        'skipFunction',
-                       // FIXME: localBasePath includes the MediaWiki installation path and
-                       // needlessly causes cache invalidation.
-                       'localBasePath',
-                       'remoteBasePath',
                        'debugRaw',
                        'raw',
                ) as $member ) {
@@ -623,7 +623,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
 
                $summary[] = array(
                        'options' => $options,
-                       'fileMtimes' => $this->getFileMTimes( $context ),
+                       'fileHashes' => $this->getFileHashes( $context ),
                        'msgBlobMtime' => $this->getMsgBlobMtime( $context->getLanguage() ),
                );
                return $summary;
@@ -923,10 +923,14 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                $localDir = dirname( $localPath );
                $remoteDir = dirname( $remotePath );
                // Get and register local file references
-               $this->localFileRefs = array_merge(
-                       $this->localFileRefs,
-                       CSSMin::getLocalFileReferences( $style, $localDir )
-               );
+               $localFileRefs = CSSMin::getAllLocalFileReferences( $style, $localDir );
+               foreach ( $localFileRefs as $file ) {
+                       if ( file_exists( $file ) ) {
+                               $this->localFileRefs[] = $file;
+                       } else {
+                               $this->missingLocalFileRefs[] = $file;
+                       }
+               }
                return CSSMin::remap(
                        $style, $localDir, $remoteDir, true
                );