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