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