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.

PConfiguration.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Friendica\Core\Config;
  3. /**
  4. * This class is responsible for the user-specific configuration values in Friendica
  5. * The values are set through the Config-DB-Table (per Config-DB-adapter @see Adapter\IPConfigAdapter )
  6. *
  7. * The configuration cache (@see Cache\IPConfigCache ) is used for temporary caching of database calls. This will
  8. * increase the performance.
  9. */
  10. class PConfiguration
  11. {
  12. /**
  13. * @var Cache\IPConfigCache
  14. */
  15. private $configCache;
  16. /**
  17. * @var Adapter\IPConfigAdapter
  18. */
  19. private $configAdapter;
  20. /**
  21. * @param Cache\IPConfigCache $configCache The configuration cache
  22. * @param Adapter\IPConfigAdapter $configAdapter The configuration DB-backend
  23. */
  24. public function __construct(Cache\IPConfigCache $configCache, Adapter\IPConfigAdapter $configAdapter)
  25. {
  26. $this->configCache = $configCache;
  27. $this->configAdapter = $configAdapter;
  28. }
  29. /**
  30. * @brief Loads all configuration values of a user's config family into a cached storage.
  31. *
  32. * All configuration values of the given user are stored with the $uid in
  33. * the cache ( @see IPConfigCache )
  34. *
  35. * @param string $uid The user_id
  36. * @param string $cat The category of the configuration value
  37. *
  38. * @return void
  39. */
  40. public function load($uid, $cat = 'config')
  41. {
  42. // If not connected, do nothing
  43. if (!$this->configAdapter->isConnected()) {
  44. return;
  45. }
  46. // load the whole category out of the DB into the cache
  47. $this->configCache->loadP($uid, $this->configAdapter->load($uid, $cat));
  48. }
  49. /**
  50. * @brief Get a particular user's config variable given the category name
  51. * ($cat) and a key.
  52. *
  53. * Get a particular user's config value from the given category ($cat)
  54. * and the $key with the $uid from a cached storage either from the $this->configAdapter
  55. * (@see IConfigAdapter ) or from the $this->configCache (@see IConfigCache ).
  56. *
  57. * @param string $uid The user_id
  58. * @param string $cat The category of the configuration value
  59. * @param string $key The configuration key to query
  60. * @param mixed $default_value optional, The value to return if key is not set (default: null)
  61. * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
  62. *
  63. * @return mixed Stored value or null if it does not exist
  64. */
  65. public function get($uid, $cat, $key, $default_value = null, $refresh = false)
  66. {
  67. // Return the value of the cache if found and no refresh is forced
  68. if (!$refresh && $this->configCache->hasP($uid, $cat, $key)) {
  69. return $this->configCache->getP($uid, $cat, $key);
  70. }
  71. // if we don't find the value in the cache and the adapter isn't ready, return the default value
  72. if (!$this->configAdapter->isConnected()) {
  73. return $default_value;
  74. }
  75. // load DB value to cache
  76. $dbvalue = $this->configAdapter->get($uid, $cat, $key);
  77. if ($dbvalue !== '!<unset>!') {
  78. $this->configCache->setP($uid, $cat, $key, $dbvalue);
  79. return $dbvalue;
  80. } else {
  81. return $default_value;
  82. }
  83. }
  84. /**
  85. * @brief Sets a configuration value for a user
  86. *
  87. * Stores a config value ($value) in the category ($family) under the key ($key)
  88. * for the user_id $uid.
  89. *
  90. * @note Please do not store booleans - convert to 0/1 integer values!
  91. *
  92. * @param string $uid The user_id
  93. * @param string $cat The category of the configuration value
  94. * @param string $key The configuration key to set
  95. * @param mixed $value The value to store
  96. *
  97. * @return bool Operation success
  98. */
  99. public function set($uid, $cat, $key, $value)
  100. {
  101. // set the cache first
  102. $cached = $this->configCache->setP($uid, $cat, $key, $value);
  103. // If there is no connected adapter, we're finished
  104. if (!$this->configAdapter->isConnected()) {
  105. return $cached;
  106. }
  107. $stored = $this->configAdapter->set($uid, $cat, $key, $value);
  108. return $cached && $stored;
  109. }
  110. /**
  111. * @brief Deletes the given key from the users's configuration.
  112. *
  113. * Removes the configured value from the stored cache in $this->configCache
  114. * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter )
  115. * with the given $uid.
  116. *
  117. * @param string $uid The user_id
  118. * @param string $cat The category of the configuration value
  119. * @param string $key The configuration key to delete
  120. *
  121. * @return bool
  122. */
  123. public function delete($uid, $cat, $key)
  124. {
  125. $cacheRemoved = $this->configCache->deleteP($uid, $cat, $key);
  126. if (!$this->configAdapter->isConnected()) {
  127. return $cacheRemoved;
  128. }
  129. $storeRemoved = $this->configAdapter->delete($uid, $cat, $key);
  130. return $cacheRemoved || $storeRemoved;
  131. }
  132. }