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