X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fcache%2FHTMLFileCache.php;h=ea2e20b1cdad0abc5cfbe76750f7bdef6482ae92;hb=f43fa6f4f0e2cb60b8543daad661b48a3e0653a9;hp=bb78aa0dbdafe64ee0dba8898d672c10d6378fbb;hpb=0421ab1e9eda6e22ba9a12c73dbb969c337ed9e9;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/cache/HTMLFileCache.php b/includes/cache/HTMLFileCache.php index bb78aa0dbd..ae8efa9c82 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,14 +31,18 @@ * @ingroup Cache */ class HTMLFileCache extends FileCacheBase { + const MODE_NORMAL = 0; // normal cache mode + const MODE_OUTAGE = 1; // fallback cache for DB outages + const MODE_REBUILD = 2; // background cache rebuild mode + /** - * Construct an ObjectFileCache from a Title and an action + * 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 - * - * @deprecated Since 1.24, instantiate this class directly */ public static function newFromTitle( $title, $action ) { return new self( $title, $action ); @@ -49,6 +55,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 +100,15 @@ class HTMLFileCache extends FileCacheBase { /** * Check if pages can be cached for this request/user * @param IContextSource $context + * @param integer $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 +129,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 +153,24 @@ class HTMLFileCache extends FileCacheBase { /** * Read from cache to context output * @param IContextSource $context + * @param integer $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 +183,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 +243,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; }