14b5f4f4eSchris<?php 24b5f4f4eSchris/** 34b5f4f4eSchris * Generic class to handle caching 44b5f4f4eSchris * 54b5f4f4eSchris * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 64b5f4f4eSchris * @author Chris Smith <chris@jalakai.co.uk> 74b5f4f4eSchris */ 84b5f4f4eSchris 94b5f4f4eSchrisif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 104b5f4f4eSchris 114b5f4f4eSchrisrequire_once(DOKU_INC.'inc/io.php'); 124b5f4f4eSchrisrequire_once(DOKU_INC.'inc/pageutils.php'); 134b5f4f4eSchrisrequire_once(DOKU_INC.'inc/parserutils.php'); 144b5f4f4eSchris 154b5f4f4eSchrisclass cache { 164b5f4f4eSchris var $key = ''; // primary identifier for this item 174b5f4f4eSchris var $ext = ''; // file ext for cache data, secondary identifier for this item 184b5f4f4eSchris var $cache = ''; // cache file name 19*0abe1d3eSchris var $depends = array(); // array containing cache dependency information, 20*0abe1d3eSchris // used by _useCache to determine cache validity 214b5f4f4eSchris 224b5f4f4eSchris var $_event = ''; // event to be triggered during useCache 234b5f4f4eSchris 244b5f4f4eSchris function cache($key,$ext) { 254b5f4f4eSchris $this->key = $key; 264b5f4f4eSchris $this->ext = $ext; 274b5f4f4eSchris $this->cache = getCacheName($key,$ext); 284b5f4f4eSchris } 294b5f4f4eSchris 304b5f4f4eSchris /** 314b5f4f4eSchris * public method to determine whether the cache can be used 324b5f4f4eSchris * 334b5f4f4eSchris * to assist in cetralisation of event triggering and calculation of cache statistics, 344b5f4f4eSchris * don't override this function override _useCache() 354b5f4f4eSchris * 364b5f4f4eSchris * @param array $depends array of cache dependencies, support dependecies: 374b5f4f4eSchris * 'age' => max age of the cache in seconds 384b5f4f4eSchris * 'files' => cache must be younger than mtime of each file 394b5f4f4eSchris * 404b5f4f4eSchris * @return bool true if cache can be used, false otherwise 414b5f4f4eSchris */ 424b5f4f4eSchris function useCache($depends=array()) { 434b5f4f4eSchris $this->depends = $depends; 44*0abe1d3eSchris $this->_addDependencies(); 454b5f4f4eSchris 464b5f4f4eSchris if ($this->_event) { 474b5f4f4eSchris return $this->_stats(trigger_event($this->_event,$this,array($this,'_useCache'))); 484b5f4f4eSchris } else { 494b5f4f4eSchris return $this->_stats($this->_useCache()); 504b5f4f4eSchris } 514b5f4f4eSchris } 524b5f4f4eSchris 53*0abe1d3eSchris /** 544b5f4f4eSchris * private method containing cache use decision logic 554b5f4f4eSchris * 56*0abe1d3eSchris * this function processes the following keys in the depends array 57*0abe1d3eSchris * purge - force a purge on any non empty value 58*0abe1d3eSchris * age - expire cache if older than age (seconds) 59*0abe1d3eSchris * files - expire cache if any file in this array was updated more recently than the cache 60*0abe1d3eSchris * 61*0abe1d3eSchris * can be overridden 624b5f4f4eSchris * 634b5f4f4eSchris * @return bool see useCache() 644b5f4f4eSchris */ 654b5f4f4eSchris function _useCache() { 664b5f4f4eSchris 67*0abe1d3eSchris if (!empty($this->depends['purge'])) return false; // purge requested? 684b5f4f4eSchris if (!($this->_time = @filemtime($this->cache))) return false; // cache exists? 694b5f4f4eSchris 704b5f4f4eSchris // cache too old? 714b5f4f4eSchris if (!empty($this->depends['age']) && ((time() - $this->_time) > $this->depends['age'])) return false; 724b5f4f4eSchris 734b5f4f4eSchris if (!empty($this->depends['files'])) { 744b5f4f4eSchris foreach ($this->depends['files'] as $file) { 754b5f4f4eSchris if ($this->_time < @filemtime($file)) return false; // cache older than files it depends on? 764b5f4f4eSchris } 774b5f4f4eSchris } 784b5f4f4eSchris 794b5f4f4eSchris return true; 804b5f4f4eSchris } 814b5f4f4eSchris 824b5f4f4eSchris /** 83*0abe1d3eSchris * add dependencies to the depends array 84*0abe1d3eSchris * 85*0abe1d3eSchris * this method should only add dependencies, 86*0abe1d3eSchris * it should not remove any existing dependencies and 87*0abe1d3eSchris * it should only overwrite a dependency when the new value is more stringent than the old 88*0abe1d3eSchris */ 89*0abe1d3eSchris function _addDependencies() { 90*0abe1d3eSchris if (isset($_REQUEST['purge'])) $this->depends['purge'] = true; // purge requested 91*0abe1d3eSchris } 92*0abe1d3eSchris 93*0abe1d3eSchris /** 944b5f4f4eSchris * retrieve the cached data 954b5f4f4eSchris * 964b5f4f4eSchris * @param bool $clean true to clean line endings, false to leave line endings alone 974b5f4f4eSchris * @return string cache contents 984b5f4f4eSchris */ 994b5f4f4eSchris function retrieveCache($clean=true) { 1004b5f4f4eSchris return io_readFile($this->cache, $clean); 1014b5f4f4eSchris } 1024b5f4f4eSchris 1034b5f4f4eSchris /** 1044b5f4f4eSchris * cache $data 1054b5f4f4eSchris * 1064b5f4f4eSchris * @param string $data the data to be cached 1074b5f4f4eSchris * @return none 1084b5f4f4eSchris */ 1094b5f4f4eSchris function storeCache($data) { 1104b5f4f4eSchris io_savefile($this->cache, $data); 1114b5f4f4eSchris } 1124b5f4f4eSchris 1134b5f4f4eSchris /** 1144b5f4f4eSchris * remove any cached data associated with this cache instance 1154b5f4f4eSchris */ 1164b5f4f4eSchris function removeCache() { 1174b5f4f4eSchris @unlink($this->cache); 1184b5f4f4eSchris } 1194b5f4f4eSchris 1204b5f4f4eSchris /** 1214b5f4f4eSchris * record cache hits statistics 1224b5f4f4eSchris * 1234b5f4f4eSchris * @param bool $success result of this cache use attempt 1244b5f4f4eSchris * @return bool pass-thru $success value 1254b5f4f4eSchris */ 1264b5f4f4eSchris function _stats($success) { 1274b5f4f4eSchris global $conf; 1284b5f4f4eSchris static $stats = NULL; 1294b5f4f4eSchris static $file; 1304b5f4f4eSchris 1314b5f4f4eSchris if (is_null($stats)) { 1324b5f4f4eSchris $file = $conf['cachedir'].'/cache_stats.txt'; 1334b5f4f4eSchris $lines = explode("\n",io_readFile($file)); 1344b5f4f4eSchris 1354b5f4f4eSchris foreach ($lines as $line) { 1364b5f4f4eSchris $i = strpos($line,','); 1374b5f4f4eSchris $stats[substr($line,0,$i)] = $line; 1384b5f4f4eSchris } 1394b5f4f4eSchris } 1404b5f4f4eSchris 1414b5f4f4eSchris if (isset($stats[$this->ext])) { 142*0abe1d3eSchris list($ext,$count,$hits) = explode(',',$stats[$this->ext]); 1434b5f4f4eSchris } else { 1444b5f4f4eSchris $ext = $this->ext; 1454b5f4f4eSchris $count = 0; 146*0abe1d3eSchris $hits = 0; 1474b5f4f4eSchris } 1484b5f4f4eSchris 1494b5f4f4eSchris $count++; 150*0abe1d3eSchris if ($success) $hits++; 151*0abe1d3eSchris $stats[$this->ext] = "$ext,$count,$hits"; 1524b5f4f4eSchris 1534b5f4f4eSchris io_saveFile($file,join("\n",$stats)); 1544b5f4f4eSchris 1554b5f4f4eSchris return $success; 1564b5f4f4eSchris } 1574b5f4f4eSchris} 1584b5f4f4eSchris 1594b5f4f4eSchrisclass cache_parser extends cache { 1604b5f4f4eSchris 1614b5f4f4eSchris var $file = ''; // source file for cache 1624b5f4f4eSchris var $mode = ''; // input mode (represents the processing the input file will undergo) 1634b5f4f4eSchris 1644b5f4f4eSchris var $_event = 'PARSER_CACHE_USE'; 1654b5f4f4eSchris 1664b5f4f4eSchris function cache_parser($id, $file, $mode) { 1674b5f4f4eSchris if ($id) $this->page = $id; 1684b5f4f4eSchris $this->file = $file; 1694b5f4f4eSchris $this->mode = $mode; 1704b5f4f4eSchris 1714b5f4f4eSchris parent::cache($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.'.$mode); 1724b5f4f4eSchris } 1734b5f4f4eSchris 1744b5f4f4eSchris function _useCache() { 1754b5f4f4eSchris 1764b5f4f4eSchris if (!@file_exists($this->file)) return false; // source exists? 177*0abe1d3eSchris return parent::_useCache(); 178*0abe1d3eSchris } 1794b5f4f4eSchris 180*0abe1d3eSchris function _addDependencies() { 181*0abe1d3eSchris 182*0abe1d3eSchris $this->depends['age'] = isset($this->depends['age']) ? 183*0abe1d3eSchris min($this->depends['age'],$conf['cachetime']) : $conf['cachetime']; 1844b5f4f4eSchris 1854b5f4f4eSchris // parser cache file dependencies ... 1864b5f4f4eSchris $files = array($this->file, // ... source 1874b5f4f4eSchris DOKU_CONF.'dokuwiki.php', // ... config 1884b5f4f4eSchris DOKU_CONF.'local.php', // ... local config 1894b5f4f4eSchris DOKU_INC.'inc/parser/parser.php', // ... parser 1904b5f4f4eSchris DOKU_INC.'inc/parser/handler.php', // ... handler 1914b5f4f4eSchris ); 1924b5f4f4eSchris 1934b5f4f4eSchris $this->depends['files'] = !empty($this->depends['files']) ? array_merge($files, $this->depends['files']) : $files; 194*0abe1d3eSchris parent::_addDependencies(); 1954b5f4f4eSchris } 1964b5f4f4eSchris 1974b5f4f4eSchris} 1984b5f4f4eSchris 1994b5f4f4eSchrisclass cache_renderer extends cache_parser { 2004b5f4f4eSchris 2014b5f4f4eSchris function _useCache() { 2024b5f4f4eSchris 203*0abe1d3eSchris if (!parent::_useCache()) return false; 2044b5f4f4eSchris 2054b5f4f4eSchris // for wiki pages, check for internal link status changes 2064b5f4f4eSchris if (isset($this->page)) { 207*0abe1d3eSchris 208*0abe1d3eSchris // check the purgefile 209*0abe1d3eSchris // - if the cache is more recent that the purgefile we know no links can have been updated 210*0abe1d3eSchris if ($this->_time < @filemtime($conf['cachedir'].'/purgefile')) { 211*0abe1d3eSchris 2124b5f4f4eSchris $links = p_get_metadata($this->page,"relation references"); 2134b5f4f4eSchris 2144b5f4f4eSchris if (!empty($links)) { 2154b5f4f4eSchris foreach ($links as $id => $exists) { 2164b5f4f4eSchris if ($exists != @file_exists(wikiFN($id,'',false))) return false; 2174b5f4f4eSchris } 2184b5f4f4eSchris } 2194b5f4f4eSchris } 220*0abe1d3eSchris } 2214b5f4f4eSchris 2224b5f4f4eSchris return true; 2234b5f4f4eSchris } 224*0abe1d3eSchris 225*0abe1d3eSchris function _addDependencies() { 226*0abe1d3eSchris 227*0abe1d3eSchris // renderer cache file dependencies ... 228*0abe1d3eSchris $files = array( 229*0abe1d3eSchris DOKU_INC.'inc/parser/'.$this->mode.'.php', // ... the renderer 230*0abe1d3eSchris ); 231*0abe1d3eSchris 232*0abe1d3eSchris if (isset($this->page)) { $files[] = metaFN($this->page,'.meta'); } // ... the page's own metadata 233*0abe1d3eSchris 234*0abe1d3eSchris $this->depends['files'] = !empty($this->depends['files']) ? array_merge($files, $this->depends['files']) : $files; 235*0abe1d3eSchris parent::_addDependencies(); 236*0abe1d3eSchris } 2374b5f4f4eSchris} 2384b5f4f4eSchris 2394b5f4f4eSchrisclass cache_instructions extends cache_parser { 2404b5f4f4eSchris 2414b5f4f4eSchris function cache_instructions($id, $file) { 2424b5f4f4eSchris parent::cache_parser($id, $file, 'i'); 2434b5f4f4eSchris } 2444b5f4f4eSchris 2454b5f4f4eSchris function retrieveCache() { 2464b5f4f4eSchris $contents = io_readFile($this->cache, false); 2474b5f4f4eSchris return !empty($contents) ? unserialize($contents) : array(); 2484b5f4f4eSchris } 2494b5f4f4eSchris 2504b5f4f4eSchris function storeCache($instructions) { 2514b5f4f4eSchris io_savefile($this->cache,serialize($instructions)); 2524b5f4f4eSchris } 2534b5f4f4eSchris} 254