X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fcache%2FHTMLFileCache.php;h=ae8efa9c82baa8b72ad4e7fb53a84e71b313f277;hb=5fa4cdf860c79b32ab6ef034c6d9420c2727f695;hp=1bab0f5a8ec8ec34843ca44387e317f1be8ffdfa;hpb=bae9c5aca69c62ff8ae32956a082c0787cb06b73;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/cache/HTMLFileCache.php b/includes/cache/HTMLFileCache.php index 1bab0f5a8e..7ae2ee0e21 100644 --- a/includes/cache/HTMLFileCache.php +++ b/includes/cache/HTMLFileCache.php @@ -21,6 +21,8 @@ * @ingroup Cache */ +use MediaWiki\MediaWikiServices; + /** * Page view caching in the file system. * The only cacheable actions are "view" and "history". Also special pages @@ -29,18 +31,9 @@ * @ingroup Cache */ class HTMLFileCache extends FileCacheBase { - /** - * Construct an HTMLFileCache object from a Title and an action - * - * @deprecated since 1.24, instantiate this class directly - * @param Title|string $title Title object or prefixed DB key string - * @param string $action - * @throws MWException - * @return HTMLFileCache - */ - public static function newFromTitle( $title, $action ) { - return new self( $title, $action ); - } + const MODE_NORMAL = 0; // normal cache mode + const MODE_OUTAGE = 1; // fallback cache for DB outages + const MODE_REBUILD = 2; // background cache rebuild mode /** * @param Title|string $title Title object or prefixed DB key string @@ -49,6 +42,7 @@ class HTMLFileCache extends FileCacheBase { */ public function __construct( $title, $action ) { parent::__construct(); + $allowedTypes = self::cacheablePageActions(); if ( !in_array( $action, $allowedTypes ) ) { throw new MWException( 'Invalid file cache type given.' ); @@ -93,14 +87,15 @@ class HTMLFileCache extends FileCacheBase { /** * Check if pages can be cached for this request/user * @param IContextSource $context + * @param int $mode One of the HTMLFileCache::MODE_* constants (since 1.28) * @return bool */ - public static function useFileCache( IContextSource $context ) { - global $wgUseFileCache, $wgDebugToolbar, $wgContLang; - if ( !$wgUseFileCache ) { + public static function useFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) { + $config = MediaWikiServices::getInstance()->getMainConfig(); + + if ( !$config->get( 'UseFileCache' ) && $mode !== self::MODE_REBUILD ) { return false; - } - if ( $wgDebugToolbar ) { + } elseif ( $config->get( 'DebugToolbar' ) ) { wfDebug( "HTML file cache skipped. \$wgDebugToolbar on\n" ); return false; @@ -121,15 +116,23 @@ class HTMLFileCache extends FileCacheBase { return false; } + $user = $context->getUser(); // Check for non-standard user language; this covers uselang, // and extensions for auto-detecting user language. $ulang = $context->getLanguage(); // Check that there are no other sources of variation - if ( $user->getId() || $user->getNewtalk() || !$ulang->equals( $wgContLang ) ) { + if ( $user->getId() || $ulang->getCode() !== $config->get( 'LanguageCode' ) ) { return false; } + + if ( $mode === self::MODE_NORMAL ) { + if ( $user->getNewtalk() ) { + return false; + } + } + // Allow extensions to disable caching return Hooks::run( 'HTMLFileCache::useFileCache', [ $context ] ); } @@ -137,17 +140,24 @@ class HTMLFileCache extends FileCacheBase { /** * Read from cache to context output * @param IContextSource $context + * @param int $mode One of the HTMLFileCache::MODE_* constants * @return void */ - public function loadFromFileCache( IContextSource $context ) { - global $wgMimeType, $wgLanguageCode; + public function loadFromFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) { + global $wgContLang; + $config = MediaWikiServices::getInstance()->getMainConfig(); wfDebug( __METHOD__ . "()\n" ); $filename = $this->cachePath(); + if ( $mode === self::MODE_OUTAGE ) { + // Avoid DB errors for queries in sendCacheControl() + $context->getTitle()->resetArticleID( 0 ); + } + $context->getOutput()->sendCacheControl(); - header( "Content-Type: $wgMimeType; charset=UTF-8" ); - header( "Content-Language: $wgLanguageCode" ); + header( "Content-Type: {$config->get( 'MimeType' )}; charset=UTF-8" ); + header( "Content-Language: {$wgContLang->getHtmlCode()}" ); if ( $this->useGzip() ) { if ( wfClientAcceptsGzip() ) { header( 'Content-Encoding: gzip' ); @@ -160,19 +170,24 @@ class HTMLFileCache extends FileCacheBase { } else { readfile( $filename ); } + $context->getOutput()->disable(); // tell $wgOut that output is taken care of } /** * Save this cache object with the given text. * Use this as an ob_start() handler. + * + * Normally this is only registed as a handler if $wgUseFileCache is on. + * If can be explicitly called by rebuildFileCache.php when it takes over + * handling file caching itself, disabling any automatic handling the the + * process. + * * @param string $text - * @return bool Whether $wgUseFileCache is enabled + * @return string|bool The annotated $text or false on error */ public function saveToFileCache( $text ) { - global $wgUseFileCache; - - if ( !$wgUseFileCache || strlen( $text ) < 512 ) { + if ( strlen( $text ) < 512 ) { // Disabled or empty/broken output (OOM and PHP errors) return $text; } @@ -215,9 +230,9 @@ class HTMLFileCache extends FileCacheBase { * @return bool Whether $wgUseFileCache is enabled */ public static function clearFileCache( Title $title ) { - global $wgUseFileCache; + $config = MediaWikiServices::getInstance()->getMainConfig(); - if ( !$wgUseFileCache ) { + if ( !$config->get( 'UseFileCache' ) ) { return false; }