xref: /dokuwiki/inc/cache.php (revision b9991f57a373d6e89ee328caf56b71886b95b5e7)
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
190abe1d3eSchris  var $depends = array(); // array containing cache dependency information,
200abe1d3eSchris                          //   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;
440abe1d3eSchris    $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
530abe1d3eSchris  /**
544b5f4f4eSchris   * private method containing cache use decision logic
554b5f4f4eSchris   *
560abe1d3eSchris   * this function processes the following keys in the depends array
570abe1d3eSchris   *   purge - force a purge on any non empty value
580abe1d3eSchris   *   age   - expire cache if older than age (seconds)
590abe1d3eSchris   *   files - expire cache if any file in this array was updated more recently than the cache
600abe1d3eSchris   *
610abe1d3eSchris   * can be overridden
624b5f4f4eSchris   *
634b5f4f4eSchris   * @return bool               see useCache()
644b5f4f4eSchris   */
654b5f4f4eSchris  function _useCache() {
664b5f4f4eSchris
670abe1d3eSchris    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  /**
830abe1d3eSchris   * add dependencies to the depends array
840abe1d3eSchris   *
850abe1d3eSchris   * this method should only add dependencies,
860abe1d3eSchris   * it should not remove any existing dependencies and
870abe1d3eSchris   * it should only overwrite a dependency when the new value is more stringent than the old
880abe1d3eSchris   */
890abe1d3eSchris  function _addDependencies() {
900abe1d3eSchris    if (isset($_REQUEST['purge'])) $this->depends['purge'] = true;   // purge requested
910abe1d3eSchris  }
920abe1d3eSchris
930abe1d3eSchris  /**
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])) {
1420abe1d3eSchris      list($ext,$count,$hits) = explode(',',$stats[$this->ext]);
1434b5f4f4eSchris    } else {
1444b5f4f4eSchris      $ext = $this->ext;
1454b5f4f4eSchris      $count = 0;
1460abe1d3eSchris      $hits = 0;
1474b5f4f4eSchris    }
1484b5f4f4eSchris
1494b5f4f4eSchris    $count++;
1500abe1d3eSchris    if ($success) $hits++;
1510abe1d3eSchris    $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?
1770abe1d3eSchris    return parent::_useCache();
1780abe1d3eSchris  }
1794b5f4f4eSchris
1800abe1d3eSchris  function _addDependencies() {
1810abe1d3eSchris
1820abe1d3eSchris    $this->depends['age'] = isset($this->depends['age']) ?
1830abe1d3eSchris                   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;
1940abe1d3eSchris    parent::_addDependencies();
1954b5f4f4eSchris  }
1964b5f4f4eSchris
1974b5f4f4eSchris}
1984b5f4f4eSchris
1994b5f4f4eSchrisclass cache_renderer extends cache_parser {
2004b5f4f4eSchris
201*b9991f57Schris  function useCache($depends=array()) {
202*b9991f57Schris    $use = parent::useCache($depends);
203*b9991f57Schris
204*b9991f57Schris    // meta data needs to be kept in step with the cache
205*b9991f57Schris    if (!$use && isset($this->page)) {
206*b9991f57Schris      p_set_metadata($this->page,array(),true);
207*b9991f57Schris    }
208*b9991f57Schris
209*b9991f57Schris    return $use;
210*b9991f57Schris  }
211*b9991f57Schris
2124b5f4f4eSchris  function _useCache() {
213*b9991f57Schris    global $conf;
2144b5f4f4eSchris
2150abe1d3eSchris    if (!parent::_useCache()) return false;
2164b5f4f4eSchris
2174b5f4f4eSchris    // for wiki pages, check for internal link status changes
2184b5f4f4eSchris    if (isset($this->page)) {
2190abe1d3eSchris
2200abe1d3eSchris      // check the purgefile
2210abe1d3eSchris      // - if the cache is more recent that the purgefile we know no links can have been updated
2220abe1d3eSchris      if ($this->_time < @filemtime($conf['cachedir'].'/purgefile')) {
2230abe1d3eSchris
2244b5f4f4eSchris        $links = p_get_metadata($this->page,"relation references");
2254b5f4f4eSchris
2264b5f4f4eSchris        if (!empty($links)) {
2274b5f4f4eSchris          foreach ($links as $id => $exists) {
2284b5f4f4eSchris            if ($exists != @file_exists(wikiFN($id,'',false))) return false;
2294b5f4f4eSchris          }
2304b5f4f4eSchris        }
2314b5f4f4eSchris      }
2320abe1d3eSchris    }
2334b5f4f4eSchris
2344b5f4f4eSchris    return true;
2354b5f4f4eSchris  }
2360abe1d3eSchris
2370abe1d3eSchris  function _addDependencies() {
2380abe1d3eSchris
2390abe1d3eSchris    // renderer cache file dependencies ...
2400abe1d3eSchris    $files = array(
2410abe1d3eSchris                   DOKU_INC.'inc/parser/'.$this->mode.'.php',            // ... the renderer
2420abe1d3eSchris             );
2430abe1d3eSchris    if (isset($this->page)) { $files[] = metaFN($this->page,'.meta'); }  // ... the page's own metadata
2440abe1d3eSchris
2450abe1d3eSchris    $this->depends['files'] = !empty($this->depends['files']) ? array_merge($files, $this->depends['files']) : $files;
2460abe1d3eSchris    parent::_addDependencies();
2470abe1d3eSchris  }
2484b5f4f4eSchris}
2494b5f4f4eSchris
2504b5f4f4eSchrisclass cache_instructions extends cache_parser {
2514b5f4f4eSchris
2524b5f4f4eSchris  function cache_instructions($id, $file) {
2534b5f4f4eSchris    parent::cache_parser($id, $file, 'i');
2544b5f4f4eSchris  }
2554b5f4f4eSchris
2564b5f4f4eSchris  function retrieveCache() {
2574b5f4f4eSchris    $contents = io_readFile($this->cache, false);
2584b5f4f4eSchris    return !empty($contents) ? unserialize($contents) : array();
2594b5f4f4eSchris  }
2604b5f4f4eSchris
2614b5f4f4eSchris  function storeCache($instructions) {
2624b5f4f4eSchris    io_savefile($this->cache,serialize($instructions));
2634b5f4f4eSchris  }
2644b5f4f4eSchris}
265