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.

JITPConfigAdapter.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Friendica\Core\Config\Adapter;
  3. use Friendica\Database\DBA;
  4. /**
  5. * JustInTime User Configuration Adapter
  6. *
  7. * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
  8. *
  9. * @author Hypolite Petovan <hypolite@mrpetovan.com>
  10. */
  11. class JITPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
  12. {
  13. private $in_db;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function load($uid, $cat)
  18. {
  19. $return = [];
  20. if (!$this->isConnected()) {
  21. return $return;
  22. }
  23. $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
  24. if (DBA::isResult($pconfigs)) {
  25. while ($pconfig = DBA::fetch($pconfigs)) {
  26. $key = $pconfig['k'];
  27. $return[$key] = $pconfig['v'];
  28. $this->in_db[$uid][$cat][$key] = true;
  29. }
  30. } else if ($cat != 'config') {
  31. // Negative caching
  32. $return[null] = "!<unset>!";
  33. }
  34. DBA::close($pconfigs);
  35. return [$cat => $return];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function get($uid, $cat, $key)
  41. {
  42. if (!$this->isConnected()) {
  43. return null;
  44. }
  45. $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
  46. if (DBA::isResult($pconfig)) {
  47. // manage array value
  48. $value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
  49. $this->in_db[$uid][$cat][$key] = true;
  50. return $value;
  51. } else {
  52. $this->in_db[$uid][$cat][$key] = false;
  53. return '!<unset>!';
  54. }
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function set($uid, $cat, $key, $value)
  60. {
  61. if (!$this->isConnected()) {
  62. return false;
  63. }
  64. // We store our setting values in a string variable.
  65. // So we have to do the conversion here so that the compare below works.
  66. // The exception are array values.
  67. $dbvalue = (!is_array($value) ? (string)$value : $value);
  68. $stored = $this->get($uid, $cat, $key);
  69. if (!isset($this->in_db[$uid])) {
  70. $this->in_db[$uid] = [];
  71. }
  72. if (!isset($this->in_db[$uid][$cat])) {
  73. $this->in_db[$uid][$cat] = [];
  74. }
  75. if (!isset($this->in_db[$uid][$cat][$key])) {
  76. $this->in_db[$uid][$cat][$key] = false;
  77. }
  78. if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$key]) {
  79. return true;
  80. }
  81. // manage array value
  82. $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
  83. $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
  84. $this->in_db[$uid][$cat][$key] = $result;
  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 (!empty($this->in_db[$uid][$cat][$key])) {
  96. unset($this->in_db[$uid][$cat][$key]);
  97. }
  98. $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
  99. return $result;
  100. }
  101. }