]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/JITPConfigAdapter.php
negative set
[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 = $pconfig['v'];
33
34                                 if (isset($value) && $value !== '') {
35                                         $return[$key] = $value;
36                                         $this->in_db[$uid][$cat][$key] = true;
37                                 }
38                         }
39                 } else if ($cat != 'config') {
40                         // Negative caching
41                         $return = "!<unset>!";
42                 }
43                 DBA::close($pconfigs);
44
45                 return [$cat => $return];
46         }
47
48         /**
49          * {@inheritdoc}
50          */
51         public function get($uid, $cat, $key)
52         {
53                 if (!$this->isConnected()) {
54                         return '!<unset>!';
55                 }
56
57                 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
58                 if (DBA::isResult($pconfig)) {
59                         // manage array value
60                         $value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
61
62                         if (isset($value) && $value !== '') {
63                                 $this->in_db[$uid][$cat][$key] = true;
64                                 return $value;
65                         }
66                 }
67
68                 $this->in_db[$uid][$cat][$key] = false;
69                 return '!<unset>!';
70         }
71
72         /**
73          * {@inheritdoc}
74          */
75         public function set($uid, $cat, $key, $value)
76         {
77                 if (!$this->isConnected()) {
78                         return false;
79                 }
80
81                 // We store our setting values in a string variable.
82                 // So we have to do the conversion here so that the compare below works.
83                 // The exception are array values.
84                 $dbvalue = (!is_array($value) ? (string)$value : $value);
85
86                 $stored = $this->get($uid, $cat, $key);
87
88                 if (!isset($this->in_db[$uid])) {
89                         $this->in_db[$uid] = [];
90                 }
91                 if (!isset($this->in_db[$uid][$cat])) {
92                         $this->in_db[$uid][$cat] = [];
93                 }
94                 if (!isset($this->in_db[$uid][$cat][$key])) {
95                         $this->in_db[$uid][$cat][$key] = false;
96                 }
97
98                 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$key]) {
99                         return true;
100                 }
101
102                 // manage array value
103                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
104
105                 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
106
107                 $this->in_db[$uid][$cat][$key] = $result;
108
109                 return $result;
110         }
111
112         /**
113          * {@inheritdoc}
114          */
115         public function delete($uid, $cat, $key)
116         {
117                 if (!$this->isConnected()) {
118                         return false;
119                 }
120
121                 if (!empty($this->in_db[$uid][$cat][$key])) {
122                         unset($this->in_db[$uid][$cat][$key]);
123                 }
124
125                 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
126
127                 return $result;
128         }
129
130         /**
131          * {@inheritdoc}
132          */
133         public function isLoaded($uid, $cat, $key)
134         {
135                 if (!$this->isConnected()) {
136                         return false;
137                 }
138
139                 return (isset($this->in_db[$uid][$cat][$key])) && $this->in_db[$uid][$cat][$key];
140         }
141 }