diff options
author | Andreas Gohr <gohr@cosmocode.de> | 2019-10-10 09:55:14 +0200 |
---|---|---|
committer | Andreas Gohr <gohr@cosmocode.de> | 2019-10-10 09:55:14 +0200 |
commit | 31a58aba4c24b34c34ad5764d1a35b7c398c3a2c (patch) | |
tree | 7f4d1546fbb69863a7d366fc1ff647f784853b68 /inc/Cache | |
parent | af7ba5aa0bd10fc0ad9ef983006305b4c5a8ed42 (diff) | |
parent | c0c77cd20b23921c9e893bb70b99f38be153875a (diff) | |
download | dokuwiki-31a58aba4c24b34c34ad5764d1a35b7c398c3a2c.tar.gz dokuwiki-31a58aba4c24b34c34ad5764d1a35b7c398c3a2c.zip |
Merge branch 'psr2'
* psr2: (160 commits)
fixed merge error
Moved parts of the Asian word handling to its own class
ignore snake_case error of substr_replace
fixed some line length errors
ignore PSR2 in the old form class
fix PSR2 error in switch statement
replaced deprecated utf8 functions
formatting cleanup
mark old utf8 functions deprecated
some more PSR2 cleanup
Some cleanup for the UTF-8 stuff
Moved all utf8 methods to their own namespaced classes
Create separate table files for UTF-8 handling
Ignore mixed concerns in loader
Use type safe comparisons in loader
Remove obsolete include
adjust phpcs exclude patterns for new plugin classes
🚚 Move Subscription class to deprecated.php
♻️ Split up ChangesSubscriptionSender into multiple classes
Minor optimizations in PluginController
...
Diffstat (limited to 'inc/Cache')
-rw-r--r-- | inc/Cache/Cache.php | 237 | ||||
-rw-r--r-- | inc/Cache/CacheInstructions.php | 46 | ||||
-rw-r--r-- | inc/Cache/CacheParser.php | 64 | ||||
-rw-r--r-- | inc/Cache/CacheRenderer.php | 94 |
4 files changed, 441 insertions, 0 deletions
diff --git a/inc/Cache/Cache.php b/inc/Cache/Cache.php new file mode 100644 index 000000000..599dc5f59 --- /dev/null +++ b/inc/Cache/Cache.php @@ -0,0 +1,237 @@ +<?php + +namespace dokuwiki\Cache; + +use \dokuwiki\Debug\PropertyDeprecationHelper; +use dokuwiki\Extension\Event; + +/** + * Generic handling of caching + */ +class Cache +{ + use PropertyDeprecationHelper; + + public $key = ''; // primary identifier for this item + public $ext = ''; // file ext for cache data, secondary identifier for this item + public $cache = ''; // cache file name + public $depends = array(); // array containing cache dependency information, + // used by makeDefaultCacheDecision to determine cache validity + + // phpcs:disable + /** + * @deprecated since 2019-02-02 use the respective getters instead! + */ + protected $_event = ''; // event to be triggered during useCache + protected $_time; + protected $_nocache = false; // if set to true, cache will not be used or stored + // phpcs:enable + + /** + * @param string $key primary identifier + * @param string $ext file extension + */ + public function __construct($key, $ext) + { + $this->key = $key; + $this->ext = $ext; + $this->cache = getCacheName($key, $ext); + + /** + * @deprecated since 2019-02-02 use the respective getters instead! + */ + $this->deprecatePublicProperty('_event'); + $this->deprecatePublicProperty('_time'); + $this->deprecatePublicProperty('_nocache'); + } + + public function getTime() + { + return $this->_time; + } + + public function getEvent() + { + return $this->_event; + } + + public function setEvent($event) + { + $this->_event = $event; + } + + /** + * public method to determine whether the cache can be used + * + * to assist in centralisation of event triggering and calculation of cache statistics, + * don't override this function override makeDefaultCacheDecision() + * + * @param array $depends array of cache dependencies, support dependecies: + * 'age' => max age of the cache in seconds + * 'files' => cache must be younger than mtime of each file + * (nb. dependency passes if file doesn't exist) + * + * @return bool true if cache can be used, false otherwise + */ + public function useCache($depends = array()) + { + $this->depends = $depends; + $this->addDependencies(); + + if ($this->_event) { + return $this->stats(Event::createAndTrigger( + $this->_event, $this, array($this, 'makeDefaultCacheDecision')) + ); + } + + return $this->stats($this->makeDefaultCacheDecision()); + } + + /** + * internal method containing cache use decision logic + * + * this function processes the following keys in the depends array + * purge - force a purge on any non empty value + * age - expire cache if older than age (seconds) + * files - expire cache if any file in this array was updated more recently than the cache + * + * Note that this function needs to be public as it is used as callback for the event handler + * + * can be overridden + * + * @internal This method may only be called by the event handler! Call \dokuwiki\Cache\Cache::useCache instead! + * + * @return bool see useCache() + */ + public function makeDefaultCacheDecision() + { + + if ($this->_nocache) { + return false; + } // caching turned off + if (!empty($this->depends['purge'])) { + return false; + } // purge requested? + if (!($this->_time = @filemtime($this->cache))) { + return false; + } // cache exists? + + // cache too old? + if (!empty($this->depends['age']) && ((time() - $this->_time) > $this->depends['age'])) { + return false; + } + + if (!empty($this->depends['files'])) { + foreach ($this->depends['files'] as $file) { + if ($this->_time <= @filemtime($file)) { + return false; + } // cache older than files it depends on? + } + } + + return true; + } + + /** + * add dependencies to the depends array + * + * this method should only add dependencies, + * it should not remove any existing dependencies and + * it should only overwrite a dependency when the new value is more stringent than the old + */ + protected function addDependencies() + { + global $INPUT; + if ($INPUT->has('purge')) { + $this->depends['purge'] = true; + } // purge requested + } + + /** + * retrieve the cached data + * + * @param bool $clean true to clean line endings, false to leave line endings alone + * @return string cache contents + */ + public function retrieveCache($clean = true) + { + return io_readFile($this->cache, $clean); + } + + /** + * cache $data + * + * @param string $data the data to be cached + * @return bool true on success, false otherwise + */ + public function storeCache($data) + { + if ($this->_nocache) { + return false; + } + + return io_savefile($this->cache, $data); + } + + /** + * remove any cached data associated with this cache instance + */ + public function removeCache() + { + @unlink($this->cache); + } + + /** + * Record cache hits statistics. + * (Only when debugging allowed, to reduce overhead.) + * + * @param bool $success result of this cache use attempt + * @return bool pass-thru $success value + */ + protected function stats($success) + { + global $conf; + static $stats = null; + static $file; + + if (!$conf['allowdebug']) { + return $success; + } + + if (is_null($stats)) { + $file = $conf['cachedir'] . '/cache_stats.txt'; + $lines = explode("\n", io_readFile($file)); + + foreach ($lines as $line) { + $i = strpos($line, ','); + $stats[substr($line, 0, $i)] = $line; + } + } + + if (isset($stats[$this->ext])) { + list($ext, $count, $hits) = explode(',', $stats[$this->ext]); + } else { + $ext = $this->ext; + $count = 0; + $hits = 0; + } + + $count++; + if ($success) { + $hits++; + } + $stats[$this->ext] = "$ext,$count,$hits"; + + io_saveFile($file, join("\n", $stats)); + + return $success; + } + + /** + * @return bool + */ + public function isNoCache() + { + return $this->_nocache; + } +} diff --git a/inc/Cache/CacheInstructions.php b/inc/Cache/CacheInstructions.php new file mode 100644 index 000000000..3c4786105 --- /dev/null +++ b/inc/Cache/CacheInstructions.php @@ -0,0 +1,46 @@ +<?php + +namespace dokuwiki\Cache; + +/** + * Caching of parser instructions + */ +class CacheInstructions extends \dokuwiki\Cache\CacheParser +{ + + /** + * @param string $id page id + * @param string $file source file for cache + */ + public function __construct($id, $file) + { + parent::__construct($id, $file, 'i'); + } + + /** + * retrieve the cached data + * + * @param bool $clean true to clean line endings, false to leave line endings alone + * @return array cache contents + */ + public function retrieveCache($clean = true) + { + $contents = io_readFile($this->cache, false); + return !empty($contents) ? unserialize($contents) : array(); + } + + /** + * cache $instructions + * + * @param array $instructions the instruction to be cached + * @return bool true on success, false otherwise + */ + public function storeCache($instructions) + { + if ($this->_nocache) { + return false; + } + + return io_savefile($this->cache, serialize($instructions)); + } +} diff --git a/inc/Cache/CacheParser.php b/inc/Cache/CacheParser.php new file mode 100644 index 000000000..86453bed5 --- /dev/null +++ b/inc/Cache/CacheParser.php @@ -0,0 +1,64 @@ +<?php + +namespace dokuwiki\Cache; + +/** + * Parser caching + */ +class CacheParser extends Cache +{ + + public $file = ''; // source file for cache + public $mode = ''; // input mode (represents the processing the input file will undergo) + public $page = ''; + + /** + * + * @param string $id page id + * @param string $file source file for cache + * @param string $mode input mode + */ + public function __construct($id, $file, $mode) + { + if ($id) { + $this->page = $id; + } + $this->file = $file; + $this->mode = $mode; + + $this->_event = 'PARSER_CACHE_USE'; + parent::__construct($file . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'], '.' . $mode); + } + + /** + * method contains cache use decision logic + * + * @return bool see useCache() + */ + public function makeDefaultCacheDecision() + { + + if (!file_exists($this->file)) { + return false; + } // source exists? + return parent::makeDefaultCacheDecision(); + } + + protected function addDependencies() + { + + // parser cache file dependencies ... + $files = array( + $this->file, // ... source + DOKU_INC . 'inc/parser/Parser.php', // ... parser + DOKU_INC . 'inc/parser/handler.php', // ... handler + ); + $files = array_merge($files, getConfigFiles('main')); // ... wiki settings + + $this->depends['files'] = !empty($this->depends['files']) ? + array_merge($files, $this->depends['files']) : + $files; + parent::addDependencies(); + } + +} diff --git a/inc/Cache/CacheRenderer.php b/inc/Cache/CacheRenderer.php new file mode 100644 index 000000000..e8a28c309 --- /dev/null +++ b/inc/Cache/CacheRenderer.php @@ -0,0 +1,94 @@ +<?php + +namespace dokuwiki\Cache; + +/** + * Caching of data of renderer + */ +class CacheRenderer extends CacheParser +{ + + /** + * method contains cache use decision logic + * + * @return bool see useCache() + */ + public function makeDefaultCacheDecision() + { + global $conf; + + if (!parent::makeDefaultCacheDecision()) { + return false; + } + + if (!isset($this->page)) { + return true; + } + + // meta cache older than file it depends on? + if ($this->_time < @filemtime(metaFN($this->page, '.meta'))) { + return false; + } + + // check current link existence is consistent with cache version + // first check the purgefile + // - if the cache is more recent than the purgefile we know no links can have been updated + if ($this->_time >= @filemtime($conf['cachedir'] . '/purgefile')) { + return true; + } + + // for wiki pages, check metadata dependencies + $metadata = p_get_metadata($this->page); + + if (!isset($metadata['relation']['references']) || + empty($metadata['relation']['references'])) { + return true; + } + + foreach ($metadata['relation']['references'] as $id => $exists) { + if ($exists != page_exists($id, '', false)) { + return false; + } + } + + return true; + } + + protected function addDependencies() + { + global $conf; + + // default renderer cache file 'age' is dependent on 'cachetime' setting, two special values: + // -1 : do not cache (should not be overridden) + // 0 : cache never expires (can be overridden) - no need to set depends['age'] + if ($conf['cachetime'] == -1) { + $this->_nocache = true; + return; + } elseif ($conf['cachetime'] > 0) { + $this->depends['age'] = isset($this->depends['age']) ? + min($this->depends['age'], $conf['cachetime']) : $conf['cachetime']; + } + + // renderer cache file dependencies ... + $files = array( + DOKU_INC . 'inc/parser/' . $this->mode . '.php', // ... the renderer + ); + + // page implies metadata and possibly some other dependencies + if (isset($this->page)) { + + // for xhtml this will render the metadata if needed + $valid = p_get_metadata($this->page, 'date valid'); + if (!empty($valid['age'])) { + $this->depends['age'] = isset($this->depends['age']) ? + min($this->depends['age'], $valid['age']) : $valid['age']; + } + } + + $this->depends['files'] = !empty($this->depends['files']) ? + array_merge($files, $this->depends['files']) : + $files; + + parent::addDependencies(); + } +} |