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 $value = $pconfig['v'];
48 if (isset($value) && $value !== '') {
49 $return[$pconfig['cat']][$pconfig['k']] = $value;
52 DBA::close($pconfigs);
54 $this->config_loaded = true;
62 public function get($uid, $cat, $key)
64 if (!$this->isConnected()) {
68 if (!$this->config_loaded) {
69 $this->load($uid, $cat);
72 $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
73 if (DBA::isResult($config)) {
75 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
77 if (isset($value) && $value !== '') {
87 public function set($uid, $cat, $key, $value)
89 if (!$this->isConnected()) {
93 if (!$this->config_loaded) {
94 $this->load($uid, $cat);
96 // We store our setting values as strings.
97 // So we have to do the conversion here so that the compare below works.
98 // The exception are array values.
99 $compare_value = !is_array($value) ? (string)$value : $value;
101 if ($this->get($uid, $cat, $key) === $compare_value) {
105 // manage array value
106 $dbvalue = is_array($value) ? serialize($value) : $value;
108 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
116 public function delete($uid, $cat, $key)
118 if (!$this->isConnected()) {
122 if (!$this->config_loaded) {
123 $this->load($uid, $cat);
126 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
134 public function isLoaded($uid, $cat, $key)
136 if (!$this->isConnected()) {
140 return $this->config_loaded;