* @ingroup Cache
*/
class HTMLFileCache {
- var $mTitle, $mFileCache;
+ var $mTitle, $mFileCache, $mType;
- public function __construct( &$title ) {
+ public function __construct( &$title, $type = 'view' ) {
$this->mTitle = $title;
- $this->mFileCache = $this->fileCacheName();
+ $this->mType = ($type == 'raw' || $type == 'view' ) ? $type : false;
+ $this->fileCacheName(); // init name
}
public function fileCacheName() {
if( !$this->mFileCache ) {
global $wgFileCacheDirectory, $wgRequest;
+ # Store raw pages (like CSS hits) elsewhere
+ $subdir = ($this->mType === 'raw') ? 'raw/' : '';
$key = $this->mTitle->getPrefixedDbkey();
$hash = md5( $key );
# Avoid extension confusion
$key = str_replace( '.', '%2E', urlencode( $key ) );
- # Store raw pages (like CSS hits) elsewhere
- $subdir = $wgRequest->getVal('action') == 'raw' ? 'raw/' : '';
$hash1 = substr( $hash, 0, 1 );
$hash2 = substr( $hash, 0, 2 );
}
public function isFileCached() {
+ if( $this->mType === false ) return false;
return file_exists( $this->fileCacheName() );
}
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.
- if( $query == 'action' && ($val == 'view' || $val == 'raw') ) continue;
- if( $query == 'usemsgcache' && $val == 'yes' ) continue;
+ else if( $query == 'action' && ($val == 'view' || $val == 'raw') ) continue;
+ else if( $query == 'usemsgcache' && $val == 'yes' ) continue;
// Below are header setting params
- if( $query == 'maxage' || $query == 'smaxage' || $query == 'ctype' || $query == 'gen' )
+ 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.
public function loadFromFileCache() {
global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
wfDebug(" loadFromFileCache()\n");
-
$filename = $this->fileCacheName();
- $wgOut->sendCacheControl();
-
- header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
- header( "Content-language: $wgContLanguageCode" );
+ // 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: $wgContLanguageCode" );
+ }
if( $this->useGzip() ) {
if( wfClientAcceptsGzip() ) {
}
}
readfile( $filename );
+ $wgOut->disable(); // tell $wgOut that output is taken care of
}
protected function checkCacheDirs() {
wfMkdirParents( $mydir2 );
}
- public function saveToFileCache( $origtext ) {
+ public function saveToFileCache( $text ) {
global $wgUseFileCache;
- if( !$wgUseFileCache ) {
- return $origtext; // return to output
+ if( !$wgUseFileCache || strlen( $text ) < 512 ) {
+ // Disabled or empty/broken output (OOM and PHP errors)
+ return $text;
}
- $text = $origtext;
- if( strcmp($text,'') == 0 ) return '';
wfDebug(" saveToFileCache()\n", false);
return $text;
}
+ public static function clearFileCache( $title ) {
+ global $wgUseFileCache;
+ if( !$wgUseFileCache ) return false;
+ $fc = new self( $title, 'view' );
+ @unlink( $fc->fileCacheName() );
+ $fc = new self( $title, 'raw' );
+ @unlink( $fc->fileCacheName() );
+ return true;
+ }
}