3 namespace Friendica\Core\Config\Adapter;
5 use Friendica\Database\DBA;
8 * Preload User Configuration Adapter
10 * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
12 * @author Hypolite Petovan <hypolite@mrpetovan.com>
14 class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
16 private $config_loaded = false;
19 * @param int $uid The UID of the current user
21 public function __construct($uid = null)
23 parent::__construct();
26 $this->load($uid, 'config');
33 public function load($uid, $cat)
37 if ($this->config_loaded) {
45 $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
46 while ($pconfig = DBA::fetch($pconfigs)) {
47 $return[$pconfig['cat']][$pconfig['k']] = $pconfig['v'];
49 DBA::close($pconfigs);
51 $this->config_loaded = true;
59 public function get($uid, $cat, $key)
61 if (!$this->isConnected()) {
65 if (!$this->config_loaded) {
66 $this->load($uid, $cat);
69 $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
70 if (DBA::isResult($config)) {
72 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
83 public function set($uid, $cat, $key, $value)
85 if (!$this->isConnected()) {
89 if (!$this->config_loaded) {
90 $this->load($uid, $cat);
92 // We store our setting values as strings.
93 // So we have to do the conversion here so that the compare below works.
94 // The exception are array values.
95 $compare_value = !is_array($value) ? (string)$value : $value;
97 if ($this->get($uid, $cat, $key) === $compare_value) {
101 // manage array value
102 $dbvalue = is_array($value) ? serialize($value) : $value;
104 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
112 public function delete($uid, $cat, $key)
114 if (!$this->isConnected()) {
118 if (!$this->config_loaded) {
119 $this->load($uid, $cat);
122 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
130 public function isLoaded($uid, $cat, $key)
132 if (!$this->isConnected()) {
136 return $this->config_loaded;