3 namespace Friendica\Core\Config;
6 use Friendica\BaseObject;
7 use Friendica\Database\DBA;
9 require_once 'include/dba.php';
12 * Preload User Configuration Adapter
14 * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
16 * @author Hypolite Petovan <mrpetovan@gmail.com>
18 class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
20 private $config_loaded = false;
22 public function __construct($uid)
24 $this->load($uid, 'config');
27 public function load($uid, $family)
29 if ($this->config_loaded) {
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']);
41 DBA::close($pconfigs);
43 $this->config_loaded = true;
46 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
48 if (!$this->config_loaded) {
49 $this->load($uid, $cat);
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']);
57 self::getApp()->deletePConfigValue($uid, $cat, $k);
61 $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
66 public function set($uid, $cat, $k, $value)
68 if (!$this->config_loaded) {
69 $this->load($uid, $cat);
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;
76 if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
80 self::getApp()->setPConfigValue($uid, $cat, $k, $value);
83 $dbvalue = is_array($value) ? serialize($value) : $value;
85 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
87 throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
93 public function delete($uid, $cat, $k)
95 if (!$this->config_loaded) {
96 $this->load($uid, $cat);
99 self::getApp()->deletePConfigValue($uid, $cat, $k);
101 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);