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