]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Config/Adapter/PreloadPConfigAdapter.php
Merge branch 'master' into develop
[friendica.git] / src / Core / Config / Adapter / PreloadPConfigAdapter.php
index e79a4a1e3cbb93dbfe09d839b9a2d48bf204487f..838f3763dfcd5f26849b3c915bbbd1baa957696b 100644 (file)
@@ -13,7 +13,10 @@ use Friendica\Database\DBA;
  */
 class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
 {
-       private $config_loaded = false;
+       /**
+        * @var array true if config for user is loaded
+        */
+       private $config_loaded;
 
        /**
         * @param int $uid The UID of the current user
@@ -22,6 +25,8 @@ class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfig
        {
                parent::__construct();
 
+               $this->config_loaded = [];
+
                if (isset($uid)) {
                        $this->load($uid, 'config');
                }
@@ -34,21 +39,24 @@ class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfig
        {
                $return = [];
 
-               if ($this->config_loaded) {
+               if (empty($uid)) {
                        return $return;
                }
 
-               if (empty($uid)) {
+               if (!$this->isLoaded($uid, $cat, null)) {
                        return $return;
                }
 
                $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
                while ($pconfig = DBA::fetch($pconfigs)) {
-                       $return[$pconfig['cat']][$pconfig['k']] = $pconfig['v'];
+                       $value = $this->toConfigValue($pconfig['v']);
+                       if (isset($value)) {
+                               $return[$pconfig['cat']][$pconfig['k']] = $value;
+                       }
                }
                DBA::close($pconfigs);
 
-               $this->config_loaded = true;
+               $this->config_loaded[$uid] = true;
 
                return $return;
        }
@@ -59,22 +67,22 @@ class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfig
        public function get($uid, $cat, $key)
        {
                if (!$this->isConnected()) {
-                       return '!<unset>!';
+                       return null;
                }
 
-               if (!$this->config_loaded) {
+               if (!$this->isLoaded($uid, $cat, $key)) {
                        $this->load($uid, $cat);
                }
 
                $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
                if (DBA::isResult($config)) {
-                       // manage array value
-                       $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
+                       $value = $this->toConfigValue($config['v']);
 
-                       return $value;
-               } else {
-                       return '!<unset>!';
+                       if (isset($value)) {
+                               return $value;
+                       }
                }
+               return null;
        }
 
        /**
@@ -86,24 +94,22 @@ class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfig
                        return false;
                }
 
-               if (!$this->config_loaded) {
+               if (!$this->isLoaded($uid, $cat, $key)) {
                        $this->load($uid, $cat);
                }
                // We store our setting values as strings.
                // So we have to do the conversion here so that the compare below works.
                // The exception are array values.
                $compare_value = !is_array($value) ? (string)$value : $value;
+               $stored_value = $this->get($uid, $cat, $key);
 
-               if ($this->get($uid, $cat, $key) === $compare_value) {
+               if (isset($stored_value) && $stored_value === $compare_value) {
                        return true;
                }
 
-               // manage array value
-               $dbvalue = is_array($value) ? serialize($value) : $value;
-
-               $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
+               $dbvalue = $this->toDbValue($value);
 
-               return $result;
+               return DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
        }
 
        /**
@@ -115,13 +121,11 @@ class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfig
                        return false;
                }
 
-               if (!$this->config_loaded) {
+               if (!$this->isLoaded($uid, $cat, $key)) {
                        $this->load($uid, $cat);
                }
 
-               $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
-
-               return $result;
+               return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
        }
 
        /**
@@ -133,6 +137,6 @@ class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfig
                        return false;
                }
 
-               return $this->config_loaded;
+               return isset($this->config_loaded[$uid]) && $this->config_loaded[$uid];
        }
 }