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