(bug 26399) Preload module info for all modules in startup module, to prevent lots...
[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 /**
24 * Dynamic JavaScript and CSS resource loading system.
25 *
26 * Most of the documention is on the MediaWiki documentation wiki starting at:
27 * http://www.mediawiki.org/wiki/ResourceLoader
28 */
29 class ResourceLoader {
30
31 /* Protected Static Members */
32
33 /** Array: List of module name/ResourceLoaderModule object pairs */
34 protected $modules = array();
35 /** Associative array mapping module name to info associative array */
36 protected $moduleInfos = array();
37
38 /* Protected Methods */
39
40 /**
41 * Loads information stored in the database about modules.
42 *
43 * This method grabs modules dependencies from the database and updates modules
44 * objects.
45 *
46 * This is not inside the module code because it is much faster to
47 * request all of the information at once than it is to have each module
48 * requests its own information. This sacrifice of modularity yields a substantial
49 * performance improvement.
50 *
51 * @param $modules Array: List of module names to preload information for
52 * @param $context ResourceLoaderContext: Context to load the information within
53 */
54 public function preloadModuleInfo( array $modules, ResourceLoaderContext $context ) {
55 if ( !count( $modules ) ) {
56 return; // or else Database*::select() will explode, plus it's cheaper!
57 }
58 $dbr = wfGetDB( DB_SLAVE );
59 $skin = $context->getSkin();
60 $lang = $context->getLanguage();
61
62 // Get file dependency information
63 $res = $dbr->select( 'module_deps', array( 'md_module', 'md_deps' ), array(
64 'md_module' => $modules,
65 'md_skin' => $context->getSkin()
66 ), __METHOD__
67 );
68
69 // Set modules' dependencies
70 $modulesWithDeps = array();
71 foreach ( $res as $row ) {
72 $this->getModule( $row->md_module )->setFileDependencies( $skin,
73 FormatJson::decode( $row->md_deps, true )
74 );
75 $modulesWithDeps[] = $row->md_module;
76 }
77
78 // Register the absence of a dependency row too
79 foreach ( array_diff( $modules, $modulesWithDeps ) as $name ) {
80 $this->getModule( $name )->setFileDependencies( $skin, array() );
81 }
82
83 // Get message blob mtimes. Only do this for modules with messages
84 $modulesWithMessages = array();
85 foreach ( $modules as $name ) {
86 if ( count( $this->getModule( $name )->getMessages() ) ) {
87 $modulesWithMessages[] = $name;
88 }
89 }
90 $modulesWithoutMessages = array_flip( $modules ); // Will be trimmed down by the loop below
91 if ( count( $modulesWithMessages ) ) {
92 $res = $dbr->select( 'msg_resource', array( 'mr_resource', 'mr_timestamp' ), array(
93 'mr_resource' => $modulesWithMessages,
94 'mr_lang' => $lang
95 ), __METHOD__
96 );
97 foreach ( $res as $row ) {
98 $this->getModule( $row->mr_resource )->setMsgBlobMtime( $lang, $row->mr_timestamp );
99 unset( $modulesWithoutMessages[$row->mr_resource] );
100 }
101 }
102 foreach ( array_keys( $modulesWithoutMessages ) as $name ) {
103 $this->getModule( $name )->setMsgBlobMtime( $lang, 0 );
104 }
105 }
106
107 /**
108 * Runs JavaScript or CSS data through a filter, caching the filtered result for future calls.
109 *
110 * Available filters are:
111 * - minify-js \see JSMin::minify
112 * - minify-css \see CSSMin::minify
113 *
114 * If $data is empty, only contains whitespace or the filter was unknown,
115 * $data is returned unmodified.
116 *
117 * @param $filter String: Name of filter to run
118 * @param $data String: Text to filter, such as JavaScript or CSS text
119 * @return String: Filtered data, or a comment containing an error message
120 */
121 protected function filter( $filter, $data ) {
122 wfProfileIn( __METHOD__ );
123
124 // For empty/whitespace-only data or for unknown filters, don't perform
125 // any caching or processing
126 if ( trim( $data ) === ''
127 || !in_array( $filter, array( 'minify-js', 'minify-css' ) ) )
128 {
129 wfProfileOut( __METHOD__ );
130 return $data;
131 }
132
133 // Try for cache hit
134 // Use CACHE_ANYTHING since filtering is very slow compared to DB queries
135 $key = wfMemcKey( 'resourceloader', 'filter', $filter, md5( $data ) );
136 $cache = wfGetCache( CACHE_ANYTHING );
137 $cacheEntry = $cache->get( $key );
138 if ( is_string( $cacheEntry ) ) {
139 wfProfileOut( __METHOD__ );
140 return $cacheEntry;
141 }
142
143 // Run the filter - we've already verified one of these will work
144 try {
145 switch ( $filter ) {
146 case 'minify-js':
147 $result = JSMin::minify( $data );
148 break;
149 case 'minify-css':
150 $result = CSSMin::minify( $data );
151 break;
152 }
153
154 // Save filtered text to Memcached
155 $cache->set( $key, $result );
156 } catch ( Exception $exception ) {
157 // Return exception as a comment
158 $result = "/*\n{$exception->__toString()}\n*/";
159 }
160
161 wfProfileOut( __METHOD__ );
162
163 return $result;
164 }
165
166 /* Methods */
167
168 /**
169 * Registers core modules and runs registration hooks.
170 */
171 public function __construct() {
172 global $IP, $wgResourceModules;
173
174 wfProfileIn( __METHOD__ );
175
176 // Register core modules
177 $this->register( include( "$IP/resources/Resources.php" ) );
178 // Register extension modules
179 wfRunHooks( 'ResourceLoaderRegisterModules', array( &$this ) );
180 $this->register( $wgResourceModules );
181
182 wfProfileOut( __METHOD__ );
183 }
184
185 /**
186 * Registers a module with the ResourceLoader system.
187 *
188 * @param $name Mixed: Name of module as a string or List of name/object pairs as an array
189 * @param $info Module info array. For backwards compatibility with 1.17alpha,
190 * this may also be a ResourceLoaderModule object. Optional when using
191 * multiple-registration calling style.
192 * @throws MWException: If a duplicate module registration is attempted
193 * @throws MWException: If something other than a ResourceLoaderModule is being registered
194 * @return Boolean: False if there were any errors, in which case one or more modules were not
195 * registered
196 */
197 public function register( $name, $info = null ) {
198 wfProfileIn( __METHOD__ );
199
200 // Allow multiple modules to be registered in one call
201 if ( is_array( $name ) ) {
202 foreach ( $name as $key => $value ) {
203 $this->register( $key, $value );
204 }
205 return;
206 }
207
208 // Disallow duplicate registrations
209 if ( isset( $this->moduleInfos[$name] ) ) {
210 // A module has already been registered by this name
211 throw new MWException(
212 'ResourceLoader duplicate registration error. ' .
213 'Another module has already been registered as ' . $name
214 );
215 }
216
217 // Attach module
218 if ( is_object( $info ) ) {
219 // Old calling convention
220 // Validate the input
221 if ( !( $info instanceof ResourceLoaderModule ) ) {
222 throw new MWException( 'ResourceLoader invalid module error. ' .
223 'Instances of ResourceLoaderModule expected.' );
224 }
225
226 $this->moduleInfos[$name] = array( 'object' => $info );
227 $info->setName( $name );
228 $this->modules[$name] = $info;
229 } else {
230 // New calling convention
231 $this->moduleInfos[$name] = $info;
232 }
233
234 wfProfileOut( __METHOD__ );
235 }
236
237 /**
238 * Get a list of module names
239 *
240 * @return Array: List of module names
241 */
242 public function getModuleNames() {
243 return array_keys( $this->moduleInfos );
244 }
245
246 /**
247 * Get the ResourceLoaderModule object for a given module name.
248 *
249 * @param $name String: Module name
250 * @return Mixed: ResourceLoaderModule if module has been registered, null otherwise
251 */
252 public function getModule( $name ) {
253 if ( !isset( $this->modules[$name] ) ) {
254 if ( !isset( $this->moduleInfos[$name] ) ) {
255 // No such module
256 return null;
257 }
258 // Construct the requested object
259 $info = $this->moduleInfos[$name];
260 if ( isset( $info['object'] ) ) {
261 // Object given in info array
262 $object = $info['object'];
263 } else {
264 if ( !isset( $info['class'] ) ) {
265 $class = 'ResourceLoaderFileModule';
266 } else {
267 $class = $info['class'];
268 }
269 $object = new $class( $info );
270 }
271 $object->setName( $name );
272 $this->modules[$name] = $object;
273 }
274
275 return $this->modules[$name];
276 }
277
278 /**
279 * Outputs a response to a resource load-request, including a content-type header.
280 *
281 * @param $context ResourceLoaderContext: Context in which a response should be formed
282 */
283 public function respond( ResourceLoaderContext $context ) {
284 global $wgResourceLoaderMaxage, $wgCacheEpoch;
285
286 // Buffer output to catch warnings. Normally we'd use ob_clean() on the
287 // top-level output buffer to clear warnings, but that breaks when ob_gzhandler
288 // is used: ob_clean() will clear the GZIP header in that case and it won't come
289 // back for subsequent output, resulting in invalid GZIP. So we have to wrap
290 // the whole thing in our own output buffer to be sure the active buffer
291 // doesn't use ob_gzhandler.
292 // See http://bugs.php.net/bug.php?id=36514
293 ob_start();
294
295 wfProfileIn( __METHOD__ );
296 $response = '';
297
298 // Split requested modules into two groups, modules and missing
299 $modules = array();
300 $missing = array();
301 foreach ( $context->getModules() as $name ) {
302 if ( isset( $this->moduleInfos[$name] ) ) {
303 $modules[$name] = $this->getModule( $name );
304 } else {
305 $missing[] = $name;
306 }
307 }
308
309 // If a version wasn't specified we need a shorter expiry time for updates
310 // to propagate to clients quickly
311 if ( is_null( $context->getVersion() ) ) {
312 $maxage = $wgResourceLoaderMaxage['unversioned']['client'];
313 $smaxage = $wgResourceLoaderMaxage['unversioned']['server'];
314 }
315 // If a version was specified we can use a longer expiry time since changing
316 // version numbers causes cache misses
317 else {
318 $maxage = $wgResourceLoaderMaxage['versioned']['client'];
319 $smaxage = $wgResourceLoaderMaxage['versioned']['server'];
320 }
321
322 // Preload information needed to the mtime calculation below
323 try {
324 $this->preloadModuleInfo( array_keys( $modules ), $context );
325 } catch( Exception $e ) {
326 // Add exception to the output as a comment
327 $response .= "/*\n{$e->__toString()}\n*/";
328 }
329
330 wfProfileIn( __METHOD__.'-getModifiedTime' );
331
332 // To send Last-Modified and support If-Modified-Since, we need to detect
333 // the last modified time
334 $mtime = wfTimestamp( TS_UNIX, $wgCacheEpoch );
335 foreach ( $modules as $module ) {
336 try {
337 // Bypass squid cache if the request includes any private modules
338 if ( $module->getGroup() === 'private' ) {
339 $smaxage = 0;
340 }
341 // Calculate maximum modified time
342 $mtime = max( $mtime, $module->getModifiedTime( $context ) );
343 } catch ( Exception $e ) {
344 // Add exception to the output as a comment
345 $response .= "/*\n{$e->__toString()}\n*/";
346 }
347 }
348
349 wfProfileOut( __METHOD__.'-getModifiedTime' );
350
351 if ( $context->getOnly() === 'styles' ) {
352 header( 'Content-Type: text/css' );
353 } else {
354 header( 'Content-Type: text/javascript' );
355 }
356 header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $mtime ) );
357 if ( $context->getDebug() ) {
358 header( 'Cache-Control: must-revalidate' );
359 } else {
360 header( "Cache-Control: public, max-age=$maxage, s-maxage=$smaxage" );
361 header( 'Expires: ' . wfTimestamp( TS_RFC2822, min( $maxage, $smaxage ) + time() ) );
362 }
363
364 // If there's an If-Modified-Since header, respond with a 304 appropriately
365 // Some clients send "timestamp;length=123". Strip the part after the first ';'
366 // so we get a valid timestamp.
367 $ims = $context->getRequest()->getHeader( 'If-Modified-Since' );
368 if ( $ims !== false ) {
369 $imsTS = strtok( $ims, ';' );
370 if ( $mtime <= wfTimestamp( TS_UNIX, $imsTS ) ) {
371 // There's another bug in ob_gzhandler (see also the comment at
372 // the top of this function) that causes it to gzip even empty
373 // responses, meaning it's impossible to produce a truly empty
374 // response (because the gzip header is always there). This is
375 // a problem because 304 responses have to be completely empty
376 // per the HTTP spec, and Firefox behaves buggily when they're not.
377 // See also http://bugs.php.net/bug.php?id=51579
378 // To work around this, we tear down all output buffering before
379 // sending the 304.
380 while ( ob_get_level() > 0 ) {
381 ob_end_clean();
382 }
383
384 header( 'HTTP/1.0 304 Not Modified' );
385 header( 'Status: 304 Not Modified' );
386 wfProfileOut( __METHOD__ );
387 return;
388 }
389 }
390
391 // Generate a response
392 $response .= $this->makeModuleResponse( $context, $modules, $missing );
393
394 // Capture any PHP warnings from the output buffer and append them to the
395 // response in a comment if we're in debug mode.
396 if ( $context->getDebug() && strlen( $warnings = ob_get_contents() ) ) {
397 $response .= "/*\n$warnings\n*/";
398 }
399
400 // Remove the output buffer and output the response
401 ob_end_clean();
402 echo $response;
403
404 wfProfileOut( __METHOD__ );
405 }
406
407 /**
408 * Generates code for a response
409 *
410 * @param $context ResourceLoaderContext: Context in which to generate a response
411 * @param $modules Array: List of module objects keyed by module name
412 * @param $missing Array: List of unavailable modules (optional)
413 * @return String: Response data
414 */
415 public function makeModuleResponse( ResourceLoaderContext $context,
416 array $modules, $missing = array() )
417 {
418 $out = '';
419 if ( $modules === array() && $missing === array() ) {
420 return '/* No modules requested. Max made me put this here */';
421 }
422
423 // Pre-fetch blobs
424 if ( $context->shouldIncludeMessages() ) {
425 //try {
426 $blobs = MessageBlobStore::get( $this, $modules, $context->getLanguage() );
427 //} catch ( Exception $e ) {
428 // Add exception to the output as a comment
429 // $out .= "/*\n{$e->__toString()}\n*/";
430 //}
431 } else {
432 $blobs = array();
433 }
434
435 // Generate output
436 foreach ( $modules as $name => $module ) {
437 wfProfileIn( __METHOD__ . '-' . $name );
438 try {
439 // Scripts
440 $scripts = '';
441 if ( $context->shouldIncludeScripts() ) {
442 $scripts .= $module->getScript( $context ) . "\n";
443 }
444
445 // Styles
446 $styles = array();
447 if ( $context->shouldIncludeStyles() ) {
448 $styles = $module->getStyles( $context );
449 }
450
451 // Messages
452 $messagesBlob = isset( $blobs[$name] ) ? $blobs[$name] : '{}';
453
454 // Append output
455 switch ( $context->getOnly() ) {
456 case 'scripts':
457 $out .= $scripts;
458 break;
459 case 'styles':
460 $out .= self::makeCombinedStyles( $styles );
461 break;
462 case 'messages':
463 $out .= self::makeMessageSetScript( new XmlJsCode( $messagesBlob ) );
464 break;
465 default:
466 // Minify CSS before embedding in mediaWiki.loader.implement call
467 // (unless in debug mode)
468 if ( !$context->getDebug() ) {
469 foreach ( $styles as $media => $style ) {
470 $styles[$media] = $this->filter( 'minify-css', $style );
471 }
472 }
473 $out .= self::makeLoaderImplementScript( $name, $scripts, $styles,
474 new XmlJsCode( $messagesBlob ) );
475 break;
476 }
477 } catch ( Exception $e ) {
478 // Add exception to the output as a comment
479 $out .= "/*\n{$e->__toString()}\n*/";
480
481 // Register module as missing
482 $missing[] = $name;
483 unset( $modules[$name] );
484 }
485 wfProfileOut( __METHOD__ . '-' . $name );
486 }
487
488 // Update module states
489 if ( $context->shouldIncludeScripts() ) {
490 // Set the state of modules loaded as only scripts to ready
491 if ( count( $modules ) && $context->getOnly() === 'scripts'
492 && !isset( $modules['startup'] ) )
493 {
494 $out .= self::makeLoaderStateScript(
495 array_fill_keys( array_keys( $modules ), 'ready' ) );
496 }
497 // Set the state of modules which were requested but unavailable as missing
498 if ( is_array( $missing ) && count( $missing ) ) {
499 $out .= self::makeLoaderStateScript( array_fill_keys( $missing, 'missing' ) );
500 }
501 }
502
503 if ( $context->getDebug() ) {
504 return $out;
505 } else {
506 if ( $context->getOnly() === 'styles' ) {
507 return $this->filter( 'minify-css', $out );
508 } else {
509 return $this->filter( 'minify-js', $out );
510 }
511 }
512 }
513
514 /* Static Methods */
515
516 /**
517 * Returns JS code to call to mediaWiki.loader.implement for a module with
518 * given properties.
519 *
520 * @param $name Module name
521 * @param $scripts Array: List of JavaScript code snippets to be executed after the
522 * module is loaded
523 * @param $styles Array: List of CSS strings keyed by media type
524 * @param $messages Mixed: List of messages associated with this module. May either be an
525 * associative array mapping message key to value, or a JSON-encoded message blob containing
526 * the same data, wrapped in an XmlJsCode object.
527 */
528 public static function makeLoaderImplementScript( $name, $scripts, $styles, $messages ) {
529 if ( is_array( $scripts ) ) {
530 $scripts = implode( $scripts, "\n" );
531 }
532 return Xml::encodeJsCall(
533 'mediaWiki.loader.implement',
534 array(
535 $name,
536 new XmlJsCode( "function() {{$scripts}}" ),
537 (object)$styles,
538 (object)$messages
539 ) );
540 }
541
542 /**
543 * Returns JS code which, when called, will register a given list of messages.
544 *
545 * @param $messages Mixed: Either an associative array mapping message key to value, or a
546 * JSON-encoded message blob containing the same data, wrapped in an XmlJsCode object.
547 */
548 public static function makeMessageSetScript( $messages ) {
549 return Xml::encodeJsCall( 'mediaWiki.messages.set', array( (object)$messages ) );
550 }
551
552 /**
553 * Combines an associative array mapping media type to CSS into a
554 * single stylesheet with @media blocks.
555 *
556 * @param $styles Array: List of CSS strings keyed by media type
557 */
558 public static function makeCombinedStyles( array $styles ) {
559 $out = '';
560 foreach ( $styles as $media => $style ) {
561 $out .= "@media $media {\n" . str_replace( "\n", "\n\t", "\t" . $style ) . "\n}\n";
562 }
563 return $out;
564 }
565
566 /**
567 * Returns a JS call to mediaWiki.loader.state, which sets the state of a
568 * module or modules to a given value. Has two calling conventions:
569 *
570 * - ResourceLoader::makeLoaderStateScript( $name, $state ):
571 * Set the state of a single module called $name to $state
572 *
573 * - ResourceLoader::makeLoaderStateScript( array( $name => $state, ... ) ):
574 * Set the state of modules with the given names to the given states
575 */
576 public static function makeLoaderStateScript( $name, $state = null ) {
577 if ( is_array( $name ) ) {
578 return Xml::encodeJsCall( 'mediaWiki.loader.state', array( $name ) );
579 } else {
580 return Xml::encodeJsCall( 'mediaWiki.loader.state', array( $name, $state ) );
581 }
582 }
583
584 /**
585 * Returns JS code which calls the script given by $script. The script will
586 * be called with local variables name, version, dependencies and group,
587 * which will have values corresponding to $name, $version, $dependencies
588 * and $group as supplied.
589 *
590 * @param $name String: Module name
591 * @param $version Integer: Module version number as a timestamp
592 * @param $dependencies Array: List of module names on which this module depends
593 * @param $group String: Group which the module is in.
594 * @param $script String: JavaScript code
595 */
596 public static function makeCustomLoaderScript( $name, $version, $dependencies, $group, $script ) {
597 $script = str_replace( "\n", "\n\t", trim( $script ) );
598 return Xml::encodeJsCall(
599 "( function( name, version, dependencies, group ) {\n\t$script\n} )",
600 array( $name, $version, $dependencies, $group ) );
601 }
602
603 /**
604 * Returns JS code which calls mediaWiki.loader.register with the given
605 * parameters. Has three calling conventions:
606 *
607 * - ResourceLoader::makeLoaderRegisterScript( $name, $version, $dependencies, $group ):
608 * Register a single module.
609 *
610 * - ResourceLoader::makeLoaderRegisterScript( array( $name1, $name2 ) ):
611 * Register modules with the given names.
612 *
613 * - ResourceLoader::makeLoaderRegisterScript( array(
614 * array( $name1, $version1, $dependencies1, $group1 ),
615 * array( $name2, $version2, $dependencies1, $group2 ),
616 * ...
617 * ) ):
618 * Registers modules with the given names and parameters.
619 *
620 * @param $name String: Module name
621 * @param $version Integer: Module version number as a timestamp
622 * @param $dependencies Array: List of module names on which this module depends
623 * @param $group String: group which the module is in.
624 */
625 public static function makeLoaderRegisterScript( $name, $version = null,
626 $dependencies = null, $group = null )
627 {
628 if ( is_array( $name ) ) {
629 return Xml::encodeJsCall( 'mediaWiki.loader.register', array( $name ) );
630 } else {
631 $version = (int) $version > 1 ? (int) $version : 1;
632 return Xml::encodeJsCall( 'mediaWiki.loader.register',
633 array( $name, $version, $dependencies, $group ) );
634 }
635 }
636
637 /**
638 * Returns JS code which runs given JS code if the client-side framework is
639 * present.
640 *
641 * @param $script String: JavaScript code
642 */
643 public static function makeLoaderConditionalScript( $script ) {
644 $script = str_replace( "\n", "\n\t", trim( $script ) );
645 return "if ( window.mediaWiki ) {\n\t$script\n}\n";
646 }
647
648 /**
649 * Returns JS code which will set the MediaWiki configuration array to
650 * the given value.
651 *
652 * @param $configuration Array: List of configuration values keyed by variable name
653 */
654 public static function makeConfigSetScript( array $configuration ) {
655 return Xml::encodeJsCall( 'mediaWiki.config.set', array( $configuration ) );
656 }
657
658 /**
659 * Determine whether debug mode was requested
660 * Order of priority is 1) request param, 2) cookie, 3) $wg setting
661 * @return bool
662 */
663 public static function inDebugMode() {
664 global $wgRequest, $wgResourceLoaderDebug;
665 static $retval = null;
666 if ( !is_null( $retval ) )
667 return $retval;
668 return $retval = $wgRequest->getFuzzyBool( 'debug',
669 $wgRequest->getCookie( 'resourceLoaderDebug', '', $wgResourceLoaderDebug ) );
670 }
671 }