2 namespace Friendica\Core\Config;
4 use Friendica\BaseObject;
5 use Friendica\Database\DBA;
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 config/local.ini.php file 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];
74 } elseif (isset($a->config[$k])) {
75 // Assign the value (mostly) from config/local.ini.php file to the cache
76 $this->cache[$k] = $a->config[$k];
77 $this->in_db[$k] = false;
79 return $a->config[$k];
82 $this->cache[$cat][$k] = '!<unset>!';
83 $this->in_db[$cat][$k] = false;
85 return $default_value;
88 public function set($cat, $k, $value)
92 // We store our setting values in a string variable.
93 // So we have to do the conversion here so that the compare below works.
94 // The exception are array values.
95 $dbvalue = (!is_array($value) ? (string)$value : $value);
97 $stored = $this->get($cat, $k, null, true);
99 if (!isset($this->in_db[$cat])) {
100 $this->in_db[$cat] = [];
102 if (!isset($this->in_db[$cat][$k])) {
103 $this->in_db[$cat] = false;
106 if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
110 self::getApp()->setConfigValue($cat, $k, $value);
112 // Assign the just added value to the cache
113 $this->cache[$cat][$k] = $dbvalue;
115 // manage array value
116 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
118 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
121 $this->in_db[$cat][$k] = true;
127 public function delete($cat, $k)
129 if (isset($this->cache[$cat][$k])) {
130 unset($this->cache[$cat][$k]);
131 unset($this->in_db[$cat][$k]);
134 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);