Index: branches/stable/phase3/includes/CacheManager.php |
— | — | @@ -0,0 +1,151 @@ |
| 2 | +<? |
| 3 | + |
| 4 | +# Handles talking to the file cache, putting stuff in and taking it back out. |
| 5 | +# Mostly called from Article.php, also from DatabaseFunctions.php for the |
| 6 | +# emergency abort/fallback to cache. |
| 7 | + |
| 8 | +# Global options that affect this module: |
| 9 | +# $wgCachePages |
| 10 | +# $wgCacheEpoch |
| 11 | +# $wgUseFileCache |
| 12 | +# $wgFileCacheDirectory |
| 13 | +# $wgUseGzip |
| 14 | + |
| 15 | +include_once( "Title.php" ); |
| 16 | + |
| 17 | +class CacheManager { |
| 18 | + var $mTitle, $mFileCache; |
| 19 | + |
| 20 | + function CacheManager( &$title ) { |
| 21 | + $this->mTitle =& $title; |
| 22 | + $this->mFileCache = ""; |
| 23 | + } |
| 24 | + |
| 25 | + function fileCacheName() { |
| 26 | + global $wgFileCacheDirectory, $wgLang; |
| 27 | + if( !$this->mFileCache ) { |
| 28 | + $hash = md5( $key = $this->mTitle->getDbkey() ); |
| 29 | + if( $this->mTitle->getNamespace() ) |
| 30 | + $key = $wgLang->getNsText( $this->mTitle->getNamespace() ) . ":" . $key; |
| 31 | + $key = str_replace( ".", "%2E", urlencode( $key ) ); |
| 32 | + |
| 33 | + $hash1 = substr( $hash, 0, 1 ); |
| 34 | + $hash2 = substr( $hash, 0, 2 ); |
| 35 | + $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html"; |
| 36 | + |
| 37 | + if($this->useGzip()) |
| 38 | + $this->mFileCache .= ".gz"; |
| 39 | + |
| 40 | + wfDebug( " fileCacheName() - {$this->mFileCache}\n" ); |
| 41 | + } |
| 42 | + return $this->mFileCache; |
| 43 | + } |
| 44 | + |
| 45 | + function isFileCached() { |
| 46 | + return file_exists( $this->fileCacheName() ); |
| 47 | + } |
| 48 | + |
| 49 | + function fileCacheTime() { |
| 50 | + return wfUnix2Timestamp( filemtime( $this->fileCacheName() ) ); |
| 51 | + } |
| 52 | + |
| 53 | + function isFileCacheGood( $timestamp ) { |
| 54 | + global $wgCacheEpoch; |
| 55 | + |
| 56 | + if( !$this->isFileCached() ) return false; |
| 57 | + |
| 58 | + $cachetime = $this->fileCacheTime(); |
| 59 | + $good = (( $timestamp <= $cachetime ) && |
| 60 | + ( $wgCacheEpoch <= $cachetime )); |
| 61 | + |
| 62 | + wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n"); |
| 63 | + return $good; |
| 64 | + } |
| 65 | + |
| 66 | + function useGzip() { |
| 67 | + global $wgUseGzip; |
| 68 | + return $wgUseGzip; |
| 69 | + } |
| 70 | + |
| 71 | + /* In handy string packages */ |
| 72 | + function fetchRawText() { |
| 73 | + return file_get_contents( $this->fileCacheName() ); |
| 74 | + } |
| 75 | + |
| 76 | + function fetchPageText() { |
| 77 | + if( $this->useGzip() ) { |
| 78 | + /* Why is there no gzfile_get_contents() or gzdecode()? */ |
| 79 | + return implode( "", gzfile( $this->fileCacheName() ) ); |
| 80 | + } else { |
| 81 | + return $this->fetchRawText(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /* Working directory to/from output */ |
| 86 | + function loadFromFileCache() { |
| 87 | + global $wgOut; |
| 88 | + wfDebug(" loadFromFileCache()\n"); |
| 89 | + |
| 90 | + $filename=$this->fileCacheName(); |
| 91 | + $wgOut->sendCacheControl(); |
| 92 | + |
| 93 | + if( $this->useGzip() ) { |
| 94 | + if( wfClientAcceptsGzip() ) { |
| 95 | + header( "Content-Encoding: gzip" ); |
| 96 | + } else { |
| 97 | + /* Send uncompressed */ |
| 98 | + readgzfile( $filename ); |
| 99 | + return; |
| 100 | + } |
| 101 | + } |
| 102 | + readfile( $filename ); |
| 103 | + } |
| 104 | + |
| 105 | + function checkCacheDirs() { |
| 106 | + $filename = $this->fileCacheName(); |
| 107 | + $mydir2=substr($filename,0,strrpos($filename,"/")); # subdirectory level 2 |
| 108 | + $mydir1=substr($mydir2,0,strrpos($mydir2,"/")); # subdirectory level 1 |
| 109 | + |
| 110 | + if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary |
| 111 | + if(!file_exists($mydir2)) { mkdir($mydir2,0775); } |
| 112 | + } |
| 113 | + |
| 114 | + function saveToFileCache( $text ) { |
| 115 | + if(strcmp($text,"") == 0) return ""; |
| 116 | + |
| 117 | + wfDebug(" saveToFileCache()\n", false); |
| 118 | + |
| 119 | + $this->checkCacheDirs(); |
| 120 | + |
| 121 | + $f = fopen( $this->fileCacheName(), "w" ); |
| 122 | + if($f) { |
| 123 | + $now = wfTimestampNow(); |
| 124 | + if( $this->useGzip() ) { |
| 125 | + $rawtext = str_replace( "</html>", |
| 126 | + "<!-- Cached/compressed $now -->\n</html>", |
| 127 | + $text ); |
| 128 | + $text = gzencode( $rawtext ); |
| 129 | + } else { |
| 130 | + $text = str_replace( "</html>", |
| 131 | + "<!-- Cached $now -->\n</html>", |
| 132 | + $text ); |
| 133 | + } |
| 134 | + fwrite( $f, $text ); |
| 135 | + fclose( $f ); |
| 136 | + if( $this->useGzip() ) { |
| 137 | + if( wfClientAcceptsGzip() ) { |
| 138 | + header( "Content-Encoding: gzip" ); |
| 139 | + return $text; |
| 140 | + } else { |
| 141 | + return $rawtext; |
| 142 | + } |
| 143 | + } else { |
| 144 | + return $text; |
| 145 | + } |
| 146 | + } |
| 147 | + return $text; |
| 148 | + } |
| 149 | + |
| 150 | +} |
| 151 | + |
| 152 | +?> |
Property changes on: branches/stable/phase3/includes/CacheManager.php |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 153 | + native |
Name: svn:keywords |
2 | 154 | + Author Date Id Revision |