]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/libs/CSSMin.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / includes / libs / CSSMin.php
1 <?php
2 /*
3  * Copyright 2010 Wikimedia Foundation
4  * 
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may
6  * not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *              http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software distributed
12  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
13  * OF ANY KIND, either express or implied. See the License for the
14  * specific language governing permissions and limitations under the License. 
15  */
16
17 /**
18  * Transforms CSS data
19  * 
20  * This class provides minification, URL remapping, URL extracting, and data-URL embedding.
21  * 
22  * @file
23  * @version 0.1.1 -- 2010-09-11
24  * @author Trevor Parscal <tparscal@wikimedia.org>
25  * @copyright Copyright 2010 Wikimedia Foundation
26  * @license http://www.apache.org/licenses/LICENSE-2.0
27  */
28 class CSSMin {
29         
30         /* Constants */
31         
32         /**
33          * Maximum file size to still qualify for in-line embedding as a data-URI
34          *
35          * 24,576 is used because Internet Explorer has a 32,768 byte limit for data URIs,
36          * which when base64 encoded will result in a 1/3 increase in size.
37          */
38         const EMBED_SIZE_LIMIT = 24576;
39         const URL_REGEX = 'url\(\s*[\'"]?(?P<file>[^\?\)\'"]*)(?P<query>\??[^\)\'"]*)[\'"]?\s*\)';
40         
41         /* Protected Static Members */
42         
43         /** @var array List of common image files extensions and mime-types */
44         protected static $mimeTypes = array(
45                 'gif' => 'image/gif',
46                 'jpe' => 'image/jpeg',
47                 'jpeg' => 'image/jpeg',
48                 'jpg' => 'image/jpeg',
49                 'png' => 'image/png',
50                 'tif' => 'image/tiff',
51                 'tiff' => 'image/tiff',
52                 'xbm' => 'image/x-xbitmap',
53         );
54         
55         /* Static Methods */
56         
57         /**
58          * Gets a list of local file paths which are referenced in a CSS style sheet
59          *
60          * @param $source string CSS data to remap
61          * @param $path string File path where the source was read from (optional)
62          * @return array List of local file references
63          */
64         public static function getLocalFileReferences( $source, $path = null ) {
65                 $files = array();
66                 $rFlags = PREG_OFFSET_CAPTURE | PREG_SET_ORDER;
67                 if ( preg_match_all( '/' . self::URL_REGEX . '/', $source, $matches, $rFlags ) ) {
68                         foreach ( $matches as $match ) {
69                                 $file = ( isset( $path )
70                                         ? rtrim( $path, '/' ) . '/'
71                                         : '' ) . "{$match['file'][0]}";
72
73                                 // Only proceed if we can access the file
74                                 if ( !is_null( $path ) && file_exists( $file ) ) {
75                                         $files[] = $file;
76                                 }
77                         }
78                 }
79                 return $files;
80         }
81         
82         protected static function getMimeType( $file ) {
83                 $realpath = realpath( $file );
84                 // Try a couple of different ways to get the mime-type of a file, in order of
85                 // preference
86                 if (
87                         $realpath
88                         && function_exists( 'finfo_file' )
89                         && function_exists( 'finfo_open' )
90                         && defined( 'FILEINFO_MIME_TYPE' )
91                 ) {
92                         // As of PHP 5.3, this is how you get the mime-type of a file; it uses the Fileinfo
93                         // PECL extension
94                         return finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $realpath );
95                 } else if ( function_exists( 'mime_content_type' ) ) {
96                         // Before this was deprecated in PHP 5.3, this was how you got the mime-type of a file
97                         return mime_content_type( $file );
98                 } else {
99                         // Worst-case scenario has happened, use the file extension to infer the mime-type
100                         $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
101                         if ( isset( self::$mimeTypes[$ext] ) ) {
102                                 return self::$mimeTypes[$ext];
103                         }
104                 }
105                 return false;
106         }
107         
108         /**
109          * Remaps CSS URL paths and automatically embeds data URIs for URL rules
110          * preceded by an /* @embed * / comment
111          *
112          * @param $source string CSS data to remap
113          * @param $local string File path where the source was read from
114          * @param $remote string URL path to the file
115          * @param $embed ???
116          * @return string Remapped CSS data
117          */
118         public static function remap( $source, $local, $remote, $embed = true ) {
119                 $pattern = '/((?P<embed>\s*\/\*\s*\@embed\s*\*\/)(?P<pre>[^\;\}]*))?' .
120                         self::URL_REGEX . '(?P<post>[^;]*)[\;]?/';
121                 $offset = 0;
122                 while ( preg_match( $pattern, $source, $match, PREG_OFFSET_CAPTURE, $offset ) ) {
123                         // Skip fully-qualified URLs and data URIs
124                         $urlScheme = parse_url( $match['file'][0], PHP_URL_SCHEME );
125                         if ( $urlScheme ) {
126                                 // Move the offset to the end of the match, leaving it alone
127                                 $offset = $match[0][1] + strlen( $match[0][0] );
128                                 continue;
129                         }
130                         // URLs with absolute paths like /w/index.php need to be expanded
131                         // to absolute URLs but otherwise left alone
132                         if ( $match['file'][0] !== '' && $match['file'][0][0] === '/' ) {
133                                 // Replace the file path with an expanded URL
134                                 $source = substr_replace( $source, wfExpandUrl( $match['file'][0] ),
135                                         $match['file'][1], strlen( $match['file'][0] )
136                                 );
137                                 // Move the offset to the end of the match, leaving it alone
138                                 $offset = $match[0][1] + strlen( $match[0][0] );
139                                 continue;
140                         }
141                         // Shortcuts
142                         $embed = $match['embed'][0];
143                         $pre = $match['pre'][0];
144                         $post = $match['post'][0];
145                         $query = $match['query'][0];
146                         $url = "{$remote}/{$match['file'][0]}";
147                         $file = "{$local}/{$match['file'][0]}";
148                         // bug 27052 - Guard against double slashes, because foo//../bar
149                         // apparently resolves to foo/bar on (some?) clients
150                         $url = preg_replace( '#([^:])//+#', '\1/', $url );
151                         $replacement = false;
152                         if ( $local !== false && file_exists( $file ) ) {
153                                 // Add version parameter as a time-stamp in ISO 8601 format,
154                                 // using Z for the timezone, meaning GMT
155                                 $url .= '?' . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $file ), -2 ) );
156                                 // Embedding requires a bit of extra processing, so let's skip that if we can
157                                 if ( $embed ) {
158                                         $type = self::getMimeType( $file );
159                                         // Detect when URLs were preceeded with embed tags, and also verify file size is
160                                         // below the limit
161                                         if (
162                                                 $type
163                                                 && $match['embed'][1] > 0
164                                                 && filesize( $file ) < self::EMBED_SIZE_LIMIT
165                                         ) {
166                                                 // Strip off any trailing = symbols (makes browsers freak out)
167                                                 $data = base64_encode( file_get_contents( $file ) );
168                                                 // Build 2 CSS properties; one which uses a base64 encoded data URI in place
169                                                 // of the @embed comment to try and retain line-number integrity, and the
170                                                 // other with a remapped an versioned URL and an Internet Explorer hack
171                                                 // making it ignored in all browsers that support data URIs
172                                                 $replacement = "{$pre}url(data:{$type};base64,{$data}){$post};";
173                                                 $replacement .= "{$pre}url({$url}){$post}!ie;";
174                                         }
175                                 }
176                                 if ( $replacement === false ) {
177                                         // Assume that all paths are relative to $remote, and make them absolute
178                                         $replacement = "{$embed}{$pre}url({$url}){$post};";                             
179                                 }
180                         } else if ( $local === false ) {
181                                 // Assume that all paths are relative to $remote, and make them absolute
182                                 $replacement = "{$embed}{$pre}url({$url}{$query}){$post};";
183                         }
184                         if ( $replacement !== false ) {
185                                 // Perform replacement on the source
186                                 $source = substr_replace(
187                                         $source, $replacement, $match[0][1], strlen( $match[0][0] )
188                                 );
189                                 // Move the offset to the end of the replacement in the source
190                                 $offset = $match[0][1] + strlen( $replacement );
191                                 continue;
192                         }
193                         // Move the offset to the end of the match, leaving it alone
194                         $offset = $match[0][1] + strlen( $match[0][0] );
195                 }
196                 return $source;
197         }
198
199         /**
200          * Removes whitespace from CSS data
201          *
202          * @param $css string CSS data to minify
203          * @return string Minified CSS data
204          */
205         public static function minify( $css ) {
206                 return trim(
207                         str_replace(
208                                 array( '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ),
209                                 array( ';', ':', '{', '{', ',', '}', '}' ),
210                                 preg_replace( array( '/\s+/', '/\/\*.*?\*\//s' ), array( ' ', '' ), $css )
211                         )
212                 );
213         }
214 }