Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
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.

PreloadPConfigAdapter.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Friendica\Core\Config\Adapter;
  3. use Friendica\Database\DBA;
  4. /**
  5. * Preload User Configuration Adapter
  6. *
  7. * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
  8. *
  9. * @author Hypolite Petovan <hypolite@mrpetovan.com>
  10. */
  11. class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
  12. {
  13. private $config_loaded = false;
  14. /**
  15. * @param int $uid The UID of the current user
  16. */
  17. public function __construct($uid = null)
  18. {
  19. parent::__construct();
  20. if (isset($uid)) {
  21. $this->load($uid, 'config');
  22. }
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function load($uid, $cat)
  28. {
  29. $return = [];
  30. if ($this->config_loaded) {
  31. return $return;
  32. }
  33. if (empty($uid)) {
  34. return $return;
  35. }
  36. $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
  37. while ($pconfig = DBA::fetch($pconfigs)) {
  38. $return[$pconfig['k']] = $pconfig['v'];
  39. }
  40. DBA::close($pconfigs);
  41. $this->config_loaded = true;
  42. return [$cat => $return];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function get($uid, $cat, $key)
  48. {
  49. if (!$this->isConnected()) {
  50. return null;
  51. }
  52. if (!$this->config_loaded) {
  53. $this->load($uid, $cat);
  54. }
  55. $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
  56. if (DBA::isResult($config)) {
  57. // manage array value
  58. $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
  59. return $value;
  60. } else {
  61. return '!<unset>!';
  62. }
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function set($uid, $cat, $key, $value)
  68. {
  69. if (!$this->isConnected()) {
  70. return false;
  71. }
  72. if (!$this->config_loaded) {
  73. $this->load($uid, $cat);
  74. }
  75. // We store our setting values as strings.
  76. // So we have to do the conversion here so that the compare below works.
  77. // The exception are array values.
  78. $compare_value = !is_array($value) ? (string)$value : $value;
  79. if ($this->get($uid, $cat, $key) === $compare_value) {
  80. return true;
  81. }
  82. // manage array value
  83. $dbvalue = is_array($value) ? serialize($value) : $value;
  84. $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
  85. return $result;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function delete($uid, $cat, $key)
  91. {
  92. if (!$this->isConnected()) {
  93. return false;
  94. }
  95. if (!$this->config_loaded) {
  96. $this->load($uid, $cat);
  97. }
  98. $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
  99. return $result;
  100. }
  101. }