3 namespace Friendica\Core\Config;
8 use Friendica\BaseObject;
9 use Friendica\Database\DBM;
11 require_once 'include/dba.php';
14 * Preload User Configuration Adapter
16 * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
18 * @author Hypolite Petovan <mrpetovan@gmail.com>
20 class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
22 private $config_loaded = false;
24 public function __construct($uid)
26 $this->load($uid, 'config');
29 public function load($uid, $family)
31 if ($this->config_loaded) {
39 $pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
40 while ($pconfig = dba::fetch($pconfigs)) {
41 self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
43 dba::close($pconfigs);
45 $this->config_loaded = true;
48 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
50 if (!$this->config_loaded) {
51 $this->load($uid, $cat);
55 $config = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
56 if (DBM::is_result($config)) {
57 self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']);
59 self::getApp()->deletePConfigValue($uid, $cat, $k);
63 $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
68 public function set($uid, $cat, $k, $value)
70 if (!$this->config_loaded) {
71 $this->load($uid, $cat);
73 // We store our setting values as strings.
74 // So we have to do the conversion here so that the compare below works.
75 // The exception are array values.
76 $compare_value = !is_array($value) ? (string)$value : $value;
78 if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
82 self::getApp()->setPConfigValue($uid, $cat, $k, $value);
85 $dbvalue = is_array($value) ? serialize($value) : $value;
87 $result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
89 throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
95 public function delete($uid, $cat, $k)
97 if (!$this->config_loaded) {
98 $this->load($uid, $cat);
101 self::getApp()->deletePConfigValue($uid, $cat, $k);
103 $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);