* @file
* @ingroup Cache
*/
-
-/**
- * Handles talking to the file cache, putting stuff in and taking it back out.
- * Mostly called from Article.php for the emergency abort/fallback to cache.
- *
- * Global options that affect this module:
- * - $wgCachePages
- * - $wgCacheEpoch
- * - $wgUseFileCache
- * - $wgCacheDirectory
- * - $wgFileCacheDirectory
- * - $wgUseGzip
- *
- * @ingroup Cache
- */
-class HTMLFileCache {
-
+class HTMLFileCache extends FileCacheBase {
/**
- * @var Title
+ * Construct an ObjectFileCache from a Title and an action
+ * @param $title Title
+ * @param $action string
+ * @return HTMLFileCache
*/
- var $mTitle;
- var $mFileCache, $mType;
-
- public function __construct( $title, $type = 'view' ) {
- $this->mTitle = $title;
- $this->mType = ($type == 'raw' || $type == 'view' ) ? $type : false;
- $this->fileCacheName(); // init name
- }
-
- public function fileCacheName() {
- if( !$this->mFileCache ) {
- global $wgCacheDirectory, $wgFileCacheDirectory, $wgFileCacheDepth;
-
- if ( $wgFileCacheDirectory ) {
- $dir = $wgFileCacheDirectory;
- } elseif ( $wgCacheDirectory ) {
- $dir = "$wgCacheDirectory/html";
- } else {
- throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
- }
-
- # Store raw pages (like CSS hits) elsewhere
- $subdir = ($this->mType === 'raw') ? 'raw/' : '';
+ public static function newFromTitle( Title $title, $action ) {
+ $cache = new self();
- $key = $this->mTitle->getPrefixedDbkey();
- if ( $wgFileCacheDepth > 0 ) {
- $hash = md5( $key );
- for ( $i = 1; $i <= $wgFileCacheDepth; $i++ ) {
- $subdir .= substr( $hash, 0, $i ) . '/';
- }
- }
- # Avoid extension confusion
- $key = str_replace( '.', '%2E', urlencode( $key ) );
- $this->mFileCache = "{$dir}/{$subdir}{$key}.html";
+ $allowedTypes = self::cacheablePageActions();
+ if ( !in_array( $action, $allowedTypes ) ) {
+ throw new MWException( "Invalid filecache type given." );
+ }
+ $cache->mKey = $title->getPrefixedDBkey();
+ $cache->mType = (string)$action;
+ $cache->mExt = 'html';
- if( $this->useGzip() ) {
- $this->mFileCache .= '.gz';
- }
+ return $cache;
+ }
- wfDebug( __METHOD__ . ": {$this->mFileCache}\n" );
- }
- return $this->mFileCache;
+ /**
+ * Cacheable actions
+ * @return array
+ */
+ protected static function cacheablePageActions() {
+ return array( 'view', 'history' );
}
- public function isFileCached() {
- if( $this->mType === false ) {
- return false;
- }
- return file_exists( $this->fileCacheName() );
+ /**
+ * Get the base file cache directory
+ * @return string
+ */
+ protected function cacheDirectory() {
+ return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
}
- public function fileCacheTime() {
- return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
+ /**
+ * Get the cache type subdirectory (with the trailing slash) or the empty string
+ * @return string
+ */
+ protected function typeSubdirectory() {
+ if ( $this->mType === 'view' ) {
+ return ''; // b/c to not skip existing cache
+ } else {
+ return $this->mType . '/';
+ }
}
/**
* Check if pages can be cached for this request/user
+ * @param $context IContextSource
* @return bool
*/
- public static function useFileCache() {
- global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
- if( !$wgUseFileCache ) {
+ public static function useFileCache( IContextSource $context ) {
+ global $wgUseFileCache, $wgShowIPinHeader, $wgContLang;
+ if ( !$wgUseFileCache ) {
return false;
}
// Get all query values
- $queryVals = $wgRequest->getValues();
- foreach( $queryVals as $query => $val ) {
- if( $query == 'title' || $query == 'curid' ) {
- continue;
+ $queryVals = $context->getRequest()->getValues();
+ foreach ( $queryVals as $query => $val ) {
+ if ( $query === 'title' || $query === 'curid' ) {
+ continue; // note: curid sets title
// Normal page view in query form can have action=view.
// Raw hits for pages also stored, like .css pages for example.
- } elseif( $query == 'action' && $val == 'view' ) {
- continue;
- } elseif( $query == 'usemsgcache' && $val == 'yes' ) {
+ } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
continue;
// Below are header setting params
- } elseif( $query == 'maxage' || $query == 'smaxage' || $query == 'ctype' || $query == 'gen' ) {
+ } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
continue;
- } else {
- return false;
}
+ return false;
}
+ $user = $context->getUser();
// Check for non-standard user language; this covers uselang,
// and extensions for auto-detecting user language.
- $ulang = $wgLang->getCode();
+ $ulang = $context->getLang()->getCode();
$clang = $wgContLang->getCode();
// Check that there are no other sources of variation
- return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
+ return !$wgShowIPinHeader && !$user->getId() && !$user->getNewtalk() && $ulang == $clang;
}
/**
- * Check if up to date cache file exists
- * @param $timestamp string
- *
- * @return bool
+ * Read from cache to context output
+ * @param $context IContextSource
+ * @return void
*/
- public function isFileCacheGood( $timestamp = '' ) {
- global $wgCacheEpoch;
-
- if( !$this->isFileCached() ) {
- return false;
- }
-
- $cachetime = $this->fileCacheTime();
- $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
-
- wfDebug( __METHOD__ . ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
- return $good;
- }
-
- public function useGzip() {
- global $wgUseGzip;
- return $wgUseGzip;
- }
-
- /* In handy string packages */
- public function fetchRawText() {
- return file_get_contents( $this->fileCacheName() );
- }
-
- public function fetchPageText() {
- if( $this->useGzip() ) {
- /* Why is there no gzfile_get_contents() or gzdecode()? */
- return implode( '', gzfile( $this->fileCacheName() ) );
- } else {
- return $this->fetchRawText();
- }
- }
+ public function loadFromFileCache( IContextSource $context ) {
+ global $wgMimeType, $wgLanguageCode;
- /* Working directory to/from output */
- public function loadFromFileCache() {
- global $wgOut, $wgMimeType, $wgLanguageCode;
wfDebug( __METHOD__ . "()\n");
- $filename = $this->fileCacheName();
- // Raw pages should handle cache control on their own,
- // even when using file cache. This reduces hits from clients.
- if( $this->mType !== 'raw' ) {
- $wgOut->sendCacheControl();
- header( "Content-Type: $wgMimeType; charset=UTF-8" );
- header( "Content-Language: $wgLanguageCode" );
- }
-
- if( $this->useGzip() ) {
- if( wfClientAcceptsGzip() ) {
+ $filename = $this->cachePath();
+ $context->getOutput()->sendCacheControl();
+ header( "Content-Type: $wgMimeType; charset=UTF-8" );
+ header( "Content-Language: $wgLanguageCode" );
+ if ( $this->useGzip() ) {
+ if ( wfClientAcceptsGzip() ) {
header( 'Content-Encoding: gzip' );
} else {
/* Send uncompressed */
}
}
readfile( $filename );
- $wgOut->disable(); // tell $wgOut that output is taken care of
- }
-
- protected function checkCacheDirs() {
- $filename = $this->fileCacheName();
- $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
- $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
-
- wfMkdirParents( $mydir1, null, __METHOD__ );
- wfMkdirParents( $mydir2, null, __METHOD__ );
+ $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.
+ * @param $text string
+ * @return bool Whether $wgUseFileCache is enabled
+ */
public function saveToFileCache( $text ) {
global $wgUseFileCache;
- if( !$wgUseFileCache || strlen( $text ) < 512 ) {
+
+ if ( !$wgUseFileCache || strlen( $text ) < 512 ) {
// Disabled or empty/broken output (OOM and PHP errors)
return $text;
}
wfDebug( __METHOD__ . "()\n", false);
- $this->checkCacheDirs();
+ $now = wfTimestampNow();
+ if ( $this->useGzip() ) {
+ $text = str_replace(
+ '</html>', '<!-- Cached/compressed '.$now." -->\n</html>", $text );
+ } else {
+ $text = str_replace(
+ '</html>', '<!-- Cached '.$now." -->\n</html>", $text );
+ }
- $f = fopen( $this->fileCacheName(), 'w' );
- if($f) {
- $now = wfTimestampNow();
- if( $this->useGzip() ) {
- $rawtext = str_replace( '</html>',
- '<!-- Cached/compressed '.$now." -->\n</html>",
- $text );
- $text = gzencode( $rawtext );
- } else {
- $text = str_replace( '</html>',
- '<!-- Cached '.$now." -->\n</html>",
- $text );
- }
- fwrite( $f, $text );
- fclose( $f );
- if( $this->useGzip() ) {
- if( wfClientAcceptsGzip() ) {
- header( 'Content-Encoding: gzip' );
- return $text;
- } else {
- return $rawtext;
- }
+ // Store text to FS...
+ $compressed = $this->saveText( $text );
+ if ( $compressed === false ) {
+ return $text; // error
+ }
+
+ // gzip output to buffer as needed and set headers...
+ if ( $this->useGzip() ) {
+ // @TODO: ugly wfClientAcceptsGzip() function - use context!
+ if ( wfClientAcceptsGzip() ) {
+ header( 'Content-Encoding: gzip' );
+ return $compressed;
} else {
return $text;
}
+ } else {
+ return $text;
}
- return $text;
}
- public static function clearFileCache( $title ) {
+ /**
+ * Clear the file caches for a page for all actions
+ * @param $title Title
+ * @return bool Whether $wgUseFileCache is enabled
+ */
+ public static function clearFileCache( Title $title ) {
global $wgUseFileCache;
if ( !$wgUseFileCache ) {
return false;
}
- wfSuppressWarnings();
-
- $fc = new self( $title, 'view' );
- unlink( $fc->fileCacheName() );
-
- $fc = new self( $title, 'raw' );
- unlink( $fc->fileCacheName() );
-
- wfRestoreWarnings();
+ foreach ( self::cacheablePageActions() as $type ) {
+ $fc = self::newFromTitle( $title, $type );
+ $fc->clearCache();
+ }
return true;
}