Update "mrpetovan" email address
[friendica.git/.git] / src / Core / Config / JITConfigAdapter.php
1 <?php
2 namespace Friendica\Core\Config;
3
4 use Friendica\BaseObject;
5 use Friendica\Database\DBA;
6
7 require_once 'include/dba.php';
8
9 /**
10  * JustInTime Configuration Adapter
11  *
12  * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
13  *
14  * @author Hypolite Petovan <hypolite@mrpetovan.com>
15  */
16 class JITConfigAdapter extends BaseObject implements IConfigAdapter
17 {
18         private $cache;
19         private $in_db;
20
21         public function load($cat = "config")
22         {
23                 // We don't preload "system" anymore.
24                 // This reduces the number of database reads a lot.
25                 if ($cat === 'system') {
26                         return;
27                 }
28
29                 $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
30                 while ($config = DBA::fetch($configs)) {
31                         $k = $config['k'];
32
33                         self::getApp()->setConfigValue($cat, $k, $config['v']);
34
35                         if ($cat !== 'config') {
36                                 $this->cache[$cat][$k] = $config['v'];
37                                 $this->in_db[$cat][$k] = true;
38                         }
39                 }
40                 DBA::close($configs);
41         }
42
43         public function get($cat, $k, $default_value = null, $refresh = false)
44         {
45                 $a = self::getApp();
46
47                 if (!$refresh) {
48                         // Do we have the cached value? Then return it
49                         if (isset($this->cache[$cat][$k])) {
50                                 if ($this->cache[$cat][$k] === '!<unset>!') {
51                                         return $default_value;
52                                 } else {
53                                         return $this->cache[$cat][$k];
54                                 }
55                         }
56                 }
57
58                 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
59                 if (DBA::isResult($config)) {
60                         // manage array value
61                         $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
62
63                         // Assign the value from the database to the cache
64                         $this->cache[$cat][$k] = $value;
65                         $this->in_db[$cat][$k] = true;
66                         return $value;
67                 } elseif (isset($a->config[$cat][$k])) {
68                         // Assign the value (mostly) from config/local.ini.php file to the cache
69                         $this->cache[$cat][$k] = $a->config[$cat][$k];
70                         $this->in_db[$cat][$k] = false;
71
72                         return $a->config[$cat][$k];
73                 } elseif (isset($a->config[$k])) {
74                         // Assign the value (mostly) from config/local.ini.php file to the cache
75                         $this->cache[$k] = $a->config[$k];
76                         $this->in_db[$k] = false;
77
78                         return $a->config[$k];
79                 }
80
81                 $this->cache[$cat][$k] = '!<unset>!';
82                 $this->in_db[$cat][$k] = false;
83
84                 return $default_value;
85         }
86
87         public function set($cat, $k, $value)
88         {
89                 $a = self::getApp();
90
91                 // We store our setting values in a string variable.
92                 // So we have to do the conversion here so that the compare below works.
93                 // The exception are array values.
94                 $dbvalue = (!is_array($value) ? (string)$value : $value);
95
96                 $stored = $this->get($cat, $k, null, true);
97
98                 if (!isset($this->in_db[$cat])) {
99                         $this->in_db[$cat] = [];
100                 }
101                 if (!isset($this->in_db[$cat][$k])) {
102                         $this->in_db[$cat] = false;
103                 }
104
105                 if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
106                         return true;
107                 }
108
109                 self::getApp()->setConfigValue($cat, $k, $value);
110
111                 // Assign the just added value to the cache
112                 $this->cache[$cat][$k] = $dbvalue;
113
114                 // manage array value
115                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
116
117                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
118
119                 if ($result) {
120                         $this->in_db[$cat][$k] = true;
121                 }
122
123                 return $result;
124         }
125
126         public function delete($cat, $k)
127         {
128                 if (isset($this->cache[$cat][$k])) {
129                         unset($this->cache[$cat][$k]);
130                         unset($this->in_db[$cat][$k]);
131                 }
132
133                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
134
135                 return $result;
136         }
137 }