Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

CacheManager.php

Go to the documentation of this file.
00001 <?php 00002 00003 # Handles talking to the file cache, putting stuff in and taking it back out. 00004 # Mostly called from Article.php, also from DatabaseFunctions.php for the 00005 # emergency abort/fallback to cache. 00006 00007 # Global options that affect this module: 00008 # $wgCachePages 00009 # $wgCacheEpoch 00010 # $wgUseFileCache 00011 # $wgFileCacheDirectory 00012 # $wgUseGzip 00013 00014 require_once( 'Title.php' ); 00015 00016 class CacheManager { 00017 var $mTitle, $mFileCache; 00018 00019 function CacheManager( &$title ) { 00020 $this->mTitle =& $title; 00021 $this->mFileCache = ''; 00022 } 00023 00024 function fileCacheName() { 00025 global $wgFileCacheDirectory, $wgLang; 00026 if( !$this->mFileCache ) { 00027 $hash = md5( $key = $this->mTitle->getDbkey() ); 00028 if( $this->mTitle->getNamespace() ) 00029 $key = $wgLang->getNsText( $this->mTitle->getNamespace() ) . ":" . $key; 00030 $key = str_replace( '.', '%2E', urlencode( $key ) ); 00031 00032 $hash1 = substr( $hash, 0, 1 ); 00033 $hash2 = substr( $hash, 0, 2 ); 00034 $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html"; 00035 00036 if($this->useGzip()) 00037 $this->mFileCache .= '.gz'; 00038 00039 wfDebug( " fileCacheName() - {$this->mFileCache}\n" ); 00040 } 00041 return $this->mFileCache; 00042 } 00043 00044 function isFileCached() { 00045 return file_exists( $this->fileCacheName() ); 00046 } 00047 00048 function fileCacheTime() { 00049 return wfUnix2Timestamp( filemtime( $this->fileCacheName() ) ); 00050 } 00051 00052 function isFileCacheGood( $timestamp ) { 00053 global $wgCacheEpoch; 00054 00055 if( !$this->isFileCached() ) return false; 00056 00057 $cachetime = $this->fileCacheTime(); 00058 $good = (( $timestamp <= $cachetime ) && 00059 ( $wgCacheEpoch <= $cachetime )); 00060 00061 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n"); 00062 return $good; 00063 } 00064 00065 function useGzip() { 00066 global $wgUseGzip; 00067 return $wgUseGzip; 00068 } 00069 00070 /* In handy string packages */ 00071 function fetchRawText() { 00072 return file_get_contents( $this->fileCacheName() ); 00073 } 00074 00075 function fetchPageText() { 00076 if( $this->useGzip() ) { 00077 /* Why is there no gzfile_get_contents() or gzdecode()? */ 00078 return implode( '', gzfile( $this->fileCacheName() ) ); 00079 } else { 00080 return $this->fetchRawText(); 00081 } 00082 } 00083 00084 /* Working directory to/from output */ 00085 function loadFromFileCache() { 00086 global $wgOut; 00087 wfDebug(" loadFromFileCache()\n"); 00088 00089 $filename=$this->fileCacheName(); 00090 $wgOut->sendCacheControl(); 00091 00092 if( $this->useGzip() ) { 00093 if( wfClientAcceptsGzip() ) { 00094 header( 'Content-Encoding: gzip' ); 00095 } else { 00096 /* Send uncompressed */ 00097 readgzfile( $filename ); 00098 return; 00099 } 00100 } 00101 readfile( $filename ); 00102 } 00103 00104 function checkCacheDirs() { 00105 $filename = $this->fileCacheName(); 00106 $mydir2=substr($filename,0,strrpos($filename,'/')); # subdirectory level 2 00107 $mydir1=substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1 00108 00109 if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary 00110 if(!file_exists($mydir2)) { mkdir($mydir2,0775); } 00111 } 00112 00113 function saveToFileCache( $text ) { 00114 if(strcmp($text,'') == 0) return ''; 00115 00116 wfDebug(" saveToFileCache()\n", false); 00117 00118 $this->checkCacheDirs(); 00119 00120 $f = fopen( $this->fileCacheName(), 'w' ); 00121 if($f) { 00122 $now = wfTimestampNow(); 00123 if( $this->useGzip() ) { 00124 $rawtext = str_replace( '</html>', 00125 '<!-- Cached/compressed '.$now." -->\n</html>", 00126 $text ); 00127 $text = gzencode( $rawtext ); 00128 } else { 00129 $text = str_replace( '</html>', 00130 '<!-- Cached '.$now." -->\n</html>", 00131 $text ); 00132 } 00133 fwrite( $f, $text ); 00134 fclose( $f ); 00135 if( $this->useGzip() ) { 00136 if( wfClientAcceptsGzip() ) { 00137 header( 'Content-Encoding: gzip' ); 00138 return $text; 00139 } else { 00140 return $rawtext; 00141 } 00142 } else { 00143 return $text; 00144 } 00145 } 00146 return $text; 00147 } 00148 00149 } 00150 00151 ?>

Generated on Tue Jun 29 23:40:03 2004 for Mediawiki by doxygen 1.3.7