-<?php\r
-\r
-namespace Friendica\Core\Config;\r
-\r
-use dba;\r
-use Exception;\r
-use Friendica\App;\r
-use Friendica\BaseObject;\r
-use Friendica\Database\DBM;\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
- $pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);\r
- while ($pconfig = dba::fetch($pconfigs)) {\r
- $this->setPreloadedValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);\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
- if ($refresh) {\r
- $config = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);\r
- if (DBM::is_result($config)) {\r
- $this->setPreloadedValue($uid, $cat, $k, $config['v']);\r
- } else {\r
- $this->deletePreloadedValue($uid, $cat, $k);\r
- }\r
- }\r
-\r
- $return = $this->getPreloadedValue($uid, $cat, $k, $default_value);\r
-\r
- return $return;\r
- }\r
-\r
- public function set($uid, $cat, $k, $value)\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->getPreloadedValue($uid, $cat, $k) === $compare_value) {\r
- return true;\r
- }\r
-\r
- $this->setPreloadedValue($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
- 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
- $this->deletePreloadedValue($uid, $cat, $k);\r
-\r
- $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);\r
-\r
- return $result;\r
- }\r
-\r
-\r
- /**\r
- * Retrieves a preloaded value from the App user config cache\r
- *\r
- * @param int $uid\r
- * @param string $cat\r
- * @param string $k\r
- * @param mixed $default\r
- */\r
- private function getPreloadedValue($uid, $cat, $k, $default = null)\r
- {\r
- $a = self::getApp();\r
-\r
- $return = $default;\r
-\r
- if (isset($a->config[$uid][$cat][$k])) {\r
- $return = $a->config[$uid][$cat][$k];\r
- }\r
-\r
- return $return;\r
- }\r
-\r
- /**\r
- * Sets a preloaded value in the App user config cache\r
- *\r
- * Accepts raw output from the pconfig table\r
- *\r
- * @param int $uid\r
- * @param string $cat\r
- * @param string $k\r
- * @param mixed $v\r
- */\r
- private function setPreloadedValue($uid, $cat, $k, $v)\r
- {\r
- $a = self::getApp();\r
-\r
- // Only arrays are serialized in database, so we have to unserialize sparingly\r
- $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;\r
-\r
- $a->config[$uid][$cat][$k] = $value;\r
- }\r
-\r
- /**\r
- * Deletes a preloaded value from the App user config cache\r
- *\r
- * @param int $uid\r
- * @param string $cat\r
- * @param string $k\r
- */\r
- private function deletePreloadedValue($uid, $cat, $k)\r
- {\r
- $a = self::getApp();\r
-\r
- if (isset($a->config[$uid][$cat][$k])) {\r
- unset($a->config[$uid][$cat][$k]);\r
- }\r
- }\r
-}\r
+<?php
+
+namespace Friendica\Core\Config;
+
+use Exception;
+use Friendica\BaseObject;
+use Friendica\Database\DBA;
+
+require_once 'include/dba.php';
+
+/**
+ * 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 extends BaseObject implements IPConfigAdapter
+{
+ private $config_loaded = false;
+
+ public function __construct($uid)
+ {
+ $this->load($uid, 'config');
+ }
+
+ 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)) {
+ self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
+ }
+ 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 (DBA::isResult($config)) {
+ self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']);
+ } else {
+ self::getApp()->deletePConfigValue($uid, $cat, $k);
+ }
+ }
+
+ $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
+
+ return $return;
+ }
+
+ 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 (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
+ return true;
+ }
+
+ self::getApp()->setPConfigValue($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;
+ }
+
+ 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]);
+
+ return $result;
+ }
+}