]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/JITPConfigAdapter.php
bugfix marking of db-values
[friendica.git] / src / Core / Config / Adapter / JITPConfigAdapter.php
1 <?php
2 namespace Friendica\Core\Config\Adapter;
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 extends AbstractDbaConfigAdapter implements IPConfigAdapter
14 {
15         private $in_db;
16
17         /**
18          * {@inheritdoc}
19          */
20         public function load($uid, $cat)
21         {
22                 $return = [];
23
24                 if (!$this->isConnected()) {
25                         return $return;
26                 }
27
28                 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
29                 if (DBA::isResult($pconfigs)) {
30                         while ($pconfig = DBA::fetch($pconfigs)) {
31                                 $key = $pconfig['k'];
32                                 $value = $this->toConfigValue($pconfig['v']);
33
34                                 // The value was in the db, so don't check it again (unless you have to)
35                                 $this->in_db[$uid][$cat][$key] = true;
36
37                                 if (isset($value)) {
38                                         $return[$key] = $value;
39                                 }
40                         }
41                 } else if ($cat != 'config') {
42                         // Negative caching
43                         $return = null;
44                 }
45                 DBA::close($pconfigs);
46
47                 return [$cat => $return];
48         }
49
50         /**
51          * {@inheritdoc}
52          *
53          * @param bool $mark if true, mark the selection of the current cat/key pair
54          */
55         public function get($uid, $cat, $key, $mark = true)
56         {
57                 if (!$this->isConnected()) {
58                         return null;
59                 }
60
61                 // The value was in the db, so don't check it again (unless you have to)
62                 if ($mark) {
63                         $this->in_db[$uid][$cat][$key] = true;
64                 }
65
66                 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
67                 if (DBA::isResult($pconfig)) {
68                         $value = $this->toConfigValue($pconfig['v']);
69
70                         if (isset($value)) {
71                                 return $value;
72                         }
73                 }
74
75                 $this->in_db[$uid][$cat][$key] = false;
76                 return null;
77         }
78
79         /**
80          * {@inheritdoc}
81          */
82         public function set($uid, $cat, $key, $value)
83         {
84                 if (!$this->isConnected()) {
85                         return false;
86                 }
87
88                 // We store our setting values in a string variable.
89                 // So we have to do the conversion here so that the compare below works.
90                 // The exception are array values.
91                 $dbvalue = (!is_array($value) ? (string)$value : $value);
92
93                 $stored = $this->get($uid, $cat, $key, false);
94
95                 if (!isset($this->in_db[$uid])) {
96                         $this->in_db[$uid] = [];
97                 }
98                 if (!isset($this->in_db[$uid][$cat])) {
99                         $this->in_db[$uid][$cat] = [];
100                 }
101                 if (!isset($this->in_db[$uid][$cat][$key])) {
102                         $this->in_db[$uid][$cat][$key] = false;
103                 }
104
105                 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$key]) {
106                         return true;
107                 }
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' => $key], true);
113
114                 $this->in_db[$uid][$cat][$key] = $result;
115
116                 return $result;
117         }
118
119         /**
120          * {@inheritdoc}
121          */
122         public function delete($uid, $cat, $key)
123         {
124                 if (!$this->isConnected()) {
125                         return false;
126                 }
127
128                 if (isset($this->in_db[$uid][$cat][$key])) {
129                         unset($this->in_db[$uid][$cat][$key]);
130                 }
131
132                 return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
133         }
134
135         /**
136          * {@inheritdoc}
137          */
138         public function isLoaded($uid, $cat, $key)
139         {
140                 if (!$this->isConnected()) {
141                         return false;
142                 }
143
144                 return (isset($this->in_db[$uid][$cat][$key])) && $this->in_db[$uid][$cat][$key];
145         }
146 }