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.

116 lines
2.8KB

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