2018-03-03 12:09:12 -05:00
|
|
|
<?php
|
|
|
|
namespace Friendica\Core\Config;
|
|
|
|
|
2018-07-20 08:19:26 -04:00
|
|
|
use Friendica\Database\DBA;
|
2018-03-03 12:09:12 -05:00
|
|
|
|
|
|
|
/**
|
2018-03-06 20:04:04 -05:00
|
|
|
* JustInTime Configuration Adapter
|
2018-03-03 12:09:12 -05:00
|
|
|
*
|
|
|
|
* Default Config Adapter. Provides the best performance for pages loading few configuration variables.
|
|
|
|
*
|
2018-09-15 19:28:38 -04:00
|
|
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
2018-03-03 12:09:12 -05:00
|
|
|
*/
|
2019-02-07 20:44:03 +01:00
|
|
|
class JITConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
|
2018-03-03 12:09:12 -05:00
|
|
|
{
|
|
|
|
private $cache;
|
|
|
|
private $in_db;
|
|
|
|
|
2019-02-03 22:22:04 +01:00
|
|
|
/**
|
|
|
|
* @var IConfigCache The config cache of this driver
|
|
|
|
*/
|
2019-02-04 09:30:48 +01:00
|
|
|
private $configCache;
|
2019-02-03 22:22:04 +01:00
|
|
|
|
|
|
|
/**
|
2019-02-04 09:30:48 +01:00
|
|
|
* @param IConfigCache $configCache The config cache of this driver
|
2019-02-03 22:22:04 +01:00
|
|
|
*/
|
2019-02-04 09:30:48 +01:00
|
|
|
public function __construct(IConfigCache $configCache)
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2019-02-04 09:30:48 +01:00
|
|
|
$this->configCache = $configCache;
|
2019-02-08 20:12:07 -05:00
|
|
|
$this->connected = DBA::connected();
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 23:42:49 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-03-03 12:09:12 -05:00
|
|
|
public function load($cat = "config")
|
|
|
|
{
|
2019-02-07 20:44:03 +01:00
|
|
|
if (!$this->isConnected()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-03 12:09:12 -05:00
|
|
|
// We don't preload "system" anymore.
|
|
|
|
// This reduces the number of database reads a lot.
|
|
|
|
if ($cat === 'system') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-20 08:19:26 -04:00
|
|
|
$configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
|
|
|
|
while ($config = DBA::fetch($configs)) {
|
2018-03-03 12:09:12 -05:00
|
|
|
$k = $config['k'];
|
2018-03-06 20:04:04 -05:00
|
|
|
|
2019-02-04 09:30:48 +01:00
|
|
|
$this->configCache->set($cat, $k, $config['v']);
|
2018-03-06 20:04:04 -05:00
|
|
|
|
|
|
|
if ($cat !== 'config') {
|
|
|
|
$this->cache[$cat][$k] = $config['v'];
|
|
|
|
$this->in_db[$cat][$k] = true;
|
2018-03-03 12:09:12 -05:00
|
|
|
}
|
|
|
|
}
|
2018-07-20 08:19:26 -04:00
|
|
|
DBA::close($configs);
|
2018-03-03 12:09:12 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 23:42:49 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-03-03 12:09:12 -05:00
|
|
|
public function get($cat, $k, $default_value = null, $refresh = false)
|
|
|
|
{
|
2019-02-07 20:44:03 +01:00
|
|
|
if (!$this->isConnected()) {
|
|
|
|
return $default_value;
|
|
|
|
}
|
|
|
|
|
2018-03-03 12:09:12 -05:00
|
|
|
if (!$refresh) {
|
|
|
|
// Do we have the cached value? Then return it
|
|
|
|
if (isset($this->cache[$cat][$k])) {
|
|
|
|
if ($this->cache[$cat][$k] === '!<unset>!') {
|
|
|
|
return $default_value;
|
|
|
|
} else {
|
|
|
|
return $this->cache[$cat][$k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 08:19:26 -04:00
|
|
|
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
|
2018-07-21 08:46:04 -04:00
|
|
|
if (DBA::isResult($config)) {
|
2018-03-03 12:09:12 -05:00
|
|
|
// manage array value
|
|
|
|
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
|
|
|
|
|
|
|
|
// Assign the value from the database to the cache
|
|
|
|
$this->cache[$cat][$k] = $value;
|
|
|
|
$this->in_db[$cat][$k] = true;
|
|
|
|
return $value;
|
2019-02-04 09:30:48 +01:00
|
|
|
} elseif ($this->configCache->get($cat, $k) !== null) {
|
2018-11-25 01:44:51 -05:00
|
|
|
// Assign the value (mostly) from config/local.config.php file to the cache
|
2019-02-04 09:30:48 +01:00
|
|
|
$this->cache[$cat][$k] = $this->configCache->get($cat, $k);
|
2018-03-03 12:09:12 -05:00
|
|
|
$this->in_db[$cat][$k] = false;
|
|
|
|
|
2019-02-04 09:30:48 +01:00
|
|
|
return $this->configCache->get($cat, $k);
|
|
|
|
} elseif ($this->configCache->get('config', $k) !== null) {
|
2018-11-25 01:44:51 -05:00
|
|
|
// Assign the value (mostly) from config/local.config.php file to the cache
|
2019-02-04 09:30:48 +01:00
|
|
|
$this->cache[$k] = $this->configCache->get('config', $k);
|
2018-07-11 22:24:20 -04:00
|
|
|
$this->in_db[$k] = false;
|
|
|
|
|
2019-02-04 09:30:48 +01:00
|
|
|
return $this->configCache->get('config', $k);
|
2018-03-03 12:09:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->cache[$cat][$k] = '!<unset>!';
|
|
|
|
$this->in_db[$cat][$k] = false;
|
|
|
|
|
|
|
|
return $default_value;
|
|
|
|
}
|
|
|
|
|
2019-02-05 23:42:49 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-03-03 12:09:12 -05:00
|
|
|
public function set($cat, $k, $value)
|
|
|
|
{
|
2019-02-07 20:44:03 +01:00
|
|
|
if (!$this->isConnected()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-03 12:09:12 -05:00
|
|
|
// We store our setting values in a string variable.
|
|
|
|
// So we have to do the conversion here so that the compare below works.
|
|
|
|
// The exception are array values.
|
|
|
|
$dbvalue = (!is_array($value) ? (string)$value : $value);
|
|
|
|
|
|
|
|
$stored = $this->get($cat, $k, null, true);
|
|
|
|
|
2018-07-18 22:30:55 +02:00
|
|
|
if (!isset($this->in_db[$cat])) {
|
|
|
|
$this->in_db[$cat] = [];
|
|
|
|
}
|
|
|
|
if (!isset($this->in_db[$cat][$k])) {
|
|
|
|
$this->in_db[$cat] = false;
|
|
|
|
}
|
|
|
|
|
2018-03-03 12:09:12 -05:00
|
|
|
if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-04 09:30:48 +01:00
|
|
|
$this->configCache->set($cat, $k, $value);
|
2018-03-03 12:09:12 -05:00
|
|
|
|
|
|
|
// Assign the just added value to the cache
|
|
|
|
$this->cache[$cat][$k] = $dbvalue;
|
|
|
|
|
|
|
|
// manage array value
|
|
|
|
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
|
|
|
|
|
2018-07-20 08:19:26 -04:00
|
|
|
$result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
|
2018-03-03 12:09:12 -05:00
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
$this->in_db[$cat][$k] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-02-05 23:42:49 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-03-03 12:09:12 -05:00
|
|
|
public function delete($cat, $k)
|
|
|
|
{
|
2019-02-07 20:44:03 +01:00
|
|
|
if (!$this->isConnected()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-03 12:09:12 -05:00
|
|
|
if (isset($this->cache[$cat][$k])) {
|
|
|
|
unset($this->cache[$cat][$k]);
|
|
|
|
unset($this->in_db[$cat][$k]);
|
|
|
|
}
|
|
|
|
|
2018-07-20 08:19:26 -04:00
|
|
|
$result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
|
2018-03-03 12:09:12 -05:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|