2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2006-10-11 08:25:26 +00:00
|
|
|
* Contain the HTMLFileCache class
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Cache
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2003-08-02 10:13:27 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Handles talking to the file cache, putting stuff in and taking it back out.
|
|
|
|
|
* Mostly called from Article.php, also from DatabaseFunctions.php for the
|
|
|
|
|
* emergency abort/fallback to cache.
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
2004-09-02 23:28:24 +00:00
|
|
|
* Global options that affect this module:
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* - $wgCachePages
|
|
|
|
|
* - $wgCacheEpoch
|
|
|
|
|
* - $wgUseFileCache
|
|
|
|
|
* - $wgFileCacheDirectory
|
|
|
|
|
* - $wgUseGzip
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Cache
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2006-10-11 08:25:26 +00:00
|
|
|
class HTMLFileCache {
|
2008-12-28 14:19:39 +00:00
|
|
|
var $mTitle, $mFileCache, $mType;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2008-12-28 14:19:39 +00:00
|
|
|
public function __construct( &$title, $type = 'view' ) {
|
2008-12-10 04:06:14 +00:00
|
|
|
$this->mTitle = $title;
|
2008-12-28 14:19:39 +00:00
|
|
|
$this->mType = ($type == 'raw' || $type == 'view' ) ? $type : false;
|
|
|
|
|
$this->fileCacheName(); // init name
|
2003-08-02 10:13:27 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2008-12-11 14:29:16 +00:00
|
|
|
public function fileCacheName() {
|
2003-08-02 10:13:27 +00:00
|
|
|
if( !$this->mFileCache ) {
|
2008-12-28 13:32:32 +00:00
|
|
|
global $wgFileCacheDirectory, $wgRequest;
|
2008-12-28 14:19:39 +00:00
|
|
|
# Store raw pages (like CSS hits) elsewhere
|
|
|
|
|
$subdir = ($this->mType === 'raw') ? 'raw/' : '';
|
2005-07-20 15:02:24 +00:00
|
|
|
$key = $this->mTitle->getPrefixedDbkey();
|
|
|
|
|
$hash = md5( $key );
|
2008-12-28 13:32:32 +00:00
|
|
|
# Avoid extension confusion
|
2004-06-08 23:56:09 +00:00
|
|
|
$key = str_replace( '.', '%2E', urlencode( $key ) );
|
2008-12-28 13:32:32 +00:00
|
|
|
|
2003-08-02 10:13:27 +00:00
|
|
|
$hash1 = substr( $hash, 0, 1 );
|
|
|
|
|
$hash2 = substr( $hash, 0, 2 );
|
2008-12-28 13:32:32 +00:00
|
|
|
$this->mFileCache = "{$wgFileCacheDirectory}/{$subdir}{$hash1}/{$hash2}/{$key}.html";
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2008-12-10 04:06:14 +00:00
|
|
|
if( $this->useGzip() )
|
2004-06-08 23:56:09 +00:00
|
|
|
$this->mFileCache .= '.gz';
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-08-02 10:13:27 +00:00
|
|
|
wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
|
|
|
|
|
}
|
|
|
|
|
return $this->mFileCache;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-11 14:29:16 +00:00
|
|
|
public function isFileCached() {
|
2008-12-28 14:19:39 +00:00
|
|
|
if( $this->mType === false ) return false;
|
2003-08-02 10:13:27 +00:00
|
|
|
return file_exists( $this->fileCacheName() );
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2008-12-11 14:29:16 +00:00
|
|
|
public function fileCacheTime() {
|
2004-11-21 02:30:25 +00:00
|
|
|
return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
|
2003-08-02 10:13:27 +00:00
|
|
|
}
|
2008-12-11 14:29:16 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if pages can be cached for this request/user
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public static function useFileCache() {
|
|
|
|
|
global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
|
2008-12-28 13:32:32 +00:00
|
|
|
if( !$wgUseFileCache ) return false;
|
2008-12-11 14:29:16 +00:00
|
|
|
// Get all query values
|
|
|
|
|
$queryVals = $wgRequest->getValues();
|
|
|
|
|
foreach( $queryVals as $query => $val ) {
|
2008-12-28 13:32:32 +00:00
|
|
|
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.
|
2008-12-28 14:19:39 +00:00
|
|
|
else if( $query == 'action' && ($val == 'view' || $val == 'raw') ) continue;
|
|
|
|
|
else if( $query == 'usemsgcache' && $val == 'yes' ) continue;
|
2008-12-28 13:32:32 +00:00
|
|
|
// Below are header setting params
|
2008-12-28 14:19:39 +00:00
|
|
|
else if( $query == 'maxage' || $query == 'smaxage' || $query == 'ctype' || $query == 'gen' )
|
2008-12-28 13:32:32 +00:00
|
|
|
continue;
|
2008-12-28 14:19:39 +00:00
|
|
|
else
|
|
|
|
|
return false;
|
2008-12-11 14:29:16 +00:00
|
|
|
}
|
|
|
|
|
// Check for non-standard user language; this covers uselang,
|
|
|
|
|
// and extensions for auto-detecting user language.
|
|
|
|
|
$ulang = $wgLang->getCode();
|
|
|
|
|
$clang = $wgContLang->getCode();
|
2008-12-28 13:32:32 +00:00
|
|
|
// Check that there are no other sources of variation
|
2008-12-11 14:29:16 +00:00
|
|
|
return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2008-12-11 14:29:16 +00:00
|
|
|
/*
|
|
|
|
|
* Check if up to date cache file exists
|
|
|
|
|
* @param $timestamp string
|
|
|
|
|
*/
|
|
|
|
|
public function isFileCacheGood( $timestamp = '' ) {
|
2003-08-02 12:41:30 +00:00
|
|
|
global $wgCacheEpoch;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-08-02 10:13:27 +00:00
|
|
|
if( !$this->isFileCached() ) return false;
|
2008-12-11 14:29:16 +00:00
|
|
|
if( !$timestamp ) return true; // should be invalidated on change
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-08-02 10:13:27 +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
|
|
|
|
2008-12-11 14:29:16 +00:00
|
|
|
wfDebug(" isFileCacheGood() - cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
|
2003-08-02 10:13:27 +00:00
|
|
|
return $good;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-11 14:29:16 +00:00
|
|
|
public function useGzip() {
|
2003-08-02 10:13:27 +00:00
|
|
|
global $wgUseGzip;
|
|
|
|
|
return $wgUseGzip;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-08-02 10:13:27 +00:00
|
|
|
/* In handy string packages */
|
2008-12-11 14:29:16 +00:00
|
|
|
public function fetchRawText() {
|
2003-08-02 10:13:27 +00:00
|
|
|
return file_get_contents( $this->fileCacheName() );
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2008-12-11 14:29:16 +00:00
|
|
|
public function fetchPageText() {
|
2003-08-02 10:13:27 +00:00
|
|
|
if( $this->useGzip() ) {
|
|
|
|
|
/* Why is there no gzfile_get_contents() or gzdecode()? */
|
2004-06-08 23:56:09 +00:00
|
|
|
return implode( '', gzfile( $this->fileCacheName() ) );
|
2003-08-02 10:13:27 +00:00
|
|
|
} else {
|
|
|
|
|
return $this->fetchRawText();
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-08-02 10:13:27 +00:00
|
|
|
/* Working directory to/from output */
|
2008-12-11 14:29:16 +00:00
|
|
|
public function loadFromFileCache() {
|
2004-10-18 18:13:39 +00:00
|
|
|
global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
|
2003-08-02 10:13:27 +00:00
|
|
|
wfDebug(" loadFromFileCache()\n");
|
2006-01-07 13:31:29 +00:00
|
|
|
|
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.
|
2008-12-28 15:14:15 +00:00
|
|
|
if( $this->mType !== 'raw' )
|
|
|
|
|
$wgOut->sendCacheControl();
|
2004-10-18 18:13:39 +00:00
|
|
|
|
|
|
|
|
header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
|
|
|
|
|
header( "Content-language: $wgContLanguageCode" );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-08-02 10:13:27 +00:00
|
|
|
if( $this->useGzip() ) {
|
|
|
|
|
if( wfClientAcceptsGzip() ) {
|
2004-06-08 23:56:09 +00:00
|
|
|
header( 'Content-Encoding: gzip' );
|
2003-08-02 10:13:27 +00:00
|
|
|
} else {
|
|
|
|
|
/* Send uncompressed */
|
|
|
|
|
readgzfile( $filename );
|
2003-08-02 12:41:30 +00:00
|
|
|
return;
|
2003-08-02 10:13:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
2003-08-02 12:41:30 +00:00
|
|
|
readfile( $filename );
|
2003-08-02 10:13:27 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2008-12-11 14:29:16 +00:00
|
|
|
protected function checkCacheDirs() {
|
2003-08-02 10:13:27 +00:00
|
|
|
$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
|
|
|
|
2008-09-04 04:15:27 +00:00
|
|
|
wfMkdirParents( $mydir1 );
|
|
|
|
|
wfMkdirParents( $mydir2 );
|
2003-08-02 10:13:27 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2008-12-11 14:29:16 +00:00
|
|
|
public function saveToFileCache( $origtext ) {
|
2008-09-25 23:02:35 +00:00
|
|
|
global $wgUseFileCache;
|
|
|
|
|
if( !$wgUseFileCache ) {
|
|
|
|
|
return $origtext; // return to output
|
|
|
|
|
}
|
2004-08-08 10:15:20 +00:00
|
|
|
$text = $origtext;
|
2008-09-25 23:02:35 +00:00
|
|
|
if( strcmp($text,'') == 0 ) return '';
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-08-02 10:13:27 +00:00
|
|
|
wfDebug(" saveToFileCache()\n", false);
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-08-02 10:13:27 +00:00
|
|
|
$this->checkCacheDirs();
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-06-08 23:56:09 +00:00
|
|
|
$f = fopen( $this->fileCacheName(), 'w' );
|
2003-08-02 10:13:27 +00:00
|
|
|
if($f) {
|
|
|
|
|
$now = wfTimestampNow();
|
|
|
|
|
if( $this->useGzip() ) {
|
2004-06-08 23:56:09 +00:00
|
|
|
$rawtext = str_replace( '</html>',
|
|
|
|
|
'<!-- Cached/compressed '.$now." -->\n</html>",
|
2003-08-02 10:13:27 +00:00
|
|
|
$text );
|
|
|
|
|
$text = gzencode( $rawtext );
|
|
|
|
|
} else {
|
2004-06-08 23:56:09 +00:00
|
|
|
$text = str_replace( '</html>',
|
|
|
|
|
'<!-- Cached '.$now." -->\n</html>",
|
2003-08-02 10:13:27 +00:00
|
|
|
$text );
|
|
|
|
|
}
|
|
|
|
|
fwrite( $f, $text );
|
|
|
|
|
fclose( $f );
|
|
|
|
|
if( $this->useGzip() ) {
|
|
|
|
|
if( wfClientAcceptsGzip() ) {
|
2004-06-08 23:56:09 +00:00
|
|
|
header( 'Content-Encoding: gzip' );
|
2003-08-02 10:13:27 +00:00
|
|
|
return $text;
|
|
|
|
|
} else {
|
|
|
|
|
return $rawtext;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-28 14:19:39 +00:00
|
|
|
public static function clearFileCache( $title ) {
|
|
|
|
|
global $wgUseFileCache;
|
|
|
|
|
if( !$wgUseFileCache ) return false;
|
2009-01-02 16:27:05 +00:00
|
|
|
$fc = new self( $title, 'view' );
|
2008-12-28 14:19:39 +00:00
|
|
|
@unlink( $fc->fileCacheName() );
|
|
|
|
|
$fc = new self( $title, 'raw' );
|
|
|
|
|
@unlink( $fc->fileCacheName() );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2003-08-02 10:13:27 +00:00
|
|
|
}
|