1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/piwigo_ynh.git synced 2024-09-03 20:06:03 +02:00
piwigo_ynh/sources/include/smarty/libs/sysplugins/smarty_cacheresource_custom.php

282 lines
9.8 KiB
PHP
Raw Normal View History

2014-07-06 13:21:10 +02:00
<?php
/**
* Smarty Internal Plugin
*
2016-08-27 21:53:29 +02:00
* @package Smarty
2014-07-06 13:21:10 +02:00
* @subpackage Cacher
*/
/**
* Cache Handler API
*
2016-08-27 21:53:29 +02:00
* @package Smarty
2014-07-06 13:21:10 +02:00
* @subpackage Cacher
2016-08-27 21:53:29 +02:00
* @author Rodney Rehm
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
{
2014-07-06 13:21:10 +02:00
/**
* fetch cached content and its modification time from data source
*
2016-08-27 21:53:29 +02:00
* @param string $id unique cache content identifier
* @param string $name template name
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param string $content cached content
* @param integer $mtime cache modification timestamp (epoch)
*
2014-07-06 13:21:10 +02:00
* @return void
*/
2016-08-27 21:53:29 +02:00
abstract protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);
2014-07-06 13:21:10 +02:00
/**
* Fetch cached content's modification timestamp from data source
* {@internal implementing this method is optional.
* Only implement it if modification times can be accessed faster than loading the complete cached content.}}
*
2016-08-27 21:53:29 +02:00
* @param string $id unique cache content identifier
* @param string $name template name
* @param string $cache_id cache id
* @param string $compile_id compile id
*
2014-07-06 13:21:10 +02:00
* @return integer|boolean timestamp (epoch) the template was modified, or false if not found
*/
protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
{
return null;
}
/**
* Save content to cache
*
2016-08-27 21:53:29 +02:00
* @param string $id unique cache content identifier
* @param string $name template name
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param integer|null $exp_time seconds till expiration or null
* @param string $content content to cache
*
* @return boolean success
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
abstract protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content);
2014-07-06 13:21:10 +02:00
/**
* Delete content from cache
*
2016-08-27 21:53:29 +02:00
* @param string $name template name
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param integer|null $exp_time seconds till expiration time in seconds or null
*
* @return integer number of deleted caches
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
abstract protected function delete($name, $cache_id, $compile_id, $exp_time);
2014-07-06 13:21:10 +02:00
/**
* populate Cached Object with meta data from Resource
*
2016-08-27 21:53:29 +02:00
* @param Smarty_Template_Cached $cached cached object
* @param Smarty_Internal_Template $_template template object
*
2014-07-06 13:21:10 +02:00
* @return void
*/
public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
{
$_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
2016-08-27 21:53:29 +02:00
$_compile_id = isset($cached->compile_id) ? preg_replace('![^\w]+!', '_', $cached->compile_id) : null;
$path = $cached->source->filepath . $_cache_id . $_compile_id;
$cached->filepath = sha1($path);
if ($_template->smarty->cache_locking) {
$cached->lock_id = sha1('lock.' . $path);
}
2014-07-06 13:21:10 +02:00
$this->populateTimestamp($cached);
}
/**
* populate Cached Object with timestamp and exists from Resource
*
2016-08-27 21:53:29 +02:00
* @param Smarty_Template_Cached $cached
*
2014-07-06 13:21:10 +02:00
* @return void
*/
public function populateTimestamp(Smarty_Template_Cached $cached)
{
$mtime = $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
if ($mtime !== null) {
$cached->timestamp = $mtime;
$cached->exists = !!$cached->timestamp;
2016-08-27 21:53:29 +02:00
2014-07-06 13:21:10 +02:00
return;
}
$timestamp = null;
$this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content, $timestamp);
$cached->timestamp = isset($timestamp) ? $timestamp : false;
$cached->exists = !!$cached->timestamp;
}
/**
* Read the cached template and process the header
*
2016-08-27 21:53:29 +02:00
* @param Smarty_Internal_Template $_template template object
* @param Smarty_Template_Cached $cached cached object
* @param bool $update flag if called because cache update
*
* @return boolean true or false if the cached content does not exist
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null, $update = false)
2014-07-06 13:21:10 +02:00
{
if (!$cached) {
$cached = $_template->cached;
}
$content = $cached->content ? $cached->content : null;
$timestamp = $cached->timestamp ? $cached->timestamp : null;
if ($content === null || !$timestamp) {
2016-08-27 21:53:29 +02:00
$this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $content, $timestamp);
2014-07-06 13:21:10 +02:00
}
if (isset($content)) {
2016-08-27 21:53:29 +02:00
/** @var Smarty_Internal_Template $_smarty_tpl
* used in evaluated code
*/
2014-07-06 13:21:10 +02:00
$_smarty_tpl = $_template;
eval("?>" . $content);
2016-08-27 21:53:29 +02:00
$cached->content = null;
2014-07-06 13:21:10 +02:00
return true;
}
2016-08-27 21:53:29 +02:00
2014-07-06 13:21:10 +02:00
return false;
}
/**
* Write the rendered template output to cache
*
2016-08-27 21:53:29 +02:00
* @param Smarty_Internal_Template $_template template object
* @param string $content content to cache
*
* @return boolean success
2014-07-06 13:21:10 +02:00
*/
public function writeCachedContent(Smarty_Internal_Template $_template, $content)
{
2016-08-27 21:53:29 +02:00
return $this->save($_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $_template->cache_lifetime, $content);
}
/**
* Read cached template from cache
*
* @param Smarty_Internal_Template $_template template object
*
* @return string content
*/
public function readCachedContent(Smarty_Internal_Template $_template)
{
$content = $_template->cached->content ? $_template->cached->content : null;
$timestamp = null;
if ($content === null) {
$timestamp = null;
$this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $content, $timestamp);
}
if (isset($content)) {
return $content;
}
return false;
2014-07-06 13:21:10 +02:00
}
/**
* Empty cache
*
2016-08-27 21:53:29 +02:00
* @param Smarty $smarty Smarty object
* @param integer $exp_time expiration time (number of seconds, not timestamp)
*
2014-07-06 13:21:10 +02:00
* @return integer number of cache files deleted
*/
2016-08-27 21:53:29 +02:00
public function clearAll(Smarty $smarty, $exp_time = null)
2014-07-06 13:21:10 +02:00
{
return $this->delete(null, null, null, $exp_time);
}
/**
* Empty cache for a specific template
*
2016-08-27 21:53:29 +02:00
* @param Smarty $smarty Smarty object
* @param string $resource_name template name
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param integer $exp_time expiration time (number of seconds, not timestamp)
*
2014-07-06 13:21:10 +02:00
* @return integer number of cache files deleted
*/
public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
{
2016-08-27 21:53:29 +02:00
$cache_name = null;
if (isset($resource_name)) {
$source = Smarty_Template_Source::load(null, $smarty, $resource_name);
if ($source->exists) {
$cache_name = $source->name;
} else {
return 0;
}
// remove from template cache
if (isset($smarty->_cache['template_objects'])) {
foreach ($smarty->_cache['template_objects'] as $key => $_tpl) {
if (isset($_tpl->cached) && $_tpl->source->uid == $source->uid) {
unset($smarty->_cache['template_objects'][$key]);
}
}
}
}
return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
2014-07-06 13:21:10 +02:00
}
/**
* Check is cache is locked for this template
*
2016-08-27 21:53:29 +02:00
* @param Smarty $smarty Smarty object
* @param Smarty_Template_Cached $cached cached object
*
* @return boolean true or false if cache is locked
2014-07-06 13:21:10 +02:00
*/
public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
{
2016-08-27 21:53:29 +02:00
$id = $cached->lock_id;
2014-07-06 13:21:10 +02:00
$name = $cached->source->name . '.lock';
2016-08-27 21:53:29 +02:00
$mtime = $this->fetchTimestamp($id, $name, $cached->cache_id, $cached->compile_id);
2014-07-06 13:21:10 +02:00
if ($mtime === null) {
2016-08-27 21:53:29 +02:00
$this->fetch($id, $name, $cached->cache_id, $cached->compile_id, $content, $mtime);
2014-07-06 13:21:10 +02:00
}
2016-08-27 21:53:29 +02:00
return $mtime && ($t = time()) - $mtime < $smarty->locking_timeout;
2014-07-06 13:21:10 +02:00
}
/**
* Lock cache for this template
*
2016-08-27 21:53:29 +02:00
* @param Smarty $smarty Smarty object
2014-07-06 13:21:10 +02:00
* @param Smarty_Template_Cached $cached cached object
2016-08-27 21:53:29 +02:00
*
* @return bool|void
2014-07-06 13:21:10 +02:00
*/
public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
{
$cached->is_locked = true;
2016-08-27 21:53:29 +02:00
$id = $cached->lock_id;
2014-07-06 13:21:10 +02:00
$name = $cached->source->name . '.lock';
2016-08-27 21:53:29 +02:00
$this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, '');
2014-07-06 13:21:10 +02:00
}
/**
* Unlock cache for this template
*
2016-08-27 21:53:29 +02:00
* @param Smarty $smarty Smarty object
2014-07-06 13:21:10 +02:00
* @param Smarty_Template_Cached $cached cached object
2016-08-27 21:53:29 +02:00
*
* @return bool|void
2014-07-06 13:21:10 +02:00
*/
public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
{
$cached->is_locked = false;
$name = $cached->source->name . '.lock';
2016-08-27 21:53:29 +02:00
$this->delete($name, $cached->cache_id, $cached->compile_id, null);
2014-07-06 13:21:10 +02:00
}
}