Merge "Add MediaWiki-Timestamp header to ResourceLoader requests"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.php
index 5bc9dc3..508be2f 100644 (file)
  * @author Trevor Parscal
  */
 
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
+
 /**
  * Dynamic JavaScript and CSS resource loading system.
  *
  * Most of the documentation is on the MediaWiki documentation wiki starting at:
  *    https://www.mediawiki.org/wiki/ResourceLoader
  */
-class ResourceLoader {
+class ResourceLoader implements LoggerAwareInterface {
        /** @var int */
        protected static $filterCacheVersion = 7;
 
@@ -77,6 +81,11 @@ class ResourceLoader {
         */
        protected $blobStore;
 
+       /**
+        * @var LoggerInterface
+        */
+       private $logger;
+
        /**
         * Load information stored in the database about modules.
         *
@@ -188,14 +197,16 @@ class ResourceLoader {
                }
 
                if ( !in_array( $filter, array( 'minify-js', 'minify-css' ) ) ) {
-                       wfDebugLog( 'resourceloader', __METHOD__ . ": Invalid filter: $filter" );
+                       $this->logger->warning( 'Invalid filter {filter}', array(
+                               'filter' => $filter
+                       ) );
                        return $data;
                }
 
                if ( !$options['cache'] ) {
                        $result = $this->applyFilter( $filter, $data );
                } else {
-                       $key = wfMemcKey( 'resourceloader', 'filter', $filter, self::$filterCacheVersion, md5( $data ) );
+                       $key = wfGlobalCacheKey( 'resourceloader', 'filter', $filter, self::$filterCacheVersion, md5( $data ) );
                        $cache = wfGetCache( wfIsHHVM() ? CACHE_ACCEL : CACHE_ANYTHING );
                        $cacheEntry = $cache->get( $key );
                        if ( is_string( $cacheEntry ) ) {
@@ -204,15 +215,22 @@ class ResourceLoader {
                        }
                        $result = '';
                        try {
-                               wfIncrStats( "resourceloader_cache.$filter.miss" );
+                               $stats = RequestContext::getMain()->getStats();
+                               $statStart = microtime( true );
+
                                $result = $this->applyFilter( $filter, $data );
+
+                               $stats->timing( "resourceloader_cache.$filter.miss", microtime( true ) - $statStart );
                                if ( $options['cacheReport'] ) {
                                        $result .= "\n/* cache key: $key */";
                                }
-                               $cache->set( $key, $result );
+                               // Set a TTL since HHVM's APC doesn't have any limitation or eviction logic.
+                               $cache->set( $key, $result, 24 * 3600 );
                        } catch ( Exception $e ) {
                                MWExceptionHandler::logException( $e );
-                               wfDebugLog( 'resourceloader', __METHOD__ . ": minification failed: $e" );
+                               $this->logger->warning( 'Minification failed: {exception}', array(
+                                       'exception' => $e
+                               ) );
                                $this->errors[] = self::formatExceptionNoComment( $e );
                        }
                }
@@ -221,16 +239,17 @@ class ResourceLoader {
        }
 
        private function applyFilter( $filter, $data ) {
-                       switch ( $filter ) {
-                               case 'minify-js':
-                                       return JavaScriptMinifier::minify( $data,
-                                               $this->config->get( 'ResourceLoaderMinifierStatementsOnOwnLine' ),
-                                               $this->config->get( 'ResourceLoaderMinifierMaxLineLength' )
-                                       );
-                               case 'minify-css':
-                                       return CSSMin::minify( $data );
-                       }
-                       return $data;
+               switch ( $filter ) {
+                       case 'minify-js':
+                               return JavaScriptMinifier::minify( $data,
+                                       $this->config->get( 'ResourceLoaderMinifierStatementsOnOwnLine' ),
+                                       $this->config->get( 'ResourceLoaderMinifierMaxLineLength' )
+                               );
+                       case 'minify-css':
+                               return CSSMin::minify( $data );
+               }
+
+               return $data;
        }
 
        /* Methods */
@@ -239,14 +258,18 @@ class ResourceLoader {
         * Register core modules and runs registration hooks.
         * @param Config|null $config
         */
-       public function __construct( Config $config = null ) {
+       public function __construct( Config $config = null, LoggerInterface $logger = null ) {
                global $IP;
 
-               if ( $config === null ) {
-                       wfDebug( __METHOD__ . ' was called without providing a Config instance' );
-                       $config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
+               if ( !$logger ) {
+                       $logger = new NullLogger();
                }
+               $this->setLogger( $logger );
 
+               if ( !$config ) {
+                       $this->logger->debug( __METHOD__ . ' was called without providing a Config instance' );
+                       $config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
+               }
                $this->config = $config;
 
                // Add 'local' source first
@@ -276,9 +299,21 @@ class ResourceLoader {
                return $this->config;
        }
 
+       public function setLogger( LoggerInterface $logger ) {
+               $this->logger = $logger;
+       }
+
+       /**
+        * @since 1.26
+        * @return MessageBlobStore
+        */
+       public function getMessageBlobStore() {
+               return $this->blobStore;
+       }
+
        /**
-        * @param MessageBlobStore $blobStore
         * @since 1.25
+        * @param MessageBlobStore $blobStore
         */
        public function setMessageBlobStore( MessageBlobStore $blobStore ) {
                $this->blobStore = $blobStore;
@@ -633,7 +668,7 @@ class ResourceLoader {
                                // Do not allow private modules to be loaded from the web.
                                // This is a security issue, see bug 34907.
                                if ( $module->getGroup() === 'private' ) {
-                                       wfDebugLog( 'resourceloader', __METHOD__ . ": request for private module '$name' denied" );
+                                       $this->logger->debug( "Request for private module '$name' denied" );
                                        $this->errors[] = "Cannot show private module \"$name\"";
                                        continue;
                                }
@@ -648,7 +683,9 @@ class ResourceLoader {
                        $this->preloadModuleInfo( array_keys( $modules ), $context );
                } catch ( Exception $e ) {
                        MWExceptionHandler::logException( $e );
-                       wfDebugLog( 'resourceloader', __METHOD__ . ": preloading module info failed: $e" );
+                       $this->logger->warning( 'Preloading module info failed: {exception}', array(
+                               'exception' => $e
+                       ) );
                        $this->errors[] = self::formatExceptionNoComment( $e );
                }
 
@@ -658,7 +695,9 @@ class ResourceLoader {
                        $versionHash = $this->getCombinedVersion( $context, array_keys( $modules ) );
                } catch ( Exception $e ) {
                        MWExceptionHandler::logException( $e );
-                       wfDebugLog( 'resourceloader', __METHOD__ . ": calculating version hash failed: $e" );
+                       $this->logger->warning( 'Calculating version hash failed: {exception}', array(
+                               'exception' => $e
+                       ) );
                        $this->errors[] = self::formatExceptionNoComment( $e );
                }
 
@@ -775,6 +814,11 @@ class ResourceLoader {
                        $exp = min( $maxage, $smaxage );
                        header( 'Expires: ' . wfTimestamp( TS_RFC2822, $exp + time() ) );
                }
+
+               // Send the current time expressed as fractional seconds since epoch,
+               // with microsecond precision. This helps distinguish hits from misses
+               // in edge caches.
+               header( 'MediaWiki-Timestamp: ' . microtime( true ) );
        }
 
        /**
@@ -804,8 +848,7 @@ class ResourceLoader {
                        // sending the 304.
                        wfResetOutputBuffers( /* $resetGzipEncoding = */ true );
 
-                       header( 'HTTP/1.0 304 Not Modified' );
-                       header( 'Status: 304 Not Modified' );
+                       HttpStatus::header( 304 );
 
                        $this->sendResponseHeaders( $context, $etag, false );
                        return true;
@@ -938,17 +981,14 @@ MESSAGE;
                // Pre-fetch blobs
                if ( $context->shouldIncludeMessages() ) {
                        try {
-                               $blobs = $this->blobStore->get( $this, $modules, $context->getLanguage() );
+                               $this->blobStore->get( $this, $modules, $context->getLanguage() );
                        } catch ( Exception $e ) {
                                MWExceptionHandler::logException( $e );
-                               wfDebugLog(
-                                       'resourceloader',
-                                       __METHOD__ . ": pre-fetching blobs from MessageBlobStore failed: $e"
-                               );
+                               $this->logger->warning( 'Prefetching MessageBlobStore failed: {exception}', array(
+                                       'exception' => $e
+                               ) );
                                $this->errors[] = self::formatExceptionNoComment( $e );
                        }
-               } else {
-                       $blobs = array();
                }
 
                foreach ( $missing as $name ) {
@@ -958,79 +998,13 @@ MESSAGE;
                // Generate output
                $isRaw = false;
                foreach ( $modules as $name => $module ) {
-                       /**
-                        * @var $module ResourceLoaderModule
-                        */
-
                        try {
-                               $scripts = '';
-                               if ( $context->shouldIncludeScripts() ) {
-                                       // If we are in debug mode, we'll want to return an array of URLs if possible
-                                       // However, we can't do this if the module doesn't support it
-                                       // We also can't do this if there is an only= parameter, because we have to give
-                                       // the module a way to return a load.php URL without causing an infinite loop
-                                       if ( $context->getDebug() && !$context->getOnly() && $module->supportsURLLoading() ) {
-                                               $scripts = $module->getScriptURLsForDebug( $context );
-                                       } else {
-                                               $scripts = $module->getScript( $context );
-                                               // rtrim() because there are usually a few line breaks
-                                               // after the last ';'. A new line at EOF, a new line
-                                               // added by ResourceLoaderFileModule::readScriptFiles, etc.
-                                               if ( is_string( $scripts )
-                                                       && strlen( $scripts )
-                                                       && substr( rtrim( $scripts ), -1 ) !== ';'
-                                               ) {
-                                                       // Append semicolon to prevent weird bugs caused by files not
-                                                       // terminating their statements right (bug 27054)
-                                                       $scripts .= ";\n";
-                                               }
-                                       }
-                               }
-                               // Styles
-                               $styles = array();
-                               if ( $context->shouldIncludeStyles() ) {
-                                       // Don't create empty stylesheets like array( '' => '' ) for modules
-                                       // that don't *have* any stylesheets (bug 38024).
-                                       $stylePairs = $module->getStyles( $context );
-                                       if ( count( $stylePairs ) ) {
-                                               // If we are in debug mode without &only= set, we'll want to return an array of URLs
-                                               // See comment near shouldIncludeScripts() for more details
-                                               if ( $context->getDebug() && !$context->getOnly() && $module->supportsURLLoading() ) {
-                                                       $styles = array(
-                                                               'url' => $module->getStyleURLsForDebug( $context )
-                                                       );
-                                               } else {
-                                                       // Minify CSS before embedding in mw.loader.implement call
-                                                       // (unless in debug mode)
-                                                       if ( !$context->getDebug() ) {
-                                                               foreach ( $stylePairs as $media => $style ) {
-                                                                       // Can be either a string or an array of strings.
-                                                                       if ( is_array( $style ) ) {
-                                                                               $stylePairs[$media] = array();
-                                                                               foreach ( $style as $cssText ) {
-                                                                                       if ( is_string( $cssText ) ) {
-                                                                                               $stylePairs[$media][] = $this->filter( 'minify-css', $cssText );
-                                                                                       }
-                                                                               }
-                                                                       } elseif ( is_string( $style ) ) {
-                                                                               $stylePairs[$media] = $this->filter( 'minify-css', $style );
-                                                                       }
-                                                               }
-                                                       }
-                                                       // Wrap styles into @media groups as needed and flatten into a numerical array
-                                                       $styles = array(
-                                                               'css' => self::makeCombinedStyles( $stylePairs )
-                                                       );
-                                               }
-                                       }
-                               }
-
-                               // Messages
-                               $messagesBlob = isset( $blobs[$name] ) ? $blobs[$name] : '{}';
+                               $content = $module->getModuleContent( $context );
 
                                // Append output
                                switch ( $context->getOnly() ) {
                                        case 'scripts':
+                                               $scripts = $content['scripts'];
                                                if ( is_string( $scripts ) ) {
                                                        // Load scripts raw...
                                                        $out .= $scripts;
@@ -1040,6 +1014,7 @@ MESSAGE;
                                                }
                                                break;
                                        case 'styles':
+                                               $styles = $content['styles'];
                                                // We no longer seperate into media, they are all combined now with
                                                // custom media type groups into @media .. {} sections as part of the css string.
                                                // Module returns either an empty array or a numerical array with css strings.
@@ -1048,16 +1023,18 @@ MESSAGE;
                                        default:
                                                $out .= self::makeLoaderImplementScript(
                                                        $name,
-                                                       $scripts,
-                                                       $styles,
-                                                       new XmlJsCode( $messagesBlob ),
-                                                       $module->getTemplates()
+                                                       isset( $content['scripts'] ) ? $content['scripts'] : '',
+                                                       isset( $content['styles'] ) ? $content['styles'] : array(),
+                                                       isset( $content['messagesBlob'] ) ? new XmlJsCode( $content['messagesBlob'] ) : array(),
+                                                       isset( $content['templates'] ) ? $content['templates'] : array()
                                                );
                                                break;
                                }
                        } catch ( Exception $e ) {
                                MWExceptionHandler::logException( $e );
-                               wfDebugLog( 'resourceloader', __METHOD__ . ": generating module package failed: $e" );
+                               $this->logger->warning( 'Generating module package failed: {exception}', array(
+                                       'exception' => $e
+                               ) );
                                $this->errors[] = self::formatExceptionNoComment( $e );
 
                                // Respond to client with error-state instead of module implementation
@@ -1138,9 +1115,9 @@ MESSAGE;
                $module = array(
                        $name,
                        $scripts,
-                       (object) $styles,
-                       (object) $messages,
-                       (object) $templates,
+                       (object)$styles,
+                       (object)$messages,
+                       (object)$templates,
                );
                self::trimArray( $module );