]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/PreloadPConfigAdapter.php
negative set
[friendica.git] / src / Core / Config / Adapter / PreloadPConfigAdapter.php
1 <?php
2
3 namespace Friendica\Core\Config\Adapter;
4
5 use Friendica\Database\DBA;
6
7 /**
8  * Preload User Configuration Adapter
9  *
10  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
11  *
12  * @author Hypolite Petovan <hypolite@mrpetovan.com>
13  */
14 class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
15 {
16         private $config_loaded = false;
17
18         /**
19          * @param int $uid The UID of the current user
20          */
21         public function __construct($uid = null)
22         {
23                 parent::__construct();
24
25                 if (isset($uid)) {
26                         $this->load($uid, 'config');
27                 }
28         }
29
30         /**
31          * {@inheritdoc}
32          */
33         public function load($uid, $cat)
34         {
35                 $return = [];
36
37                 if ($this->config_loaded) {
38                         return $return;
39                 }
40
41                 if (empty($uid)) {
42                         return $return;
43                 }
44
45                 $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
46                 while ($pconfig = DBA::fetch($pconfigs)) {
47                         $value = $pconfig['v'];
48                         if (isset($value) && $value !== '') {
49                                 $return[$pconfig['cat']][$pconfig['k']] = $value;
50                         } else {
51                                 $return[$pconfig['cat']][$pconfig['k']] = '!<unset>!';
52                         }
53                 }
54                 DBA::close($pconfigs);
55
56                 $this->config_loaded = true;
57
58                 return $return;
59         }
60
61         /**
62          * {@inheritdoc}
63          */
64         public function get($uid, $cat, $key)
65         {
66                 if (!$this->isConnected()) {
67                         return '!<unset>!';
68                 }
69
70                 if (!$this->config_loaded) {
71                         $this->load($uid, $cat);
72                 }
73
74                 $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
75                 if (DBA::isResult($config)) {
76                         // manage array value
77                         $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
78
79                         if (isset($value) && $value !== '') {
80                                 return $value;
81                         }
82                 }
83                 return '!<unset>!';
84         }
85
86         /**
87          * {@inheritdoc}
88          */
89         public function set($uid, $cat, $key, $value)
90         {
91                 if (!$this->isConnected()) {
92                         return false;
93                 }
94
95                 if (!$this->config_loaded) {
96                         $this->load($uid, $cat);
97                 }
98                 // We store our setting values as strings.
99                 // So we have to do the conversion here so that the compare below works.
100                 // The exception are array values.
101                 $compare_value = !is_array($value) ? (string)$value : $value;
102
103                 if ($this->get($uid, $cat, $key) === $compare_value) {
104                         return true;
105                 }
106
107                 // manage array value
108                 $dbvalue = is_array($value) ? serialize($value) : $value;
109
110                 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
111
112                 return $result;
113         }
114
115         /**
116          * {@inheritdoc}
117          */
118         public function delete($uid, $cat, $key)
119         {
120                 if (!$this->isConnected()) {
121                         return false;
122                 }
123
124                 if (!$this->config_loaded) {
125                         $this->load($uid, $cat);
126                 }
127
128                 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
129
130                 return $result;
131         }
132
133         /**
134          * {@inheritdoc}
135          */
136         public function isLoaded($uid, $cat, $key)
137         {
138                 if (!$this->isConnected()) {
139                         return false;
140                 }
141
142                 return $this->config_loaded;
143         }
144 }