From d760d339895d40e9de86ab98471d233179502a8a Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 3 Mar 2018 12:09:12 -0500 Subject: [PATCH 01/10] Add Config adapter interfaces/classes --- src/Core/Config/IConfigAdapter.php | 72 ++++++++++++ src/Core/Config/IPConfigAdapter.php | 77 +++++++++++++ src/Core/Config/JITConfigAdapter.php | 132 ++++++++++++++++++++++ src/Core/Config/JITPConfigAdapter.php | 117 +++++++++++++++++++ src/Core/Config/PreloadConfigAdapter.php | 119 +++++++++++++++++++ src/Core/Config/PreloadPConfigAdapter.php | 102 +++++++++++++++++ 6 files changed, 619 insertions(+) create mode 100644 src/Core/Config/IConfigAdapter.php create mode 100644 src/Core/Config/IPConfigAdapter.php create mode 100644 src/Core/Config/JITConfigAdapter.php create mode 100644 src/Core/Config/JITPConfigAdapter.php create mode 100644 src/Core/Config/PreloadConfigAdapter.php create mode 100644 src/Core/Config/PreloadPConfigAdapter.php diff --git a/src/Core/Config/IConfigAdapter.php b/src/Core/Config/IConfigAdapter.php new file mode 100644 index 000000000..ee5ca3ca5 --- /dev/null +++ b/src/Core/Config/IConfigAdapter.php @@ -0,0 +1,72 @@ + + */ +interface IConfigAdapter +{ + /** + * @brief Loads all configuration values into a cached storage. + * + * All configuration values of the system are stored in global cache + * which is available under the global variable $a->config + * + * @param string $cat The category of the configuration values to load + * + * @return void + */ + public function load($cat = "config"); + + /** + * @brief Get a particular user's config variable given the category name + * ($family) and a key. + * + * Get a particular config value from the given category ($family) + * and the $key from a cached storage in $a->config[$uid]. + * $instore is only used by the set_config function + * to determine if the key already exists in the DB + * If a key is found in the DB but doesn't exist in + * local config cache, pull it into the cache so we don't have + * to hit the DB again for this item. + * + * @param string $cat The category of the configuration value + * @param string $k The configuration key to query + * @param mixed $default_value optional, The value to return if key is not set (default: null) + * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false) + * + * @return mixed Stored value or null if it does not exist + */ + public function get($cat, $k, $default_value = null, $refresh = false); + + /** + * @brief Sets a configuration value for system config + * + * Stores a config value ($value) in the category ($family) under the key ($key) + * for the user_id $uid. + * + * Note: Please do not store booleans - convert to 0/1 integer values! + * + * @param string $family The category of the configuration value + * @param string $key The configuration key to set + * @param mixed $value The value to store + * + * @return mixed Stored $value or false if the database update failed + */ + public function set($cat, $k, $value); + + /** + * @brief Deletes the given key from the system configuration. + * + * Removes the configured value from the stored cache in $a->config + * and removes it from the database. + * + * @param string $cat The category of the configuration value + * @param string $k The configuration key to delete + * + * @return mixed + */ + public function delete($cat, $k); +} diff --git a/src/Core/Config/IPConfigAdapter.php b/src/Core/Config/IPConfigAdapter.php new file mode 100644 index 000000000..f78654d39 --- /dev/null +++ b/src/Core/Config/IPConfigAdapter.php @@ -0,0 +1,77 @@ +config[$uid]. + * + * @param string $uid The user_id + * @param string $cat The category of the configuration value + * + * @return void + */ + public function load($uid, $cat); + + /** + * @brief Get a particular user's config variable given the category name + * ($family) and a key. + * + * Get a particular user's config value from the given category ($family) + * and the $key from a cached storage in $a->config[$uid]. + * + * @param string $uid The user_id + * @param string $cat The category of the configuration value + * @param string $k The configuration key to query + * @param mixed $default_value optional, The value to return if key is not set (default: null) + * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false) + * + * @return mixed Stored value or null if it does not exist + */ + public function get($uid, $cat, $k, $default_value = null, $refresh = false); + + /** + * @brief Sets a configuration value for a user + * + * Stores a config value ($value) in the category ($family) under the key ($key) + * for the user_id $uid. + * + * @note Please do not store booleans - convert to 0/1 integer values! + * + * @param string $uid The user_id + * @param string $cat The category of the configuration value + * @param string $k The configuration key to set + * @param string $value The value to store + * + * @return mixed Stored $value or false + */ + public function set($uid, $cat, $k, $value); + + /** + * @brief Deletes the given key from the users's configuration. + * + * Removes the configured value from the stored cache in $a->config[$uid] + * and removes it from the database. + * + * @param string $uid The user_id + * @param string $cat The category of the configuration value + * @param string $k The configuration key to delete + * + * @return mixed + */ + public function delete($uid, $cat, $k); +} diff --git a/src/Core/Config/JITConfigAdapter.php b/src/Core/Config/JITConfigAdapter.php new file mode 100644 index 000000000..1bc3bf5a8 --- /dev/null +++ b/src/Core/Config/JITConfigAdapter.php @@ -0,0 +1,132 @@ + + */ +class JITConfigAdapter extends BaseObject implements IConfigAdapter +{ + private $cache; + private $in_db; + + public function load($cat = "config") + { + // We don't preload "system" anymore. + // This reduces the number of database reads a lot. + if ($cat === 'system') { + return; + } + + $a = self::getApp(); + + $configs = dba::select('config', ['v', 'k'], ['cat' => $cat]); + while ($config = dba::fetch($configs)) { + $k = $config['k']; + if ($cat === 'config') { + $a->config[$k] = $config['v']; + } else { + $a->config[$cat][$k] = $config['v']; + self::$cache[$cat][$k] = $config['v']; + self::$in_db[$cat][$k] = true; + } + } + dba::close($configs); + } + + public function get($cat, $k, $default_value = null, $refresh = false) + { + $a = self::getApp(); + + if (!$refresh) { + // Do we have the cached value? Then return it + if (isset($this->cache[$cat][$k])) { + if ($this->cache[$cat][$k] === '!!') { + return $default_value; + } else { + return $this->cache[$cat][$k]; + } + } + } + + $config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]); + if (DBM::is_result($config)) { + // manage array value + $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']); + + // Assign the value from the database to the cache + $this->cache[$cat][$k] = $value; + $this->in_db[$cat][$k] = true; + return $value; + } elseif (isset($a->config[$cat][$k])) { + // Assign the value (mostly) from the .htconfig.php to the cache + $this->cache[$cat][$k] = $a->config[$cat][$k]; + $this->in_db[$cat][$k] = false; + + return $a->config[$cat][$k]; + } + + $this->cache[$cat][$k] = '!!'; + $this->in_db[$cat][$k] = false; + + return $default_value; + } + + public function set($cat, $k, $value) + { + $a = self::getApp(); + + // We store our setting values in a string variable. + // So we have to do the conversion here so that the compare below works. + // The exception are array values. + $dbvalue = (!is_array($value) ? (string)$value : $value); + + $stored = $this->get($cat, $k, null, true); + + if (($stored === $dbvalue) && $this->in_db[$cat][$k]) { + return true; + } + + if ($cat === 'config') { + $a->config[$k] = $dbvalue; + } elseif ($cat != 'system') { + $a->config[$cat][$k] = $dbvalue; + } + + // Assign the just added value to the cache + $this->cache[$cat][$k] = $dbvalue; + + // manage array value + $dbvalue = (is_array($value) ? serialize($value) : $dbvalue); + + $result = dba::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true); + + if ($result) { + $this->in_db[$cat][$k] = true; + return $value; + } + + return $result; + } + + public function delete($cat, $k) + { + if (isset($this->cache[$cat][$k])) { + unset($this->cache[$cat][$k]); + unset($this->in_db[$cat][$k]); + } + + $result = dba::delete('config', ['cat' => $cat, 'k' => $k]); + + return $result; + } +} diff --git a/src/Core/Config/JITPConfigAdapter.php b/src/Core/Config/JITPConfigAdapter.php new file mode 100644 index 000000000..27f2ed862 --- /dev/null +++ b/src/Core/Config/JITPConfigAdapter.php @@ -0,0 +1,117 @@ + + */ +class JITPConfigAdapter extends BaseObject implements IPConfigAdapter +{ + private $in_db; + + public function load($uid, $cat) + { + $a = self::getApp(); + + $pconfigs = dba::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]); + if (DBM::is_result($pconfigs)) { + while ($pconfig = dba::fetch($pconfigs)) { + $k = $pconfig['k']; + $a->config[$uid][$cat][$k] = $pconfig['v']; + $this->in_db[$uid][$cat][$k] = true; + } + } else if ($cat != 'config') { + // Negative caching + $a->config[$uid][$cat] = "!!"; + } + dba::close($pconfigs); + } + + public function get($uid, $cat, $k, $default_value = null, $refresh = false) + { + $a = self::getApp(); + + if (!$refresh) { + // Looking if the whole family isn't set + if (isset($a->config[$uid][$cat])) { + if ($a->config[$uid][$cat] === '!!') { + return $default_value; + } + } + + if (isset($a->config[$uid][$cat][$k])) { + if ($a->config[$uid][$cat][$k] === '!!') { + return $default_value; + } + return $a->config[$uid][$cat][$k]; + } + } + + $pconfig = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]); + if (DBM::is_result($pconfig)) { + $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']); + $a->config[$uid][$cat][$k] = $val; + $this->in_db[$uid][$cat][$k] = true; + + return $val; + } else { + $a->config[$uid][$cat][$k] = '!!'; + $this->in_db[$uid][$cat][$k] = false; + + return $default_value; + } + } + + public function set($uid, $cat, $k, $value) + { + $a = self::getApp(); + + // We store our setting values in a string variable. + // So we have to do the conversion here so that the compare below works. + // The exception are array values. + $dbvalue = (!is_array($value) ? (string)$value : $value); + + $stored = $this->get($uid, $cat, $k, null, true); + + if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) { + return true; + } + + $a->config[$uid][$cat][$k] = $dbvalue; + + // manage array value + $dbvalue = (is_array($value) ? serialize($value) : $dbvalue); + + $result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true); + + if ($result) { + $this->in_db[$uid][$cat][$k] = true; + return $value; + } + + return $result; + } + + public function delete($uid, $cat, $k) + { + $a = self::getApp(); + + if (!empty($a->config[$uid][$cat][$k])) { + unset($a->config[$uid][$cat][$k]); + unset($this->in_db[$uid][$cat][$k]); + } + + $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]); + + return $result; + } +} diff --git a/src/Core/Config/PreloadConfigAdapter.php b/src/Core/Config/PreloadConfigAdapter.php new file mode 100644 index 000000000..c10f2ee64 --- /dev/null +++ b/src/Core/Config/PreloadConfigAdapter.php @@ -0,0 +1,119 @@ + + */ +class PreloadConfigAdapter extends BaseObject implements IConfigAdapter +{ + private $config_loaded = false; + + public function __construct() + { + $this->load(); + } + + public function load($family = 'config') + { + if ($this->config_loaded) { + return; + } + + $a = self::getApp(); + + $configs = dba::select('config', ['cat', 'v', 'k']); + while ($config = dba::fetch($configs)) { + $cat = $config['cat']; + $k = $config['k']; + $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']); + + if ($cat === 'config') { + $a->config[$k] = $value; + } else { + $a->config[$cat][$k] = $value; + } + } + dba::close($configs); + + $this->config_loaded = true; + } + + public function get($cat, $k, $default_value = null, $refresh = false) + { + $a = self::getApp(); + + $return = $default_value; + + if ($cat === 'config') { + if (isset($a->config[$k])) { + $return = $a->config[$k]; + } + } else { + if (isset($a->config[$cat][$k])) { + $return = $a->config[$cat][$k]; + } + } + + return $return; + } + + public function set($cat, $k, $value) + { + $a = self::getApp(); + + // 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->get($cat, $k) === $compare_value) { + return true; + } + + if ($cat === 'config') { + $a->config[$k] = $value; + } else { + $a->config[$cat][$k] = $value; + } + + // manage array value + $dbvalue = is_array($value) ? serialize($value) : $value; + + $result = dba::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true); + if (!$result) { + throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']'); + } + + return true; + } + + public function delete($cat, $k) + { + $a = self::getApp(); + + if ($cat === 'config') { + if (isset($a->config[$k])) { + unset($a->config[$k]); + } + } else { + if (isset($a->config[$cat][$k])) { + unset($a->config[$cat][$k]); + } + } + + $result = dba::delete('config', ['cat' => $cat, 'k' => $k]); + + return $result; + } +} diff --git a/src/Core/Config/PreloadPConfigAdapter.php b/src/Core/Config/PreloadPConfigAdapter.php new file mode 100644 index 000000000..002094a51 --- /dev/null +++ b/src/Core/Config/PreloadPConfigAdapter.php @@ -0,0 +1,102 @@ + + */ +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; + } + + $a = self::getApp(); + + $pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]); + while ($pconfig = dba::fetch($pconfigs)) { + $cat = $pconfig['cat']; + $k = $pconfig['k']; + $value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']); + + $a->config[$uid][$cat][$k] = $value; + } + dba::close($pconfigs); + + $this->config_loaded = true; + } + + public function get($uid, $cat, $k, $default_value = null, $refresh = false) + { + $a = self::getApp(); + + $return = $default_value; + + if (isset($a->config[$uid][$cat][$k])) { + $return = $a->config[$uid][$cat][$k]; + } + + return $return; + } + + public function set($uid, $cat, $k, $value) + { + $a = self::getApp(); + + // 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->get($uid, $cat, $k) === $compare_value) { + return true; + } + + $a->config[$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) + { + $a = self::getApp(); + + if (!isset($a->config[$uid][$cat][$k])) { + return true; + } + + unset($a->config[$uid][$cat][$k]); + + $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]); + + return $result; + } +} From 6fec0433758ec1df4f798941939250dd169269a7 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 3 Mar 2018 12:10:55 -0500 Subject: [PATCH 02/10] Update Config class with adapter --- index.php | 1 + src/Core/Config.php | 127 +++++++++++--------------------------------- 2 files changed, 32 insertions(+), 96 deletions(-) diff --git a/index.php b/index.php index ef8440099..4fad08da8 100644 --- a/index.php +++ b/index.php @@ -78,6 +78,7 @@ if (!$install) { exit(); } + Config::init(); Session::init(); Addon::loadHooks(); Addon::callHooks('init_1'); diff --git a/src/Core/Config.php b/src/Core/Config.php index 5e162a3fa..bff5d81ff 100644 --- a/src/Core/Config.php +++ b/src/Core/Config.php @@ -8,26 +8,35 @@ */ namespace Friendica\Core; -use Friendica\Database\DBM; -use dba; +use Friendica\BaseObject; +use Friendica\Core\Config; require_once 'include/dba.php'; /** - * @brief Arbitrary sytem configuration storage + * @brief Arbitrary system configuration storage * * Note: * If we ever would decide to return exactly the variable type as entered, * we will have fun with the additional features. :-) - * - * The config class always returns strings but in the default features - * we use a "false" to determine if the config value isn't set. - * */ -class Config +class Config extends BaseObject { - private static $cache; - private static $in_db; + /** + * @var Friendica\Core\Config\IConfigAdapter + */ + private static $adapter = null; + + public static function init() + { + $a = self::getApp(); + + if (isset($a->config['system']['config_adapter']) && $a->config['system']['config_adapter'] == 'preload') { + self::$adapter = new Config\PreloadConfigAdapter(); + } else { + self::$adapter = new Config\JITConfigAdapter(); + } + } /** * @brief Loads all configuration values of family into a cached storage. @@ -41,26 +50,11 @@ class Config */ public static function load($family = "config") { - // We don't preload "system" anymore. - // This reduces the number of database reads a lot. - if ($family === 'system') { - return; + if (empty(self::$adapter)) { + self::init(); } - $a = get_app(); - - $r = dba::select('config', ['v', 'k'], ['cat' => $family]); - while ($rr = dba::fetch($r)) { - $k = $rr['k']; - if ($family === 'config') { - $a->config[$k] = $rr['v']; - } else { - $a->config[$family][$k] = $rr['v']; - self::$cache[$family][$k] = $rr['v']; - self::$in_db[$family][$k] = true; - } - } - dba::close($r); + self::$adapter->load($family); } /** @@ -84,40 +78,11 @@ class Config */ public static function get($family, $key, $default_value = null, $refresh = false) { - $a = get_app(); - - if (!$refresh) { - // Do we have the cached value? Then return it - if (isset(self::$cache[$family][$key])) { - if (self::$cache[$family][$key] === '!!') { - return $default_value; - } else { - return self::$cache[$family][$key]; - } - } + if (empty(self::$adapter)) { + self::init(); } - $config = dba::selectFirst('config', ['v'], ['cat' => $family, 'k' => $key]); - if (DBM::is_result($config)) { - // manage array value - $val = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']); - - // Assign the value from the database to the cache - self::$cache[$family][$key] = $val; - self::$in_db[$family][$key] = true; - return $val; - } elseif (isset($a->config[$family][$key])) { - // Assign the value (mostly) from the .htconfig.php to the cache - self::$cache[$family][$key] = $a->config[$family][$key]; - self::$in_db[$family][$key] = false; - - return $a->config[$family][$key]; - } - - self::$cache[$family][$key] = '!!'; - self::$in_db[$family][$key] = false; - - return $default_value; + return self::$adapter->get($family, $key, $default_value, $refresh); } /** @@ -136,38 +101,11 @@ class Config */ public static function set($family, $key, $value) { - $a = get_app(); - - // We store our setting values in a string variable. - // So we have to do the conversion here so that the compare below works. - // The exception are array values. - $dbvalue = (!is_array($value) ? (string)$value : $value); - - $stored = self::get($family, $key, null, true); - - if (($stored === $dbvalue) && self::$in_db[$family][$key]) { - return true; + if (empty(self::$adapter)) { + self::init(); } - if ($family === 'config') { - $a->config[$key] = $dbvalue; - } elseif ($family != 'system') { - $a->config[$family][$key] = $dbvalue; - } - - // Assign the just added value to the cache - self::$cache[$family][$key] = $dbvalue; - - // manage array value - $dbvalue = (is_array($value) ? serialize($value) : $dbvalue); - - $ret = dba::update('config', ['v' => $dbvalue], ['cat' => $family, 'k' => $key], true); - - if ($ret) { - self::$in_db[$family][$key] = true; - return $value; - } - return $ret; + return self::$adapter->set($family, $key, $value); } /** @@ -183,13 +121,10 @@ class Config */ public static function delete($family, $key) { - if (isset(self::$cache[$family][$key])) { - unset(self::$cache[$family][$key]); - unset(self::$in_db[$family][$key]); + if (empty(self::$adapter)) { + self::init(); } - $ret = dba::delete('config', ['cat' => $family, 'k' => $key]); - - return $ret; + return self::$adapter->delete($family, $key); } } From 7afcd6d49e6afed1bc4a94602370eee7859e401e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 3 Mar 2018 12:13:20 -0500 Subject: [PATCH 03/10] Update PConfig class with adapter --- src/Core/PConfig.php | 120 +++++++++++++------------------------------ 1 file changed, 36 insertions(+), 84 deletions(-) diff --git a/src/Core/PConfig.php b/src/Core/PConfig.php index 82d469083..bfa52f5a3 100644 --- a/src/Core/PConfig.php +++ b/src/Core/PConfig.php @@ -1,20 +1,18 @@ config['system']['config_adapter']) && $a->config['system']['config_adapter'] == 'preload') { + self::$adapter = new Config\PreloadPConfigAdapter($uid); + } else { + self::$adapter = new Config\JITPConfigAdapter($uid); + } + } /** * @brief Loads all configuration values of a user's config family into a cached storage. @@ -39,20 +51,11 @@ class PConfig */ public static function load($uid, $family) { - $a = get_app(); - - $r = dba::select('pconfig', ['v', 'k'], ['cat' => $family, 'uid' => $uid]); - if (DBM::is_result($r)) { - while ($rr = dba::fetch($r)) { - $k = $rr['k']; - $a->config[$uid][$family][$k] = $rr['v']; - self::$in_db[$uid][$family][$k] = true; - } - } else if ($family != 'config') { - // Negative caching - $a->config[$uid][$family] = "!!"; + if (empty(self::$adapter)) { + self::init($uid); } - dba::close($r); + + self::$adapter->load($uid, $family); } /** @@ -72,37 +75,11 @@ class PConfig */ public static function get($uid, $family, $key, $default_value = null, $refresh = false) { - $a = get_app(); - - if (!$refresh) { - // Looking if the whole family isn't set - if (isset($a->config[$uid][$family])) { - if ($a->config[$uid][$family] === '!!') { - return $default_value; - } - } - - if (isset($a->config[$uid][$family][$key])) { - if ($a->config[$uid][$family][$key] === '!!') { - return $default_value; - } - return $a->config[$uid][$family][$key]; - } + if (empty(self::$adapter)) { + self::init($uid); } - $pconfig = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $family, 'k' => $key]); - if (DBM::is_result($pconfig)) { - $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']); - $a->config[$uid][$family][$key] = $val; - self::$in_db[$uid][$family][$key] = true; - - return $val; - } else { - $a->config[$uid][$family][$key] = '!!'; - self::$in_db[$uid][$family][$key] = false; - - return $default_value; - } + return self::$adapter->get($uid, $family, $key, $default_value, $refresh); } /** @@ -122,31 +99,11 @@ class PConfig */ public static function set($uid, $family, $key, $value) { - $a = get_app(); - - // We store our setting values in a string variable. - // So we have to do the conversion here so that the compare below works. - // The exception are array values. - $dbvalue = (!is_array($value) ? (string)$value : $value); - - $stored = self::get($uid, $family, $key, null, true); - - if (($stored === $dbvalue) && self::$in_db[$uid][$family][$key]) { - return true; + if (empty(self::$adapter)) { + self::init($uid); } - $a->config[$uid][$family][$key] = $dbvalue; - - // manage array value - $dbvalue = (is_array($value) ? serialize($value) : $dbvalue); - - $ret = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $family, 'k' => $key], true); - - if ($ret) { - self::$in_db[$uid][$family][$key] = true; - return $value; - } - return $ret; + return self::$adapter->set($uid, $family, $key, $value); } /** @@ -163,15 +120,10 @@ class PConfig */ public static function delete($uid, $family, $key) { - $a = get_app(); - - if (x($a->config[$uid][$family], $key)) { - unset($a->config[$uid][$family][$key]); - unset(self::$in_db[$uid][$family][$key]); + if (empty(self::$adapter)) { + self::init($uid); } - $ret = dba::delete('pconfig', ['uid' => $uid, 'cat' => $family, 'k' => $key]); - - return $ret; + return self::$adapter->delete($uid, $family, $key); } } From 4b9c52aad0ba24476e4d04d98bd0b52f66f3ba36 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 3 Mar 2018 12:13:43 -0500 Subject: [PATCH 04/10] Update htconfig documentation --- doc/htconfig.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/htconfig.md b/doc/htconfig.md index bed6f7902..1f1b62bd4 100644 --- a/doc/htconfig.md +++ b/doc/htconfig.md @@ -27,6 +27,7 @@ Example: To set the automatic database cleanup process add this line to your .ht * **always_show_preview** (Boolean) - Only show small preview picures. Default value is false. * **block_local_dir** (Boolean) - Blocks the access to the directory of the local users. * **auth_cookie_lifetime** (Integer) - Number of days that should pass without any activity before a user who chose "Remember me" when logging in is considered logged out. Defaults to 7. +* **config_adapter** (jit|preload) - Allow to switch the configuration adapter to improve performances at the cost of memory consumption. Default value is "jit" * **curl_range_bytes** - Maximum number of bytes that should be fetched. Default is 0, which mean "no limit". * **db_log** - Name of a logfile to log slow database queries * **db_loglimit** - If a database call lasts longer than this value it is logged From dcd1f1861180b812c9b4f08209ea0610af437df0 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 4 Mar 2018 22:44:33 -0500 Subject: [PATCH 05/10] Add refresh feature to Preload (P)Config adapters - Add private methods to manipulat the App config variable --- src/Core/Config/PreloadConfigAdapter.php | 111 +++++++++++++++------- src/Core/Config/PreloadPConfigAdapter.php | 98 ++++++++++++++----- 2 files changed, 150 insertions(+), 59 deletions(-) diff --git a/src/Core/Config/PreloadConfigAdapter.php b/src/Core/Config/PreloadConfigAdapter.php index c10f2ee64..4abcc4a1a 100644 --- a/src/Core/Config/PreloadConfigAdapter.php +++ b/src/Core/Config/PreloadConfigAdapter.php @@ -4,7 +4,9 @@ namespace Friendica\Core\Config; use dba; use Exception; +use Friendica\App; use Friendica\BaseObject; +use Friendica\Database\DBM; require_once 'include/dba.php'; @@ -30,19 +32,9 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter return; } - $a = self::getApp(); - $configs = dba::select('config', ['cat', 'v', 'k']); while ($config = dba::fetch($configs)) { - $cat = $config['cat']; - $k = $config['k']; - $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']); - - if ($cat === 'config') { - $a->config[$k] = $value; - } else { - $a->config[$cat][$k] = $value; - } + $this->setPreloadedValue($config['cat'], $config['k'], $config['v']); } dba::close($configs); @@ -51,41 +43,32 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter public function get($cat, $k, $default_value = null, $refresh = false) { - $a = self::getApp(); - - $return = $default_value; - - if ($cat === 'config') { - if (isset($a->config[$k])) { - $return = $a->config[$k]; - } - } else { - if (isset($a->config[$cat][$k])) { - $return = $a->config[$cat][$k]; + if ($refresh) { + $config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]); + if (DBM::is_result($config)) { + $this->setPreloadedValue($cat, $k, $config['v']); + } else { + $this->deletePreloadedValue($cat, $k); } } + $return = $this->getPreloadedValue($cat, $k, $default_value); + return $return; } public function set($cat, $k, $value) { - $a = self::getApp(); - // 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->get($cat, $k) === $compare_value) { + if ($this->getPreloadedValue($cat, $k) === $compare_value) { return true; } - if ($cat === 'config') { - $a->config[$k] = $value; - } else { - $a->config[$cat][$k] = $value; - } + $this->setPreloadedValue($cat, $k, $value); // manage array value $dbvalue = is_array($value) ? serialize($value) : $value; @@ -99,6 +82,70 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter } public function delete($cat, $k) + { + $this->deletePreloadedValue($cat, $k); + + $result = dba::delete('config', ['cat' => $cat, 'k' => $k]); + + return $result; + } + + /** + * Retrieves a preloaded value from the App config cache + * + * @param string $cat + * @param string $k + * @param mixed $default + */ + private function getPreloadedValue($cat, $k, $default = null) + { + $a = self::getApp(); + + $return = $default; + + if ($cat === 'config') { + if (isset($a->config[$k])) { + $return = $a->config[$k]; + } + } else { + if (isset($a->config[$cat][$k])) { + $return = $a->config[$cat][$k]; + } + } + + return $return; + } + + /** + * Sets a preloaded value in the App config cache + * + * Accepts raw output from the config table + * + * @param string $cat + * @param string $k + * @param mixed $v + */ + private function setPreloadedValue($cat, $k, $v) + { + $a = self::getApp(); + + // Only arrays are serialized in database, so we have to unserialize sparingly + $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v; + + if ($cat === 'config') { + $a->config[$k] = $value; + } else { + $a->config[$cat][$k] = $value; + } + } + + /** + * Deletes a preloaded value from the App config cache + * + * @param string $cat + * @param string $k + */ + private function deletePreloadedValue($cat, $k) { $a = self::getApp(); @@ -111,9 +158,5 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter unset($a->config[$cat][$k]); } } - - $result = dba::delete('config', ['cat' => $cat, 'k' => $k]); - - return $result; } } diff --git a/src/Core/Config/PreloadPConfigAdapter.php b/src/Core/Config/PreloadPConfigAdapter.php index 002094a51..d6a44e07c 100644 --- a/src/Core/Config/PreloadPConfigAdapter.php +++ b/src/Core/Config/PreloadPConfigAdapter.php @@ -4,7 +4,9 @@ namespace Friendica\Core\Config; use dba; use Exception; +use Friendica\App; use Friendica\BaseObject; +use Friendica\Database\DBM; require_once 'include/dba.php'; @@ -30,15 +32,9 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter return; } - $a = self::getApp(); - $pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]); while ($pconfig = dba::fetch($pconfigs)) { - $cat = $pconfig['cat']; - $k = $pconfig['k']; - $value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']); - - $a->config[$uid][$cat][$k] = $value; + $this->setPreloadedValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']); } dba::close($pconfigs); @@ -47,37 +43,37 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter public function get($uid, $cat, $k, $default_value = null, $refresh = false) { - $a = self::getApp(); - - $return = $default_value; - - if (isset($a->config[$uid][$cat][$k])) { - $return = $a->config[$uid][$cat][$k]; + if ($refresh) { + $config = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]); + if (DBM::is_result($config)) { + $this->setPreloadedValue($uid, $cat, $k, $config['v']); + } else { + $this->deletePreloadedValue($uid, $cat, $k); + } } + $return = $this->getPreloadedValue($uid, $cat, $k, $default_value); + return $return; } public function set($uid, $cat, $k, $value) { - $a = self::getApp(); - // 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->get($uid, $cat, $k) === $compare_value) { + if ($this->getPreloadedValue($uid, $cat, $k) === $compare_value) { return true; } - $a->config[$uid][$cat][$k] = $value; + $this->setPreloadedValue($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 . ']'); } @@ -87,16 +83,68 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter public function delete($uid, $cat, $k) { - $a = self::getApp(); - - if (!isset($a->config[$uid][$cat][$k])) { - return true; - } - - unset($a->config[$uid][$cat][$k]); + $this->deletePreloadedValue($uid, $cat, $k); $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]); return $result; } + + + /** + * Retrieves a preloaded value from the App user config cache + * + * @param int $uid + * @param string $cat + * @param string $k + * @param mixed $default + */ + private function getPreloadedValue($uid, $cat, $k, $default = null) + { + $a = self::getApp(); + + $return = $default; + + if (isset($a->config[$uid][$cat][$k])) { + $return = $a->config[$uid][$cat][$k]; + } + + return $return; + } + + /** + * Sets a preloaded value in the App user config cache + * + * Accepts raw output from the pconfig table + * + * @param int $uid + * @param string $cat + * @param string $k + * @param mixed $v + */ + private function setPreloadedValue($uid, $cat, $k, $v) + { + $a = self::getApp(); + + // Only arrays are serialized in database, so we have to unserialize sparingly + $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v; + + $a->config[$uid][$cat][$k] = $value; + } + + /** + * Deletes a preloaded value from the App user config cache + * + * @param int $uid + * @param string $cat + * @param string $k + */ + private function deletePreloadedValue($uid, $cat, $k) + { + $a = self::getApp(); + + if (isset($a->config[$uid][$cat][$k])) { + unset($a->config[$uid][$cat][$k]); + } + } } From 047f94996773ac248442c9565991f3558dc9aa36 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 4 Mar 2018 22:59:26 -0500 Subject: [PATCH 06/10] Remove irrelevant else case --- src/Core/Config/PreloadConfigAdapter.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Core/Config/PreloadConfigAdapter.php b/src/Core/Config/PreloadConfigAdapter.php index 4abcc4a1a..6d4350042 100644 --- a/src/Core/Config/PreloadConfigAdapter.php +++ b/src/Core/Config/PreloadConfigAdapter.php @@ -47,8 +47,6 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter $config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]); if (DBM::is_result($config)) { $this->setPreloadedValue($cat, $k, $config['v']); - } else { - $this->deletePreloadedValue($cat, $k); } } From 87f2d185541eeb2475c16c844b172c5fa89a6527 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 6 Mar 2018 20:04:04 -0500 Subject: [PATCH 07/10] Move *ConfigValue functions to App --- src/App.php | 112 ++++++++++++++++++++++ src/Core/Config.php | 4 +- src/Core/Config/JITConfigAdapter.php | 22 ++--- src/Core/Config/JITPConfigAdapter.php | 22 +++-- src/Core/Config/PreloadConfigAdapter.php | 86 ++--------------- src/Core/Config/PreloadPConfigAdapter.php | 76 ++------------- 6 files changed, 150 insertions(+), 172 deletions(-) diff --git a/src/App.php b/src/App.php index 0acbfe6f4..2330bc118 100644 --- a/src/App.php +++ b/src/App.php @@ -944,4 +944,116 @@ class App return true; } + + /** + * @param string $cat Config category + * @param string $k Config key + * @param mixed $default Default value if it isn't set + */ + public function getConfigValue($cat, $k, $default = null) + { + $return = $default; + + if ($cat === 'config') { + if (isset($this->config[$k])) { + $return = $this->config[$k]; + } + } else { + if (isset($this->config[$cat][$k])) { + $return = $this->config[$cat][$k]; + } + } + + return $return; + } + + /** + * Sets a value in the config cache. Accepts raw output from the config table + * + * @param string $cat Config category + * @param string $k Config key + * @param mixed $v Value to set + */ + public function setConfigValue($cat, $k, $v) + { + // Only arrays are serialized in database, so we have to unserialize sparingly + $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v; + + if ($cat === 'config') { + $this->config[$k] = $value; + } else { + $this->config[$cat][$k] = $value; + } + } + + /** + * Deletes a value from the config cache + * + * @param string $cat Config category + * @param string $k Config key + */ + public function deleteConfigValue($cat, $k) + { + if ($cat === 'config') { + if (isset($this->config[$k])) { + unset($this->config[$k]); + } + } else { + if (isset($this->config[$cat][$k])) { + unset($this->config[$cat][$k]); + } + } + } + + + /** + * Retrieves a value from the user config cache + * + * @param int $uid User Id + * @param string $cat Config category + * @param string $k Config key + * @param mixed $default Default value if key isn't set + */ + public function getPConfigValue($uid, $cat, $k, $default = null) + { + $return = $default; + + if (isset($this->config[$uid][$cat][$k])) { + $return = $this->config[$uid][$cat][$k]; + } + + return $return; + } + + /** + * Sets a value in the user config cache + * + * Accepts raw output from the pconfig table + * + * @param int $uid User Id + * @param string $cat Config category + * @param string $k Config key + * @param mixed $v Value to set + */ + public function setPConfigValue($uid, $cat, $k, $v) + { + // Only arrays are serialized in database, so we have to unserialize sparingly + $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v; + + $this->config[$uid][$cat][$k] = $value; + } + + /** + * Deletes a value from the user config cache + * + * @param int $uid User Id + * @param string $cat Config category + * @param string $k Config key + */ + public function deletePConfigValue($uid, $cat, $k) + { + if (isset($this->config[$uid][$cat][$k])) { + unset($this->config[$uid][$cat][$k]); + } + } } diff --git a/src/Core/Config.php b/src/Core/Config.php index bff5d81ff..3c1d3245f 100644 --- a/src/Core/Config.php +++ b/src/Core/Config.php @@ -29,9 +29,7 @@ class Config extends BaseObject public static function init() { - $a = self::getApp(); - - if (isset($a->config['system']['config_adapter']) && $a->config['system']['config_adapter'] == 'preload') { + if (self::getApp()->getConfigValue('system', 'config_adapter') == 'preload') { self::$adapter = new Config\PreloadConfigAdapter(); } else { self::$adapter = new Config\JITConfigAdapter(); diff --git a/src/Core/Config/JITConfigAdapter.php b/src/Core/Config/JITConfigAdapter.php index 1bc3bf5a8..0e7731690 100644 --- a/src/Core/Config/JITConfigAdapter.php +++ b/src/Core/Config/JITConfigAdapter.php @@ -8,7 +8,7 @@ use Friendica\Database\DBM; require_once 'include/dba.php'; /** - * JustInTime ConfigAdapter + * JustInTime Configuration Adapter * * Default Config Adapter. Provides the best performance for pages loading few configuration variables. * @@ -27,17 +27,15 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter return; } - $a = self::getApp(); - $configs = dba::select('config', ['v', 'k'], ['cat' => $cat]); while ($config = dba::fetch($configs)) { $k = $config['k']; - if ($cat === 'config') { - $a->config[$k] = $config['v']; - } else { - $a->config[$cat][$k] = $config['v']; - self::$cache[$cat][$k] = $config['v']; - self::$in_db[$cat][$k] = true; + + self::getApp()->setConfigValue($cat, $k, $config['v']); + + if ($cat !== 'config') { + $this->cache[$cat][$k] = $config['v']; + $this->in_db[$cat][$k] = true; } } dba::close($configs); @@ -96,11 +94,7 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter return true; } - if ($cat === 'config') { - $a->config[$k] = $dbvalue; - } elseif ($cat != 'system') { - $a->config[$cat][$k] = $dbvalue; - } + self::getApp()->setConfigValue($cat, $k, $value); // Assign the just added value to the cache $this->cache[$cat][$k] = $dbvalue; diff --git a/src/Core/Config/JITPConfigAdapter.php b/src/Core/Config/JITPConfigAdapter.php index 27f2ed862..ce9c5b646 100644 --- a/src/Core/Config/JITPConfigAdapter.php +++ b/src/Core/Config/JITPConfigAdapter.php @@ -8,7 +8,7 @@ use Friendica\Database\DBM; require_once 'include/dba.php'; /** - * JustInTime PConfigAdapter + * JustInTime User Configuration Adapter * * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables. * @@ -26,7 +26,9 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter if (DBM::is_result($pconfigs)) { while ($pconfig = dba::fetch($pconfigs)) { $k = $pconfig['k']; - $a->config[$uid][$cat][$k] = $pconfig['v']; + + self::getApp()->setPConfigValue($uid, $cat, $k, $pconfig['v']); + $this->in_db[$uid][$cat][$k] = true; } } else if ($cat != 'config') { @@ -59,12 +61,15 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter $pconfig = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]); if (DBM::is_result($pconfig)) { $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']); - $a->config[$uid][$cat][$k] = $val; + + self::getApp()->setPConfigValue($uid, $cat, $k, $val); + $this->in_db[$uid][$cat][$k] = true; return $val; } else { - $a->config[$uid][$cat][$k] = '!!'; + self::getApp()->setPConfigValue($uid, $cat, $k, '!!'); + $this->in_db[$uid][$cat][$k] = false; return $default_value; @@ -73,8 +78,6 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter public function set($uid, $cat, $k, $value) { - $a = self::getApp(); - // We store our setting values in a string variable. // So we have to do the conversion here so that the compare below works. // The exception are array values. @@ -86,7 +89,7 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter return true; } - $a->config[$uid][$cat][$k] = $dbvalue; + self::getApp()->setPConfigValue($uid, $cat, $k, $value); // manage array value $dbvalue = (is_array($value) ? serialize($value) : $dbvalue); @@ -103,10 +106,9 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter public function delete($uid, $cat, $k) { - $a = self::getApp(); + self::getApp()->deletePConfigValue($uid, $cat, $k); - if (!empty($a->config[$uid][$cat][$k])) { - unset($a->config[$uid][$cat][$k]); + if (!empty($this->in_db[$uid][$cat][$k])) { unset($this->in_db[$uid][$cat][$k]); } diff --git a/src/Core/Config/PreloadConfigAdapter.php b/src/Core/Config/PreloadConfigAdapter.php index 6d4350042..f87b47f16 100644 --- a/src/Core/Config/PreloadConfigAdapter.php +++ b/src/Core/Config/PreloadConfigAdapter.php @@ -11,9 +11,9 @@ use Friendica\Database\DBM; require_once 'include/dba.php'; /** - * Preload ConfigAdapter + * Preload Configuration Adapter * - * Minimize the number of database queries to retrieve configuration values at the cost of memory. + * Minimizes the number of database queries to retrieve configuration values at the cost of memory. * * @author Hypolite Petovan */ @@ -34,7 +34,7 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter $configs = dba::select('config', ['cat', 'v', 'k']); while ($config = dba::fetch($configs)) { - $this->setPreloadedValue($config['cat'], $config['k'], $config['v']); + self::getApp()->setConfigValue($config['cat'], $config['k'], $config['v']); } dba::close($configs); @@ -46,11 +46,11 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter if ($refresh) { $config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]); if (DBM::is_result($config)) { - $this->setPreloadedValue($cat, $k, $config['v']); + self::getApp()->setConfigValue($cat, $k, $config['v']); } } - $return = $this->getPreloadedValue($cat, $k, $default_value); + $return = self::getApp()->getConfigValue($cat, $k, $default_value); return $return; } @@ -62,11 +62,11 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter // The exception are array values. $compare_value = !is_array($value) ? (string)$value : $value; - if ($this->getPreloadedValue($cat, $k) === $compare_value) { + if (self::getApp()->getConfigValue($cat, $k) === $compare_value) { return true; } - $this->setPreloadedValue($cat, $k, $value); + self::getApp()->setConfigValue($cat, $k, $value); // manage array value $dbvalue = is_array($value) ? serialize($value) : $value; @@ -81,80 +81,10 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter public function delete($cat, $k) { - $this->deletePreloadedValue($cat, $k); + self::getApp()->deleteConfigValue($cat, $k); $result = dba::delete('config', ['cat' => $cat, 'k' => $k]); return $result; } - - /** - * Retrieves a preloaded value from the App config cache - * - * @param string $cat - * @param string $k - * @param mixed $default - */ - private function getPreloadedValue($cat, $k, $default = null) - { - $a = self::getApp(); - - $return = $default; - - if ($cat === 'config') { - if (isset($a->config[$k])) { - $return = $a->config[$k]; - } - } else { - if (isset($a->config[$cat][$k])) { - $return = $a->config[$cat][$k]; - } - } - - return $return; - } - - /** - * Sets a preloaded value in the App config cache - * - * Accepts raw output from the config table - * - * @param string $cat - * @param string $k - * @param mixed $v - */ - private function setPreloadedValue($cat, $k, $v) - { - $a = self::getApp(); - - // Only arrays are serialized in database, so we have to unserialize sparingly - $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v; - - if ($cat === 'config') { - $a->config[$k] = $value; - } else { - $a->config[$cat][$k] = $value; - } - } - - /** - * Deletes a preloaded value from the App config cache - * - * @param string $cat - * @param string $k - */ - private function deletePreloadedValue($cat, $k) - { - $a = self::getApp(); - - if ($cat === 'config') { - if (isset($a->config[$k])) { - unset($a->config[$k]); - } - } else { - if (isset($a->config[$cat][$k])) { - unset($a->config[$cat][$k]); - } - } - } } diff --git a/src/Core/Config/PreloadPConfigAdapter.php b/src/Core/Config/PreloadPConfigAdapter.php index d6a44e07c..d23541033 100644 --- a/src/Core/Config/PreloadPConfigAdapter.php +++ b/src/Core/Config/PreloadPConfigAdapter.php @@ -11,9 +11,9 @@ use Friendica\Database\DBM; require_once 'include/dba.php'; /** - * Preload PConfigAdapter + * Preload User Configuration Adapter * - * Minimize the number of database queries to retrieve configuration values at the cost of memory. + * Minimizes the number of database queries to retrieve configuration values at the cost of memory. * * @author Hypolite Petovan */ @@ -34,7 +34,7 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter $pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]); while ($pconfig = dba::fetch($pconfigs)) { - $this->setPreloadedValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']); + self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']); } dba::close($pconfigs); @@ -46,13 +46,13 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter if ($refresh) { $config = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]); if (DBM::is_result($config)) { - $this->setPreloadedValue($uid, $cat, $k, $config['v']); + self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']); } else { - $this->deletePreloadedValue($uid, $cat, $k); + self::getApp()->deletePConfigValue($uid, $cat, $k); } } - $return = $this->getPreloadedValue($uid, $cat, $k, $default_value); + $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value); return $return; } @@ -64,11 +64,11 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter // The exception are array values. $compare_value = !is_array($value) ? (string)$value : $value; - if ($this->getPreloadedValue($uid, $cat, $k) === $compare_value) { + if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) { return true; } - $this->setPreloadedValue($uid, $cat, $k, $value); + self::getApp()->setPConfigValue($uid, $cat, $k, $value); // manage array value $dbvalue = is_array($value) ? serialize($value) : $value; @@ -83,68 +83,10 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter public function delete($uid, $cat, $k) { - $this->deletePreloadedValue($uid, $cat, $k); + self::getApp()->deletePConfigValue($uid, $cat, $k); $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]); return $result; } - - - /** - * Retrieves a preloaded value from the App user config cache - * - * @param int $uid - * @param string $cat - * @param string $k - * @param mixed $default - */ - private function getPreloadedValue($uid, $cat, $k, $default = null) - { - $a = self::getApp(); - - $return = $default; - - if (isset($a->config[$uid][$cat][$k])) { - $return = $a->config[$uid][$cat][$k]; - } - - return $return; - } - - /** - * Sets a preloaded value in the App user config cache - * - * Accepts raw output from the pconfig table - * - * @param int $uid - * @param string $cat - * @param string $k - * @param mixed $v - */ - private function setPreloadedValue($uid, $cat, $k, $v) - { - $a = self::getApp(); - - // Only arrays are serialized in database, so we have to unserialize sparingly - $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v; - - $a->config[$uid][$cat][$k] = $value; - } - - /** - * Deletes a preloaded value from the App user config cache - * - * @param int $uid - * @param string $cat - * @param string $k - */ - private function deletePreloadedValue($uid, $cat, $k) - { - $a = self::getApp(); - - if (isset($a->config[$uid][$cat][$k])) { - unset($a->config[$uid][$cat][$k]); - } - } } From 354bec58c080c7207d94a0fd5feb54e8b1056460 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 7 Mar 2018 12:34:47 -0500 Subject: [PATCH 08/10] Ensure BaseObject::setApp() whenever App is instantiated --- boot.php | 2 ++ index.php | 4 +--- scripts/auth_ejabberd.php | 2 ++ scripts/dbstructure.php | 1 + scripts/worker.php | 2 ++ util/maintenance.php | 7 ++++--- util/typo.php | 7 ++++--- 7 files changed, 16 insertions(+), 9 deletions(-) diff --git a/boot.php b/boot.php index a069dcafc..9df563188 100644 --- a/boot.php +++ b/boot.php @@ -20,6 +20,7 @@ require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; use Friendica\App; +use Friendica\BaseObject; use Friendica\Core\Addon; use Friendica\Core\Cache; use Friendica\Core\Config; @@ -536,6 +537,7 @@ function get_app() if (empty($a)) { $a = new App(dirname(__DIR__)); + BaseObject::setApp($a); } return $a; diff --git a/index.php b/index.php index 4fad08da8..fba16263a 100644 --- a/index.php +++ b/index.php @@ -24,9 +24,7 @@ use Friendica\Module\Login; require_once 'boot.php'; -if (empty($a)) { - $a = new App(__DIR__); -} +$a = new App(__DIR__); BaseObject::setApp($a); // We assume that the index.php is called by a frontend process diff --git a/scripts/auth_ejabberd.php b/scripts/auth_ejabberd.php index 5c516f398..3acd0590e 100755 --- a/scripts/auth_ejabberd.php +++ b/scripts/auth_ejabberd.php @@ -33,6 +33,7 @@ */ use Friendica\App; +use Friendica\BaseObject; use Friendica\Util\ExAuth; if (sizeof($_SERVER["argv"]) == 0) { @@ -53,6 +54,7 @@ require_once "boot.php"; require_once "include/dba.php"; $a = new App(dirname(__DIR__)); +BaseObject::setApp($a); @include ".htconfig.php"; dba::connect($db_host, $db_user, $db_pass, $db_data); diff --git a/scripts/dbstructure.php b/scripts/dbstructure.php index 3787af225..ff5b53c62 100755 --- a/scripts/dbstructure.php +++ b/scripts/dbstructure.php @@ -13,6 +13,7 @@ require_once "boot.php"; require_once "include/dba.php"; $a = new App(dirname(__DIR__)); +BaseObject::setApp($a); @include ".htconfig.php"; dba::connect($db_host, $db_user, $db_pass, $db_data); diff --git a/scripts/worker.php b/scripts/worker.php index a821fc1ab..1980a21b1 100755 --- a/scripts/worker.php +++ b/scripts/worker.php @@ -6,6 +6,7 @@ */ use Friendica\App; +use Friendica\BaseObject; use Friendica\Core\Addon; use Friendica\Core\Config; use Friendica\Core\Worker; @@ -26,6 +27,7 @@ require_once "boot.php"; require_once "include/dba.php"; $a = new App(dirname(__DIR__)); +BaseObject::setApp($a); require_once ".htconfig.php"; dba::connect($db_host, $db_user, $db_pass, $db_data); diff --git a/util/maintenance.php b/util/maintenance.php index a697e66d5..f0d777776 100644 --- a/util/maintenance.php +++ b/util/maintenance.php @@ -2,16 +2,17 @@ /** * @file util/maintenance.php */ + use Friendica\App; +use Friendica\BaseObject; use Friendica\Core\Config; use Friendica\Core\L10n; require_once 'boot.php'; require_once 'include/dba.php'; -if (empty($a)) { - $a = new App(dirname(__DIR__)); -} +$a = new App(dirname(__DIR__)); +BaseObject::setApp($a); @include(".htconfig.php"); diff --git a/util/typo.php b/util/typo.php index 313033f3e..6a5188039 100755 --- a/util/typo.php +++ b/util/typo.php @@ -5,7 +5,9 @@ // Run this from cmdline in basedir and quickly see if we've // got any parse errors in our application files. + use Friendica\App; +use Friendica\BaseObject; error_reporting(E_ERROR | E_WARNING | E_PARSE); ini_set('display_errors', '1'); @@ -13,9 +15,8 @@ ini_set('log_errors', '0'); include 'boot.php'; -if (empty($a)) { - $a = new App(dirname(__DIR__)); -} +$a = new App(dirname(__DIR__)); +BaseObject::setApp($a); if (x($a->config, 'php_path')) { $phpath = $a->config['php_path']; From 7e765199e84809d9aaf771df4828fe677579bdcd Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 7 Mar 2018 12:35:16 -0500 Subject: [PATCH 09/10] Ensure Config::load() in scripts --- scripts/auth_ejabberd.php | 3 +++ scripts/dbstructure.php | 2 ++ util/typo.php | 8 +++----- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/scripts/auth_ejabberd.php b/scripts/auth_ejabberd.php index 3acd0590e..5ca4f5851 100755 --- a/scripts/auth_ejabberd.php +++ b/scripts/auth_ejabberd.php @@ -34,6 +34,7 @@ use Friendica\App; use Friendica\BaseObject; +use Friendica\Core\Config; use Friendica\Util\ExAuth; if (sizeof($_SERVER["argv"]) == 0) { @@ -60,6 +61,8 @@ BaseObject::setApp($a); dba::connect($db_host, $db_user, $db_pass, $db_data); unset($db_host, $db_user, $db_pass, $db_data); +Config::load(); + $oAuth = new ExAuth(); $oAuth->readStdin(); diff --git a/scripts/dbstructure.php b/scripts/dbstructure.php index ff5b53c62..ae9ab46f8 100755 --- a/scripts/dbstructure.php +++ b/scripts/dbstructure.php @@ -19,6 +19,8 @@ BaseObject::setApp($a); dba::connect($db_host, $db_user, $db_pass, $db_data); unset($db_host, $db_user, $db_pass, $db_data); +Config::load(); + if ($_SERVER["argc"] == 2) { switch ($_SERVER["argv"][1]) { case "dryrun": diff --git a/util/typo.php b/util/typo.php index 6a5188039..fba761e81 100755 --- a/util/typo.php +++ b/util/typo.php @@ -18,11 +18,9 @@ include 'boot.php'; $a = new App(dirname(__DIR__)); BaseObject::setApp($a); -if (x($a->config, 'php_path')) { - $phpath = $a->config['php_path']; -} else { - $phpath = 'php'; -} +@include '.htconfig.php'; + +$phpath = $a->getConfigValue('config', 'php_path', 'php'); echo 'Directory: src' . PHP_EOL; $Iterator = new RecursiveDirectoryIterator('src'); From b40461975a0e3091a375120c73048ec672e7cd5f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 7 Mar 2018 12:45:30 -0500 Subject: [PATCH 10/10] Remove unecessary added Config::load() --- scripts/auth_ejabberd.php | 2 -- scripts/dbstructure.php | 2 -- 2 files changed, 4 deletions(-) diff --git a/scripts/auth_ejabberd.php b/scripts/auth_ejabberd.php index 5ca4f5851..32aa30898 100755 --- a/scripts/auth_ejabberd.php +++ b/scripts/auth_ejabberd.php @@ -61,8 +61,6 @@ BaseObject::setApp($a); dba::connect($db_host, $db_user, $db_pass, $db_data); unset($db_host, $db_user, $db_pass, $db_data); -Config::load(); - $oAuth = new ExAuth(); $oAuth->readStdin(); diff --git a/scripts/dbstructure.php b/scripts/dbstructure.php index ae9ab46f8..ff5b53c62 100755 --- a/scripts/dbstructure.php +++ b/scripts/dbstructure.php @@ -19,8 +19,6 @@ BaseObject::setApp($a); dba::connect($db_host, $db_user, $db_pass, $db_data); unset($db_host, $db_user, $db_pass, $db_data); -Config::load(); - if ($_SERVER["argc"] == 2) { switch ($_SERVER["argv"][1]) { case "dryrun":