2019-02-10 19:52:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core\Config\Adapter;
|
|
|
|
|
|
|
|
use Friendica\Database\DBA;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Preload Configuration Adapter
|
|
|
|
*
|
|
|
|
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
|
|
|
|
*
|
|
|
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
|
|
|
*/
|
|
|
|
class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
|
|
|
|
{
|
|
|
|
private $config_loaded = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function load($cat = 'config')
|
|
|
|
{
|
|
|
|
$return = [];
|
|
|
|
|
|
|
|
if (!$this->isConnected()) {
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->config_loaded) {
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$configs = DBA::select('config', ['cat', 'v', 'k']);
|
|
|
|
while ($config = DBA::fetch($configs)) {
|
2019-02-18 14:00:34 +01:00
|
|
|
$value = $config['v'];
|
2019-02-23 00:09:57 +01:00
|
|
|
if (isset($value)) {
|
2019-02-18 14:00:34 +01:00
|
|
|
$return[$config['cat']][$config['k']] = $value;
|
|
|
|
}
|
2019-02-10 19:52:21 +01:00
|
|
|
}
|
|
|
|
DBA::close($configs);
|
|
|
|
|
|
|
|
$this->config_loaded = true;
|
|
|
|
|
2019-02-11 21:36:26 +01:00
|
|
|
return $return;
|
2019-02-10 19:52:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function get($cat, $key)
|
|
|
|
{
|
|
|
|
if (!$this->isConnected()) {
|
2019-02-22 23:51:13 +01:00
|
|
|
return null;
|
2019-02-10 19:52:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
|
|
|
|
if (DBA::isResult($config)) {
|
|
|
|
// manage array value
|
|
|
|
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
|
|
|
|
|
2019-02-23 00:09:57 +01:00
|
|
|
if (isset($value)) {
|
2019-02-18 11:27:51 +01:00
|
|
|
return $value;
|
|
|
|
}
|
2019-02-10 19:52:21 +01:00
|
|
|
}
|
2019-02-18 11:27:51 +01:00
|
|
|
|
2019-02-22 23:51:13 +01:00
|
|
|
return null;
|
2019-02-10 19:52:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function set($cat, $key, $value)
|
|
|
|
{
|
|
|
|
if (!$this->isConnected()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We store our setting values as strings.
|
|
|
|
// So we have to do the conversion here so that the compare below works.
|
|
|
|
// The exception are array values.
|
|
|
|
$compare_value = !is_array($value) ? (string)$value : $value;
|
|
|
|
|
|
|
|
if ($this->get($cat, $key) === $compare_value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// manage array value
|
|
|
|
$dbvalue = is_array($value) ? serialize($value) : $value;
|
|
|
|
|
2019-02-22 23:51:13 +01:00
|
|
|
return DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
|
2019-02-10 19:52:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function delete($cat, $key)
|
|
|
|
{
|
|
|
|
if (!$this->isConnected()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-22 23:51:13 +01:00
|
|
|
return DBA::delete('config', ['cat' => $cat, 'k' => $key]);
|
2019-02-10 19:52:21 +01:00
|
|
|
}
|
2019-02-11 21:36:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function isLoaded($cat, $key)
|
|
|
|
{
|
|
|
|
if (!$this->isConnected()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->config_loaded;
|
|
|
|
}
|
2019-02-10 19:52:21 +01:00
|
|
|
}
|