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) {
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']);
39 dba::close($pconfigs);
41 $this->config_loaded = true;
44 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
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']);
51 self::getApp()->deletePConfigValue($uid, $cat, $k);
55 $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
60 public function set($uid, $cat, $k, $value)
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;
67 if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
71 self::getApp()->setPConfigValue($uid, $cat, $k, $value);
74 $dbvalue = is_array($value) ? serialize($value) : $value;
76 $result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
78 throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
84 public function delete($uid, $cat, $k)
86 self::getApp()->deletePConfigValue($uid, $cat, $k);
88 $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);