Slight cache tweaks; fixes gzip encoding header for newly recached pages
[mediawiki.git] / includes / CacheManager.php
blob225cacef0efbefe397ce3c9de4f3229f80245833
1 <?
3 include_once( "Title.php" );
5 class CacheManager {
6 var $mTitle, $mFileCache;
8 function CacheManager( &$title ) {
9 $this->mTitle =& $title;
10 $this->mFileCache = "";
13 function fileCacheName() {
14 global $wgFileCacheDirectory, $wgLang;
15 if( !$this->mFileCache ) {
16 $hash = md5( $key = $this->mTitle->getDbkey() );
17 if( $this->mTitle->getNamespace() )
18 $key = $wgLang->getNsText( $this->mTitle->getNamespace() ) . ":" . $key;
19 $key = str_replace( ".", "%2E", urlencode( $key ) );
21 $hash1 = substr( $hash, 0, 1 );
22 $hash2 = substr( $hash, 0, 2 );
23 $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
25 if($this->useGzip())
26 $this->mFileCache .= ".gz";
28 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
30 return $this->mFileCache;
33 function isFileCached() {
34 return file_exists( $this->fileCacheName() );
37 function fileCacheTime() {
38 return wfUnix2Timestamp( filemtime( $this->fileCacheName() ) );
41 function isFileCacheGood( $timestamp ) {
42 global $wgCacheEpoch;
44 if( !$this->isFileCached() ) return false;
46 $cachetime = $this->fileCacheTime();
47 $good = (( $timestamp <= $cachetime ) &&
48 ( $wgCacheEpoch <= $cachetime ));
50 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
51 return $good;
54 function useGzip() {
55 global $wgUseGzip;
56 return $wgUseGzip;
59 /* In handy string packages */
60 function fetchRawText() {
61 return file_get_contents( $this->fileCacheName() );
64 function fetchPageText() {
65 if( $this->useGzip() ) {
66 /* Why is there no gzfile_get_contents() or gzdecode()? */
67 return implode( "", gzfile( $this->fileCacheName() ) );
68 } else {
69 return $this->fetchRawText();
73 /* Working directory to/from output */
74 function loadFromFileCache() {
75 global $wgOut;
76 wfDebug(" loadFromFileCache()\n");
78 $filename=$this->fileCacheName();
79 $wgOut->sendCacheControl();
81 if( $this->useGzip() ) {
82 if( wfClientAcceptsGzip() ) {
83 header( "Content-Encoding: gzip" );
84 } else {
85 /* Send uncompressed */
86 readgzfile( $filename );
87 return;
90 readfile( $filename );
93 function checkCacheDirs() {
94 $filename = $this->fileCacheName();
95 $mydir2=substr($filename,0,strrpos($filename,"/")); # subdirectory level 2
96 $mydir1=substr($mydir2,0,strrpos($mydir2,"/")); # subdirectory level 1
98 if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary
99 if(!file_exists($mydir2)) { mkdir($mydir2,0775); }
102 function saveToFileCache( $text ) {
103 if(strcmp($text,"") == 0) return "";
105 wfDebug(" saveToFileCache()\n", false);
107 $this->checkCacheDirs();
109 $f = fopen( $this->fileCacheName(), "w" );
110 if($f) {
111 $now = wfTimestampNow();
112 if( $this->useGzip() ) {
113 $rawtext = str_replace( "</html>",
114 "<!-- Cached/compressed $now -->\n</html>",
115 $text );
116 $text = gzencode( $rawtext );
117 } else {
118 $text = str_replace( "</html>",
119 "<!-- Cached $now -->\n</html>",
120 $text );
122 fwrite( $f, $text );
123 fclose( $f );
124 if( $this->useGzip() ) {
125 if( wfClientAcceptsGzip() ) {
126 header( "Content-Encoding: gzip" );
127 return $text;
128 } else {
129 return $rawtext;
131 } else {
132 return $text;
135 return $text;