Add small HtmlCacheUpdater service class to normalize purging code
[lhc/web/wiklou.git] / includes / cache / HTMLFileCache.php
index f8cd754..6d0b87e 100644 (file)
@@ -91,7 +91,6 @@ class HTMLFileCache extends FileCacheBase {
         * @return bool
         */
        public static function useFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
-               global $wgContLang;
                $config = MediaWikiServices::getInstance()->getMainConfig();
 
                if ( !$config->get( 'UseFileCache' ) && $mode !== self::MODE_REBUILD ) {
@@ -124,14 +123,13 @@ class HTMLFileCache extends FileCacheBase {
                $ulang = $context->getLanguage();
 
                // Check that there are no other sources of variation
-               if ( $user->getId() || !$ulang->equals( $wgContLang ) ) {
+               if ( $user->getId() ||
+                       !$ulang->equals( MediaWikiServices::getInstance()->getContentLanguage() ) ) {
                        return false;
                }
 
-               if ( $mode === self::MODE_NORMAL ) {
-                       if ( $user->getNewtalk() ) {
-                               return false;
-                       }
+               if ( ( $mode === self::MODE_NORMAL ) && $user->getNewtalk() ) {
+                       return false;
                }
 
                // Allow extensions to disable caching
@@ -145,7 +143,6 @@ class HTMLFileCache extends FileCacheBase {
         * @return void
         */
        public function loadFromFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
-               global $wgContLang;
                $config = MediaWikiServices::getInstance()->getMainConfig();
 
                wfDebug( __METHOD__ . "()\n" );
@@ -158,7 +155,8 @@ class HTMLFileCache extends FileCacheBase {
 
                $context->getOutput()->sendCacheControl();
                header( "Content-Type: {$config->get( 'MimeType' )}; charset=UTF-8" );
-               header( "Content-Language: {$wgContLang->getHtmlCode()}" );
+               header( 'Content-Language: ' .
+                       MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() );
                if ( $this->useGzip() ) {
                        if ( wfClientAcceptsGzip() ) {
                                header( 'Content-Encoding: gzip' );
@@ -211,35 +209,43 @@ class HTMLFileCache extends FileCacheBase {
                }
 
                // gzip output to buffer as needed and set headers...
-               if ( $this->useGzip() ) {
-                       // @todo Ugly wfClientAcceptsGzip() function - use context!
-                       if ( wfClientAcceptsGzip() ) {
-                               header( 'Content-Encoding: gzip' );
+               // @todo Ugly wfClientAcceptsGzip() function - use context!
+               if ( $this->useGzip() && wfClientAcceptsGzip() ) {
+                       header( 'Content-Encoding: gzip' );
 
-                               return $compressed;
-                       } else {
-                               return $text;
+                       return $compressed;
+               }
+
+               return $text;
+       }
+
+       /**
+        * @param string[] $prefixedDbKeys List of prefixed DB keys for pages to purge
+        * @since 1.34
+        */
+       public static function purge( array $prefixedDbKeys ) {
+               foreach ( $prefixedDbKeys as $prefixedDbKey ) {
+                       foreach ( self::cacheablePageActions() as $type ) {
+                               $fc = new self( $prefixedDbKey, $type );
+                               $fc->clearCache();
                        }
-               } else {
-                       return $text;
                }
        }
 
        /**
         * Clear the file caches for a page for all actions
-        * @param Title $title
+        * @param Traversable|Title[]|Title $titles
         * @return bool Whether $wgUseFileCache is enabled
         */
-       public static function clearFileCache( Title $title ) {
+       public static function clearFileCache( $titles ) {
                $config = MediaWikiServices::getInstance()->getMainConfig();
-
                if ( !$config->get( 'UseFileCache' ) ) {
                        return false;
                }
 
-               foreach ( self::cacheablePageActions() as $type ) {
-                       $fc = new self( $title, $type );
-                       $fc->clearCache();
+               $titleIterator = ( $titles instanceof Title ) ? [ $titles ] : $titles;
+               foreach ( $titleIterator as $title ) {
+                       self::purge( [ $title->getPrefixedDBkey() ] );
                }
 
                return true;