]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Config/PreloadPConfigAdapter.php
Merge pull request #5776 from annando/fix-contact
[friendica.git] / src / Core / Config / PreloadPConfigAdapter.php
index af77598389af025268305f369b45f6dbc37f058f..ebccb018bce6a85eb0776fd525cff8bdadbb68f6 100644 (file)
@@ -2,11 +2,9 @@
 
 namespace Friendica\Core\Config;
 
-use dba;
 use Exception;
-use Friendica\App;
 use Friendica\BaseObject;
-use Friendica\Database\DBM;
+use Friendica\Database\DBA;
 
 require_once 'include/dba.php';
 
@@ -15,7 +13,7 @@ require_once 'include/dba.php';
  *
  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
  *
- * @author Hypolite Petovan <mrpetovan@gmail.com>
+ * @author Hypolite Petovan <hypolite@mrpetovan.com>
  */
 class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
 {
@@ -32,20 +30,28 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
                        return;
                }
 
-               $pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
-               while ($pconfig = dba::fetch($pconfigs)) {
+               if (empty($uid)) {
+                       return;
+               }
+
+               $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
+               while ($pconfig = DBA::fetch($pconfigs)) {
                        self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
                }
-               dba::close($pconfigs);
+               DBA::close($pconfigs);
 
                $this->config_loaded = true;
        }
 
        public function get($uid, $cat, $k, $default_value = null, $refresh = false)
        {
+               if (!$this->config_loaded) {
+                       $this->load($uid, $cat);
+               }
+
                if ($refresh) {
-                       $config = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
-                       if (DBM::is_result($config)) {
+                       $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
+                       if (DBA::isResult($config)) {
                                self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']);
                        } else {
                                self::getApp()->deletePConfigValue($uid, $cat, $k);
@@ -59,6 +65,9 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
 
        public function set($uid, $cat, $k, $value)
        {
+               if (!$this->config_loaded) {
+                       $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.
@@ -73,7 +82,7 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
                // manage array value
                $dbvalue = is_array($value) ? serialize($value) : $value;
 
-               $result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
+               $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
                if (!$result) {
                        throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
                }
@@ -83,9 +92,13 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
 
        public function delete($uid, $cat, $k)
        {
+               if (!$this->config_loaded) {
+                       $this->load($uid, $cat);
+               }
+
                self::getApp()->deletePConfigValue($uid, $cat, $k);
 
-               $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
+               $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
 
                return $result;
        }