3 namespace Friendica\Core\Config;
\r
7 use Friendica\BaseObject;
\r
9 require_once 'include/dba.php';
\r
12 * Preload PConfigAdapter
\r
14 * Minimize the number of database queries to retrieve configuration values at the cost of memory.
\r
16 * @author Hypolite Petovan <mrpetovan@gmail.com>
\r
18 class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
\r
20 private $config_loaded = false;
\r
22 public function __construct($uid)
\r
24 $this->load($uid, 'config');
\r
27 public function load($uid, $family)
\r
29 if ($this->config_loaded) {
\r
33 $a = self::getApp();
\r
35 $pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
\r
36 while ($pconfig = dba::fetch($pconfigs)) {
\r
37 $cat = $pconfig['cat'];
\r
39 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
\r
41 $a->config[$uid][$cat][$k] = $value;
\r
43 dba::close($pconfigs);
\r
45 $this->config_loaded = true;
\r
48 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
\r
50 $a = self::getApp();
\r
52 $return = $default_value;
\r
54 if (isset($a->config[$uid][$cat][$k])) {
\r
55 $return = $a->config[$uid][$cat][$k];
\r
61 public function set($uid, $cat, $k, $value)
\r
63 $a = self::getApp();
\r
65 // We store our setting values as strings.
\r
66 // So we have to do the conversion here so that the compare below works.
\r
67 // The exception are array values.
\r
68 $compare_value = !is_array($value) ? (string)$value : $value;
\r
70 if ($this->get($uid, $cat, $k) === $compare_value) {
\r
74 $a->config[$uid][$cat][$k] = $value;
\r
76 // manage array value
\r
77 $dbvalue = is_array($value) ? serialize($value) : $value;
\r
79 $result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
\r
82 throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
\r
88 public function delete($uid, $cat, $k)
\r
90 $a = self::getApp();
\r
92 if (!isset($a->config[$uid][$cat][$k])) {
\r
96 unset($a->config[$uid][$cat][$k]);
\r
98 $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
\r