]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/PreloadPConfigAdapter.php
Merge pull request #6400 from annando/issue-6394
[friendica.git] / src / Core / Config / PreloadPConfigAdapter.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 use Exception;
6 use Friendica\BaseObject;
7 use Friendica\Database\DBA;
8
9 /**
10  * Preload User Configuration Adapter
11  *
12  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
13  *
14  * @author Hypolite Petovan <hypolite@mrpetovan.com>
15  */
16 class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
17 {
18         private $config_loaded = false;
19
20         public function __construct($uid)
21         {
22                 $this->load($uid, 'config');
23         }
24
25         public function load($uid, $family)
26         {
27                 if ($this->config_loaded) {
28                         return;
29                 }
30
31                 if (empty($uid)) {
32                         return;
33                 }
34
35                 $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
36                 while ($pconfig = DBA::fetch($pconfigs)) {
37                         self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
38                 }
39                 DBA::close($pconfigs);
40
41                 $this->config_loaded = true;
42         }
43
44         public function get($uid, $cat, $k, $default_value = null, $refresh = false)
45         {
46                 if (!$this->config_loaded) {
47                         $this->load($uid, $cat);
48                 }
49
50                 if ($refresh) {
51                         $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
52                         if (DBA::isResult($config)) {
53                                 self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']);
54                         } else {
55                                 self::getApp()->deletePConfigValue($uid, $cat, $k);
56                         }
57                 }
58
59                 $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
60
61                 return $return;
62         }
63
64         public function set($uid, $cat, $k, $value)
65         {
66                 if (!$this->config_loaded) {
67                         $this->load($uid, $cat);
68                 }
69                 // We store our setting values as strings.
70                 // So we have to do the conversion here so that the compare below works.
71                 // The exception are array values.
72                 $compare_value = !is_array($value) ? (string)$value : $value;
73
74                 if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
75                         return true;
76                 }
77
78                 self::getApp()->setPConfigValue($uid, $cat, $k, $value);
79
80                 // manage array value
81                 $dbvalue = is_array($value) ? serialize($value) : $value;
82
83                 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
84                 if (!$result) {
85                         throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
86                 }
87
88                 return true;
89         }
90
91         public function delete($uid, $cat, $k)
92         {
93                 if (!$this->config_loaded) {
94                         $this->load($uid, $cat);
95                 }
96
97                 self::getApp()->deletePConfigValue($uid, $cat, $k);
98
99                 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
100
101                 return $result;
102         }
103 }