2 namespace Friendica\Core\Config;
5 use Friendica\BaseObject;
6 use Friendica\Database\DBM;
8 require_once 'include/dba.php';
11 * JustInTime Configuration Adapter
13 * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
15 * @author Hypolite Petovan <mrpetovan@gmail.com>
17 class JITConfigAdapter extends BaseObject implements IConfigAdapter
22 public function load($cat = "config")
24 // We don't preload "system" anymore.
25 // This reduces the number of database reads a lot.
26 if ($cat === 'system') {
30 $configs = dba::select('config', ['v', 'k'], ['cat' => $cat]);
31 while ($config = dba::fetch($configs)) {
34 self::getApp()->setConfigValue($cat, $k, $config['v']);
36 if ($cat !== 'config') {
37 $this->cache[$cat][$k] = $config['v'];
38 $this->in_db[$cat][$k] = true;
44 public function get($cat, $k, $default_value = null, $refresh = false)
49 // Do we have the cached value? Then return it
50 if (isset($this->cache[$cat][$k])) {
51 if ($this->cache[$cat][$k] === '!<unset>!') {
52 return $default_value;
54 return $this->cache[$cat][$k];
59 $config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
60 if (DBM::is_result($config)) {
62 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
64 // Assign the value from the database to the cache
65 $this->cache[$cat][$k] = $value;
66 $this->in_db[$cat][$k] = true;
68 } elseif (isset($a->config[$cat][$k])) {
69 // Assign the value (mostly) from the .htconfig.php to the cache
70 $this->cache[$cat][$k] = $a->config[$cat][$k];
71 $this->in_db[$cat][$k] = false;
73 return $a->config[$cat][$k];
76 $this->cache[$cat][$k] = '!<unset>!';
77 $this->in_db[$cat][$k] = false;
79 return $default_value;
82 public function set($cat, $k, $value)
86 // We store our setting values in a string variable.
87 // So we have to do the conversion here so that the compare below works.
88 // The exception are array values.
89 $dbvalue = (!is_array($value) ? (string)$value : $value);
91 $stored = $this->get($cat, $k, null, true);
93 if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
97 self::getApp()->setConfigValue($cat, $k, $value);
99 // Assign the just added value to the cache
100 $this->cache[$cat][$k] = $dbvalue;
102 // manage array value
103 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
105 $result = dba::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
108 $this->in_db[$cat][$k] = true;
115 public function delete($cat, $k)
117 if (isset($this->cache[$cat][$k])) {
118 unset($this->cache[$cat][$k]);
119 unset($this->in_db[$cat][$k]);
122 $result = dba::delete('config', ['cat' => $cat, 'k' => $k]);