3 namespace Friendica\Core\Config;
6 use Friendica\Database\DBA;
9 * Preload User Configuration Adapter
11 * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
13 * @author Hypolite Petovan <hypolite@mrpetovan.com>
15 class PreloadPConfigAdapter implements IPConfigAdapter
17 private $config_loaded = false;
20 * The config cache of this adapter
26 * @param IPConfigCache $config The config cache of this adapter
27 * @param int $uid The UID of the current user
29 public function __construct($config, $uid = null)
31 $this->config = $config;
33 $this->load($uid, 'config');
37 public function load($uid, $family)
39 if ($this->config_loaded) {
47 $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
48 while ($pconfig = DBA::fetch($pconfigs)) {
49 $this->config->setP($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
51 DBA::close($pconfigs);
53 $this->config_loaded = true;
56 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
58 if (!$this->config_loaded) {
59 $this->load($uid, $cat);
63 $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
64 if (DBA::isResult($config)) {
65 $this->config->setP($uid, $cat, $k, $config['v']);
67 $this->config->deleteP($uid, $cat, $k);
71 return $this->config->getP($uid, $cat, $k, $default_value);;
74 public function set($uid, $cat, $k, $value)
76 if (!$this->config_loaded) {
77 $this->load($uid, $cat);
79 // We store our setting values as strings.
80 // So we have to do the conversion here so that the compare below works.
81 // The exception are array values.
82 $compare_value = !is_array($value) ? (string)$value : $value;
84 if ($this->config->getP($uid, $cat, $k) === $compare_value) {
88 $this->config->setP($uid, $cat, $k, $value);
91 $dbvalue = is_array($value) ? serialize($value) : $value;
93 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
95 throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
101 public function delete($uid, $cat, $k)
103 if (!$this->config_loaded) {
104 $this->load($uid, $cat);
107 $this->config->deleteP($uid, $cat, $k);
109 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);