documentation, a couple notes about code and some whitespaces adjustements to make...
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.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 * @file
19 * @author Roan Kattouw
20 * @author Trevor Parscal
21 */
22
23 defined( 'MEDIAWIKI' ) || die( 1 );
24
25 /**
26 * Dynamic JavaScript and CSS resource loading system.
27 *
28 * Most of the documention is on the MediaWiki documentation wiki starting at
29 * http://www.mediawiki.org/wiki/ResourceLoader
30 */
31 class ResourceLoader {
32
33 /* Protected Static Members */
34
35 // @var array list of module name/ResourceLoaderModule object pairs
36 protected $modules = array();
37
38 /* Protected Methods */
39
40 /**
41 * Loads information stored in the database about modules
42 *
43 * This is not inside the module code because it's so much more performant to request all of the information at once
44 * than it is to have each module requests it's own information.
45 *
46 * This method grab modules dependencies from the database and initialize modules object.
47 * A first pass compute dependencies, a second one the blob mtime.
48 *
49 * @param $modules Array List of module names to preload information for
50 * @param $context ResourceLoaderContext Context to load the information within
51 */
52 protected function preloadModuleInfo( array $modules, ResourceLoaderContext $context ) {
53 if ( !count( $modules ) ) {
54 return; # or Database*::select() will explode
55 }
56 $dbr = wfGetDb( DB_SLAVE );
57 $skin = $context->getSkin();
58 $lang = $context->getLanguage();
59
60 // Get file dependency information
61 $res = $dbr->select( 'module_deps', array( 'md_module', 'md_deps' ), array(
62 'md_module' => $modules,
63 'md_skin' => $context->getSkin()
64 ), __METHOD__
65 );
66
67 // Set modules dependecies
68 $modulesWithDeps = array();
69 foreach ( $res as $row ) {
70 $this->modules[$row->md_module]->setFileDependencies( $skin,
71 FormatJson::decode( $row->md_deps, true )
72 );
73 $modulesWithDeps[] = $row->md_module;
74 }
75 // Register the absence of a dependency row too
76 foreach ( array_diff( $modules, $modulesWithDeps ) as $name ) {
77 $this->modules[$name]->setFileDependencies( $skin, array() );
78 }
79
80 // Get message blob mtimes. Only do this for modules with messages
81 $modulesWithMessages = array();
82 $modulesWithoutMessages = array();
83 foreach ( $modules as $name ) {
84 if ( count( $this->modules[$name]->getMessages() ) ) {
85 $modulesWithMessages[] = $name;
86 } else {
87 $modulesWithoutMessages[] = $name;
88 }
89 }
90 if ( count( $modulesWithMessages ) ) {
91 $res = $dbr->select( 'msg_resource', array( 'mr_resource', 'mr_timestamp' ), array(
92 'mr_resource' => $modulesWithMessages,
93 'mr_lang' => $lang
94 ), __METHOD__
95 );
96 foreach ( $res as $row ) {
97 $this->modules[$row->mr_resource]->setMsgBlobMtime( $lang, $row->mr_timestamp );
98 }
99 }
100 foreach ( $modulesWithoutMessages as $name ) {
101 $this->modules[$name]->setMsgBlobMtime( $lang, 0 );
102 }
103 }
104
105 /**
106 * Runs text (js,CSS) through a filter, caching the filtered result for future calls.
107 *
108 * Availables filters are:
109 * - minify-js \see JSMin::minify
110 * - minify-css \see CSSMin::minify
111 * - flip-css \see CSSJanus::transform
112 * When the filter names does not exist, text is returned as is.
113 *
114 * @param $filter String: name of filter to run
115 * @param $data String: text to filter, such as JavaScript or CSS text
116 * @param $file String: path to file being filtered, (optional: only required for CSS to resolve paths)
117 * @return String: filtered data
118 */
119 protected function filter( $filter, $data ) {
120 global $wgMemc;
121 wfProfileIn( __METHOD__ );
122
123 // For empty or whitespace-only things, don't do any processing
124 # FIXME: we should return the data unfiltered if $filter is not supported.
125 # that would save up a md5 computation and one memcached get.
126 if ( trim( $data ) === '' ) {
127 wfProfileOut( __METHOD__ );
128 return $data;
129 }
130
131 // Try memcached
132 $key = wfMemcKey( 'resourceloader', 'filter', $filter, md5( $data ) );
133 $cached = $wgMemc->get( $key );
134
135 if ( $cached !== false && $cached !== null ) {
136 wfProfileOut( __METHOD__ );
137 return $cached;
138 }
139
140 // Run the filter
141 try {
142 switch ( $filter ) {
143 # note: if adding a new filter. Please update method documentation above.
144 case 'minify-js':
145 $result = JSMin::minify( $data );
146 break;
147 case 'minify-css':
148 $result = CSSMin::minify( $data );
149 break;
150 case 'flip-css':
151 $result = CSSJanus::transform( $data, true, false );
152 break;
153 default:
154 // Don't cache anything, just pass right through
155 wfProfileOut( __METHOD__ );
156 return $data;
157 }
158 } catch ( Exception $exception ) {
159 throw new MWException( 'Filter threw an exception: ' . $exception->getMessage() );
160 }
161
162 // Save filtered text to memcached
163 $wgMemc->set( $key, $result );
164
165 wfProfileOut( __METHOD__ );
166 return $result;
167 }
168
169 /* Methods */
170
171 /**
172 * Registers core modules and runs registration hooks
173 */
174 public function __construct() {
175 global $IP;
176
177 wfProfileIn( __METHOD__ );
178
179 // Register core modules
180 $this->register( include( "$IP/resources/Resources.php" ) );
181 // Register extension modules
182 wfRunHooks( 'ResourceLoaderRegisterModules', array( &$this ) );
183
184 wfProfileOut( __METHOD__ );
185 }
186
187 /**
188 * Registers a module with the ResourceLoader system.
189 *
190 * Note that registering the same object under multiple names is not supported
191 * and may silently fail in all kinds of interesting ways.
192 *
193 * @param $name Mixed: string of name of module or array of name/object pairs
194 * @param $object ResourceLoaderModule: module object (optional when using
195 * multiple-registration calling style)
196 * @return Boolean: false if there were any errors, in which case one or more
197 * modules were not registered
198 *
199 * @todo We need much more clever error reporting, not just in detailing what
200 * happened, but in bringing errors to the client in a way that they can
201 * easily see them if they want to, such as by using FireBug
202 */
203 public function register( $name, ResourceLoaderModule $object = null ) {
204 wfProfileIn( __METHOD__ );
205
206 // Allow multiple modules to be registered in one call
207 if ( is_array( $name ) && !isset( $object ) ) {
208 foreach ( $name as $key => $value ) {
209 $this->register( $key, $value );
210 }
211
212 wfProfileOut( __METHOD__ );
213 return;
214 }
215
216 // Disallow duplicate registrations
217 if ( isset( $this->modules[$name] ) ) {
218 // A module has already been registered by this name
219 throw new MWException( 'Another module has already been registered as ' . $name );
220 }
221
222 // Validate the input (type hinting lets null through)
223 if ( !( $object instanceof ResourceLoaderModule ) ) {
224 throw new MWException( 'Invalid ResourceLoader module error. Instances of ResourceLoaderModule expected.' );
225 }
226
227 // Attach module
228 $this->modules[$name] = $object;
229 $object->setName( $name );
230
231 wfProfileOut( __METHOD__ );
232 }
233
234 /**
235 * Gets a map of all modules and their options
236 *
237 * @return Array: array( modulename => ResourceLoaderModule )
238 */
239 public function getModules() {
240 return $this->modules;
241 }
242
243 /**
244 * Get the ResourceLoaderModule object for a given module name
245 *
246 * @param $name String: module name
247 * @return mixed ResourceLoaderModule or null if not registered
248 */
249 public function getModule( $name ) {
250 return isset( $this->modules[$name] ) ? $this->modules[$name] : null;
251 }
252
253 /**
254 * Outputs a response to a resource load-request, including a content-type header
255 *
256 * @param $context ResourceLoaderContext object
257 */
258 public function respond( ResourceLoaderContext $context ) {
259 global $wgResourceLoaderMaxage, $wgCacheEpoch;
260
261 wfProfileIn( __METHOD__ );
262
263 // Split requested modules into two groups, modules and missing
264 $modules = array();
265 $missing = array();
266
267 foreach ( $context->getModules() as $name ) {
268 if ( isset( $this->modules[$name] ) ) {
269 $modules[$name] = $this->modules[$name];
270 } else {
271 $missing[] = $name;
272 }
273 }
274
275 // If a version wasn't specified we need a shorter expiry time for updates to
276 // propagate to clients quickly
277 if ( is_null( $context->getVersion() ) ) {
278 $maxage = $wgResourceLoaderMaxage['unversioned']['client'];
279 $smaxage = $wgResourceLoaderMaxage['unversioned']['server'];
280 }
281 // If a version was specified we can use a longer expiry time since changing
282 // version numbers causes cache misses
283 else {
284 $maxage = $wgResourceLoaderMaxage['versioned']['client'];
285 $smaxage = $wgResourceLoaderMaxage['versioned']['server'];
286 }
287
288 // Preload information needed to the mtime calculation below
289 $this->preloadModuleInfo( array_keys( $modules ), $context );
290
291 // To send Last-Modified and support If-Modified-Since, we need to detect
292 // the last modified time
293 wfProfileIn( __METHOD__.'-getModifiedTime' );
294 $mtime = wfTimestamp( TS_UNIX, $wgCacheEpoch );
295 foreach ( $modules as $module ) {
296 // Bypass squid cache if the request includes any private modules
297 if ( $module->getGroup() === 'private' ) {
298 $smaxage = 0;
299 }
300 // Calculate maximum modified time
301 $mtime = max( $mtime, $module->getModifiedTime( $context ) );
302 }
303 wfProfileOut( __METHOD__.'-getModifiedTime' );
304
305 header( 'Content-Type: ' . ( $context->getOnly() === 'styles' ? 'text/css' : 'text/javascript' ) );
306 header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $mtime ) );
307 header( "Cache-Control: public, max-age=$maxage, s-maxage=$smaxage" );
308 header( 'Expires: ' . wfTimestamp( TS_RFC2822, min( $maxage, $smaxage ) + time() ) );
309
310 // If there's an If-Modified-Since header, respond with a 304 appropriately
311 $ims = $context->getRequest()->getHeader( 'If-Modified-Since' );
312 if ( $ims !== false && $mtime <= wfTimestamp( TS_UNIX, $ims ) ) {
313 header( 'HTTP/1.0 304 Not Modified' );
314 header( 'Status: 304 Not Modified' );
315 wfProfileOut( __METHOD__ );
316 return;
317 }
318
319 $response = $this->makeModuleResponse( $context, $modules, $missing );
320 if ( $context->getDebug() && strlen( $warnings = ob_get_contents() ) ) {
321 $response .= "/*\n$warnings\n*/";
322 }
323 // Clear any warnings from the buffer
324 ob_clean();
325 echo $response;
326
327 wfProfileOut( __METHOD__ );
328 }
329
330 /**
331 *
332 * @param $context ResourceLoaderContext
333 * @param $modules Array array( modulename => ResourceLoaderModule )
334 * @param $missing Unavailables modules (Default null)
335 */
336 public function makeModuleResponse( ResourceLoaderContext $context, array $modules, $missing = null ) {
337 // Pre-fetch blobs
338 $blobs = $context->shouldIncludeMessages() ?
339 MessageBlobStore::get( $this, $modules, $context->getLanguage() ) : array();
340
341 // Generate output
342 $out = '';
343 foreach ( $modules as $name => $module ) {
344 wfProfileIn( __METHOD__ . '-' . $name );
345
346 // Scripts
347 $scripts = '';
348 if ( $context->shouldIncludeScripts() ) {
349 $scripts .= $module->getScript( $context ) . "\n";
350 }
351
352 // Styles
353 $styles = array();
354 if ( $context->shouldIncludeStyles() && ( count( $styles = $module->getStyles( $context ) ) ) ) {
355 // Flip CSS on a per-module basis
356 if ( $this->modules[$name]->getFlip( $context ) ) {
357 foreach ( $styles as $media => $style ) {
358 $styles[$media] = $this->filter( 'flip-css', $style );
359 }
360 }
361 }
362
363 // Messages
364 $messages = isset( $blobs[$name] ) ? $blobs[$name] : '{}';
365
366 // Append output
367 switch ( $context->getOnly() ) {
368 case 'scripts':
369 $out .= $scripts;
370 break;
371 case 'styles':
372 $out .= self::makeCombinedStyles( $styles );
373 break;
374 case 'messages':
375 $out .= self::makeMessageSetScript( $messages );
376 break;
377 default:
378 // Minify CSS before embedding in mediaWiki.loader.implement call (unless in debug mode)
379 if ( !$context->getDebug() ) {
380 foreach ( $styles as $media => $style ) {
381 $styles[$media] = $this->filter( 'minify-css', $style );
382 }
383 }
384 $out .= self::makeLoaderImplementScript( $name, $scripts, $styles, $messages );
385 break;
386 }
387
388 wfProfileOut( __METHOD__ . '-' . $name );
389 }
390
391 // Update module states
392 if ( $context->shouldIncludeScripts() ) {
393 // Set the state of modules loaded as only scripts to ready
394 if ( count( $modules ) && $context->getOnly() === 'scripts' && !isset( $modules['startup'] ) ) {
395 $out .= self::makeLoaderStateScript( array_fill_keys( array_keys( $modules ), 'ready' ) );
396 }
397 // Set the state of modules which were requested but unavailable as missing
398 if ( is_array( $missing ) && count( $missing ) ) {
399 $out .= self::makeLoaderStateScript( array_fill_keys( $missing, 'missing' ) );
400 }
401 }
402
403 if ( $context->getDebug() ) {
404 return $out;
405 } else {
406 if ( $context->getOnly() === 'styles' ) {
407 return $this->filter( 'minify-css', $out );
408 } else {
409 return $this->filter( 'minify-js', $out );
410 }
411 }
412 }
413
414 /* Static Methods */
415
416 public static function makeLoaderImplementScript( $name, $scripts, $styles, $messages ) {
417 if ( is_array( $scripts ) ) {
418 $scripts = implode( $scripts, "\n" );
419 }
420 if ( is_array( $styles ) ) {
421 $styles = count( $styles ) ? FormatJson::encode( $styles ) : 'null';
422 }
423 if ( is_array( $messages ) ) {
424 $messages = count( $messages ) ? FormatJson::encode( $messages ) : 'null';
425 }
426 return "mediaWiki.loader.implement( '$name', function() {{$scripts}},\n$styles,\n$messages );\n";
427 }
428
429 public static function makeMessageSetScript( $messages ) {
430 if ( is_array( $messages ) ) {
431 $messages = count( $messages ) ? FormatJson::encode( $messages ) : 'null';
432 }
433 return "mediaWiki.msg.set( $messages );\n";
434 }
435
436 public static function makeCombinedStyles( array $styles ) {
437 $out = '';
438 foreach ( $styles as $media => $style ) {
439 $out .= "@media $media {\n" . str_replace( "\n", "\n\t", "\t" . $style ) . "\n}\n";
440 }
441 return $out;
442 }
443
444 public static function makeLoaderStateScript( $name, $state = null ) {
445 if ( is_array( $name ) ) {
446 $statuses = FormatJson::encode( $name );
447 return "mediaWiki.loader.state( $statuses );\n";
448 } else {
449 $name = Xml::escapeJsString( $name );
450 $state = Xml::escapeJsString( $state );
451 return "mediaWiki.loader.state( '$name', '$state' );\n";
452 }
453 }
454
455 public static function makeCustomLoaderScript( $name, $version, $dependencies, $group, $script ) {
456 $name = Xml::escapeJsString( $name );
457 $version = (int) $version > 1 ? (int) $version : 1;
458 $dependencies = FormatJson::encode( $dependencies );
459 $group = FormatJson::encode( $group );
460 $script = str_replace( "\n", "\n\t", trim( $script ) );
461 return "( function( name, version, dependencies, group ) {\n\t$script\n} )" .
462 "( '$name', $version, $dependencies, $group );\n";
463 }
464
465 public static function makeLoaderRegisterScript( $name, $version = null, $dependencies = null, $group = null ) {
466 if ( is_array( $name ) ) {
467 $registrations = FormatJson::encode( $name );
468 return "mediaWiki.loader.register( $registrations );\n";
469 } else {
470 $name = Xml::escapeJsString( $name );
471 $version = (int) $version > 1 ? (int) $version : 1;
472 $dependencies = FormatJson::encode( $dependencies );
473 $group = FormatJson::encode( $group );
474 return "mediaWiki.loader.register( '$name', $version, $dependencies, $group );\n";
475 }
476 }
477
478 public static function makeLoaderConditionalScript( $script ) {
479 $script = str_replace( "\n", "\n\t", trim( $script ) );
480 return "if ( window.mediaWiki ) {\n\t$script\n}\n";
481 }
482
483 public static function makeConfigSetScript( array $configuration ) {
484 $configuration = FormatJson::encode( $configuration );
485 return "mediaWiki.config.set( $configuration );\n";
486 }
487 }