]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/JITPConfigAdapter.php
fixing reset()
[friendica.git] / src / Core / Config / JITPConfigAdapter.php
1 <?php
2 namespace Friendica\Core\Config;
3
4 use Friendica\Database\DBA;
5
6 /**
7  * JustInTime User Configuration Adapter
8  *
9  * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
10  *
11  * @author Hypolite Petovan <hypolite@mrpetovan.com>
12  */
13 class JITPConfigAdapter implements IPConfigAdapter
14 {
15         private $in_db;
16
17         /**
18          * The config cache of this adapter
19          * @var IPConfigCache
20          */
21         private $configCache;
22
23         /**
24          * @param IPConfigCache $configCache The config cache of this adapter
25          */
26         public function __construct(IPConfigCache $configCache)
27         {
28                 $this->configCache = $configCache;
29         }
30
31         /**
32          * {@inheritdoc}
33          */
34         public function load($uid, $cat)
35         {
36                 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
37                 if (DBA::isResult($pconfigs)) {
38                         while ($pconfig = DBA::fetch($pconfigs)) {
39                                 $k = $pconfig['k'];
40
41                                 $this->configCache->setP($uid, $cat, $k, $pconfig['v']);
42
43                                 $this->in_db[$uid][$cat][$k] = true;
44                         }
45                 } else if ($cat != 'config') {
46                         // Negative caching
47                         $this->configCache->setP($uid, $cat, null, "!<unset>!");
48                 }
49                 DBA::close($pconfigs);
50         }
51
52         /**
53          * {@inheritdoc}
54          */
55         public function get($uid, $cat, $k, $default_value = null, $refresh = false)
56         {
57                 if (!$refresh) {
58                         // Looking if the whole family isn't set
59                         if ($this->configCache->getP($uid, $cat) !== null) {
60                                 if ($this->configCache->getP($uid, $cat) === '!<unset>!') {
61                                         return $default_value;
62                                 }
63                         }
64
65                         if ($this->configCache->getP($uid, $cat, $k) !== null) {
66                                 if ($this->configCache->getP($uid, $cat, $k) === '!<unset>!') {
67                                         return $default_value;
68                                 }
69                                 return $this->configCache->getP($uid, $cat, $k);
70                         }
71                 }
72
73                 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
74                 if (DBA::isResult($pconfig)) {
75                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
76
77                         $this->configCache->setP($uid, $cat, $k, $val);
78
79                         $this->in_db[$uid][$cat][$k] = true;
80
81                         return $val;
82                 } else {
83                         $this->configCache->setP($uid, $cat, $k, '!<unset>!');
84
85                         $this->in_db[$uid][$cat][$k] = false;
86
87                         return $default_value;
88                 }
89         }
90
91         /**
92          * {@inheritdoc}
93          */
94         public function set($uid, $cat, $k, $value)
95         {
96                 // We store our setting values in a string variable.
97                 // So we have to do the conversion here so that the compare below works.
98                 // The exception are array values.
99                 $dbvalue = (!is_array($value) ? (string)$value : $value);
100
101                 $stored = $this->get($uid, $cat, $k, null, true);
102
103                 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
104                         return true;
105                 }
106
107                 $this->configCache->setP($uid, $cat, $k, $value);
108
109                 // manage array value
110                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
111
112                 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
113
114                 if ($result) {
115                         $this->in_db[$uid][$cat][$k] = true;
116                 }
117
118                 return $result;
119         }
120
121         /**
122          * {@inheritdoc}
123          */
124         public function delete($uid, $cat, $k)
125         {
126                 $this->configCache->deleteP($uid, $cat, $k);
127
128                 if (!empty($this->in_db[$uid][$cat][$k])) {
129                         unset($this->in_db[$uid][$cat][$k]);
130                 }
131
132                 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
133
134                 return $result;
135         }
136 }