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 = $pconfig['v'];
53 if (isset($value) && $value !== '') {
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)) {
80 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
82 if (isset($value) && $value !== '') {
92 public function set($uid, $cat, $key, $value)
94 if (!$this->isConnected()) {
98 if (!$this->isLoaded($uid, $cat, $key)) {
99 $this->load($uid, $cat);
101 // We store our setting values as strings.
102 // So we have to do the conversion here so that the compare below works.
103 // The exception are array values.
104 $compare_value = !is_array($value) ? (string)$value : $value;
106 if ($this->get($uid, $cat, $key) === $compare_value) {
110 // manage array value
111 $dbvalue = is_array($value) ? serialize($value) : $value;
113 return DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
119 public function delete($uid, $cat, $key)
121 if (!$this->isConnected()) {
125 if (!$this->isLoaded($uid, $cat, $key)) {
126 $this->load($uid, $cat);
129 return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
135 public function isLoaded($uid, $cat, $key)
137 if (!$this->isConnected()) {
141 return isset($this->config_loaded[$uid]) && $this->config_loaded[$uid];