Merge pull request #5438 from MrPetovan/task/5410-remove-dbm-class
[friendica.git/.git] / src / Core / Config / JITPConfigAdapter.php
1 <?php
2 namespace Friendica\Core\Config;
3
4 use Friendica\BaseObject;
5 use Friendica\Database\DBA;
6
7 require_once 'include/dba.php';
8
9 /**
10  * JustInTime User Configuration Adapter
11  *
12  * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
13  *
14  * @author Hypolite Petovan <mrpetovan@gmail.com>
15  */
16 class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
17 {
18         private $in_db;
19
20         public function load($uid, $cat)
21         {
22                 $a = self::getApp();
23
24                 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
25                 if (DBA::isResult($pconfigs)) {
26                         while ($pconfig = DBA::fetch($pconfigs)) {
27                                 $k = $pconfig['k'];
28
29                                 self::getApp()->setPConfigValue($uid, $cat, $k, $pconfig['v']);
30
31                                 $this->in_db[$uid][$cat][$k] = true;
32                         }
33                 } else if ($cat != 'config') {
34                         // Negative caching
35                         $a->config[$uid][$cat] = "!<unset>!";
36                 }
37                 DBA::close($pconfigs);
38         }
39
40         public function get($uid, $cat, $k, $default_value = null, $refresh = false)
41         {
42                 $a = self::getApp();
43
44                 if (!$refresh) {
45                         // Looking if the whole family isn't set
46                         if (isset($a->config[$uid][$cat])) {
47                                 if ($a->config[$uid][$cat] === '!<unset>!') {
48                                         return $default_value;
49                                 }
50                         }
51
52                         if (isset($a->config[$uid][$cat][$k])) {
53                                 if ($a->config[$uid][$cat][$k] === '!<unset>!') {
54                                         return $default_value;
55                                 }
56                                 return $a->config[$uid][$cat][$k];
57                         }
58                 }
59
60                 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
61                 if (DBA::isResult($pconfig)) {
62                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
63
64                         self::getApp()->setPConfigValue($uid, $cat, $k, $val);
65
66                         $this->in_db[$uid][$cat][$k] = true;
67
68                         return $val;
69                 } else {
70                         self::getApp()->setPConfigValue($uid, $cat, $k, '!<unset>!');
71
72                         $this->in_db[$uid][$cat][$k] = false;
73
74                         return $default_value;
75                 }
76         }
77
78         public function set($uid, $cat, $k, $value)
79         {
80                 // We store our setting values in a string variable.
81                 // So we have to do the conversion here so that the compare below works.
82                 // The exception are array values.
83                 $dbvalue = (!is_array($value) ? (string)$value : $value);
84
85                 $stored = $this->get($uid, $cat, $k, null, true);
86
87                 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
88                         return true;
89                 }
90
91                 self::getApp()->setPConfigValue($uid, $cat, $k, $value);
92
93                 // manage array value
94                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
95
96                 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
97
98                 if ($result) {
99                         $this->in_db[$uid][$cat][$k] = true;
100                 }
101
102                 return $result;
103         }
104
105         public function delete($uid, $cat, $k)
106         {
107                 self::getApp()->deletePConfigValue($uid, $cat, $k);
108
109                 if (!empty($this->in_db[$uid][$cat][$k])) {
110                         unset($this->in_db[$uid][$cat][$k]);
111                 }
112
113                 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
114
115                 return $result;
116         }
117 }