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
17 * @var array true if config for user is loaded
19 private $config_loaded;
22 * @param int $uid The UID of the current user
24 public function __construct($uid = null)
26 parent::__construct();
28 $this->config_loaded = [];
31 $this->load($uid, 'config');
38 public function load($uid, $cat)
46 if (!$this->isLoaded($uid, $cat, null)) {
50 $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
51 while ($pconfig = DBA::fetch($pconfigs)) {
52 $value = $this->toConfigValue($pconfig['v']);
54 $return[$pconfig['cat']][$pconfig['k']] = $value;
57 DBA::close($pconfigs);
59 $this->config_loaded[$uid] = true;
67 public function get($uid, $cat, $key)
69 if (!$this->isConnected()) {
73 if (!$this->isLoaded($uid, $cat, $key)) {
74 $this->load($uid, $cat);
77 $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
78 if (DBA::isResult($config)) {
79 $value = $this->toConfigValue($config['v']);
91 public function set($uid, $cat, $key, $value)
93 if (!$this->isConnected()) {
97 if (!$this->isLoaded($uid, $cat, $key)) {
98 $this->load($uid, $cat);
100 // We store our setting values as strings.
101 // So we have to do the conversion here so that the compare below works.
102 // The exception are array values.
103 $compare_value = !is_array($value) ? (string)$value : $value;
104 $stored_value = $this->get($uid, $cat, $key);
106 if (isset($stored_value) && $stored_value === $compare_value) {
110 $dbvalue = $this->toDbValue($value);
112 return DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
118 public function delete($uid, $cat, $key)
120 if (!$this->isConnected()) {
124 if (!$this->isLoaded($uid, $cat, $key)) {
125 $this->load($uid, $cat);
128 return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
134 public function isLoaded($uid, $cat, $key)
136 if (!$this->isConnected()) {
140 return isset($this->config_loaded[$uid]) && $this->config_loaded[$uid];