ResourcesTest: Detect missing files in url(...) expressions
[lhc/web/wiklou.git] / includes / libs / CSSMin.php
index b386a92..2648921 100644 (file)
@@ -60,13 +60,15 @@ class CSSMin {
        /* Static Methods */
 
        /**
-        * Gets a list of local file paths which are referenced in a CSS style sheet
+        * Gets a list of local file paths which are referenced in a CSS style sheet.
         *
-        * This function will always return an empty array if the second parameter is not given or null
-        * for backwards-compatibility.
+        * If you wish non-existent files to be listed too, use getAllLocalFileReferences().
         *
-        * @param string $source CSS data to remap
-        * @param string $path File path where the source was read from (optional)
+        * For backwards-compatibility, if the second parameter is not given or null,
+        * this function will return an empty array instead of erroring out.
+        *
+        * @param string $source CSS stylesheet source to process
+        * @param string $path File path where the source was read from
         * @return array List of local file references
         */
        public static function getLocalFileReferences( $source, $path = null ) {
@@ -74,6 +76,25 @@ class CSSMin {
                        return array();
                }
 
+               $files = self::getAllLocalFileReferences( $source, $path );
+
+               // Skip non-existent files
+               $files = array_filter( $files, function ( $file ) {
+                       return file_exists( $file );
+               } );
+
+               return $files;
+       }
+
+       /**
+        * Gets a list of local file paths which are referenced in a CSS style sheet, including
+        * non-existent files.
+        *
+        * @param string $source CSS stylesheet source to process
+        * @param string $path File path where the source wa
+        * @return array List of local file references
+        */
+       public static function getAllLocalFileReferences( $source, $path ) {
                $path = rtrim( $path, '/' ) . '/';
                $files = array();
 
@@ -87,13 +108,7 @@ class CSSMin {
                                        break;
                                }
 
-                               $file = $path . $url;
-                               // Skip non-existent files
-                               if ( file_exists( $file ) ) {
-                                       break;
-                               }
-
-                               $files[] = $file;
+                               $files[] = $path . $url;
                        }
                }
                return $files;
@@ -399,7 +414,7 @@ class CSSMin {
 
                if ( $local === false ) {
                        // Assume that all paths are relative to $remote, and make them absolute
-                       return $remote . '/' . $url;
+                       $url = $remote . '/' . $url;
                } else {
                        // We drop the query part here and instead make the path relative to $remote
                        $url = "{$remote}/{$file}";
@@ -418,8 +433,11 @@ class CSSMin {
                        }
                        // If any of these conditions failed (file missing, we don't want to embed it
                        // or it's not embeddable), return the URL (possibly with ?timestamp part)
-                       return $url;
                }
+               if ( function_exists( 'wfRemoveDotSegments' ) ) {
+                       $url = wfRemoveDotSegments( $url );
+               }
+               return $url;
        }
 
        /**