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.

136 lines
3.3KB

  1. <?php
  2. namespace Friendica\Core\Config;
  3. use Friendica\BaseObject;
  4. use Friendica\Database\DBA;
  5. /**
  6. * JustInTime Configuration Adapter
  7. *
  8. * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
  9. *
  10. * @author Hypolite Petovan <hypolite@mrpetovan.com>
  11. */
  12. class JITConfigAdapter extends BaseObject implements IConfigAdapter
  13. {
  14. private $cache;
  15. private $in_db;
  16. public function load($cat = "config")
  17. {
  18. // We don't preload "system" anymore.
  19. // This reduces the number of database reads a lot.
  20. if ($cat === 'system') {
  21. return;
  22. }
  23. $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
  24. while ($config = DBA::fetch($configs)) {
  25. $k = $config['k'];
  26. self::getApp()->setConfigValue($cat, $k, $config['v']);
  27. if ($cat !== 'config') {
  28. $this->cache[$cat][$k] = $config['v'];
  29. $this->in_db[$cat][$k] = true;
  30. }
  31. }
  32. DBA::close($configs);
  33. }
  34. public function get($cat, $k, $default_value = null, $refresh = false)
  35. {
  36. $a = self::getApp();
  37. if (!$refresh) {
  38. // Do we have the cached value? Then return it
  39. if (isset($this->cache[$cat][$k])) {
  40. if ($this->cache[$cat][$k] === '!<unset>!') {
  41. return $default_value;
  42. } else {
  43. return $this->cache[$cat][$k];
  44. }
  45. }
  46. }
  47. $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
  48. if (DBA::isResult($config)) {
  49. // manage array value
  50. $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
  51. // Assign the value from the database to the cache
  52. $this->cache[$cat][$k] = $value;
  53. $this->in_db[$cat][$k] = true;
  54. return $value;
  55. } elseif (isset($a->config[$cat][$k])) {
  56. // Assign the value (mostly) from config/local.config.php file to the cache
  57. $this->cache[$cat][$k] = $a->config[$cat][$k];
  58. $this->in_db[$cat][$k] = false;
  59. return $a->config[$cat][$k];
  60. } elseif (isset($a->config[$k])) {
  61. // Assign the value (mostly) from config/local.config.php file to the cache
  62. $this->cache[$k] = $a->config[$k];
  63. $this->in_db[$k] = false;
  64. return $a->config[$k];
  65. }
  66. $this->cache[$cat][$k] = '!<unset>!';
  67. $this->in_db[$cat][$k] = false;
  68. return $default_value;
  69. }
  70. public function set($cat, $k, $value)
  71. {
  72. $a = self::getApp();
  73. // We store our setting values in a string variable.
  74. // So we have to do the conversion here so that the compare below works.
  75. // The exception are array values.
  76. $dbvalue = (!is_array($value) ? (string)$value : $value);
  77. $stored = $this->get($cat, $k, null, true);
  78. if (!isset($this->in_db[$cat])) {
  79. $this->in_db[$cat] = [];
  80. }
  81. if (!isset($this->in_db[$cat][$k])) {
  82. $this->in_db[$cat] = false;
  83. }
  84. if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
  85. return true;
  86. }
  87. self::getApp()->setConfigValue($cat, $k, $value);
  88. // Assign the just added value to the cache
  89. $this->cache[$cat][$k] = $dbvalue;
  90. // manage array value
  91. $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
  92. $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
  93. if ($result) {
  94. $this->in_db[$cat][$k] = true;
  95. }
  96. return $result;
  97. }
  98. public function delete($cat, $k)
  99. {
  100. if (isset($this->cache[$cat][$k])) {
  101. unset($this->cache[$cat][$k]);
  102. unset($this->in_db[$cat][$k]);
  103. }
  104. $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
  105. return $result;
  106. }
  107. }