Suggest running update.php on database error
[lhc/web/wiklou.git] / includes / CSSMin.php
1 <?php
2
3 class CSSMin {
4 /* Constants */
5
6 /**
7 * Maximum file size to still qualify for in-line embedding as a data-URI
8 *
9 * 24,576 is used because Internet Explorer has a 32,768 byte limit for data URIs, which when base64 encoded will
10 * result in a 1/3 increase in size.
11 */
12 const EMBED_SIZE_LIMIT = 24576;
13
14 /* Static Methods */
15
16 /**
17 * Gets a list of local file paths which are referenced in a CSS style sheet
18 *
19 * @param $source string CSS data to remap
20 * @param $path string File path where the source was read from (optional)
21 * @return array List of local file references
22 */
23 public static function getLocalFileReferences( $source, $path = null ) {
24 $pattern = '/url\([\'"]?(?<file>[^\?\)\:]*)\??[^\)]*[\'"]?\)/';
25 $files = array();
26
27 if ( preg_match_all( $pattern, $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER ) ) {
28 foreach ( $matches as $match ) {
29 $file = ( isset( $path ) ? rtrim( $path, '/' ) . '/' : '' ) . "{$match['file'][0]}";
30
31 // Only proceed if we can access the file
32 if ( file_exists( $file ) ) {
33 $files[] = $file;
34 }
35 }
36 }
37
38 return $files;
39 }
40
41 /**
42 * Remaps CSS URL paths and automatically embeds data URIs for URL rules preceded by an /* @embed * / comment
43 *
44 * @param $source string CSS data to remap
45 * @param $path string File path where the source was read from
46 * @return string Remapped CSS data
47 */
48 public static function remap( $source, $path ) {
49 global $wgUseDataURLs;
50
51 $pattern = '/((?<embed>\s*\/\*\s*\@embed\s*\*\/)(?<rule>[^\;\}]*))?url\([\'"]?(?<file>[^\?\)\:]*)\??[^\)]*[\'"]?\)(?<extra>[^;]*)[\;]?/';
52 $offset = 0;
53
54 while ( preg_match( $pattern, $source, $match, PREG_OFFSET_CAPTURE, $offset ) ) {
55 // Shortcuts
56 $embed = $match['embed'][0];
57 $rule = $match['rule'][0];
58 $extra = $match['extra'][0];
59 $file = "{$path}/{$match['file'][0]}";
60
61 // Only proceed if we can access the file
62 if ( file_exists( $file ) ) {
63 // Add version parameter as a time-stamp in ISO 8601 format, using Z for the timezone, meaning GMT
64 $url = "{$file}?" . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $file ), -2 ) );
65
66 // Detect when URLs were preceeded with embed tags, and also verify file size is below the limit
67 if ( $wgUseDataURLs && $match['embed'][1] > 0 && filesize( $file ) < self::EMBED_SIZE_LIMIT ) {
68 // If we ever get to PHP 5.3, we should use the Fileinfo extension instead of mime_content_type
69 $type = mime_content_type( $file );
70 // Strip off any trailing = symbols (makes browsers freak out)
71 $data = base64_encode( file_get_contents( $file ) );
72 // Build 2 CSS properties; one which uses a base64 encoded data URI in place of the @embed
73 // comment to try and retain line-number integrity , and the other with a remapped an versioned
74 // URL and an Internet Explorer hack making it ignored in all browsers that support data URIs
75 $replacement = "{$rule}url(data:{$type};base64,{$data}){$extra};{$rule}url({$url}){$extra}!ie;";
76 } else {
77 // Build a CSS property with a remapped and versioned URL
78 $replacement = "{$embed}{$rule}url({$url}){$extra};";
79 }
80
81 // Perform replacement on the source
82 $source = substr_replace( $source, $replacement, $match[0][1], strlen( $match[0][0] ) );
83 // Move the offset to the end of the replacement in the source
84 $offset = $match[0][1] + strlen( $replacement );
85 continue;
86 }
87 // Move the offset to the end of the match, leaving it alone
88 $offset = $match[0][1] + strlen( $match[0][0] );
89 }
90
91 return $source;
92 }
93
94 /**
95 * Removes whitespace from CSS data
96 *
97 * @param $source string CSS data to minify
98 * @return string Minified CSS data
99 */
100 public static function minify( $css ) {
101 return trim(
102 str_replace(
103 array( '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ),
104 array( ';', ':', '{', '{', ',', '}', '}' ),
105 preg_replace( array( '/\s+/', '/\/\*.*?\*\//s' ), array( ' ', '' ), $css )
106 )
107 );
108 }
109 }