Move DatabaseType to Database.php
[lhc/web/wiklou.git] / includes / CSSMin.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 */
18
19 /**
20 * Transforms CSS data
21 *
22 * This class provides minification, URL remapping, URL extracting, and data-URL embedding.
23 *
24 * @author Trevor Parscal
25 */
26 class CSSMin {
27
28 /* Constants */
29
30 /**
31 * Maximum file size to still qualify for in-line embedding as a data-URI
32 *
33 * 24,576 is used because Internet Explorer has a 32,768 byte limit for data URIs, which when base64 encoded will
34 * result in a 1/3 increase in size.
35 */
36 const EMBED_SIZE_LIMIT = 24576;
37
38 /* Static Methods */
39
40 /**
41 * Gets a list of local file paths which are referenced in a CSS style sheet
42 *
43 * @param $source string CSS data to remap
44 * @param $path string File path where the source was read from (optional)
45 * @return array List of local file references
46 */
47 public static function getLocalFileReferences( $source, $path = null ) {
48 $pattern = '/url\([\'"]?(?<file>[^\?\)\:]*)\??[^\)]*[\'"]?\)/';
49 $files = array();
50
51 if ( preg_match_all( $pattern, $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER ) ) {
52 foreach ( $matches as $match ) {
53 $file = ( isset( $path ) ? rtrim( $path, '/' ) . '/' : '' ) . "{$match['file'][0]}";
54
55 // Only proceed if we can access the file
56 if ( file_exists( $file ) ) {
57 $files[] = $file;
58 }
59 }
60 }
61
62 return $files;
63 }
64
65 /**
66 * Remaps CSS URL paths and automatically embeds data URIs for URL rules preceded by an /* @embed * / comment
67 *
68 * @param $source string CSS data to remap
69 * @param $path string File path where the source was read from
70 * @return string Remapped CSS data
71 */
72 public static function remap( $source, $path ) {
73 global $wgUseDataURLs;
74
75 $pattern = '/((?<embed>\s*\/\*\s*\@embed\s*\*\/)(?<rule>[^\;\}]*))?url\([\'"]?(?<file>[^\?\)\:]*)\??[^\)]*[\'"]?\)(?<extra>[^;]*)[\;]?/';
76 $offset = 0;
77
78 while ( preg_match( $pattern, $source, $match, PREG_OFFSET_CAPTURE, $offset ) ) {
79 // Shortcuts
80 $embed = $match['embed'][0];
81 $rule = $match['rule'][0];
82 $extra = $match['extra'][0];
83 $file = "{$path}/{$match['file'][0]}";
84
85 // Only proceed if we can access the file
86 if ( file_exists( $file ) ) {
87 // Add version parameter as a time-stamp in ISO 8601 format, using Z for the timezone, meaning GMT
88 $url = "{$file}?" . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $file ), -2 ) );
89
90 // Detect when URLs were preceeded with embed tags, and also verify file size is below the limit
91 if ( $wgUseDataURLs && $match['embed'][1] > 0 && filesize( $file ) < self::EMBED_SIZE_LIMIT ) {
92 // If we ever get to PHP 5.3, we should use the Fileinfo extension instead of mime_content_type
93 $type = mime_content_type( $file );
94 // Strip off any trailing = symbols (makes browsers freak out)
95 $data = base64_encode( file_get_contents( $file ) );
96 // Build 2 CSS properties; one which uses a base64 encoded data URI in place of the @embed
97 // comment to try and retain line-number integrity , and the other with a remapped an versioned
98 // URL and an Internet Explorer hack making it ignored in all browsers that support data URIs
99 $replacement = "{$rule}url(data:{$type};base64,{$data}){$extra};{$rule}url({$url}){$extra}!ie;";
100 } else {
101 // Build a CSS property with a remapped and versioned URL
102 $replacement = "{$embed}{$rule}url({$url}){$extra};";
103 }
104
105 // Perform replacement on the source
106 $source = substr_replace( $source, $replacement, $match[0][1], strlen( $match[0][0] ) );
107 // Move the offset to the end of the replacement in the source
108 $offset = $match[0][1] + strlen( $replacement );
109 continue;
110 }
111 // Move the offset to the end of the match, leaving it alone
112 $offset = $match[0][1] + strlen( $match[0][0] );
113 }
114
115 return $source;
116 }
117
118 /**
119 * Removes whitespace from CSS data
120 *
121 * @param $source string CSS data to minify
122 * @return string Minified CSS data
123 */
124 public static function minify( $css ) {
125 return trim(
126 str_replace(
127 array( '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ),
128 array( ';', ':', '{', '{', ',', '}', '}' ),
129 preg_replace( array( '/\s+/', '/\/\*.*?\*\//s' ), array( ' ', '' ), $css )
130 )
131 );
132 }
133 }