New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor cache.php to better conform with PSR 2 #2689
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0db5771
Refactor cache.php into different files
micgro42 42c00b4
Rename methods to conform with PSR 2
micgro42 72c2bae
Fix typo and rename method in comments too
micgro42 43ff105
Adjust comment to better reflect method
micgro42 d2f1d7a
Rename fields and add deprecated __get/__set
micgro42 fec08cc
Use DokuWiki's own deprecation reporters
micgro42 b4b0b31
Use deprecation helper
micgro42 dc7da77
Use setter instead of deprecate public property
micgro42 0c5eb5e
Refactor deprecation logging into its own class
micgro42 27cff89
Remove PHP user deprecation errors
micgro42 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
<?php | ||
|
||
namespace dokuwiki\Cache; | ||
|
||
/** | ||
* Generic handling of caching | ||
*/ | ||
class Cache | ||
{ | ||
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 | ||
|
||
protected $event = ''; // event to be triggered during useCache | ||
protected $time; | ||
protected $nocache = false; // if set to true, cache will not be used or stored | ||
|
||
/** | ||
* @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! | ||
*/ | ||
public function __get($key) | ||
{ | ||
if ($key === '_event') { | ||
trigger_error( | ||
'\dokuwiki\Cache\Cache::_event is deprecated since 2019-02-02. Use \dokuwiki\Cache\Cache::getEvent()', | ||
E_USER_DEPRECATED | ||
); | ||
dbg_deprecated('\dokuwiki\Cache\Cache::getEvent()'); | ||
return $this->getEvent(); | ||
} | ||
|
||
if ($key === '_time') { | ||
trigger_error( | ||
'\dokuwiki\Cache\Cache::_time is deprecated since 2019-02-02. Use \dokuwiki\Cache\Cache::getTime()', | ||
E_USER_DEPRECATED | ||
); | ||
dbg_deprecated('\dokuwiki\Cache\Cache::getTime()'); | ||
return $this->getTime(); | ||
} | ||
return $this->$$key; | ||
} | ||
|
||
public function __set($name, $value) | ||
{ | ||
if ($name === '_event') { | ||
trigger_error( | ||
'\dokuwiki\Cache\Cache::_event is deprecated since 2019-02-02. Use \dokuwiki\Cache\Cache::getEvent()', | ||
E_USER_DEPRECATED | ||
); | ||
dbg_deprecated('\dokuwiki\Cache\Cache::getEvent()'); | ||
$this->setEvent($value); | ||
} | ||
$this->$$name = $value; | ||
} | ||
|
||
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(trigger_event($this->event, $this, array($this, 'makeDefaultCacheDecision'))); | ||
} else { | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this error is useful. Don't these errors work like a non-catchable exception, ending code execution? Or do I remember this wrong? Deprecated accesses should continue to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope. The code
results only in the following output in my error log:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if you have display_errors on in your php.ini?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expect that you then see those error on your website (which you want in dev).
One shouldn't have
display_erros
on in production.That being said, maybe this isn't such a good idea after all. As I still get these notices in
css.php
andjs.php
where those error are especially annoying.I will try to have another look at it on Thursday or Friday evening,