]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Config/PreloadPConfigAdapter.php
Merge pull request #6593 from tobiasd/20190206-can
[friendica.git] / src / Core / Config / PreloadPConfigAdapter.php
index 002094a51692f00b85dd2ff5f7fa5bfaed5f2355..af97815adef38d159ab6ba44f27766d10ed7ca9d 100644 (file)
-<?php\r
-\r
-namespace Friendica\Core\Config;\r
-\r
-use dba;\r
-use Exception;\r
-use Friendica\BaseObject;\r
-\r
-require_once 'include/dba.php';\r
-\r
-/**\r
- * Preload PConfigAdapter\r
- *\r
- * Minimize the number of database queries to retrieve configuration values at the cost of memory.\r
- *\r
- * @author Hypolite Petovan <mrpetovan@gmail.com>\r
- */\r
-class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter\r
-{\r
-       private $config_loaded = false;\r
-\r
-       public function __construct($uid)\r
-       {\r
-               $this->load($uid, 'config');\r
-       }\r
-\r
-       public function load($uid, $family)\r
-       {\r
-               if ($this->config_loaded) {\r
-                       return;\r
-               }\r
-\r
-               $a = self::getApp();\r
-\r
-               $pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);\r
-               while ($pconfig = dba::fetch($pconfigs)) {\r
-                       $cat   = $pconfig['cat'];\r
-                       $k     = $pconfig['k'];\r
-                       $value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);\r
-\r
-                       $a->config[$uid][$cat][$k] = $value;\r
-               }\r
-               dba::close($pconfigs);\r
-\r
-               $this->config_loaded = true;\r
-       }\r
-\r
-       public function get($uid, $cat, $k, $default_value = null, $refresh = false)\r
-       {\r
-               $a = self::getApp();\r
-\r
-               $return = $default_value;\r
-\r
-               if (isset($a->config[$uid][$cat][$k])) {\r
-                       $return = $a->config[$uid][$cat][$k];\r
-               }\r
-\r
-               return $return;\r
-       }\r
-\r
-       public function set($uid, $cat, $k, $value)\r
-       {\r
-               $a = self::getApp();\r
-\r
-               // We store our setting values as strings.\r
-               // So we have to do the conversion here so that the compare below works.\r
-               // The exception are array values.\r
-               $compare_value = !is_array($value) ? (string)$value : $value;\r
-\r
-               if ($this->get($uid, $cat, $k) === $compare_value) {\r
-                       return true;\r
-               }\r
-\r
-               $a->config[$uid][$cat][$k] = $value;\r
-\r
-               // manage array value\r
-               $dbvalue = is_array($value) ? serialize($value) : $value;\r
-\r
-               $result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);\r
-\r
-               if (!$result) {\r
-                       throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');\r
-               }\r
-\r
-               return true;\r
-       }\r
-\r
-       public function delete($uid, $cat, $k)\r
-       {\r
-               $a = self::getApp();\r
-\r
-               if (!isset($a->config[$uid][$cat][$k])) {\r
-                       return true;\r
-               }\r
-\r
-               unset($a->config[$uid][$cat][$k]);\r
-\r
-               $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);\r
-\r
-               return $result;\r
-       }\r
-}\r
+<?php
+
+namespace Friendica\Core\Config;
+
+use Exception;
+use Friendica\Database\DBA;
+
+/**
+ * Preload User Configuration Adapter
+ *
+ * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
+ *
+ * @author Hypolite Petovan <hypolite@mrpetovan.com>
+ */
+class PreloadPConfigAdapter implements IPConfigAdapter
+{
+       private $config_loaded = false;
+
+       /**
+        * The config cache of this adapter
+        * @var IPConfigCache
+        */
+       private $configCache;
+
+       /**
+        * @param IPConfigCache $configCache The config cache of this adapter
+        * @param int           $uid    The UID of the current user
+        */
+       public function __construct(IPConfigCache $configCache, $uid = null)
+       {
+               $this->configCache = $configCache;
+               if (isset($uid)) {
+                       $this->load($uid, 'config');
+               }
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function load($uid, $family)
+       {
+               if ($this->config_loaded) {
+                       return;
+               }
+
+               if (empty($uid)) {
+                       return;
+               }
+
+               $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
+               while ($pconfig = DBA::fetch($pconfigs)) {
+                       $this->configCache->setP($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
+               }
+               DBA::close($pconfigs);
+
+               $this->config_loaded = true;
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       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 (DBA::isResult($config)) {
+                               $this->configCache->setP($uid, $cat, $k, $config['v']);
+                       } else {
+                               $this->configCache->deleteP($uid, $cat, $k);
+                       }
+               }
+
+               return $this->configCache->getP($uid, $cat, $k, $default_value);;
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       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.
+               $compare_value = !is_array($value) ? (string)$value : $value;
+
+               if ($this->configCache->getP($uid, $cat, $k) === $compare_value) {
+                       return true;
+               }
+
+               $this->configCache->setP($uid, $cat, $k, $value);
+
+               // manage array value
+               $dbvalue = is_array($value) ? serialize($value) : $value;
+
+               $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 . ']');
+               }
+
+               return true;
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function delete($uid, $cat, $k)
+       {
+               if (!$this->config_loaded) {
+                       $this->load($uid, $cat);
+               }
+
+               $this->configCache->deleteP($uid, $cat, $k);
+
+               $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
+
+               return $result;
+       }
+}