wiki.techinc.nl/includes/HTMLFileCache.php

234 lines
6.2 KiB
PHP
Raw Normal View History

<?php
/**
* Contain the HTMLFileCache class
* @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.
2006-01-07 13:09:30 +00:00
*
* Global options that affect this module:
* - $wgCachePages
* - $wgCacheEpoch
* - $wgUseFileCache
* Introduced a new system for localisation caching. The system is based around fast fetches of individual messages, minimising memory overhead and startup time in the typical case. It handles both core messages (formerly in Language.php) and extension messages (formerly in MessageCache.php). Profiling indicates a significant win for average throughput. * The serialized message cache, which would have been redundant, has been removed. Similar performance characteristics can be achieved with $wgLocalisationCacheConf['manualRecache'] = true; * Added a maintenance script rebuildLocalisationCache.php for offline rebuilding of the localisation cache. * Extension i18n files can now contain any of the variables which can be set in Messages*.php. It is possible, and recommended, to use this feature instead of the hooks for special page aliases and magic words. * $wgExtensionAliasesFiles, LanguageGetMagic and LanguageGetSpecialPageAliases are retained for backwards compatibility. $wgMessageCache->addMessages() and related functions have been removed. wfLoadExtensionMessages() is a no-op and can continue to be called for b/c. * Introduced $wgCacheDirectory as a default location for the various local caches that have accumulated. Suggested $IP/cache as a good place for it in the default LocalSettings.php and created this directory with a deny-all .htaccess. * Patched Exception.php to avoid using the message cache when an exception is thrown from within LocalisationCache, since this tends to fail horribly. * Removed Language::getLocalisationArray(), Language::loadLocalisation(), Language::load() * Fixed FileDependency::__sleep() * In Cdb.php, fixed newlines in debug messages In MessageCache::get(): * Replaced calls to $wgContLang capitalisation functions with plain PHP functions, reducing the typical case from 99us to 93us. Message cache keys are already documented as being restricted to ASCII. * Implemented a more efficient way to filter out bogus language codes, reducing the "foo/en" case from 430us to 101us * Optimised wfRunHooks() in the typical do-nothing case, from ~30us to ~3us. This reduced MessageCache::get() typical case time from 93us to 38us. * Removed hook MessageNotInMwNs to save an extra 3us per cache hit. Reimplemented the only user (LocalisationUpdate) using the new hook LocalisationCacheRecache.
2009-06-28 07:11:43 +00:00
* - $wgCacheDirectory
* - $wgFileCacheDirectory
* - $wgUseGzip
*
* @ingroup Cache
*/
class HTMLFileCache {
var $mTitle, $mFileCache, $mType;
2006-01-07 13:31:29 +00:00
public function __construct( &$title, $type = 'view' ) {
2008-12-10 04:06:14 +00:00
$this->mTitle = $title;
$this->mType = ($type == 'raw' || $type == 'view' ) ? $type : false;
$this->fileCacheName(); // init name
}
2006-01-07 13:31:29 +00:00
public function fileCacheName() {
if( !$this->mFileCache ) {
global $wgCacheDirectory, $wgFileCacheDirectory, $wgFileCacheDepth;
* Introduced a new system for localisation caching. The system is based around fast fetches of individual messages, minimising memory overhead and startup time in the typical case. It handles both core messages (formerly in Language.php) and extension messages (formerly in MessageCache.php). Profiling indicates a significant win for average throughput. * The serialized message cache, which would have been redundant, has been removed. Similar performance characteristics can be achieved with $wgLocalisationCacheConf['manualRecache'] = true; * Added a maintenance script rebuildLocalisationCache.php for offline rebuilding of the localisation cache. * Extension i18n files can now contain any of the variables which can be set in Messages*.php. It is possible, and recommended, to use this feature instead of the hooks for special page aliases and magic words. * $wgExtensionAliasesFiles, LanguageGetMagic and LanguageGetSpecialPageAliases are retained for backwards compatibility. $wgMessageCache->addMessages() and related functions have been removed. wfLoadExtensionMessages() is a no-op and can continue to be called for b/c. * Introduced $wgCacheDirectory as a default location for the various local caches that have accumulated. Suggested $IP/cache as a good place for it in the default LocalSettings.php and created this directory with a deny-all .htaccess. * Patched Exception.php to avoid using the message cache when an exception is thrown from within LocalisationCache, since this tends to fail horribly. * Removed Language::getLocalisationArray(), Language::loadLocalisation(), Language::load() * Fixed FileDependency::__sleep() * In Cdb.php, fixed newlines in debug messages In MessageCache::get(): * Replaced calls to $wgContLang capitalisation functions with plain PHP functions, reducing the typical case from 99us to 93us. Message cache keys are already documented as being restricted to ASCII. * Implemented a more efficient way to filter out bogus language codes, reducing the "foo/en" case from 430us to 101us * Optimised wfRunHooks() in the typical do-nothing case, from ~30us to ~3us. This reduced MessageCache::get() typical case time from 93us to 38us. * Removed hook MessageNotInMwNs to save an extra 3us per cache hit. Reimplemented the only user (LocalisationUpdate) using the new hook LocalisationCacheRecache.
2009-06-28 07:11:43 +00:00
if ( $wgFileCacheDirectory ) {
$dir = $wgFileCacheDirectory;
} elseif ( $wgCacheDirectory ) {
$dir = "$wgCacheDirectory/html";
} else {
* Introduced a new system for localisation caching. The system is based around fast fetches of individual messages, minimising memory overhead and startup time in the typical case. It handles both core messages (formerly in Language.php) and extension messages (formerly in MessageCache.php). Profiling indicates a significant win for average throughput. * The serialized message cache, which would have been redundant, has been removed. Similar performance characteristics can be achieved with $wgLocalisationCacheConf['manualRecache'] = true; * Added a maintenance script rebuildLocalisationCache.php for offline rebuilding of the localisation cache. * Extension i18n files can now contain any of the variables which can be set in Messages*.php. It is possible, and recommended, to use this feature instead of the hooks for special page aliases and magic words. * $wgExtensionAliasesFiles, LanguageGetMagic and LanguageGetSpecialPageAliases are retained for backwards compatibility. $wgMessageCache->addMessages() and related functions have been removed. wfLoadExtensionMessages() is a no-op and can continue to be called for b/c. * Introduced $wgCacheDirectory as a default location for the various local caches that have accumulated. Suggested $IP/cache as a good place for it in the default LocalSettings.php and created this directory with a deny-all .htaccess. * Patched Exception.php to avoid using the message cache when an exception is thrown from within LocalisationCache, since this tends to fail horribly. * Removed Language::getLocalisationArray(), Language::loadLocalisation(), Language::load() * Fixed FileDependency::__sleep() * In Cdb.php, fixed newlines in debug messages In MessageCache::get(): * Replaced calls to $wgContLang capitalisation functions with plain PHP functions, reducing the typical case from 99us to 93us. Message cache keys are already documented as being restricted to ASCII. * Implemented a more efficient way to filter out bogus language codes, reducing the "foo/en" case from 430us to 101us * Optimised wfRunHooks() in the typical do-nothing case, from ~30us to ~3us. This reduced MessageCache::get() typical case time from 93us to 38us. * Removed hook MessageNotInMwNs to save an extra 3us per cache hit. Reimplemented the only user (LocalisationUpdate) using the new hook LocalisationCacheRecache.
2009-06-28 07:11:43 +00:00
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/' : '';
$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";
2006-01-07 13:31:29 +00:00
2010-10-14 20:53:04 +00:00
if( $this->useGzip() ) {
$this->mFileCache .= '.gz';
2010-10-14 20:53:04 +00:00
}
2006-01-07 13:31:29 +00:00
wfDebug( __METHOD__ . ": {$this->mFileCache}\n" );
}
return $this->mFileCache;
}
public function isFileCached() {
if( $this->mType === false ) return false;
return file_exists( $this->fileCacheName() );
}
2006-01-07 13:31:29 +00:00
public function fileCacheTime() {
2004-11-21 02:30:25 +00:00
return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
}
/**
* Check if pages can be cached for this request/user
* @return bool
*/
public static function useFileCache() {
global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
if( !$wgUseFileCache ) return false;
// Get all query values
$queryVals = $wgRequest->getValues();
foreach( $queryVals as $query => $val ) {
if( $query == 'title' || $query == 'curid' ) continue;
// Normal page view in query form can have action=view.
// Raw hits for pages also stored, like .css pages for example.
else if( $query == 'action' && ($val == 'view' || $val == 'raw') ) continue;
else if( $query == 'usemsgcache' && $val == 'yes' ) continue;
// Below are header setting params
else if( $query == 'maxage' || $query == 'smaxage' || $query == 'ctype' || $query == 'gen' )
continue;
else
return false;
}
// Check for non-standard user language; this covers uselang,
// and extensions for auto-detecting user language.
$ulang = $wgLang->getCode();
$clang = $wgContLang->getCode();
// Check that there are no other sources of variation
return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
}
2006-01-07 13:31:29 +00:00
/*
* Check if up to date cache file exists
* @param $timestamp string
*/
public function isFileCacheGood( $timestamp = '' ) {
global $wgCacheEpoch;
2006-01-07 13:31:29 +00:00
if( !$this->isFileCached() ) return false;
2006-01-07 13:31:29 +00:00
$cachetime = $this->fileCacheTime();
2008-12-10 04:06:14 +00:00
$good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
2006-01-07 13:31:29 +00:00
wfDebug( __METHOD__ . ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
return $good;
}
public function useGzip() {
global $wgUseGzip;
return $wgUseGzip;
}
2006-01-07 13:31:29 +00:00
/* In handy string packages */
public function fetchRawText() {
return file_get_contents( $this->fileCacheName() );
}
2006-01-07 13:31:29 +00:00
public function fetchPageText() {
if( $this->useGzip() ) {
/* Why is there no gzfile_get_contents() or gzdecode()? */
return implode( '', gzfile( $this->fileCacheName() ) );
} else {
return $this->fetchRawText();
}
}
2006-01-07 13:31:29 +00:00
/* Working directory to/from output */
public function loadFromFileCache() {
global $wgOut, $wgMimeType, $wgOutputEncoding, $wgLanguageCode;
wfDebug( __METHOD__ . "()\n");
2008-12-10 04:06:14 +00:00
$filename = $this->fileCacheName();
2008-12-28 15:17:21 +00:00
// 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={$wgOutputEncoding}" );
header( "Content-Language: $wgLanguageCode" );
}
2006-01-07 13:31:29 +00:00
if( $this->useGzip() ) {
if( wfClientAcceptsGzip() ) {
header( 'Content-Encoding: gzip' );
} else {
/* Send uncompressed */
readgzfile( $filename );
return;
}
}
readfile( $filename );
$wgOut->disable(); // tell $wgOut that output is taken care of
}
2006-01-07 13:31:29 +00:00
protected function checkCacheDirs() {
$filename = $this->fileCacheName();
2008-12-10 04:06:14 +00:00
$mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
$mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
2006-01-07 13:31:29 +00:00
wfMkdirParents( $mydir1 );
wfMkdirParents( $mydir2 );
}
2006-01-07 13:31:29 +00:00
public function saveToFileCache( $text ) {
global $wgUseFileCache;
if( !$wgUseFileCache || strlen( $text ) < 512 ) {
// Disabled or empty/broken output (OOM and PHP errors)
return $text;
}
2006-01-07 13:31:29 +00:00
wfDebug( __METHOD__ . "()\n", false);
2006-01-07 13:31:29 +00:00
$this->checkCacheDirs();
2006-01-07 13:31:29 +00:00
$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;
}
} else {
return $text;
}
}
return $text;
}
public static function clearFileCache( $title ) {
global $wgUseFileCache;
if ( !$wgUseFileCache ) {
return false;
}
wfSuppressWarnings();
2009-01-02 16:27:05 +00:00
$fc = new self( $title, 'view' );
unlink( $fc->fileCacheName() );
$fc = new self( $title, 'raw' );
unlink( $fc->fileCacheName() );
wfRestoreWarnings();
return true;
}
}