Merge "Default is not necessary for toggle fields"
[mediawiki.git] / includes / resourceloader / ResourceLoader.php
blobebcdab33c12f309dc6798cc9e569a264c398c9f6
1 <?php
2 /**
3 * Base class for resource loading system.
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.
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.
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
20 * @file
21 * @author Roan Kattouw
22 * @author Trevor Parscal
25 /**
26 * Dynamic JavaScript and CSS resource loading system.
28 * Most of the documention is on the MediaWiki documentation wiki starting at:
29 * http://www.mediawiki.org/wiki/ResourceLoader
31 class ResourceLoader {
33 /* Protected Static Members */
34 protected static $filterCacheVersion = 7;
35 protected static $requiredSourceProperties = array( 'loadScript' );
37 /** Array: List of module name/ResourceLoaderModule object pairs */
38 protected $modules = array();
40 /** Associative array mapping module name to info associative array */
41 protected $moduleInfos = array();
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();
47 /** array( 'source-id' => array( 'loadScript' => 'http://.../load.php' ) ) **/
48 protected $sources = array();
50 /** @var bool */
51 protected $hasErrors = false;
53 /* Protected Methods */
55 /**
56 * Loads information stored in the database about modules.
58 * This method grabs modules dependencies from the database and updates modules
59 * objects.
61 * This is not inside the module code because it is much faster to
62 * request all of the information at once than it is to have each module
63 * requests its own information. This sacrifice of modularity yields a substantial
64 * performance improvement.
66 * @param array $modules List of module names to preload information for
67 * @param $context ResourceLoaderContext: Context to load the information within
69 public function preloadModuleInfo( array $modules, ResourceLoaderContext $context ) {
70 if ( !count( $modules ) ) {
71 return; // or else Database*::select() will explode, plus it's cheaper!
73 $dbr = wfGetDB( DB_SLAVE );
74 $skin = $context->getSkin();
75 $lang = $context->getLanguage();
77 // Get file dependency information
78 $res = $dbr->select( 'module_deps', array( 'md_module', 'md_deps' ), array(
79 'md_module' => $modules,
80 'md_skin' => $skin
81 ), __METHOD__
84 // Set modules' dependencies
85 $modulesWithDeps = array();
86 foreach ( $res as $row ) {
87 $this->getModule( $row->md_module )->setFileDependencies( $skin,
88 FormatJson::decode( $row->md_deps, true )
90 $modulesWithDeps[] = $row->md_module;
93 // Register the absence of a dependency row too
94 foreach ( array_diff( $modules, $modulesWithDeps ) as $name ) {
95 $this->getModule( $name )->setFileDependencies( $skin, array() );
98 // Get message blob mtimes. Only do this for modules with messages
99 $modulesWithMessages = array();
100 foreach ( $modules as $name ) {
101 if ( count( $this->getModule( $name )->getMessages() ) ) {
102 $modulesWithMessages[] = $name;
105 $modulesWithoutMessages = array_flip( $modules ); // Will be trimmed down by the loop below
106 if ( count( $modulesWithMessages ) ) {
107 $res = $dbr->select( 'msg_resource', array( 'mr_resource', 'mr_timestamp' ), array(
108 'mr_resource' => $modulesWithMessages,
109 'mr_lang' => $lang
110 ), __METHOD__
112 foreach ( $res as $row ) {
113 $this->getModule( $row->mr_resource )->setMsgBlobMtime( $lang,
114 wfTimestamp( TS_UNIX, $row->mr_timestamp ) );
115 unset( $modulesWithoutMessages[$row->mr_resource] );
118 foreach ( array_keys( $modulesWithoutMessages ) as $name ) {
119 $this->getModule( $name )->setMsgBlobMtime( $lang, 0 );
124 * Runs JavaScript or CSS data through a filter, caching the filtered result for future calls.
126 * Available filters are:
127 * - minify-js \see JavaScriptMinifier::minify
128 * - minify-css \see CSSMin::minify
130 * If $data is empty, only contains whitespace or the filter was unknown,
131 * $data is returned unmodified.
133 * @param string $filter Name of filter to run
134 * @param string $data Text to filter, such as JavaScript or CSS text
135 * @return String: Filtered data, or a comment containing an error message
137 protected function filter( $filter, $data ) {
138 global $wgResourceLoaderMinifierStatementsOnOwnLine, $wgResourceLoaderMinifierMaxLineLength;
139 wfProfileIn( __METHOD__ );
141 // For empty/whitespace-only data or for unknown filters, don't perform
142 // any caching or processing
143 if ( trim( $data ) === ''
144 || !in_array( $filter, array( 'minify-js', 'minify-css' ) ) )
146 wfProfileOut( __METHOD__ );
147 return $data;
150 // Try for cache hit
151 // Use CACHE_ANYTHING since filtering is very slow compared to DB queries
152 $key = wfMemcKey( 'resourceloader', 'filter', $filter, self::$filterCacheVersion, md5( $data ) );
153 $cache = wfGetCache( CACHE_ANYTHING );
154 $cacheEntry = $cache->get( $key );
155 if ( is_string( $cacheEntry ) ) {
156 wfProfileOut( __METHOD__ );
157 return $cacheEntry;
160 $result = '';
161 // Run the filter - we've already verified one of these will work
162 try {
163 switch ( $filter ) {
164 case 'minify-js':
165 $result = JavaScriptMinifier::minify( $data,
166 $wgResourceLoaderMinifierStatementsOnOwnLine,
167 $wgResourceLoaderMinifierMaxLineLength
169 $result .= "\n/* cache key: $key */";
170 break;
171 case 'minify-css':
172 $result = CSSMin::minify( $data );
173 $result .= "\n/* cache key: $key */";
174 break;
177 // Save filtered text to Memcached
178 $cache->set( $key, $result );
179 } catch ( Exception $exception ) {
180 wfDebugLog( 'resourceloader', __METHOD__ . ": minification failed: $exception" );
181 $this->hasErrors = true;
182 // Return exception as a comment
183 $result = self::makeComment( $exception->__toString() );
186 wfProfileOut( __METHOD__ );
188 return $result;
191 /* Methods */
194 * Registers core modules and runs registration hooks.
196 public function __construct() {
197 global $IP, $wgResourceModules, $wgResourceLoaderSources, $wgLoadScript, $wgEnableJavaScriptTest;
199 wfProfileIn( __METHOD__ );
201 // Add 'local' source first
202 $this->addSource( 'local', array( 'loadScript' => $wgLoadScript, 'apiScript' => wfScript( 'api' ) ) );
204 // Add other sources
205 $this->addSource( $wgResourceLoaderSources );
207 // Register core modules
208 $this->register( include "$IP/resources/Resources.php" );
209 // Register extension modules
210 wfRunHooks( 'ResourceLoaderRegisterModules', array( &$this ) );
211 $this->register( $wgResourceModules );
213 if ( $wgEnableJavaScriptTest === true ) {
214 $this->registerTestModules();
217 wfProfileOut( __METHOD__ );
221 * Registers a module with the ResourceLoader system.
223 * @param $name Mixed: Name of module as a string or List of name/object pairs as an array
224 * @param array $info Module info array. For backwards compatibility with 1.17alpha,
225 * this may also be a ResourceLoaderModule object. Optional when using
226 * multiple-registration calling style.
227 * @throws MWException: If a duplicate module registration is attempted
228 * @throws MWException: If a module name contains illegal characters (pipes or commas)
229 * @throws MWException: If something other than a ResourceLoaderModule is being registered
230 * @return Boolean: False if there were any errors, in which case one or more modules were not
231 * registered
233 public function register( $name, $info = null ) {
234 wfProfileIn( __METHOD__ );
236 // Allow multiple modules to be registered in one call
237 $registrations = is_array( $name ) ? $name : array( $name => $info );
238 foreach ( $registrations as $name => $info ) {
239 // Disallow duplicate registrations
240 if ( isset( $this->moduleInfos[$name] ) ) {
241 wfProfileOut( __METHOD__ );
242 // A module has already been registered by this name
243 throw new MWException(
244 'ResourceLoader duplicate registration error. ' .
245 'Another module has already been registered as ' . $name
249 // Check $name for validity
250 if ( !self::isValidModuleName( $name ) ) {
251 wfProfileOut( __METHOD__ );
252 throw new MWException( "ResourceLoader module name '$name' is invalid, see ResourceLoader::isValidModuleName()" );
255 // Attach module
256 if ( is_object( $info ) ) {
257 // Old calling convention
258 // Validate the input
259 if ( !( $info instanceof ResourceLoaderModule ) ) {
260 wfProfileOut( __METHOD__ );
261 throw new MWException( 'ResourceLoader invalid module error. ' .
262 'Instances of ResourceLoaderModule expected.' );
265 $this->moduleInfos[$name] = array( 'object' => $info );
266 $info->setName( $name );
267 $this->modules[$name] = $info;
268 } else {
269 // New calling convention
270 $this->moduleInfos[$name] = $info;
274 wfProfileOut( __METHOD__ );
279 public function registerTestModules() {
280 global $IP, $wgEnableJavaScriptTest;
282 if ( $wgEnableJavaScriptTest !== true ) {
283 throw new MWException( 'Attempt to register JavaScript test modules but <code>$wgEnableJavaScriptTest</code> is false. Edit your <code>LocalSettings.php</code> to enable it.' );
286 wfProfileIn( __METHOD__ );
288 // Get core test suites
289 $testModules = array();
290 $testModules['qunit'] = include "$IP/tests/qunit/QUnitTestResources.php";
291 // Get other test suites (e.g. from extensions)
292 wfRunHooks( 'ResourceLoaderTestModules', array( &$testModules, &$this ) );
294 // Add the testrunner (which configures QUnit) to the dependencies.
295 // Since it must be ready before any of the test suites are executed.
296 foreach ( $testModules['qunit'] as &$module ) {
297 // Make sure all test modules are top-loading so that when QUnit starts
298 // on document-ready, it will run once and finish. If some tests arrive
299 // later (possibly after QUnit has already finished) they will be ignored.
300 $module['position'] = 'top';
301 $module['dependencies'][] = 'mediawiki.tests.qunit.testrunner';
304 foreach ( $testModules as $id => $names ) {
305 // Register test modules
306 $this->register( $testModules[$id] );
308 // Keep track of their names so that they can be loaded together
309 $this->testModuleNames[$id] = array_keys( $testModules[$id] );
312 wfProfileOut( __METHOD__ );
316 * Add a foreign source of modules.
318 * Source properties:
319 * 'loadScript': URL (either fully-qualified or protocol-relative) of load.php for this source
321 * @param $id Mixed: source ID (string), or array( id1 => props1, id2 => props2, ... )
322 * @param array $properties source properties
323 * @throws MWException
325 public function addSource( $id, $properties = null ) {
326 // Allow multiple sources to be registered in one call
327 if ( is_array( $id ) ) {
328 foreach ( $id as $key => $value ) {
329 $this->addSource( $key, $value );
331 return;
334 // Disallow duplicates
335 if ( isset( $this->sources[$id] ) ) {
336 throw new MWException(
337 'ResourceLoader duplicate source addition error. ' .
338 'Another source has already been registered as ' . $id
342 // Validate properties
343 foreach ( self::$requiredSourceProperties as $prop ) {
344 if ( !isset( $properties[$prop] ) ) {
345 throw new MWException( "Required property $prop missing from source ID $id" );
349 $this->sources[$id] = $properties;
353 * Get a list of module names
355 * @return Array: List of module names
357 public function getModuleNames() {
358 return array_keys( $this->moduleInfos );
362 * Get a list of test module names for one (or all) frameworks.
363 * If the given framework id is unknkown, or if the in-object variable is not an array,
364 * then it will return an empty array.
366 * @param string $framework Optional. Get only the test module names for one
367 * particular framework.
368 * @return Array
370 public function getTestModuleNames( $framework = 'all' ) {
371 /// @todo api siteinfo prop testmodulenames modulenames
372 if ( $framework == 'all' ) {
373 return $this->testModuleNames;
374 } elseif ( isset( $this->testModuleNames[$framework] ) && is_array( $this->testModuleNames[$framework] ) ) {
375 return $this->testModuleNames[$framework];
376 } else {
377 return array();
382 * Get the ResourceLoaderModule object for a given module name.
384 * @param string $name Module name
385 * @return ResourceLoaderModule if module has been registered, null otherwise
387 public function getModule( $name ) {
388 if ( !isset( $this->modules[$name] ) ) {
389 if ( !isset( $this->moduleInfos[$name] ) ) {
390 // No such module
391 return null;
393 // Construct the requested object
394 $info = $this->moduleInfos[$name];
395 /** @var ResourceLoaderModule $object */
396 if ( isset( $info['object'] ) ) {
397 // Object given in info array
398 $object = $info['object'];
399 } else {
400 if ( !isset( $info['class'] ) ) {
401 $class = 'ResourceLoaderFileModule';
402 } else {
403 $class = $info['class'];
405 $object = new $class( $info );
407 $object->setName( $name );
408 $this->modules[$name] = $object;
411 return $this->modules[$name];
415 * Get the list of sources
417 * @return Array: array( id => array of properties, .. )
419 public function getSources() {
420 return $this->sources;
424 * Outputs a response to a resource load-request, including a content-type header.
426 * @param $context ResourceLoaderContext: Context in which a response should be formed
428 public function respond( ResourceLoaderContext $context ) {
429 global $wgCacheEpoch, $wgUseFileCache;
431 // Use file cache if enabled and available...
432 if ( $wgUseFileCache ) {
433 $fileCache = ResourceFileCache::newFromContext( $context );
434 if ( $this->tryRespondFromFileCache( $fileCache, $context ) ) {
435 return; // output handled
439 // Buffer output to catch warnings. Normally we'd use ob_clean() on the
440 // top-level output buffer to clear warnings, but that breaks when ob_gzhandler
441 // is used: ob_clean() will clear the GZIP header in that case and it won't come
442 // back for subsequent output, resulting in invalid GZIP. So we have to wrap
443 // the whole thing in our own output buffer to be sure the active buffer
444 // doesn't use ob_gzhandler.
445 // See http://bugs.php.net/bug.php?id=36514
446 ob_start();
448 wfProfileIn( __METHOD__ );
449 $errors = '';
451 // Split requested modules into two groups, modules and missing
452 $modules = array();
453 $missing = array();
454 foreach ( $context->getModules() as $name ) {
455 if ( isset( $this->moduleInfos[$name] ) ) {
456 $module = $this->getModule( $name );
457 // Do not allow private modules to be loaded from the web.
458 // This is a security issue, see bug 34907.
459 if ( $module->getGroup() === 'private' ) {
460 wfDebugLog( 'resourceloader', __METHOD__ . ": request for private module '$name' denied" );
461 $this->hasErrors = true;
462 // Add exception to the output as a comment
463 $errors .= self::makeComment( "Cannot show private module \"$name\"" );
465 continue;
467 $modules[$name] = $module;
468 } else {
469 $missing[] = $name;
473 // Preload information needed to the mtime calculation below
474 try {
475 $this->preloadModuleInfo( array_keys( $modules ), $context );
476 } catch ( Exception $e ) {
477 wfDebugLog( 'resourceloader', __METHOD__ . ": preloading module info failed: $e" );
478 $this->hasErrors = true;
479 // Add exception to the output as a comment
480 $errors .= self::makeComment( $e->__toString() );
483 wfProfileIn( __METHOD__ . '-getModifiedTime' );
485 // To send Last-Modified and support If-Modified-Since, we need to detect
486 // the last modified time
487 $mtime = wfTimestamp( TS_UNIX, $wgCacheEpoch );
488 foreach ( $modules as $module ) {
490 * @var $module ResourceLoaderModule
492 try {
493 // Calculate maximum modified time
494 $mtime = max( $mtime, $module->getModifiedTime( $context ) );
495 } catch ( Exception $e ) {
496 wfDebugLog( 'resourceloader', __METHOD__ . ": calculating maximum modified time failed: $e" );
497 $this->hasErrors = true;
498 // Add exception to the output as a comment
499 $errors .= self::makeComment( $e->__toString() );
503 wfProfileOut( __METHOD__ . '-getModifiedTime' );
505 // If there's an If-Modified-Since header, respond with a 304 appropriately
506 if ( $this->tryRespondLastModified( $context, $mtime ) ) {
507 wfProfileOut( __METHOD__ );
508 return; // output handled (buffers cleared)
511 // Generate a response
512 $response = $this->makeModuleResponse( $context, $modules, $missing );
514 // Prepend comments indicating exceptions
515 $response = $errors . $response;
517 // Capture any PHP warnings from the output buffer and append them to the
518 // response in a comment if we're in debug mode.
519 if ( $context->getDebug() && strlen( $warnings = ob_get_contents() ) ) {
520 $response = self::makeComment( $warnings ) . $response;
521 $this->hasErrors = true;
524 // Save response to file cache unless there are errors
525 if ( isset( $fileCache ) && !$errors && !$missing ) {
526 // Cache single modules...and other requests if there are enough hits
527 if ( ResourceFileCache::useFileCache( $context ) ) {
528 if ( $fileCache->isCacheWorthy() ) {
529 $fileCache->saveText( $response );
530 } else {
531 $fileCache->incrMissesRecent( $context->getRequest() );
536 // Send content type and cache related headers
537 $this->sendResponseHeaders( $context, $mtime, $this->hasErrors );
539 // Remove the output buffer and output the response
540 ob_end_clean();
541 echo $response;
543 wfProfileOut( __METHOD__ );
547 * Send content type and last modified headers to the client.
548 * @param $context ResourceLoaderContext
549 * @param string $mtime TS_MW timestamp to use for last-modified
550 * @param bool $errors Whether there are commented-out errors in the response
551 * @return void
553 protected function sendResponseHeaders( ResourceLoaderContext $context, $mtime, $errors ) {
554 global $wgResourceLoaderMaxage;
555 // If a version wasn't specified we need a shorter expiry time for updates
556 // to propagate to clients quickly
557 // If there were errors, we also need a shorter expiry time so we can recover quickly
558 if ( is_null( $context->getVersion() ) || $errors ) {
559 $maxage = $wgResourceLoaderMaxage['unversioned']['client'];
560 $smaxage = $wgResourceLoaderMaxage['unversioned']['server'];
561 // If a version was specified we can use a longer expiry time since changing
562 // version numbers causes cache misses
563 } else {
564 $maxage = $wgResourceLoaderMaxage['versioned']['client'];
565 $smaxage = $wgResourceLoaderMaxage['versioned']['server'];
567 if ( $context->getOnly() === 'styles' ) {
568 header( 'Content-Type: text/css; charset=utf-8' );
569 header( 'Access-Control-Allow-Origin: *' );
570 } else {
571 header( 'Content-Type: text/javascript; charset=utf-8' );
573 header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $mtime ) );
574 if ( $context->getDebug() ) {
575 // Do not cache debug responses
576 header( 'Cache-Control: private, no-cache, must-revalidate' );
577 header( 'Pragma: no-cache' );
578 } else {
579 header( "Cache-Control: public, max-age=$maxage, s-maxage=$smaxage" );
580 $exp = min( $maxage, $smaxage );
581 header( 'Expires: ' . wfTimestamp( TS_RFC2822, $exp + time() ) );
586 * If there's an If-Modified-Since header, respond with a 304 appropriately
587 * and clear out the output buffer. If the client cache is too old then do nothing.
588 * @param $context ResourceLoaderContext
589 * @param string $mtime The TS_MW timestamp to check the header against
590 * @return bool True iff 304 header sent and output handled
592 protected function tryRespondLastModified( ResourceLoaderContext $context, $mtime ) {
593 // If there's an If-Modified-Since header, respond with a 304 appropriately
594 // Some clients send "timestamp;length=123". Strip the part after the first ';'
595 // so we get a valid timestamp.
596 $ims = $context->getRequest()->getHeader( 'If-Modified-Since' );
597 // Never send 304s in debug mode
598 if ( $ims !== false && !$context->getDebug() ) {
599 $imsTS = strtok( $ims, ';' );
600 if ( $mtime <= wfTimestamp( TS_UNIX, $imsTS ) ) {
601 // There's another bug in ob_gzhandler (see also the comment at
602 // the top of this function) that causes it to gzip even empty
603 // responses, meaning it's impossible to produce a truly empty
604 // response (because the gzip header is always there). This is
605 // a problem because 304 responses have to be completely empty
606 // per the HTTP spec, and Firefox behaves buggily when they're not.
607 // See also http://bugs.php.net/bug.php?id=51579
608 // To work around this, we tear down all output buffering before
609 // sending the 304.
610 // On some setups, ob_get_level() doesn't seem to go down to zero
611 // no matter how often we call ob_get_clean(), so instead of doing
612 // the more intuitive while ( ob_get_level() > 0 ) ob_get_clean();
613 // we have to be safe here and avoid an infinite loop.
614 // Caching the level is not an option, need to allow it to
615 // shorten the loop on-the-fly (bug 46836)
616 for ( $i = 0; $i < ob_get_level(); $i++ ) {
617 ob_end_clean();
620 header( 'HTTP/1.0 304 Not Modified' );
621 header( 'Status: 304 Not Modified' );
622 return true;
625 return false;
629 * Send out code for a response from file cache if possible
631 * @param $fileCache ResourceFileCache: Cache object for this request URL
632 * @param $context ResourceLoaderContext: Context in which to generate a response
633 * @return bool If this found a cache file and handled the response
635 protected function tryRespondFromFileCache(
636 ResourceFileCache $fileCache, ResourceLoaderContext $context
638 global $wgResourceLoaderMaxage;
639 // Buffer output to catch warnings.
640 ob_start();
641 // Get the maximum age the cache can be
642 $maxage = is_null( $context->getVersion() )
643 ? $wgResourceLoaderMaxage['unversioned']['server']
644 : $wgResourceLoaderMaxage['versioned']['server'];
645 // Minimum timestamp the cache file must have
646 $good = $fileCache->isCacheGood( wfTimestamp( TS_MW, time() - $maxage ) );
647 if ( !$good ) {
648 try { // RL always hits the DB on file cache miss...
649 wfGetDB( DB_SLAVE );
650 } catch ( DBConnectionError $e ) { // ...check if we need to fallback to cache
651 $good = $fileCache->isCacheGood(); // cache existence check
654 if ( $good ) {
655 $ts = $fileCache->cacheTimestamp();
656 // Send content type and cache headers
657 $this->sendResponseHeaders( $context, $ts, false );
658 // If there's an If-Modified-Since header, respond with a 304 appropriately
659 if ( $this->tryRespondLastModified( $context, $ts ) ) {
660 return false; // output handled (buffers cleared)
662 $response = $fileCache->fetchText();
663 // Capture any PHP warnings from the output buffer and append them to the
664 // response in a comment if we're in debug mode.
665 if ( $context->getDebug() && strlen( $warnings = ob_get_contents() ) ) {
666 $response = "/*\n$warnings\n*/\n" . $response;
668 // Remove the output buffer and output the response
669 ob_end_clean();
670 echo $response . "\n/* Cached {$ts} */";
671 return true; // cache hit
673 // Clear buffer
674 ob_end_clean();
676 return false; // cache miss
680 * Generate a CSS or JS comment block
682 * @param $text string
683 * @return string
685 public static function makeComment( $text ) {
686 $encText = str_replace( '*/', '* /', $text );
687 return "/*\n$encText\n*/\n";
691 * Generates code for a response
693 * @param $context ResourceLoaderContext: Context in which to generate a response
694 * @param array $modules List of module objects keyed by module name
695 * @param array $missing List of unavailable modules (optional)
696 * @return String: Response data
698 public function makeModuleResponse( ResourceLoaderContext $context,
699 array $modules, $missing = array()
701 $out = '';
702 $exceptions = '';
703 if ( $modules === array() && $missing === array() ) {
704 return '/* No modules requested. Max made me put this here */';
707 wfProfileIn( __METHOD__ );
708 // Pre-fetch blobs
709 if ( $context->shouldIncludeMessages() ) {
710 try {
711 $blobs = MessageBlobStore::get( $this, $modules, $context->getLanguage() );
712 } catch ( Exception $e ) {
713 wfDebugLog( 'resourceloader', __METHOD__ . ": pre-fetching blobs from MessageBlobStore failed: $e" );
714 $this->hasErrors = true;
715 // Add exception to the output as a comment
716 $exceptions .= self::makeComment( $e->__toString() );
718 } else {
719 $blobs = array();
722 // Generate output
723 $isRaw = false;
724 foreach ( $modules as $name => $module ) {
726 * @var $module ResourceLoaderModule
729 wfProfileIn( __METHOD__ . '-' . $name );
730 try {
731 $scripts = '';
732 if ( $context->shouldIncludeScripts() ) {
733 // If we are in debug mode, we'll want to return an array of URLs if possible
734 // However, we can't do this if the module doesn't support it
735 // We also can't do this if there is an only= parameter, because we have to give
736 // the module a way to return a load.php URL without causing an infinite loop
737 if ( $context->getDebug() && !$context->getOnly() && $module->supportsURLLoading() ) {
738 $scripts = $module->getScriptURLsForDebug( $context );
739 } else {
740 $scripts = $module->getScript( $context );
741 if ( is_string( $scripts ) && strlen( $scripts ) && substr( $scripts, -1 ) !== ';' ) {
742 // bug 27054: Append semicolon to prevent weird bugs
743 // caused by files not terminating their statements right
744 $scripts .= ";\n";
748 // Styles
749 $styles = array();
750 if ( $context->shouldIncludeStyles() ) {
751 // Don't create empty stylesheets like array( '' => '' ) for modules
752 // that don't *have* any stylesheets (bug 38024).
753 $stylePairs = $module->getStyles( $context );
754 if ( count ( $stylePairs ) ) {
755 // If we are in debug mode without &only= set, we'll want to return an array of URLs
756 // See comment near shouldIncludeScripts() for more details
757 if ( $context->getDebug() && !$context->getOnly() && $module->supportsURLLoading() ) {
758 $styles = array(
759 'url' => $module->getStyleURLsForDebug( $context )
761 } else {
762 // Minify CSS before embedding in mw.loader.implement call
763 // (unless in debug mode)
764 if ( !$context->getDebug() ) {
765 foreach ( $stylePairs as $media => $style ) {
766 // Can be either a string or an array of strings.
767 if ( is_array( $style ) ) {
768 $stylePairs[$media] = array();
769 foreach ( $style as $cssText ) {
770 if ( is_string( $cssText ) ) {
771 $stylePairs[$media][] = $this->filter( 'minify-css', $cssText );
774 } elseif ( is_string( $style ) ) {
775 $stylePairs[$media] = $this->filter( 'minify-css', $style );
779 // Wrap styles into @media groups as needed and flatten into a numerical array
780 $styles = array(
781 'css' => self::makeCombinedStyles( $stylePairs )
787 // Messages
788 $messagesBlob = isset( $blobs[$name] ) ? $blobs[$name] : '{}';
790 // Append output
791 switch ( $context->getOnly() ) {
792 case 'scripts':
793 if ( is_string( $scripts ) ) {
794 // Load scripts raw...
795 $out .= $scripts;
796 } elseif ( is_array( $scripts ) ) {
797 // ...except when $scripts is an array of URLs
798 $out .= self::makeLoaderImplementScript( $name, $scripts, array(), array() );
800 break;
801 case 'styles':
802 // We no longer seperate into media, they are all combined now with
803 // custom media type groups into @media .. {} sections as part of the css string.
804 // Module returns either an empty array or a numerical array with css strings.
805 $out .= isset( $styles['css'] ) ? implode( '', $styles['css'] ) : '';
806 break;
807 case 'messages':
808 $out .= self::makeMessageSetScript( new XmlJsCode( $messagesBlob ) );
809 break;
810 default:
811 $out .= self::makeLoaderImplementScript(
812 $name,
813 $scripts,
814 $styles,
815 new XmlJsCode( $messagesBlob )
817 break;
819 } catch ( Exception $e ) {
820 wfDebugLog( 'resourceloader', __METHOD__ . ": generating module package failed: $e" );
821 $this->hasErrors = true;
822 // Add exception to the output as a comment
823 $exceptions .= self::makeComment( $e->__toString() );
825 // Register module as missing
826 $missing[] = $name;
827 unset( $modules[$name] );
829 $isRaw |= $module->isRaw();
830 wfProfileOut( __METHOD__ . '-' . $name );
833 // Update module states
834 if ( $context->shouldIncludeScripts() && !$context->getRaw() && !$isRaw ) {
835 // Set the state of modules loaded as only scripts to ready
836 if ( count( $modules ) && $context->getOnly() === 'scripts' ) {
837 $out .= self::makeLoaderStateScript(
838 array_fill_keys( array_keys( $modules ), 'ready' ) );
840 // Set the state of modules which were requested but unavailable as missing
841 if ( is_array( $missing ) && count( $missing ) ) {
842 $out .= self::makeLoaderStateScript( array_fill_keys( $missing, 'missing' ) );
846 if ( !$context->getDebug() ) {
847 if ( $context->getOnly() === 'styles' ) {
848 $out = $this->filter( 'minify-css', $out );
849 } else {
850 $out = $this->filter( 'minify-js', $out );
854 wfProfileOut( __METHOD__ );
855 return $exceptions . $out;
858 /* Static Methods */
861 * Returns JS code to call to mw.loader.implement for a module with
862 * given properties.
864 * @param string $name Module name
865 * @param $scripts Mixed: List of URLs to JavaScript files or String of JavaScript code
866 * @param $styles Mixed: Array of CSS strings keyed by media type, or an array of lists of URLs to
867 * CSS files keyed by media type
868 * @param $messages Mixed: List of messages associated with this module. May either be an
869 * associative array mapping message key to value, or a JSON-encoded message blob containing
870 * the same data, wrapped in an XmlJsCode object.
872 * @throws MWException
873 * @return string
875 public static function makeLoaderImplementScript( $name, $scripts, $styles, $messages ) {
876 if ( is_string( $scripts ) ) {
877 $scripts = new XmlJsCode( "function () {\n{$scripts}\n}" );
878 } elseif ( !is_array( $scripts ) ) {
879 throw new MWException( 'Invalid scripts error. Array of URLs or string of code expected.' );
881 return Xml::encodeJsCall(
882 'mw.loader.implement',
883 array(
884 $name,
885 $scripts,
886 // Force objects. mw.loader.implement requires them to be javascript objects.
887 // Although these variables are associative arrays, which become javascript
888 // objects through json_encode. In many cases they will be empty arrays, and
889 // PHP/json_encode() consider empty arrays to be numerical arrays and
890 // output javascript "[]" instead of "{}". This fixes that.
891 (object)$styles,
892 (object)$messages
894 ResourceLoader::inDebugMode()
899 * Returns JS code which, when called, will register a given list of messages.
901 * @param $messages Mixed: Either an associative array mapping message key to value, or a
902 * JSON-encoded message blob containing the same data, wrapped in an XmlJsCode object.
904 * @return string
906 public static function makeMessageSetScript( $messages ) {
907 return Xml::encodeJsCall( 'mw.messages.set', array( (object)$messages ) );
911 * Combines an associative array mapping media type to CSS into a
912 * single stylesheet with "@media" blocks.
914 * @param array $stylePairs Array keyed by media type containing (arrays of) CSS strings.
916 * @return Array
918 private static function makeCombinedStyles( array $stylePairs ) {
919 $out = array();
920 foreach ( $stylePairs as $media => $styles ) {
921 // ResourceLoaderFileModule::getStyle can return the styles
922 // as a string or an array of strings. This is to allow separation in
923 // the front-end.
924 $styles = (array)$styles;
925 foreach ( $styles as $style ) {
926 $style = trim( $style );
927 // Don't output an empty "@media print { }" block (bug 40498)
928 if ( $style !== '' ) {
929 // Transform the media type based on request params and config
930 // The way that this relies on $wgRequest to propagate request params is slightly evil
931 $media = OutputPage::transformCssMedia( $media );
933 if ( $media === '' || $media == 'all' ) {
934 $out[] = $style;
935 } elseif ( is_string( $media ) ) {
936 $out[] = "@media $media {\n" . str_replace( "\n", "\n\t", "\t" . $style ) . "}";
938 // else: skip
942 return $out;
946 * Returns a JS call to mw.loader.state, which sets the state of a
947 * module or modules to a given value. Has two calling conventions:
949 * - ResourceLoader::makeLoaderStateScript( $name, $state ):
950 * Set the state of a single module called $name to $state
952 * - ResourceLoader::makeLoaderStateScript( array( $name => $state, ... ) ):
953 * Set the state of modules with the given names to the given states
955 * @param $name string
956 * @param $state
958 * @return string
960 public static function makeLoaderStateScript( $name, $state = null ) {
961 if ( is_array( $name ) ) {
962 return Xml::encodeJsCall( 'mw.loader.state', array( $name ) );
963 } else {
964 return Xml::encodeJsCall( 'mw.loader.state', array( $name, $state ) );
969 * Returns JS code which calls the script given by $script. The script will
970 * be called with local variables name, version, dependencies and group,
971 * which will have values corresponding to $name, $version, $dependencies
972 * and $group as supplied.
974 * @param string $name Module name
975 * @param $version Integer: Module version number as a timestamp
976 * @param array $dependencies List of module names on which this module depends
977 * @param string $group Group which the module is in.
978 * @param string $source Source of the module, or 'local' if not foreign.
979 * @param string $script JavaScript code
981 * @return string
983 public static function makeCustomLoaderScript( $name, $version, $dependencies, $group, $source, $script ) {
984 $script = str_replace( "\n", "\n\t", trim( $script ) );
985 return Xml::encodeJsCall(
986 "( function ( name, version, dependencies, group, source ) {\n\t$script\n} )",
987 array( $name, $version, $dependencies, $group, $source ) );
991 * Returns JS code which calls mw.loader.register with the given
992 * parameters. Has three calling conventions:
994 * - ResourceLoader::makeLoaderRegisterScript( $name, $version, $dependencies, $group, $source ):
995 * Register a single module.
997 * - ResourceLoader::makeLoaderRegisterScript( array( $name1, $name2 ) ):
998 * Register modules with the given names.
1000 * - ResourceLoader::makeLoaderRegisterScript( array(
1001 * array( $name1, $version1, $dependencies1, $group1, $source1 ),
1002 * array( $name2, $version2, $dependencies1, $group2, $source2 ),
1003 * ...
1004 * ) ):
1005 * Registers modules with the given names and parameters.
1007 * @param string $name Module name
1008 * @param $version Integer: Module version number as a timestamp
1009 * @param array $dependencies List of module names on which this module depends
1010 * @param string $group group which the module is in.
1011 * @param string $source source of the module, or 'local' if not foreign
1013 * @return string
1015 public static function makeLoaderRegisterScript( $name, $version = null,
1016 $dependencies = null, $group = null, $source = null
1018 if ( is_array( $name ) ) {
1019 return Xml::encodeJsCall( 'mw.loader.register', array( $name ) );
1020 } else {
1021 $version = (int)$version > 1 ? (int)$version : 1;
1022 return Xml::encodeJsCall( 'mw.loader.register',
1023 array( $name, $version, $dependencies, $group, $source ) );
1028 * Returns JS code which calls mw.loader.addSource() with the given
1029 * parameters. Has two calling conventions:
1031 * - ResourceLoader::makeLoaderSourcesScript( $id, $properties ):
1032 * Register a single source
1034 * - ResourceLoader::makeLoaderSourcesScript( array( $id1 => $props1, $id2 => $props2, ... ) );
1035 * Register sources with the given IDs and properties.
1037 * @param string $id source ID
1038 * @param array $properties source properties (see addSource())
1040 * @return string
1042 public static function makeLoaderSourcesScript( $id, $properties = null ) {
1043 if ( is_array( $id ) ) {
1044 return Xml::encodeJsCall( 'mw.loader.addSource', array( $id ) );
1045 } else {
1046 return Xml::encodeJsCall( 'mw.loader.addSource', array( $id, $properties ) );
1051 * Returns JS code which runs given JS code if the client-side framework is
1052 * present.
1054 * @param string $script JavaScript code
1056 * @return string
1058 public static function makeLoaderConditionalScript( $script ) {
1059 return "if(window.mw){\n" . trim( $script ) . "\n}";
1063 * Returns JS code which will set the MediaWiki configuration array to
1064 * the given value.
1066 * @param array $configuration List of configuration values keyed by variable name
1068 * @return string
1070 public static function makeConfigSetScript( array $configuration ) {
1071 return Xml::encodeJsCall( 'mw.config.set', array( $configuration ), ResourceLoader::inDebugMode() );
1075 * Convert an array of module names to a packed query string.
1077 * For example, array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' )
1078 * becomes 'foo.bar,baz|bar.baz,quux'
1079 * @param array $modules of module names (strings)
1080 * @return string Packed query string
1082 public static function makePackedModulesString( $modules ) {
1083 $groups = array(); // array( prefix => array( suffixes ) )
1084 foreach ( $modules as $module ) {
1085 $pos = strrpos( $module, '.' );
1086 $prefix = $pos === false ? '' : substr( $module, 0, $pos );
1087 $suffix = $pos === false ? $module : substr( $module, $pos + 1 );
1088 $groups[$prefix][] = $suffix;
1091 $arr = array();
1092 foreach ( $groups as $prefix => $suffixes ) {
1093 $p = $prefix === '' ? '' : $prefix . '.';
1094 $arr[] = $p . implode( ',', $suffixes );
1096 $str = implode( '|', $arr );
1097 return $str;
1101 * Determine whether debug mode was requested
1102 * Order of priority is 1) request param, 2) cookie, 3) $wg setting
1103 * @return bool
1105 public static function inDebugMode() {
1106 global $wgRequest, $wgResourceLoaderDebug;
1107 static $retval = null;
1108 if ( !is_null( $retval ) ) {
1109 return $retval;
1111 return $retval = $wgRequest->getFuzzyBool( 'debug',
1112 $wgRequest->getCookie( 'resourceLoaderDebug', '', $wgResourceLoaderDebug ) );
1116 * Build a load.php URL
1117 * @param array $modules of module names (strings)
1118 * @param string $lang Language code
1119 * @param string $skin Skin name
1120 * @param string|null $user User name. If null, the &user= parameter is omitted
1121 * @param string|null $version Versioning timestamp
1122 * @param bool $debug Whether the request should be in debug mode
1123 * @param string|null $only &only= parameter
1124 * @param bool $printable Printable mode
1125 * @param bool $handheld Handheld mode
1126 * @param array $extraQuery Extra query parameters to add
1127 * @return string URL to load.php. May be protocol-relative (if $wgLoadScript is procol-relative)
1129 public static function makeLoaderURL( $modules, $lang, $skin, $user = null, $version = null, $debug = false, $only = null,
1130 $printable = false, $handheld = false, $extraQuery = array() ) {
1131 global $wgLoadScript;
1132 $query = self::makeLoaderQuery( $modules, $lang, $skin, $user, $version, $debug,
1133 $only, $printable, $handheld, $extraQuery
1136 // Prevent the IE6 extension check from being triggered (bug 28840)
1137 // by appending a character that's invalid in Windows extensions ('*')
1138 return wfExpandUrl( wfAppendQuery( $wgLoadScript, $query ) . '&*', PROTO_RELATIVE );
1142 * Build a query array (array representation of query string) for load.php. Helper
1143 * function for makeLoaderURL().
1145 * @param array $modules
1146 * @param string $lang
1147 * @param string $skin
1148 * @param string $user
1149 * @param string $version
1150 * @param bool $debug
1151 * @param string $only
1152 * @param bool $printable
1153 * @param bool $handheld
1154 * @param array $extraQuery
1156 * @return array
1158 public static function makeLoaderQuery( $modules, $lang, $skin, $user = null, $version = null, $debug = false, $only = null,
1159 $printable = false, $handheld = false, $extraQuery = array() ) {
1160 $query = array(
1161 'modules' => self::makePackedModulesString( $modules ),
1162 'lang' => $lang,
1163 'skin' => $skin,
1164 'debug' => $debug ? 'true' : 'false',
1166 if ( $user !== null ) {
1167 $query['user'] = $user;
1169 if ( $version !== null ) {
1170 $query['version'] = $version;
1172 if ( $only !== null ) {
1173 $query['only'] = $only;
1175 if ( $printable ) {
1176 $query['printable'] = 1;
1178 if ( $handheld ) {
1179 $query['handheld'] = 1;
1181 $query += $extraQuery;
1183 // Make queries uniform in order
1184 ksort( $query );
1185 return $query;
1189 * Check a module name for validity.
1191 * Module names may not contain pipes (|), commas (,) or exclamation marks (!) and can be
1192 * at most 255 bytes.
1194 * @param string $moduleName Module name to check
1195 * @return bool Whether $moduleName is a valid module name
1197 public static function isValidModuleName( $moduleName ) {
1198 return !preg_match( '/[|,!]/', $moduleName ) && strlen( $moduleName ) <= 255;