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