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']);
74 if (isset($value) && $value !== '') {
84 public function set($uid, $cat, $key, $value)
86 if (!$this->isConnected()) {
90 if (!$this->config_loaded) {
91 $this->load($uid, $cat);
93 // We store our setting values as strings.
94 // So we have to do the conversion here so that the compare below works.
95 // The exception are array values.
96 $compare_value = !is_array($value) ? (string)$value : $value;
98 if ($this->get($uid, $cat, $key) === $compare_value) {
102 // manage array value
103 $dbvalue = is_array($value) ? serialize($value) : $value;
105 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
113 public function delete($uid, $cat, $key)
115 if (!$this->isConnected()) {
119 if (!$this->config_loaded) {
120 $this->load($uid, $cat);
123 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
131 public function isLoaded($uid, $cat, $key)
133 if (!$this->isConnected()) {
137 return $this->config_loaded;