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 9fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 104b5f4f4eSchris 114b5f4f4eSchrisclass cache { 12*c59b3e00SGerrit Uitslag public $key = ''; // primary identifier for this item 13*c59b3e00SGerrit Uitslag public $ext = ''; // file ext for cache data, secondary identifier for this item 14*c59b3e00SGerrit Uitslag public $cache = ''; // cache file name 15*c59b3e00SGerrit Uitslag public $depends = array(); // array containing cache dependency information, 160abe1d3eSchris // used by _useCache to determine cache validity 174b5f4f4eSchris 184b5f4f4eSchris var $_event = ''; // event to be triggered during useCache 19d7fd4c3eSGerrit Uitslag var $_time; 204b5f4f4eSchris 21*c59b3e00SGerrit Uitslag /** 22*c59b3e00SGerrit Uitslag * @param string $key primary identifier 23*c59b3e00SGerrit Uitslag * @param string $ext file extension 24*c59b3e00SGerrit Uitslag */ 25*c59b3e00SGerrit Uitslag public function cache($key,$ext) { 264b5f4f4eSchris $this->key = $key; 274b5f4f4eSchris $this->ext = $ext; 284b5f4f4eSchris $this->cache = getCacheName($key,$ext); 294b5f4f4eSchris } 304b5f4f4eSchris 314b5f4f4eSchris /** 324b5f4f4eSchris * public method to determine whether the cache can be used 334b5f4f4eSchris * 344b5f4f4eSchris * to assist in cetralisation of event triggering and calculation of cache statistics, 354b5f4f4eSchris * don't override this function override _useCache() 364b5f4f4eSchris * 374b5f4f4eSchris * @param array $depends array of cache dependencies, support dependecies: 384b5f4f4eSchris * 'age' => max age of the cache in seconds 394b5f4f4eSchris * 'files' => cache must be younger than mtime of each file 40ce6b63d9Schris * (nb. dependency passes if file doesn't exist) 414b5f4f4eSchris * 424b5f4f4eSchris * @return bool true if cache can be used, false otherwise 434b5f4f4eSchris */ 44*c59b3e00SGerrit Uitslag public function useCache($depends=array()) { 454b5f4f4eSchris $this->depends = $depends; 460abe1d3eSchris $this->_addDependencies(); 474b5f4f4eSchris 484b5f4f4eSchris if ($this->_event) { 494b5f4f4eSchris return $this->_stats(trigger_event($this->_event,$this,array($this,'_useCache'))); 504b5f4f4eSchris } else { 514b5f4f4eSchris return $this->_stats($this->_useCache()); 524b5f4f4eSchris } 534b5f4f4eSchris } 544b5f4f4eSchris 550abe1d3eSchris /** 564b5f4f4eSchris * private method containing cache use decision logic 574b5f4f4eSchris * 580abe1d3eSchris * this function processes the following keys in the depends array 590abe1d3eSchris * purge - force a purge on any non empty value 600abe1d3eSchris * age - expire cache if older than age (seconds) 610abe1d3eSchris * files - expire cache if any file in this array was updated more recently than the cache 620abe1d3eSchris * 630abe1d3eSchris * can be overridden 644b5f4f4eSchris * 654b5f4f4eSchris * @return bool see useCache() 664b5f4f4eSchris */ 67*c59b3e00SGerrit Uitslag protected function _useCache() { 684b5f4f4eSchris 690abe1d3eSchris if (!empty($this->depends['purge'])) return false; // purge requested? 704b5f4f4eSchris if (!($this->_time = @filemtime($this->cache))) return false; // cache exists? 714b5f4f4eSchris 724b5f4f4eSchris // cache too old? 734b5f4f4eSchris if (!empty($this->depends['age']) && ((time() - $this->_time) > $this->depends['age'])) return false; 744b5f4f4eSchris 754b5f4f4eSchris if (!empty($this->depends['files'])) { 764b5f4f4eSchris foreach ($this->depends['files'] as $file) { 77a257b0bdSMartin Doucha if ($this->_time <= @filemtime($file)) return false; // cache older than files it depends on? 784b5f4f4eSchris } 794b5f4f4eSchris } 804b5f4f4eSchris 814b5f4f4eSchris return true; 824b5f4f4eSchris } 834b5f4f4eSchris 844b5f4f4eSchris /** 850abe1d3eSchris * add dependencies to the depends array 860abe1d3eSchris * 870abe1d3eSchris * this method should only add dependencies, 880abe1d3eSchris * it should not remove any existing dependencies and 890abe1d3eSchris * it should only overwrite a dependency when the new value is more stringent than the old 900abe1d3eSchris */ 91*c59b3e00SGerrit Uitslag protected function _addDependencies() { 927d01a0eaSTom N Harris global $INPUT; 937d01a0eaSTom N Harris if ($INPUT->has('purge')) $this->depends['purge'] = true; // purge requested 940abe1d3eSchris } 950abe1d3eSchris 960abe1d3eSchris /** 974b5f4f4eSchris * retrieve the cached data 984b5f4f4eSchris * 994b5f4f4eSchris * @param bool $clean true to clean line endings, false to leave line endings alone 1004b5f4f4eSchris * @return string cache contents 1014b5f4f4eSchris */ 102*c59b3e00SGerrit Uitslag public function retrieveCache($clean=true) { 1034b5f4f4eSchris return io_readFile($this->cache, $clean); 1044b5f4f4eSchris } 1054b5f4f4eSchris 1064b5f4f4eSchris /** 1074b5f4f4eSchris * cache $data 1084b5f4f4eSchris * 1094b5f4f4eSchris * @param string $data the data to be cached 110cbaf4259SChris Smith * @return bool true on success, false otherwise 1114b5f4f4eSchris */ 112*c59b3e00SGerrit Uitslag public function storeCache($data) { 113cbaf4259SChris Smith return io_savefile($this->cache, $data); 1144b5f4f4eSchris } 1154b5f4f4eSchris 1164b5f4f4eSchris /** 1174b5f4f4eSchris * remove any cached data associated with this cache instance 1184b5f4f4eSchris */ 119*c59b3e00SGerrit Uitslag public function removeCache() { 1204b5f4f4eSchris @unlink($this->cache); 1214b5f4f4eSchris } 1224b5f4f4eSchris 1234b5f4f4eSchris /** 12433c1bd61SBen Coburn * Record cache hits statistics. 12533c1bd61SBen Coburn * (Only when debugging allowed, to reduce overhead.) 1264b5f4f4eSchris * 1274b5f4f4eSchris * @param bool $success result of this cache use attempt 1284b5f4f4eSchris * @return bool pass-thru $success value 1294b5f4f4eSchris */ 130*c59b3e00SGerrit Uitslag protected function _stats($success) { 1314b5f4f4eSchris global $conf; 13249eb6e38SAndreas Gohr static $stats = null; 1334b5f4f4eSchris static $file; 1344b5f4f4eSchris 13533c1bd61SBen Coburn if (!$conf['allowdebug']) { return $success; } 13633c1bd61SBen Coburn 1374b5f4f4eSchris if (is_null($stats)) { 1384b5f4f4eSchris $file = $conf['cachedir'].'/cache_stats.txt'; 1394b5f4f4eSchris $lines = explode("\n",io_readFile($file)); 1404b5f4f4eSchris 1414b5f4f4eSchris foreach ($lines as $line) { 1424b5f4f4eSchris $i = strpos($line,','); 1434b5f4f4eSchris $stats[substr($line,0,$i)] = $line; 1444b5f4f4eSchris } 1454b5f4f4eSchris } 1464b5f4f4eSchris 1474b5f4f4eSchris if (isset($stats[$this->ext])) { 1480abe1d3eSchris list($ext,$count,$hits) = explode(',',$stats[$this->ext]); 1494b5f4f4eSchris } else { 1504b5f4f4eSchris $ext = $this->ext; 1514b5f4f4eSchris $count = 0; 1520abe1d3eSchris $hits = 0; 1534b5f4f4eSchris } 1544b5f4f4eSchris 1554b5f4f4eSchris $count++; 1560abe1d3eSchris if ($success) $hits++; 1570abe1d3eSchris $stats[$this->ext] = "$ext,$count,$hits"; 1584b5f4f4eSchris 1594b5f4f4eSchris io_saveFile($file,join("\n",$stats)); 1604b5f4f4eSchris 1614b5f4f4eSchris return $success; 1624b5f4f4eSchris } 1634b5f4f4eSchris} 1644b5f4f4eSchris 1654b5f4f4eSchrisclass cache_parser extends cache { 1664b5f4f4eSchris 167*c59b3e00SGerrit Uitslag public $file = ''; // source file for cache 168*c59b3e00SGerrit Uitslag public $mode = ''; // input mode (represents the processing the input file will undergo) 1694b5f4f4eSchris 1704b5f4f4eSchris var $_event = 'PARSER_CACHE_USE'; 1714b5f4f4eSchris 172*c59b3e00SGerrit Uitslag /** 173*c59b3e00SGerrit Uitslag * 174*c59b3e00SGerrit Uitslag * @param string $id page id 175*c59b3e00SGerrit Uitslag * @param string $file source file for cache 176*c59b3e00SGerrit Uitslag * @param string $mode input mode 177*c59b3e00SGerrit Uitslag */ 178*c59b3e00SGerrit Uitslag public function cache_parser($id, $file, $mode) { 1794b5f4f4eSchris if ($id) $this->page = $id; 1804b5f4f4eSchris $this->file = $file; 1814b5f4f4eSchris $this->mode = $mode; 1824b5f4f4eSchris 1834b5f4f4eSchris parent::cache($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.'.$mode); 1844b5f4f4eSchris } 1854b5f4f4eSchris 186*c59b3e00SGerrit Uitslag /** 187*c59b3e00SGerrit Uitslag * method contains cache use decision logic 188*c59b3e00SGerrit Uitslag * 189*c59b3e00SGerrit Uitslag * @return bool see useCache() 190*c59b3e00SGerrit Uitslag */ 191*c59b3e00SGerrit Uitslag protected function _useCache() { 1924b5f4f4eSchris 1934b5f4f4eSchris if (!@file_exists($this->file)) return false; // source exists? 1940abe1d3eSchris return parent::_useCache(); 1950abe1d3eSchris } 1964b5f4f4eSchris 197*c59b3e00SGerrit Uitslag protected function _addDependencies() { 198*c59b3e00SGerrit Uitslag global $conf; 1990abe1d3eSchris 2000abe1d3eSchris $this->depends['age'] = isset($this->depends['age']) ? 2010abe1d3eSchris min($this->depends['age'],$conf['cachetime']) : $conf['cachetime']; 2024b5f4f4eSchris 2034b5f4f4eSchris // parser cache file dependencies ... 2044b5f4f4eSchris $files = array($this->file, // ... source 2054b5f4f4eSchris DOKU_INC.'inc/parser/parser.php', // ... parser 2064b5f4f4eSchris DOKU_INC.'inc/parser/handler.php', // ... handler 2074b5f4f4eSchris ); 208f8121585SChris Smith $files = array_merge($files, getConfigFiles('main')); // ... wiki settings 2094b5f4f4eSchris 2104b5f4f4eSchris $this->depends['files'] = !empty($this->depends['files']) ? array_merge($files, $this->depends['files']) : $files; 2110abe1d3eSchris parent::_addDependencies(); 2124b5f4f4eSchris } 2134b5f4f4eSchris 2144b5f4f4eSchris} 2154b5f4f4eSchris 2164b5f4f4eSchrisclass cache_renderer extends cache_parser { 217*c59b3e00SGerrit Uitslag 218*c59b3e00SGerrit Uitslag /** 219*c59b3e00SGerrit Uitslag * method contains cache use decision logic 220*c59b3e00SGerrit Uitslag * 221*c59b3e00SGerrit Uitslag * @return bool see useCache() 222*c59b3e00SGerrit Uitslag */ 223*c59b3e00SGerrit Uitslag protected function _useCache() { 224b9991f57Schris global $conf; 2254b5f4f4eSchris 2260abe1d3eSchris if (!parent::_useCache()) return false; 2274b5f4f4eSchris 228c0322273SAdrian Lang if (!isset($this->page)) { 229c0322273SAdrian Lang return true; 230c0322273SAdrian Lang } 231c0322273SAdrian Lang 2322302d6beSMartin Doucha if ($this->_time < @filemtime(metaFN($this->page,'.meta'))) return false; // meta cache older than file it depends on? 2332302d6beSMartin Doucha 234c0322273SAdrian Lang // check current link existence is consistent with cache version 235c0322273SAdrian Lang // first check the purgefile 236c0322273SAdrian Lang // - if the cache is more recent than the purgefile we know no links can have been updated 2372e3d6a01SKazutaka Miyasaka if ($this->_time >= @filemtime($conf['cachedir'].'/purgefile')) { 238c0322273SAdrian Lang return true; 239c0322273SAdrian Lang } 240c0322273SAdrian Lang 241ce6b63d9Schris // for wiki pages, check metadata dependencies 242ce6b63d9Schris $metadata = p_get_metadata($this->page); 2430abe1d3eSchris 244c0322273SAdrian Lang if (!isset($metadata['relation']['references']) || 2452e3d6a01SKazutaka Miyasaka empty($metadata['relation']['references'])) { 246c0322273SAdrian Lang return true; 247c0322273SAdrian Lang } 2480abe1d3eSchris 249c0322273SAdrian Lang foreach ($metadata['relation']['references'] as $id => $exists) { 250103c256aSChris Smith if ($exists != page_exists($id,'',false)) return false; 2514b5f4f4eSchris } 2524b5f4f4eSchris 2534b5f4f4eSchris return true; 2544b5f4f4eSchris } 2550abe1d3eSchris 256*c59b3e00SGerrit Uitslag protected function _addDependencies() { 2570abe1d3eSchris 2580abe1d3eSchris // renderer cache file dependencies ... 2590abe1d3eSchris $files = array( 2600abe1d3eSchris DOKU_INC.'inc/parser/'.$this->mode.'.php', // ... the renderer 2610abe1d3eSchris ); 262ce6b63d9Schris 263ce6b63d9Schris // page implies metadata and possibly some other dependencies 264ce6b63d9Schris if (isset($this->page)) { 2650a69dff7Schris 26698214867SMichael Hamann $valid = p_get_metadata($this->page, 'date valid'); // for xhtml this will render the metadata if needed 2670a69dff7Schris if (!empty($valid['age'])) { 2680a69dff7Schris $this->depends['age'] = isset($this->depends['age']) ? 2690a69dff7Schris min($this->depends['age'],$valid['age']) : $valid['age']; 2700a69dff7Schris } 271ce6b63d9Schris } 2720abe1d3eSchris 2730abe1d3eSchris $this->depends['files'] = !empty($this->depends['files']) ? array_merge($files, $this->depends['files']) : $files; 2740abe1d3eSchris parent::_addDependencies(); 2750abe1d3eSchris } 2764b5f4f4eSchris} 2774b5f4f4eSchris 2784b5f4f4eSchrisclass cache_instructions extends cache_parser { 2794b5f4f4eSchris 280*c59b3e00SGerrit Uitslag /** 281*c59b3e00SGerrit Uitslag * @param string $id page id 282*c59b3e00SGerrit Uitslag * @param string $file source file for cache 283*c59b3e00SGerrit Uitslag */ 284*c59b3e00SGerrit Uitslag public function cache_instructions($id, $file) { 2854b5f4f4eSchris parent::cache_parser($id, $file, 'i'); 2864b5f4f4eSchris } 2874b5f4f4eSchris 288*c59b3e00SGerrit Uitslag /** 289*c59b3e00SGerrit Uitslag * retrieve the cached data 290*c59b3e00SGerrit Uitslag * 291*c59b3e00SGerrit Uitslag * @param bool $clean true to clean line endings, false to leave line endings alone 292*c59b3e00SGerrit Uitslag * @return string cache contents 293*c59b3e00SGerrit Uitslag */ 294*c59b3e00SGerrit Uitslag public function retrieveCache($clean=true) { 2954b5f4f4eSchris $contents = io_readFile($this->cache, false); 2964b5f4f4eSchris return !empty($contents) ? unserialize($contents) : array(); 2974b5f4f4eSchris } 2984b5f4f4eSchris 299*c59b3e00SGerrit Uitslag /** 300*c59b3e00SGerrit Uitslag * cache $instructions 301*c59b3e00SGerrit Uitslag * 302*c59b3e00SGerrit Uitslag * @param string $instructions the instruction to be cached 303*c59b3e00SGerrit Uitslag * @return bool true on success, false otherwise 304*c59b3e00SGerrit Uitslag */ 305*c59b3e00SGerrit Uitslag public function storeCache($instructions) { 306cbaf4259SChris Smith return io_savefile($this->cache,serialize($instructions)); 3074b5f4f4eSchris } 3084b5f4f4eSchris} 309