translation update
[dokuwiki.git] / inc / cache.php
blob5eac949345808328231dca38938410a9358c468d
1 <?php
2 /**
3 * Generic class to handle caching
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Chris Smith <chris@jalakai.co.uk>
7 */
9 if(!defined('DOKU_INC')) die('meh.');
11 class cache {
12 var $key = ''; // primary identifier for this item
13 var $ext = ''; // file ext for cache data, secondary identifier for this item
14 var $cache = ''; // cache file name
15 var $depends = array(); // array containing cache dependency information,
16 // used by _useCache to determine cache validity
18 var $_event = ''; // event to be triggered during useCache
20 function cache($key,$ext) {
21 $this->key = $key;
22 $this->ext = $ext;
23 $this->cache = getCacheName($key,$ext);
26 /**
27 * public method to determine whether the cache can be used
29 * to assist in cetralisation of event triggering and calculation of cache statistics,
30 * don't override this function override _useCache()
32 * @param array $depends array of cache dependencies, support dependecies:
33 * 'age' => max age of the cache in seconds
34 * 'files' => cache must be younger than mtime of each file
35 * (nb. dependency passes if file doesn't exist)
37 * @return bool true if cache can be used, false otherwise
39 function useCache($depends=array()) {
40 $this->depends = $depends;
41 $this->_addDependencies();
43 if ($this->_event) {
44 return $this->_stats(trigger_event($this->_event,$this,array($this,'_useCache')));
45 } else {
46 return $this->_stats($this->_useCache());
50 /**
51 * private method containing cache use decision logic
53 * this function processes the following keys in the depends array
54 * purge - force a purge on any non empty value
55 * age - expire cache if older than age (seconds)
56 * files - expire cache if any file in this array was updated more recently than the cache
58 * can be overridden
60 * @return bool see useCache()
62 function _useCache() {
64 if (!empty($this->depends['purge'])) return false; // purge requested?
65 if (!($this->_time = @filemtime($this->cache))) return false; // cache exists?
67 // cache too old?
68 if (!empty($this->depends['age']) && ((time() - $this->_time) > $this->depends['age'])) return false;
70 if (!empty($this->depends['files'])) {
71 foreach ($this->depends['files'] as $file) {
72 if ($this->_time <= @filemtime($file)) return false; // cache older than files it depends on?
76 return true;
79 /**
80 * add dependencies to the depends array
82 * this method should only add dependencies,
83 * it should not remove any existing dependencies and
84 * it should only overwrite a dependency when the new value is more stringent than the old
86 function _addDependencies() {
87 global $INPUT;
88 if ($INPUT->has('purge')) $this->depends['purge'] = true; // purge requested
91 /**
92 * retrieve the cached data
94 * @param bool $clean true to clean line endings, false to leave line endings alone
95 * @return string cache contents
97 function retrieveCache($clean=true) {
98 return io_readFile($this->cache, $clean);
102 * cache $data
104 * @param string $data the data to be cached
105 * @return bool true on success, false otherwise
107 function storeCache($data) {
108 return io_savefile($this->cache, $data);
112 * remove any cached data associated with this cache instance
114 function removeCache() {
115 @unlink($this->cache);
119 * Record cache hits statistics.
120 * (Only when debugging allowed, to reduce overhead.)
122 * @param bool $success result of this cache use attempt
123 * @return bool pass-thru $success value
125 function _stats($success) {
126 global $conf;
127 static $stats = null;
128 static $file;
130 if (!$conf['allowdebug']) { return $success; }
132 if (is_null($stats)) {
133 $file = $conf['cachedir'].'/cache_stats.txt';
134 $lines = explode("\n",io_readFile($file));
136 foreach ($lines as $line) {
137 $i = strpos($line,',');
138 $stats[substr($line,0,$i)] = $line;
142 if (isset($stats[$this->ext])) {
143 list($ext,$count,$hits) = explode(',',$stats[$this->ext]);
144 } else {
145 $ext = $this->ext;
146 $count = 0;
147 $hits = 0;
150 $count++;
151 if ($success) $hits++;
152 $stats[$this->ext] = "$ext,$count,$hits";
154 io_saveFile($file,join("\n",$stats));
156 return $success;
160 class cache_parser extends cache {
162 var $file = ''; // source file for cache
163 var $mode = ''; // input mode (represents the processing the input file will undergo)
165 var $_event = 'PARSER_CACHE_USE';
167 function cache_parser($id, $file, $mode) {
168 if ($id) $this->page = $id;
169 $this->file = $file;
170 $this->mode = $mode;
172 parent::cache($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.'.$mode);
175 function _useCache() {
177 if (!@file_exists($this->file)) return false; // source exists?
178 return parent::_useCache();
181 function _addDependencies() {
182 global $conf, $config_cascade;
184 $this->depends['age'] = isset($this->depends['age']) ?
185 min($this->depends['age'],$conf['cachetime']) : $conf['cachetime'];
187 // parser cache file dependencies ...
188 $files = array($this->file, // ... source
189 DOKU_INC.'inc/parser/parser.php', // ... parser
190 DOKU_INC.'inc/parser/handler.php', // ... handler
192 $files = array_merge($files, getConfigFiles('main')); // ... wiki settings
194 $this->depends['files'] = !empty($this->depends['files']) ? array_merge($files, $this->depends['files']) : $files;
195 parent::_addDependencies();
200 class cache_renderer extends cache_parser {
201 function _useCache() {
202 global $conf;
204 if (!parent::_useCache()) return false;
206 if (!isset($this->page)) {
207 return true;
210 if ($this->_time < @filemtime(metaFN($this->page,'.meta'))) return false; // meta cache older than file it depends on?
212 // check current link existence is consistent with cache version
213 // first check the purgefile
214 // - if the cache is more recent than the purgefile we know no links can have been updated
215 if ($this->_time >= @filemtime($conf['cachedir'].'/purgefile')) {
216 return true;
219 // for wiki pages, check metadata dependencies
220 $metadata = p_get_metadata($this->page);
222 if (!isset($metadata['relation']['references']) ||
223 empty($metadata['relation']['references'])) {
224 return true;
227 foreach ($metadata['relation']['references'] as $id => $exists) {
228 if ($exists != page_exists($id,'',false)) return false;
231 return true;
234 function _addDependencies() {
236 // renderer cache file dependencies ...
237 $files = array(
238 DOKU_INC.'inc/parser/'.$this->mode.'.php', // ... the renderer
241 // page implies metadata and possibly some other dependencies
242 if (isset($this->page)) {
244 $valid = p_get_metadata($this->page, 'date valid'); // for xhtml this will render the metadata if needed
245 if (!empty($valid['age'])) {
246 $this->depends['age'] = isset($this->depends['age']) ?
247 min($this->depends['age'],$valid['age']) : $valid['age'];
251 $this->depends['files'] = !empty($this->depends['files']) ? array_merge($files, $this->depends['files']) : $files;
252 parent::_addDependencies();
256 class cache_instructions extends cache_parser {
258 function cache_instructions($id, $file) {
259 parent::cache_parser($id, $file, 'i');
262 function retrieveCache($clean=true) {
263 $contents = io_readFile($this->cache, false);
264 return !empty($contents) ? unserialize($contents) : array();
267 function storeCache($instructions) {
268 return io_savefile($this->cache,serialize($instructions));