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