]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/JITPConfigAdapter.php
Merge pull request #6678 from rabuzarus/20190217_-_fix_magicLinks_mentions
[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                         $this->in_db[$uid][$cat][$key] = true;
61                         return $value;
62                 } else {
63
64                         $this->in_db[$uid][$cat][$key] = false;
65                         return '!<unset>!';
66                 }
67         }
68
69         /**
70          * {@inheritdoc}
71          */
72         public function set($uid, $cat, $key, $value)
73         {
74                 if (!$this->isConnected()) {
75                         return false;
76                 }
77
78                 // We store our setting values in a string variable.
79                 // So we have to do the conversion here so that the compare below works.
80                 // The exception are array values.
81                 $dbvalue = (!is_array($value) ? (string)$value : $value);
82
83                 $stored = $this->get($uid, $cat, $key);
84
85                 if (!isset($this->in_db[$uid])) {
86                         $this->in_db[$uid] = [];
87                 }
88                 if (!isset($this->in_db[$uid][$cat])) {
89                         $this->in_db[$uid][$cat] = [];
90                 }
91                 if (!isset($this->in_db[$uid][$cat][$key])) {
92                         $this->in_db[$uid][$cat][$key] = false;
93                 }
94
95                 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$key]) {
96                         return true;
97                 }
98
99                 // manage array value
100                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
101
102                 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
103
104                 $this->in_db[$uid][$cat][$key] = $result;
105
106                 return $result;
107         }
108
109         /**
110          * {@inheritdoc}
111          */
112         public function delete($uid, $cat, $key)
113         {
114                 if (!$this->isConnected()) {
115                         return false;
116                 }
117
118                 if (!empty($this->in_db[$uid][$cat][$key])) {
119                         unset($this->in_db[$uid][$cat][$key]);
120                 }
121
122                 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
123
124                 return $result;
125         }
126
127         /**
128          * {@inheritdoc}
129          */
130         public function isLoaded($uid, $cat, $key)
131         {
132                 if (!$this->isConnected()) {
133                         return false;
134                 }
135
136                 return (isset($this->in_db[$uid][$cat][$key])) && $this->in_db[$uid][$cat][$key];
137         }
138 }