* @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();
+ $type = $type ? $type : 'view';
+ $this->mType = ($type == 'raw' || $type == 'view' ) ? $type : false;
+ $this->fileCacheName(); // init name
}
public function fileCacheName() {
- global $wgFileCacheDirectory;
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 ) );
-
+
$hash1 = substr( $hash, 0, 1 );
$hash2 = substr( $hash, 0, 2 );
- $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
+ $this->mFileCache = "{$wgFileCacheDirectory}/{$subdir}{$hash1}/{$hash2}/{$key}.html";
if( $this->useGzip() )
$this->mFileCache .= '.gz';
}
public function isFileCached() {
+ if( $this->mType === false ) return false;
return file_exists( $this->fileCacheName() );
}
*/
public static function useFileCache() {
global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
- if( !$wgUseFileCache )
- return false;
+ if( !$wgUseFileCache ) return false;
// Get all query values
$queryVals = $wgRequest->getValues();
foreach( $queryVals as $query => $val ) {
- // Normal page view in query form can have action=view
- if( $query !== 'title' && $query !== 'curid' && !($query == 'action' && $val == 'view') ) {
+ 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;
}
wfDebug(" loadFromFileCache()\n");
$filename = $this->fileCacheName();
- $wgOut->sendCacheControl();
+ // 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" );
return $text;
}
+ public static function clearFileCache( $title ) {
+ global $wgUseFileCache;
+ if( !$wgUseFileCache ) return false;
+ $fc = new self( $title, '' );
+ @unlink( $fc->fileCacheName() );
+ $fc = new self( $title, 'raw' );
+ @unlink( $fc->fileCacheName() );
+ return true;
+ }
}