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