3 namespace Friendica\Core\Config;
6 use Friendica\BaseObject;
7 use Friendica\Database\DBA;
8 use Friendica\Database\DBM;
10 require_once 'include/dba.php';
13 * Preload User Configuration Adapter
15 * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
17 * @author Hypolite Petovan <mrpetovan@gmail.com>
19 class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
21 private $config_loaded = false;
23 public function __construct($uid)
25 $this->load($uid, 'config');
28 public function load($uid, $family)
30 if ($this->config_loaded) {
38 $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
39 while ($pconfig = DBA::fetch($pconfigs)) {
40 self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
42 DBA::close($pconfigs);
44 $this->config_loaded = true;
47 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
49 if (!$this->config_loaded) {
50 $this->load($uid, $cat);
54 $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
55 if (DBM::is_result($config)) {
56 self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']);
58 self::getApp()->deletePConfigValue($uid, $cat, $k);
62 $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
67 public function set($uid, $cat, $k, $value)
69 if (!$this->config_loaded) {
70 $this->load($uid, $cat);
72 // We store our setting values as strings.
73 // So we have to do the conversion here so that the compare below works.
74 // The exception are array values.
75 $compare_value = !is_array($value) ? (string)$value : $value;
77 if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
81 self::getApp()->setPConfigValue($uid, $cat, $k, $value);
84 $dbvalue = is_array($value) ? serialize($value) : $value;
86 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
88 throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
94 public function delete($uid, $cat, $k)
96 if (!$this->config_loaded) {
97 $this->load($uid, $cat);
100 self::getApp()->deletePConfigValue($uid, $cat, $k);
102 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);