Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.5KB

  1. <?php
  2. namespace Friendica\Core\Config;
  3. use Exception;
  4. use Friendica\BaseObject;
  5. use Friendica\Database\DBA;
  6. /**
  7. * Preload User Configuration Adapter
  8. *
  9. * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
  10. *
  11. * @author Hypolite Petovan <hypolite@mrpetovan.com>
  12. */
  13. class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
  14. {
  15. private $config_loaded = false;
  16. public function __construct($uid)
  17. {
  18. $this->load($uid, 'config');
  19. }
  20. public function load($uid, $family)
  21. {
  22. if ($this->config_loaded) {
  23. return;
  24. }
  25. if (empty($uid)) {
  26. return;
  27. }
  28. $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
  29. while ($pconfig = DBA::fetch($pconfigs)) {
  30. self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
  31. }
  32. DBA::close($pconfigs);
  33. $this->config_loaded = true;
  34. }
  35. public function get($uid, $cat, $k, $default_value = null, $refresh = false)
  36. {
  37. if (!$this->config_loaded) {
  38. $this->load($uid, $cat);
  39. }
  40. if ($refresh) {
  41. $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
  42. if (DBA::isResult($config)) {
  43. self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']);
  44. } else {
  45. self::getApp()->deletePConfigValue($uid, $cat, $k);
  46. }
  47. }
  48. $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
  49. return $return;
  50. }
  51. public function set($uid, $cat, $k, $value)
  52. {
  53. if (!$this->config_loaded) {
  54. $this->load($uid, $cat);
  55. }
  56. // We store our setting values as strings.
  57. // So we have to do the conversion here so that the compare below works.
  58. // The exception are array values.
  59. $compare_value = !is_array($value) ? (string)$value : $value;
  60. if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
  61. return true;
  62. }
  63. self::getApp()->setPConfigValue($uid, $cat, $k, $value);
  64. // manage array value
  65. $dbvalue = is_array($value) ? serialize($value) : $value;
  66. $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
  67. if (!$result) {
  68. throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
  69. }
  70. return true;
  71. }
  72. public function delete($uid, $cat, $k)
  73. {
  74. if (!$this->config_loaded) {
  75. $this->load($uid, $cat);
  76. }
  77. self::getApp()->deletePConfigValue($uid, $cat, $k);
  78. $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
  79. return $result;
  80. }
  81. }