]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/PreloadPConfigAdapter.php
Line feeds fixed, not change in functionality
[friendica.git] / src / Core / Config / PreloadPConfigAdapter.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 use dba;
6 use Exception;
7 use Friendica\App;
8 use Friendica\BaseObject;
9 use Friendica\Database\DBM;
10
11 require_once 'include/dba.php';
12
13 /**
14  * Preload User Configuration Adapter
15  *
16  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
17  *
18  * @author Hypolite Petovan <mrpetovan@gmail.com>
19  */
20 class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
21 {
22         private $config_loaded = false;
23
24         public function __construct($uid)
25         {
26                 $this->load($uid, 'config');
27         }
28
29         public function load($uid, $family)
30         {
31                 if ($this->config_loaded) {
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 ($refresh) {
47                         $config = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
48                         if (DBM::is_result($config)) {
49                                 self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']);
50                         } else {
51                                 self::getApp()->deletePConfigValue($uid, $cat, $k);
52                         }
53                 }
54
55                 $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
56
57                 return $return;
58         }
59
60         public function set($uid, $cat, $k, $value)
61         {
62                 // We store our setting values as strings.
63                 // So we have to do the conversion here so that the compare below works.
64                 // The exception are array values.
65                 $compare_value = !is_array($value) ? (string)$value : $value;
66
67                 if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
68                         return true;
69                 }
70
71                 self::getApp()->setPConfigValue($uid, $cat, $k, $value);
72
73                 // manage array value
74                 $dbvalue = is_array($value) ? serialize($value) : $value;
75
76                 $result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
77                 if (!$result) {
78                         throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
79                 }
80
81                 return true;
82         }
83
84         public function delete($uid, $cat, $k)
85         {
86                 self::getApp()->deletePConfigValue($uid, $cat, $k);
87
88                 $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
89
90                 return $result;
91         }
92 }