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;
56 $return[$pconfig['cat']][$pconfig['k']] = '!<unset>!';
59 DBA::close($pconfigs);
61 $this->config_loaded[$uid] = true;
69 public function get($uid, $cat, $key)
71 if (!$this->isConnected()) {
75 if (!$this->isLoaded($uid, $cat, $key)) {
76 $this->load($uid, $cat);
79 $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
80 if (DBA::isResult($config)) {
82 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
84 if (isset($value) && $value !== '') {
94 public function set($uid, $cat, $key, $value)
96 if (!$this->isConnected()) {
100 if (!$this->isLoaded($uid, $cat, $key)) {
101 $this->load($uid, $cat);
103 // We store our setting values as strings.
104 // So we have to do the conversion here so that the compare below works.
105 // The exception are array values.
106 $compare_value = !is_array($value) ? (string)$value : $value;
108 if ($this->get($uid, $cat, $key) === $compare_value) {
112 // manage array value
113 $dbvalue = is_array($value) ? serialize($value) : $value;
115 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
123 public function delete($uid, $cat, $key)
125 if (!$this->isConnected()) {
129 if (!$this->isLoaded($uid, $cat, $key)) {
130 $this->load($uid, $cat);
133 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
141 public function isLoaded($uid, $cat, $key)
143 if (!$this->isConnected()) {
147 return isset($this->config_loaded[$uid]) && $this->config_loaded[$uid];