Merge "jquery.makeCollapsible: cleanup"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.php
1 <?php
2 /**
3 * Base class for resource loading system.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Roan Kattouw
22 * @author Trevor Parscal
23 */
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 protected static $filterCacheVersion = 7;
35 protected static $requiredSourceProperties = array( 'loadScript' );
36
37 /** Array: List of module name/ResourceLoaderModule object pairs */
38 protected $modules = array();
39
40 /** Associative array mapping module name to info associative array */
41 protected $moduleInfos = array();
42
43 /** Associative array mapping framework ids to a list of names of test suite modules */
44 /** like array( 'qunit' => array( 'mediawiki.tests.qunit.suites', 'ext.foo.tests', .. ), .. ) */
45 protected $testModuleNames = array();
46
47 /** array( 'source-id' => array( 'loadScript' => 'http://.../load.php' ) ) **/
48 protected $sources = array();
49
50 /* Protected Methods */
51
52 /**
53 * Loads information stored in the database about modules.
54 *
55 * This method grabs modules dependencies from the database and updates modules
56 * objects.
57 *
58 * This is not inside the module code because it is much faster to
59 * request all of the information at once than it is to have each module
60 * requests its own information. This sacrifice of modularity yields a substantial
61 * performance improvement.
62 *
63 * @param array $modules List of module names to preload information for
64 * @param $context ResourceLoaderContext: Context to load the information within
65 */
66 public function preloadModuleInfo( array $modules, ResourceLoaderContext $context ) {
67 if ( !count( $modules ) ) {
68 return; // or else Database*::select() will explode, plus it's cheaper!
69 }
70 $dbr = wfGetDB( DB_SLAVE );
71 $skin = $context->getSkin();
72 $lang = $context->getLanguage();
73
74 // Get file dependency information
75 $res = $dbr->select( 'module_deps', array( 'md_module', 'md_deps' ), array(
76 'md_module' => $modules,
77 'md_skin' => $skin
78 ), __METHOD__
79 );
80
81 // Set modules' dependencies
82 $modulesWithDeps = array();
83 foreach ( $res as $row ) {
84 $this->getModule( $row->md_module )->setFileDependencies( $skin,
85 FormatJson::decode( $row->md_deps, true )
86 );
87 $modulesWithDeps[] = $row->md_module;
88 }
89
90 // Register the absence of a dependency row too
91 foreach ( array_diff( $modules, $modulesWithDeps ) as $name ) {
92 $this->getModule( $name )->setFileDependencies( $skin, array() );
93 }
94
95 // Get message blob mtimes. Only do this for modules with messages
96 $modulesWithMessages = array();
97 foreach ( $modules as $name ) {
98 if ( count( $this->getModule( $name )->getMessages() ) ) {
99 $modulesWithMessages[] = $name;
100 }
101 }
102 $modulesWithoutMessages = array_flip( $modules ); // Will be trimmed down by the loop below
103 if ( count( $modulesWithMessages ) ) {
104 $res = $dbr->select( 'msg_resource', array( 'mr_resource', 'mr_timestamp' ), array(
105 'mr_resource' => $modulesWithMessages,
106 'mr_lang' => $lang
107 ), __METHOD__
108 );
109 foreach ( $res as $row ) {
110 $this->getModule( $row->mr_resource )->setMsgBlobMtime( $lang,
111 wfTimestamp( TS_UNIX, $row->mr_timestamp ) );
112 unset( $modulesWithoutMessages[$row->mr_resource] );
113 }
114 }
115 foreach ( array_keys( $modulesWithoutMessages ) as $name ) {
116 $this->getModule( $name )->setMsgBlobMtime( $lang, 0 );
117 }
118 }
119
120 /**
121 * Runs JavaScript or CSS data through a filter, caching the filtered result for future calls.
122 *
123 * Available filters are:
124 * - minify-js \see JavaScriptMinifier::minify
125 * - minify-css \see CSSMin::minify
126 *
127 * If $data is empty, only contains whitespace or the filter was unknown,
128 * $data is returned unmodified.
129 *
130 * @param string $filter Name of filter to run
131 * @param string $data Text to filter, such as JavaScript or CSS text
132 * @return String: Filtered data, or a comment containing an error message
133 */
134 protected function filter( $filter, $data ) {
135 global $wgResourceLoaderMinifierStatementsOnOwnLine, $wgResourceLoaderMinifierMaxLineLength;
136 wfProfileIn( __METHOD__ );
137
138 // For empty/whitespace-only data or for unknown filters, don't perform
139 // any caching or processing
140 if ( trim( $data ) === ''
141 || !in_array( $filter, array( 'minify-js', 'minify-css' ) ) )
142 {
143 wfProfileOut( __METHOD__ );
144 return $data;
145 }
146
147 // Try for cache hit
148 // Use CACHE_ANYTHING since filtering is very slow compared to DB queries
149 $key = wfMemcKey( 'resourceloader', 'filter', $filter, self::$filterCacheVersion, md5( $data ) );
150 $cache = wfGetCache( CACHE_ANYTHING );
151 $cacheEntry = $cache->get( $key );
152 if ( is_string( $cacheEntry ) ) {
153 wfProfileOut( __METHOD__ );
154 return $cacheEntry;
155 }
156
157 $result = '';
158 // Run the filter - we've already verified one of these will work
159 try {
160 switch ( $filter ) {
161 case 'minify-js':
162 $result = JavaScriptMinifier::minify( $data,
163 $wgResourceLoaderMinifierStatementsOnOwnLine,
164 $wgResourceLoaderMinifierMaxLineLength
165 );
166 $result .= "\n/* cache key: $key */";
167 break;
168 case 'minify-css':
169 $result = CSSMin::minify( $data );
170 $result .= "\n/* cache key: $key */";
171 break;
172 }
173
174 // Save filtered text to Memcached
175 $cache->set( $key, $result );
176 } catch ( Exception $exception ) {
177 wfDebugLog( 'resourceloader', __METHOD__ . ": minification failed: $e" );
178 $this->hasErrors = true;
179 // Return exception as a comment
180 $result = $this->makeComment( $exception->__toString() );
181 }
182
183 wfProfileOut( __METHOD__ );
184
185 return $result;
186 }
187
188 /* Methods */
189
190 /**
191 * Registers core modules and runs registration hooks.
192 */
193 public function __construct() {
194 global $IP, $wgResourceModules, $wgResourceLoaderSources, $wgLoadScript, $wgEnableJavaScriptTest;
195
196 wfProfileIn( __METHOD__ );
197
198 // Add 'local' source first
199 $this->addSource( 'local', array( 'loadScript' => $wgLoadScript, 'apiScript' => wfScript( 'api' ) ) );
200
201 // Add other sources
202 $this->addSource( $wgResourceLoaderSources );
203
204 // Register core modules
205 $this->register( include "$IP/resources/Resources.php" );
206 // Register extension modules
207 wfRunHooks( 'ResourceLoaderRegisterModules', array( &$this ) );
208 $this->register( $wgResourceModules );
209
210 if ( $wgEnableJavaScriptTest === true ) {
211 $this->registerTestModules();
212 }
213
214 wfProfileOut( __METHOD__ );
215 }
216
217 /**
218 * Registers a module with the ResourceLoader system.
219 *
220 * @param $name Mixed: Name of module as a string or List of name/object pairs as an array
221 * @param array $info Module info array. For backwards compatibility with 1.17alpha,
222 * this may also be a ResourceLoaderModule object. Optional when using
223 * multiple-registration calling style.
224 * @throws MWException: If a duplicate module registration is attempted
225 * @throws MWException: If a module name contains illegal characters (pipes or commas)
226 * @throws MWException: If something other than a ResourceLoaderModule is being registered
227 * @return Boolean: False if there were any errors, in which case one or more modules were not
228 * registered
229 */
230 public function register( $name, $info = null ) {
231 wfProfileIn( __METHOD__ );
232
233 // Allow multiple modules to be registered in one call
234 $registrations = is_array( $name ) ? $name : array( $name => $info );
235 foreach ( $registrations as $name => $info ) {
236 // Disallow duplicate registrations
237 if ( isset( $this->moduleInfos[$name] ) ) {
238 wfProfileOut( __METHOD__ );
239 // A module has already been registered by this name
240 throw new MWException(
241 'ResourceLoader duplicate registration error. ' .
242 'Another module has already been registered as ' . $name
243 );
244 }
245
246 // Check $name for validity
247 if ( !self::isValidModuleName( $name ) ) {
248 wfProfileOut( __METHOD__ );
249 throw new MWException( "ResourceLoader module name '$name' is invalid, see ResourceLoader::isValidModuleName()" );
250 }
251
252 // Attach module
253 if ( is_object( $info ) ) {
254 // Old calling convention
255 // Validate the input
256 if ( !( $info instanceof ResourceLoaderModule ) ) {
257 wfProfileOut( __METHOD__ );
258 throw new MWException( 'ResourceLoader invalid module error. ' .
259 'Instances of ResourceLoaderModule expected.' );
260 }
261
262 $this->moduleInfos[$name] = array( 'object' => $info );
263 $info->setName( $name );
264 $this->modules[$name] = $info;
265 } else {
266 // New calling convention
267 $this->moduleInfos[$name] = $info;
268 }
269 }
270
271 wfProfileOut( __METHOD__ );
272 }
273
274 /**
275 */
276 public function registerTestModules() {
277 global $IP, $wgEnableJavaScriptTest;
278
279 if ( $wgEnableJavaScriptTest !== true ) {
280 throw new MWException( 'Attempt to register JavaScript test modules but <tt>$wgEnableJavaScriptTest</tt> is false. Edit your <tt>LocalSettings.php</tt> to enable it.' );
281 }
282
283 wfProfileIn( __METHOD__ );
284
285 // Get core test suites
286 $testModules = array();
287 $testModules['qunit'] = include "$IP/tests/qunit/QUnitTestResources.php";
288 // Get other test suites (e.g. from extensions)
289 wfRunHooks( 'ResourceLoaderTestModules', array( &$testModules, &$this ) );
290
291 // Add the testrunner (which configures QUnit) to the dependencies.
292 // Since it must be ready before any of the test suites are executed.
293 foreach ( $testModules['qunit'] as &$module ) {
294 // Make sure all test modules are top-loading so that when QUnit starts
295 // on document-ready, it will run once and finish. If some tests arrive
296 // later (possibly after QUnit has already finished) they will be ignored.
297 $module['position'] = 'top';
298 $module['dependencies'][] = 'mediawiki.tests.qunit.testrunner';
299 }
300
301 foreach ( $testModules as $id => $names ) {
302 // Register test modules
303 $this->register( $testModules[$id] );
304
305 // Keep track of their names so that they can be loaded together
306 $this->testModuleNames[$id] = array_keys( $testModules[$id] );
307 }
308
309 wfProfileOut( __METHOD__ );
310 }
311
312 /**
313 * Add a foreign source of modules.
314 *
315 * Source properties:
316 * 'loadScript': URL (either fully-qualified or protocol-relative) of load.php for this source
317 *
318 * @param $id Mixed: source ID (string), or array( id1 => props1, id2 => props2, ... )
319 * @param array $properties source properties
320 * @throws MWException
321 */
322 public function addSource( $id, $properties = null ) {
323 // Allow multiple sources to be registered in one call
324 if ( is_array( $id ) ) {
325 foreach ( $id as $key => $value ) {
326 $this->addSource( $key, $value );
327 }
328 return;
329 }
330
331 // Disallow duplicates
332 if ( isset( $this->sources[$id] ) ) {
333 throw new MWException(
334 'ResourceLoader duplicate source addition error. ' .
335 'Another source has already been registered as ' . $id
336 );
337 }
338
339 // Validate properties
340 foreach ( self::$requiredSourceProperties as $prop ) {
341 if ( !isset( $properties[$prop] ) ) {
342 throw new MWException( "Required property $prop missing from source ID $id" );
343 }
344 }
345
346 $this->sources[$id] = $properties;
347 }
348
349 /**
350 * Get a list of module names
351 *
352 * @return Array: List of module names
353 */
354 public function getModuleNames() {
355 return array_keys( $this->moduleInfos );
356 }
357
358 /**
359 * Get a list of test module names for one (or all) frameworks.
360 * If the given framework id is unknkown, or if the in-object variable is not an array,
361 * then it will return an empty array.
362 *
363 * @param string $framework Optional. Get only the test module names for one
364 * particular framework.
365 * @return Array
366 */
367 public function getTestModuleNames( $framework = 'all' ) {
368 /// @todo api siteinfo prop testmodulenames modulenames
369 if ( $framework == 'all' ) {
370 return $this->testModuleNames;
371 } elseif ( isset( $this->testModuleNames[$framework] ) && is_array( $this->testModuleNames[$framework] ) ) {
372 return $this->testModuleNames[$framework];
373 } else {
374 return array();
375 }
376 }
377
378 /**
379 * Get the ResourceLoaderModule object for a given module name.
380 *
381 * @param string $name Module name
382 * @return ResourceLoaderModule if module has been registered, null otherwise
383 */
384 public function getModule( $name ) {
385 if ( !isset( $this->modules[$name] ) ) {
386 if ( !isset( $this->moduleInfos[$name] ) ) {
387 // No such module
388 return null;
389 }
390 // Construct the requested object
391 $info = $this->moduleInfos[$name];
392 if ( isset( $info['object'] ) ) {
393 // Object given in info array
394 $object = $info['object'];
395 } else {
396 if ( !isset( $info['class'] ) ) {
397 $class = 'ResourceLoaderFileModule';
398 } else {
399 $class = $info['class'];
400 }
401 $object = new $class( $info );
402 }
403 $object->setName( $name );
404 $this->modules[$name] = $object;
405 }
406
407 return $this->modules[$name];
408 }
409
410 /**
411 * Get the list of sources
412 *
413 * @return Array: array( id => array of properties, .. )
414 */
415 public function getSources() {
416 return $this->sources;
417 }
418
419 /**
420 * Outputs a response to a resource load-request, including a content-type header.
421 *
422 * @param $context ResourceLoaderContext: Context in which a response should be formed
423 */
424 public function respond( ResourceLoaderContext $context ) {
425 global $wgCacheEpoch, $wgUseFileCache;
426
427 // Use file cache if enabled and available...
428 if ( $wgUseFileCache ) {
429 $fileCache = ResourceFileCache::newFromContext( $context );
430 if ( $this->tryRespondFromFileCache( $fileCache, $context ) ) {
431 return; // output handled
432 }
433 }
434
435 // Buffer output to catch warnings. Normally we'd use ob_clean() on the
436 // top-level output buffer to clear warnings, but that breaks when ob_gzhandler
437 // is used: ob_clean() will clear the GZIP header in that case and it won't come
438 // back for subsequent output, resulting in invalid GZIP. So we have to wrap
439 // the whole thing in our own output buffer to be sure the active buffer
440 // doesn't use ob_gzhandler.
441 // See http://bugs.php.net/bug.php?id=36514
442 ob_start();
443
444 wfProfileIn( __METHOD__ );
445 $errors = '';
446 $this->hasErrors = false;
447
448 // Split requested modules into two groups, modules and missing
449 $modules = array();
450 $missing = array();
451 foreach ( $context->getModules() as $name ) {
452 if ( isset( $this->moduleInfos[$name] ) ) {
453 $module = $this->getModule( $name );
454 // Do not allow private modules to be loaded from the web.
455 // This is a security issue, see bug 34907.
456 if ( $module->getGroup() === 'private' ) {
457 wfDebugLog( 'resourceloader', __METHOD__ . ": request for private module denied: $e" );
458 $this->hasErrors = true;
459 // Add exception to the output as a comment
460 $errors .= $this->makeComment( "Cannot show private module \"$name\"" );
461
462 continue;
463 }
464 $modules[$name] = $module;
465 } else {
466 $missing[] = $name;
467 }
468 }
469
470 // Preload information needed to the mtime calculation below
471 try {
472 $this->preloadModuleInfo( array_keys( $modules ), $context );
473 } catch ( Exception $e ) {
474 wfDebugLog( 'resourceloader', __METHOD__ . ": preloading module info failed: $e" );
475 $this->hasErrors = true;
476 // Add exception to the output as a comment
477 $errors .= $this->makeComment( $e->__toString() );
478 }
479
480 wfProfileIn( __METHOD__ . '-getModifiedTime' );
481
482 // To send Last-Modified and support If-Modified-Since, we need to detect
483 // the last modified time
484 $mtime = wfTimestamp( TS_UNIX, $wgCacheEpoch );
485 foreach ( $modules as $module ) {
486 /**
487 * @var $module ResourceLoaderModule
488 */
489 try {
490 // Calculate maximum modified time
491 $mtime = max( $mtime, $module->getModifiedTime( $context ) );
492 } catch ( Exception $e ) {
493 wfDebugLog( 'resourceloader', __METHOD__ . ": calculating maximum modified time failed: $e" );
494 $this->hasErrors = true;
495 // Add exception to the output as a comment
496 $errors .= $this->makeComment( $e->__toString() );
497 }
498 }
499
500 wfProfileOut( __METHOD__ . '-getModifiedTime' );
501
502 // If there's an If-Modified-Since header, respond with a 304 appropriately
503 if ( $this->tryRespondLastModified( $context, $mtime ) ) {
504 wfProfileOut( __METHOD__ );
505 return; // output handled (buffers cleared)
506 }
507
508 // Generate a response
509 $response = $this->makeModuleResponse( $context, $modules, $missing );
510
511 // Prepend comments indicating exceptions
512 $response = $errors . $response;
513
514 // Capture any PHP warnings from the output buffer and append them to the
515 // response in a comment if we're in debug mode.
516 if ( $context->getDebug() && strlen( $warnings = ob_get_contents() ) ) {
517 $response = $this->makeComment( $warnings ) . $response;
518 $this->hasErrors = true;
519 }
520
521 // Save response to file cache unless there are errors
522 if ( isset( $fileCache ) && !$errors && !$missing ) {
523 // Cache single modules...and other requests if there are enough hits
524 if ( ResourceFileCache::useFileCache( $context ) ) {
525 if ( $fileCache->isCacheWorthy() ) {
526 $fileCache->saveText( $response );
527 } else {
528 $fileCache->incrMissesRecent( $context->getRequest() );
529 }
530 }
531 }
532
533 // Send content type and cache related headers
534 $this->sendResponseHeaders( $context, $mtime, $this->hasErrors );
535
536 // Remove the output buffer and output the response
537 ob_end_clean();
538 echo $response;
539
540 wfProfileOut( __METHOD__ );
541 }
542
543 /**
544 * Send content type and last modified headers to the client.
545 * @param $context ResourceLoaderContext
546 * @param string $mtime TS_MW timestamp to use for last-modified
547 * @param bool $error Whether there are commented-out errors in the response
548 * @return void
549 */
550 protected function sendResponseHeaders( ResourceLoaderContext $context, $mtime, $errors ) {
551 global $wgResourceLoaderMaxage;
552 // If a version wasn't specified we need a shorter expiry time for updates
553 // to propagate to clients quickly
554 // If there were errors, we also need a shorter expiry time so we can recover quickly
555 if ( is_null( $context->getVersion() ) || $errors ) {
556 $maxage = $wgResourceLoaderMaxage['unversioned']['client'];
557 $smaxage = $wgResourceLoaderMaxage['unversioned']['server'];
558 // If a version was specified we can use a longer expiry time since changing
559 // version numbers causes cache misses
560 } else {
561 $maxage = $wgResourceLoaderMaxage['versioned']['client'];
562 $smaxage = $wgResourceLoaderMaxage['versioned']['server'];
563 }
564 if ( $context->getOnly() === 'styles' ) {
565 header( 'Content-Type: text/css; charset=utf-8' );
566 header( 'Access-Control-Allow-Origin: *' );
567 } else {
568 header( 'Content-Type: text/javascript; charset=utf-8' );
569 }
570 header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $mtime ) );
571 if ( $context->getDebug() ) {
572 // Do not cache debug responses
573 header( 'Cache-Control: private, no-cache, must-revalidate' );
574 header( 'Pragma: no-cache' );
575 } else {
576 header( "Cache-Control: public, max-age=$maxage, s-maxage=$smaxage" );
577 $exp = min( $maxage, $smaxage );
578 header( 'Expires: ' . wfTimestamp( TS_RFC2822, $exp + time() ) );
579 }
580 }
581
582 /**
583 * If there's an If-Modified-Since header, respond with a 304 appropriately
584 * and clear out the output buffer. If the client cache is too old then do nothing.
585 * @param $context ResourceLoaderContext
586 * @param string $mtime The TS_MW timestamp to check the header against
587 * @return bool True iff 304 header sent and output handled
588 */
589 protected function tryRespondLastModified( ResourceLoaderContext $context, $mtime ) {
590 // If there's an If-Modified-Since header, respond with a 304 appropriately
591 // Some clients send "timestamp;length=123". Strip the part after the first ';'
592 // so we get a valid timestamp.
593 $ims = $context->getRequest()->getHeader( 'If-Modified-Since' );
594 // Never send 304s in debug mode
595 if ( $ims !== false && !$context->getDebug() ) {
596 $imsTS = strtok( $ims, ';' );
597 if ( $mtime <= wfTimestamp( TS_UNIX, $imsTS ) ) {
598 // There's another bug in ob_gzhandler (see also the comment at
599 // the top of this function) that causes it to gzip even empty
600 // responses, meaning it's impossible to produce a truly empty
601 // response (because the gzip header is always there). This is
602 // a problem because 304 responses have to be completely empty
603 // per the HTTP spec, and Firefox behaves buggily when they're not.
604 // See also http://bugs.php.net/bug.php?id=51579
605 // To work around this, we tear down all output buffering before
606 // sending the 304.
607 // On some setups, ob_get_level() doesn't seem to go down to zero
608 // no matter how often we call ob_get_clean(), so instead of doing
609 // the more intuitive while ( ob_get_level() > 0 ) ob_get_clean();
610 // we have to be safe here and avoid an infinite loop.
611 // Caching the level is not an option, need to allow it to
612 // shorten the loop on-the-fly (bug 46836)
613 for ( $i = 0; $i < ob_get_level(); $i++ ) {
614 ob_end_clean();
615 }
616
617 header( 'HTTP/1.0 304 Not Modified' );
618 header( 'Status: 304 Not Modified' );
619 return true;
620 }
621 }
622 return false;
623 }
624
625 /**
626 * Send out code for a response from file cache if possible
627 *
628 * @param $fileCache ResourceFileCache: Cache object for this request URL
629 * @param $context ResourceLoaderContext: Context in which to generate a response
630 * @return bool If this found a cache file and handled the response
631 */
632 protected function tryRespondFromFileCache(
633 ResourceFileCache $fileCache, ResourceLoaderContext $context
634 ) {
635 global $wgResourceLoaderMaxage;
636 // Buffer output to catch warnings.
637 ob_start();
638 // Get the maximum age the cache can be
639 $maxage = is_null( $context->getVersion() )
640 ? $wgResourceLoaderMaxage['unversioned']['server']
641 : $wgResourceLoaderMaxage['versioned']['server'];
642 // Minimum timestamp the cache file must have
643 $good = $fileCache->isCacheGood( wfTimestamp( TS_MW, time() - $maxage ) );
644 if ( !$good ) {
645 try { // RL always hits the DB on file cache miss...
646 wfGetDB( DB_SLAVE );
647 } catch ( DBConnectionError $e ) { // ...check if we need to fallback to cache
648 $good = $fileCache->isCacheGood(); // cache existence check
649 }
650 }
651 if ( $good ) {
652 $ts = $fileCache->cacheTimestamp();
653 // Send content type and cache headers
654 $this->sendResponseHeaders( $context, $ts, false );
655 // If there's an If-Modified-Since header, respond with a 304 appropriately
656 if ( $this->tryRespondLastModified( $context, $ts ) ) {
657 return false; // output handled (buffers cleared)
658 }
659 $response = $fileCache->fetchText();
660 // Capture any PHP warnings from the output buffer and append them to the
661 // response in a comment if we're in debug mode.
662 if ( $context->getDebug() && strlen( $warnings = ob_get_contents() ) ) {
663 $response = "/*\n$warnings\n*/\n" . $response;
664 }
665 // Remove the output buffer and output the response
666 ob_end_clean();
667 echo $response . "\n/* Cached {$ts} */";
668 return true; // cache hit
669 }
670 // Clear buffer
671 ob_end_clean();
672
673 return false; // cache miss
674 }
675
676 protected function makeComment( $text ) {
677 $encText = str_replace( '*/', '* /', $text );
678 return "/*\n$encText\n*/\n";
679 }
680
681 /**
682 * Generates code for a response
683 *
684 * @param $context ResourceLoaderContext: Context in which to generate a response
685 * @param array $modules List of module objects keyed by module name
686 * @param array $missing List of unavailable modules (optional)
687 * @return String: Response data
688 */
689 public function makeModuleResponse( ResourceLoaderContext $context,
690 array $modules, $missing = array()
691 ) {
692 $out = '';
693 $exceptions = '';
694 if ( $modules === array() && $missing === array() ) {
695 return '/* No modules requested. Max made me put this here */';
696 }
697
698 wfProfileIn( __METHOD__ );
699 // Pre-fetch blobs
700 if ( $context->shouldIncludeMessages() ) {
701 try {
702 $blobs = MessageBlobStore::get( $this, $modules, $context->getLanguage() );
703 } catch ( Exception $e ) {
704 wfDebugLog( 'resourceloader', __METHOD__ . ": pre-fetching blobs from MessageBlobStore failed: $e" );
705 $this->hasErrors = true;
706 // Add exception to the output as a comment
707 $exceptions .= $this->makeComment( $e->__toString() );
708 }
709 } else {
710 $blobs = array();
711 }
712
713 // Generate output
714 $isRaw = false;
715 foreach ( $modules as $name => $module ) {
716 /**
717 * @var $module ResourceLoaderModule
718 */
719
720 wfProfileIn( __METHOD__ . '-' . $name );
721 try {
722 $scripts = '';
723 if ( $context->shouldIncludeScripts() ) {
724 // If we are in debug mode, we'll want to return an array of URLs if possible
725 // However, we can't do this if the module doesn't support it
726 // We also can't do this if there is an only= parameter, because we have to give
727 // the module a way to return a load.php URL without causing an infinite loop
728 if ( $context->getDebug() && !$context->getOnly() && $module->supportsURLLoading() ) {
729 $scripts = $module->getScriptURLsForDebug( $context );
730 } else {
731 $scripts = $module->getScript( $context );
732 if ( is_string( $scripts ) && strlen( $scripts ) && substr( $scripts, -1 ) !== ';' ) {
733 // bug 27054: Append semicolon to prevent weird bugs
734 // caused by files not terminating their statements right
735 $scripts .= ";\n";
736 }
737 }
738 }
739 // Styles
740 $styles = array();
741 if ( $context->shouldIncludeStyles() ) {
742 // Don't create empty stylesheets like array( '' => '' ) for modules
743 // that don't *have* any stylesheets (bug 38024).
744 $stylePairs = $module->getStyles( $context );
745 if ( count ( $stylePairs ) ) {
746 // If we are in debug mode without &only= set, we'll want to return an array of URLs
747 // See comment near shouldIncludeScripts() for more details
748 if ( $context->getDebug() && !$context->getOnly() && $module->supportsURLLoading() ) {
749 $styles = array(
750 'url' => $module->getStyleURLsForDebug( $context )
751 );
752 } else {
753 // Minify CSS before embedding in mw.loader.implement call
754 // (unless in debug mode)
755 if ( !$context->getDebug() ) {
756 foreach ( $stylePairs as $media => $style ) {
757 // Can be either a string or an array of strings.
758 if ( is_array( $style ) ) {
759 $stylePairs[$media] = array();
760 foreach ( $style as $cssText ) {
761 if ( is_string( $cssText ) ) {
762 $stylePairs[$media][] = $this->filter( 'minify-css', $cssText );
763 }
764 }
765 } elseif ( is_string( $style ) ) {
766 $stylePairs[$media] = $this->filter( 'minify-css', $style );
767 }
768 }
769 }
770 // Wrap styles into @media groups as needed and flatten into a numerical array
771 $styles = array(
772 'css' => self::makeCombinedStyles( $stylePairs )
773 );
774 }
775 }
776 }
777
778 // Messages
779 $messagesBlob = isset( $blobs[$name] ) ? $blobs[$name] : '{}';
780
781 // Append output
782 switch ( $context->getOnly() ) {
783 case 'scripts':
784 if ( is_string( $scripts ) ) {
785 // Load scripts raw...
786 $out .= $scripts;
787 } elseif ( is_array( $scripts ) ) {
788 // ...except when $scripts is an array of URLs
789 $out .= self::makeLoaderImplementScript( $name, $scripts, array(), array() );
790 }
791 break;
792 case 'styles':
793 // We no longer seperate into media, they are all combined now with
794 // custom media type groups into @media .. {} sections as part of the css string.
795 // Module returns either an empty array or a numerical array with css strings.
796 $out .= isset( $styles['css'] ) ? implode( '', $styles['css'] ) : '';
797 break;
798 case 'messages':
799 $out .= self::makeMessageSetScript( new XmlJsCode( $messagesBlob ) );
800 break;
801 default:
802 $out .= self::makeLoaderImplementScript(
803 $name,
804 $scripts,
805 $styles,
806 new XmlJsCode( $messagesBlob )
807 );
808 break;
809 }
810 } catch ( Exception $e ) {
811 wfDebugLog( 'resourceloader', __METHOD__ . ": generating module package failed: $e" );
812 $this->hasErrors = true;
813 // Add exception to the output as a comment
814 $exceptions .= $this->makeComment( $e->__toString() );
815
816 // Register module as missing
817 $missing[] = $name;
818 unset( $modules[$name] );
819 }
820 $isRaw |= $module->isRaw();
821 wfProfileOut( __METHOD__ . '-' . $name );
822 }
823
824 // Update module states
825 if ( $context->shouldIncludeScripts() && !$context->getRaw() && !$isRaw ) {
826 // Set the state of modules loaded as only scripts to ready
827 if ( count( $modules ) && $context->getOnly() === 'scripts' ) {
828 $out .= self::makeLoaderStateScript(
829 array_fill_keys( array_keys( $modules ), 'ready' ) );
830 }
831 // Set the state of modules which were requested but unavailable as missing
832 if ( is_array( $missing ) && count( $missing ) ) {
833 $out .= self::makeLoaderStateScript( array_fill_keys( $missing, 'missing' ) );
834 }
835 }
836
837 if ( !$context->getDebug() ) {
838 if ( $context->getOnly() === 'styles' ) {
839 $out = $this->filter( 'minify-css', $out );
840 } else {
841 $out = $this->filter( 'minify-js', $out );
842 }
843 }
844
845 wfProfileOut( __METHOD__ );
846 return $exceptions . $out;
847 }
848
849 /* Static Methods */
850
851 /**
852 * Returns JS code to call to mw.loader.implement for a module with
853 * given properties.
854 *
855 * @param string $name Module name
856 * @param $scripts Mixed: List of URLs to JavaScript files or String of JavaScript code
857 * @param $styles Mixed: Array of CSS strings keyed by media type, or an array of lists of URLs to
858 * CSS files keyed by media type
859 * @param $messages Mixed: List of messages associated with this module. May either be an
860 * associative array mapping message key to value, or a JSON-encoded message blob containing
861 * the same data, wrapped in an XmlJsCode object.
862 *
863 * @throws MWException
864 * @return string
865 */
866 public static function makeLoaderImplementScript( $name, $scripts, $styles, $messages ) {
867 if ( is_string( $scripts ) ) {
868 $scripts = new XmlJsCode( "function () {\n{$scripts}\n}" );
869 } elseif ( !is_array( $scripts ) ) {
870 throw new MWException( 'Invalid scripts error. Array of URLs or string of code expected.' );
871 }
872 return Xml::encodeJsCall(
873 'mw.loader.implement',
874 array(
875 $name,
876 $scripts,
877 // Force objects. mw.loader.implement requires them to be javascript objects.
878 // Although these variables are associative arrays, which become javascript
879 // objects through json_encode. In many cases they will be empty arrays, and
880 // PHP/json_encode() consider empty arrays to be numerical arrays and
881 // output javascript "[]" instead of "{}". This fixes that.
882 (object)$styles,
883 (object)$messages
884 ),
885 ResourceLoader::inDebugMode()
886 );
887 }
888
889 /**
890 * Returns JS code which, when called, will register a given list of messages.
891 *
892 * @param $messages Mixed: Either an associative array mapping message key to value, or a
893 * JSON-encoded message blob containing the same data, wrapped in an XmlJsCode object.
894 *
895 * @return string
896 */
897 public static function makeMessageSetScript( $messages ) {
898 return Xml::encodeJsCall( 'mw.messages.set', array( (object)$messages ) );
899 }
900
901 /**
902 * Combines an associative array mapping media type to CSS into a
903 * single stylesheet with "@media" blocks.
904 *
905 * @param array $stylePairs Array keyed by media type containing (arrays of) CSS strings.
906 *
907 * @return Array
908 */
909 private static function makeCombinedStyles( array $stylePairs ) {
910 $out = array();
911 foreach ( $stylePairs as $media => $styles ) {
912 // ResourceLoaderFileModule::getStyle can return the styles
913 // as a string or an array of strings. This is to allow separation in
914 // the front-end.
915 $styles = (array)$styles;
916 foreach ( $styles as $style ) {
917 $style = trim( $style );
918 // Don't output an empty "@media print { }" block (bug 40498)
919 if ( $style !== '' ) {
920 // Transform the media type based on request params and config
921 // The way that this relies on $wgRequest to propagate request params is slightly evil
922 $media = OutputPage::transformCssMedia( $media );
923
924 if ( $media === '' || $media == 'all' ) {
925 $out[] = $style;
926 } elseif ( is_string( $media ) ) {
927 $out[] = "@media $media {\n" . str_replace( "\n", "\n\t", "\t" . $style ) . "}";
928 }
929 // else: skip
930 }
931 }
932 }
933 return $out;
934 }
935
936 /**
937 * Returns a JS call to mw.loader.state, which sets the state of a
938 * module or modules to a given value. Has two calling conventions:
939 *
940 * - ResourceLoader::makeLoaderStateScript( $name, $state ):
941 * Set the state of a single module called $name to $state
942 *
943 * - ResourceLoader::makeLoaderStateScript( array( $name => $state, ... ) ):
944 * Set the state of modules with the given names to the given states
945 *
946 * @param $name string
947 * @param $state
948 *
949 * @return string
950 */
951 public static function makeLoaderStateScript( $name, $state = null ) {
952 if ( is_array( $name ) ) {
953 return Xml::encodeJsCall( 'mw.loader.state', array( $name ) );
954 } else {
955 return Xml::encodeJsCall( 'mw.loader.state', array( $name, $state ) );
956 }
957 }
958
959 /**
960 * Returns JS code which calls the script given by $script. The script will
961 * be called with local variables name, version, dependencies and group,
962 * which will have values corresponding to $name, $version, $dependencies
963 * and $group as supplied.
964 *
965 * @param string $name Module name
966 * @param $version Integer: Module version number as a timestamp
967 * @param array $dependencies List of module names on which this module depends
968 * @param string $group Group which the module is in.
969 * @param string $source Source of the module, or 'local' if not foreign.
970 * @param string $script JavaScript code
971 *
972 * @return string
973 */
974 public static function makeCustomLoaderScript( $name, $version, $dependencies, $group, $source, $script ) {
975 $script = str_replace( "\n", "\n\t", trim( $script ) );
976 return Xml::encodeJsCall(
977 "( function ( name, version, dependencies, group, source ) {\n\t$script\n} )",
978 array( $name, $version, $dependencies, $group, $source ) );
979 }
980
981 /**
982 * Returns JS code which calls mw.loader.register with the given
983 * parameters. Has three calling conventions:
984 *
985 * - ResourceLoader::makeLoaderRegisterScript( $name, $version, $dependencies, $group, $source ):
986 * Register a single module.
987 *
988 * - ResourceLoader::makeLoaderRegisterScript( array( $name1, $name2 ) ):
989 * Register modules with the given names.
990 *
991 * - ResourceLoader::makeLoaderRegisterScript( array(
992 * array( $name1, $version1, $dependencies1, $group1, $source1 ),
993 * array( $name2, $version2, $dependencies1, $group2, $source2 ),
994 * ...
995 * ) ):
996 * Registers modules with the given names and parameters.
997 *
998 * @param string $name Module name
999 * @param $version Integer: Module version number as a timestamp
1000 * @param array $dependencies List of module names on which this module depends
1001 * @param string $group group which the module is in.
1002 * @param string $source source of the module, or 'local' if not foreign
1003 *
1004 * @return string
1005 */
1006 public static function makeLoaderRegisterScript( $name, $version = null,
1007 $dependencies = null, $group = null, $source = null
1008 ) {
1009 if ( is_array( $name ) ) {
1010 return Xml::encodeJsCall( 'mw.loader.register', array( $name ) );
1011 } else {
1012 $version = (int)$version > 1 ? (int)$version : 1;
1013 return Xml::encodeJsCall( 'mw.loader.register',
1014 array( $name, $version, $dependencies, $group, $source ) );
1015 }
1016 }
1017
1018 /**
1019 * Returns JS code which calls mw.loader.addSource() with the given
1020 * parameters. Has two calling conventions:
1021 *
1022 * - ResourceLoader::makeLoaderSourcesScript( $id, $properties ):
1023 * Register a single source
1024 *
1025 * - ResourceLoader::makeLoaderSourcesScript( array( $id1 => $props1, $id2 => $props2, ... ) );
1026 * Register sources with the given IDs and properties.
1027 *
1028 * @param string $id source ID
1029 * @param array $properties source properties (see addSource())
1030 *
1031 * @return string
1032 */
1033 public static function makeLoaderSourcesScript( $id, $properties = null ) {
1034 if ( is_array( $id ) ) {
1035 return Xml::encodeJsCall( 'mw.loader.addSource', array( $id ) );
1036 } else {
1037 return Xml::encodeJsCall( 'mw.loader.addSource', array( $id, $properties ) );
1038 }
1039 }
1040
1041 /**
1042 * Returns JS code which runs given JS code if the client-side framework is
1043 * present.
1044 *
1045 * @param string $script JavaScript code
1046 *
1047 * @return string
1048 */
1049 public static function makeLoaderConditionalScript( $script ) {
1050 return "if(window.mw){\n" . trim( $script ) . "\n}";
1051 }
1052
1053 /**
1054 * Returns JS code which will set the MediaWiki configuration array to
1055 * the given value.
1056 *
1057 * @param array $configuration List of configuration values keyed by variable name
1058 *
1059 * @return string
1060 */
1061 public static function makeConfigSetScript( array $configuration ) {
1062 return Xml::encodeJsCall( 'mw.config.set', array( $configuration ), ResourceLoader::inDebugMode() );
1063 }
1064
1065 /**
1066 * Convert an array of module names to a packed query string.
1067 *
1068 * For example, array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' )
1069 * becomes 'foo.bar,baz|bar.baz,quux'
1070 * @param array $modules of module names (strings)
1071 * @return string Packed query string
1072 */
1073 public static function makePackedModulesString( $modules ) {
1074 $groups = array(); // array( prefix => array( suffixes ) )
1075 foreach ( $modules as $module ) {
1076 $pos = strrpos( $module, '.' );
1077 $prefix = $pos === false ? '' : substr( $module, 0, $pos );
1078 $suffix = $pos === false ? $module : substr( $module, $pos + 1 );
1079 $groups[$prefix][] = $suffix;
1080 }
1081
1082 $arr = array();
1083 foreach ( $groups as $prefix => $suffixes ) {
1084 $p = $prefix === '' ? '' : $prefix . '.';
1085 $arr[] = $p . implode( ',', $suffixes );
1086 }
1087 $str = implode( '|', $arr );
1088 return $str;
1089 }
1090
1091 /**
1092 * Determine whether debug mode was requested
1093 * Order of priority is 1) request param, 2) cookie, 3) $wg setting
1094 * @return bool
1095 */
1096 public static function inDebugMode() {
1097 global $wgRequest, $wgResourceLoaderDebug;
1098 static $retval = null;
1099 if ( !is_null( $retval ) ) {
1100 return $retval;
1101 }
1102 return $retval = $wgRequest->getFuzzyBool( 'debug',
1103 $wgRequest->getCookie( 'resourceLoaderDebug', '', $wgResourceLoaderDebug ) );
1104 }
1105
1106 /**
1107 * Build a load.php URL
1108 * @param array $modules of module names (strings)
1109 * @param string $lang Language code
1110 * @param string $skin Skin name
1111 * @param string|null $user User name. If null, the &user= parameter is omitted
1112 * @param string|null $version Versioning timestamp
1113 * @param bool $debug Whether the request should be in debug mode
1114 * @param string|null $only &only= parameter
1115 * @param bool $printable Printable mode
1116 * @param bool $handheld Handheld mode
1117 * @param array $extraQuery Extra query parameters to add
1118 * @return string URL to load.php. May be protocol-relative (if $wgLoadScript is procol-relative)
1119 */
1120 public static function makeLoaderURL( $modules, $lang, $skin, $user = null, $version = null, $debug = false, $only = null,
1121 $printable = false, $handheld = false, $extraQuery = array() ) {
1122 global $wgLoadScript;
1123 $query = self::makeLoaderQuery( $modules, $lang, $skin, $user, $version, $debug,
1124 $only, $printable, $handheld, $extraQuery
1125 );
1126
1127 // Prevent the IE6 extension check from being triggered (bug 28840)
1128 // by appending a character that's invalid in Windows extensions ('*')
1129 return wfExpandUrl( wfAppendQuery( $wgLoadScript, $query ) . '&*', PROTO_RELATIVE );
1130 }
1131
1132 /**
1133 * Build a query array (array representation of query string) for load.php. Helper
1134 * function for makeLoaderURL().
1135 * @return array
1136 */
1137 public static function makeLoaderQuery( $modules, $lang, $skin, $user = null, $version = null, $debug = false, $only = null,
1138 $printable = false, $handheld = false, $extraQuery = array() ) {
1139 $query = array(
1140 'modules' => self::makePackedModulesString( $modules ),
1141 'lang' => $lang,
1142 'skin' => $skin,
1143 'debug' => $debug ? 'true' : 'false',
1144 );
1145 if ( $user !== null ) {
1146 $query['user'] = $user;
1147 }
1148 if ( $version !== null ) {
1149 $query['version'] = $version;
1150 }
1151 if ( $only !== null ) {
1152 $query['only'] = $only;
1153 }
1154 if ( $printable ) {
1155 $query['printable'] = 1;
1156 }
1157 if ( $handheld ) {
1158 $query['handheld'] = 1;
1159 }
1160 $query += $extraQuery;
1161
1162 // Make queries uniform in order
1163 ksort( $query );
1164 return $query;
1165 }
1166
1167 /**
1168 * Check a module name for validity.
1169 *
1170 * Module names may not contain pipes (|), commas (,) or exclamation marks (!) and can be
1171 * at most 255 bytes.
1172 *
1173 * @param string $moduleName Module name to check
1174 * @return bool Whether $moduleName is a valid module name
1175 */
1176 public static function isValidModuleName( $moduleName ) {
1177 return !preg_match( '/[|,!]/', $moduleName ) && strlen( $moduleName ) <= 255;
1178 }
1179 }