]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/JITPConfigAdapter.php
223cc542dcdb01e58d80e311d7d033dbef943994
[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 $config;
22
23         /**
24          * @param IPConfigCache $config The config cache of this adapter
25          */
26         public function __construct($config)
27         {
28                 $this->config = $config;
29         }
30
31         public function load($uid, $cat)
32         {
33                 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
34                 if (DBA::isResult($pconfigs)) {
35                         while ($pconfig = DBA::fetch($pconfigs)) {
36                                 $k = $pconfig['k'];
37
38                                 $this->config->setP($uid, $cat, $k, $pconfig['v']);
39
40                                 $this->in_db[$uid][$cat][$k] = true;
41                         }
42                 } else if ($cat != 'config') {
43                         // Negative caching
44                         $this->config->setP($uid, $cat, null, "!<unset>!");
45                 }
46                 DBA::close($pconfigs);
47         }
48
49         public function get($uid, $cat, $k, $default_value = null, $refresh = false)
50         {
51                 if (!$refresh) {
52                         // Looking if the whole family isn't set
53                         if ($this->config->getP($uid, $cat) !== null) {
54                                 if ($this->config->getP($uid, $cat) === '!<unset>!') {
55                                         return $default_value;
56                                 }
57                         }
58
59                         if ($this->config->getP($uid, $cat, $k) !== null) {
60                                 if ($this->config->getP($uid, $cat, $k) === '!<unset>!') {
61                                         return $default_value;
62                                 }
63                                 return $this->config->getP($uid, $cat, $k);
64                         }
65                 }
66
67                 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
68                 if (DBA::isResult($pconfig)) {
69                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
70
71                         $this->config->setP($uid, $cat, $k, $val);
72
73                         $this->in_db[$uid][$cat][$k] = true;
74
75                         return $val;
76                 } else {
77                         $this->config->setP($uid, $cat, $k, '!<unset>!');
78
79                         $this->in_db[$uid][$cat][$k] = false;
80
81                         return $default_value;
82                 }
83         }
84
85         public function set($uid, $cat, $k, $value)
86         {
87                 // We store our setting values in a string variable.
88                 // So we have to do the conversion here so that the compare below works.
89                 // The exception are array values.
90                 $dbvalue = (!is_array($value) ? (string)$value : $value);
91
92                 $stored = $this->get($uid, $cat, $k, null, true);
93
94                 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
95                         return true;
96                 }
97
98                 $this->config->setP($uid, $cat, $k, $value);
99
100                 // manage array value
101                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
102
103                 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
104
105                 if ($result) {
106                         $this->in_db[$uid][$cat][$k] = true;
107                 }
108
109                 return $result;
110         }
111
112         public function delete($uid, $cat, $k)
113         {
114                 $this->config->deleteP($uid, $cat, $k);
115
116                 if (!empty($this->in_db[$uid][$cat][$k])) {
117                         unset($this->in_db[$uid][$cat][$k]);
118                 }
119
120                 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
121
122                 return $result;
123         }
124 }