Update WrappedString use statements
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.php
index 2f29200..36bf656 100644 (file)
@@ -26,8 +26,8 @@ use MediaWiki\MediaWikiServices;
 use Psr\Log\LoggerAwareInterface;
 use Psr\Log\LoggerInterface;
 use Psr\Log\NullLogger;
-use WrappedString\WrappedString;
 use Wikimedia\Rdbms\DBConnectionError;
+use Wikimedia\WrappedString;
 
 /**
  * Dynamic JavaScript and CSS resource loading system.
@@ -79,6 +79,15 @@ class ResourceLoader implements LoggerAwareInterface {
         */
        protected $errors = [];
 
+       /**
+        * List of extra HTTP response headers provided by loaded modules.
+        *
+        * Populated by makeModuleResponse().
+        *
+        * @var array
+        */
+       protected $extraHeaders = [];
+
        /**
         * @var MessageBlobStore
         */
@@ -227,8 +236,6 @@ class ResourceLoader implements LoggerAwareInterface {
                return $data;
        }
 
-       /* Methods */
-
        /**
         * Register core modules and runs registration hooks.
         * @param Config $config [optional]
@@ -646,7 +653,7 @@ class ResourceLoader implements LoggerAwareInterface {
         *
         * @since 1.26
         * @param ResourceLoaderContext $context
-        * @param string[] $modules List of known module names
+        * @param string[] $moduleNames List of known module names
         * @return string Hash
         */
        public function getCombinedVersion( ResourceLoaderContext $context, array $moduleNames ) {
@@ -683,7 +690,6 @@ class ResourceLoader implements LoggerAwareInterface {
         *
         * @since 1.28
         * @param ResourceLoaderContext $context
-        * @param string[] $modules List of module names
         * @return string Hash
         */
        public function makeVersionQuery( ResourceLoaderContext $context ) {
@@ -719,6 +725,8 @@ class ResourceLoader implements LoggerAwareInterface {
                // See https://bugs.php.net/bug.php?id=36514
                ob_start();
 
+               $this->measureResponseTime( RequestContext::getMain()->getTiming() );
+
                // Find out which modules are missing and instantiate the others
                $modules = [];
                $missing = [];
@@ -794,7 +802,7 @@ class ResourceLoader implements LoggerAwareInterface {
                        }
                }
 
-               $this->sendResponseHeaders( $context, $etag, (bool)$this->errors );
+               $this->sendResponseHeaders( $context, $etag, (bool)$this->errors, $this->extraHeaders );
 
                // Remove the output buffer and output the response
                ob_end_clean();
@@ -819,6 +827,16 @@ class ResourceLoader implements LoggerAwareInterface {
                echo $response;
        }
 
+       protected function measureResponseTime( Timing $timing ) {
+               DeferredUpdates::addCallableUpdate( function () use ( $timing ) {
+                       $measure = $timing->measure( 'responseTime', 'requestStart', 'requestShutdown' );
+                       if ( $measure !== false ) {
+                               $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
+                               $stats->timing( 'resourceloader.responseTime', $measure['duration'] * 1000 );
+                       }
+               } );
+       }
+
        /**
         * Send main response headers to the client.
         *
@@ -827,9 +845,12 @@ class ResourceLoader implements LoggerAwareInterface {
         * @param ResourceLoaderContext $context
         * @param string $etag ETag header value
         * @param bool $errors Whether there are errors in the response
+        * @param string[] $extra Array of extra HTTP response headers
         * @return void
         */
-       protected function sendResponseHeaders( ResourceLoaderContext $context, $etag, $errors ) {
+       protected function sendResponseHeaders(
+               ResourceLoaderContext $context, $etag, $errors, array $extra = []
+       ) {
                \MediaWiki\HeaderCallback::warnIfHeadersSent();
                $rlMaxage = $this->config->get( 'ResourceLoaderMaxage' );
                // Use a short cache expiry so that updates propagate to clients quickly, if:
@@ -873,6 +894,9 @@ class ResourceLoader implements LoggerAwareInterface {
                        $exp = min( $maxage, $smaxage );
                        header( 'Expires: ' . wfTimestamp( TS_RFC2822, $exp + time() ) );
                }
+               foreach ( $extra as $header ) {
+                       header( $header );
+               }
        }
 
        /**
@@ -1008,6 +1032,9 @@ class ResourceLoader implements LoggerAwareInterface {
        /**
         * Generate code for a response.
         *
+        * Calling this method also populates the `errors` and `headers` members,
+        * later used by respond().
+        *
         * @param ResourceLoaderContext $context Context in which to generate a response
         * @param ResourceLoaderModule[] $modules List of module objects keyed by module name
         * @param string[] $missing List of requested module names that are unregistered (optional)
@@ -1052,6 +1079,10 @@ MESSAGE;
                                $implementKey = $name . '@' . $module->getVersionHash( $context );
                                $strContent = '';
 
+                               if ( isset( $content['headers'] ) ) {
+                                       $this->extraHeaders = array_merge( $this->extraHeaders, $content['headers'] );
+                               }
+
                                // Append output
                                switch ( $context->getOnly() ) {
                                        case 'scripts':
@@ -1176,8 +1207,6 @@ MESSAGE;
                return $moduleNames;
        }
 
-       /* Static Methods */
-
        /**
         * Return JS code that calls mw.loader.implement with given module properties.
         *