same minor typo as in LocalSettings (in example config url)
[mediawiki.git] / includes / CacheManager.php
blobf5701e4e0be16db3f5ee6eaa5e80439115d1973f
1 <?
3 # Handles talking to the file cache, putting stuff in and taking it back out.
4 # Mostly called from Article.php, also from DatabaseFunctions.php for the
5 # emergency abort/fallback to cache.
7 # Global options that affect this module:
8 # $wgCachePages
9 # $wgCacheEpoch
10 # $wgUseFileCache
11 # $wgFileCacheDirectory
12 # $wgUseGzip
14 include_once( "Title.php" );
16 class CacheManager {
17 var $mTitle, $mFileCache;
19 function CacheManager( &$title ) {
20 $this->mTitle =& $title;
21 $this->mFileCache = "";
24 function fileCacheName() {
25 global $wgFileCacheDirectory, $wgLang;
26 if( !$this->mFileCache ) {
27 $hash = md5( $key = $this->mTitle->getDbkey() );
28 if( $this->mTitle->getNamespace() )
29 $key = $wgLang->getNsText( $this->mTitle->getNamespace() ) . ":" . $key;
30 $key = str_replace( ".", "%2E", urlencode( $key ) );
32 $hash1 = substr( $hash, 0, 1 );
33 $hash2 = substr( $hash, 0, 2 );
34 $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
36 if($this->useGzip())
37 $this->mFileCache .= ".gz";
39 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
41 return $this->mFileCache;
44 function isFileCached() {
45 return file_exists( $this->fileCacheName() );
48 function fileCacheTime() {
49 return wfUnix2Timestamp( filemtime( $this->fileCacheName() ) );
52 function isFileCacheGood( $timestamp ) {
53 global $wgCacheEpoch;
55 if( !$this->isFileCached() ) return false;
57 $cachetime = $this->fileCacheTime();
58 $good = (( $timestamp <= $cachetime ) &&
59 ( $wgCacheEpoch <= $cachetime ));
61 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
62 return $good;
65 function useGzip() {
66 global $wgUseGzip;
67 return $wgUseGzip;
70 /* In handy string packages */
71 function fetchRawText() {
72 return file_get_contents( $this->fileCacheName() );
75 function fetchPageText() {
76 if( $this->useGzip() ) {
77 /* Why is there no gzfile_get_contents() or gzdecode()? */
78 return implode( "", gzfile( $this->fileCacheName() ) );
79 } else {
80 return $this->fetchRawText();
84 /* Working directory to/from output */
85 function loadFromFileCache() {
86 global $wgOut;
87 wfDebug(" loadFromFileCache()\n");
89 $filename=$this->fileCacheName();
90 $wgOut->sendCacheControl();
92 if( $this->useGzip() ) {
93 if( wfClientAcceptsGzip() ) {
94 header( "Content-Encoding: gzip" );
95 } else {
96 /* Send uncompressed */
97 readgzfile( $filename );
98 return;
101 readfile( $filename );
104 function checkCacheDirs() {
105 $filename = $this->fileCacheName();
106 $mydir2=substr($filename,0,strrpos($filename,"/")); # subdirectory level 2
107 $mydir1=substr($mydir2,0,strrpos($mydir2,"/")); # subdirectory level 1
109 if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary
110 if(!file_exists($mydir2)) { mkdir($mydir2,0775); }
113 function saveToFileCache( $text ) {
114 if(strcmp($text,"") == 0) return "";
116 wfDebug(" saveToFileCache()\n", false);
118 $this->checkCacheDirs();
120 $f = fopen( $this->fileCacheName(), "w" );
121 if($f) {
122 $now = wfTimestampNow();
123 if( $this->useGzip() ) {
124 $rawtext = str_replace( "</html>",
125 "<!-- Cached/compressed $now -->\n</html>",
126 $text );
127 $text = gzencode( $rawtext );
128 } else {
129 $text = str_replace( "</html>",
130 "<!-- Cached $now -->\n</html>",
131 $text );
133 fwrite( $f, $text );
134 fclose( $f );
135 if( $this->useGzip() ) {
136 if( wfClientAcceptsGzip() ) {
137 header( "Content-Encoding: gzip" );
138 return $text;
139 } else {
140 return $rawtext;
142 } else {
143 return $text;
146 return $text;