variable_store.module 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @file
  4. * Variable API module - Database storage
  5. *
  6. * This module provides database storage for variable realms
  7. */
  8. /**
  9. * Get variable store
  10. */
  11. function &variable_store($realm, $key) {
  12. $variable_store = &drupal_static('variable_store');
  13. if (!isset($variable_store[$realm][$key])) {
  14. $variable_store[$realm][$key] = _variable_store_load($realm, $key);
  15. }
  16. return $variable_store[$realm][$key];
  17. }
  18. /**
  19. * Implementation of hook_boot()
  20. */
  21. function variable_store_boot() {
  22. // Do nothing, we just want this module to be available for boot.
  23. }
  24. /**
  25. * Delete variable from db
  26. */
  27. function variable_store_del($realm, $key, $name) {
  28. $store = &variable_store($realm, $key);
  29. db_delete('variable_store')
  30. ->condition('realm', $realm)
  31. ->condition('realm_key', $key)
  32. ->condition('name', $name)
  33. ->execute();
  34. unset($store[$name]);
  35. cache_clear_all('variable:' . $realm . ':' . $key, 'cache_bootstrap');
  36. }
  37. /**
  38. * Get single variable from store
  39. */
  40. function variable_store_get($realm, $key, $name, $default = NULL) {
  41. $variables = variable_store($realm, $key);
  42. return $variables && isset($variables[$name]) ? $variables[$name] : $default;
  43. }
  44. /**
  45. * Delete realm variable or full realm from store.
  46. *
  47. * @param $realm
  48. * Realm name to delete. NULL to delete all realms.
  49. * @param $key
  50. * Realm key to delete. NULL to delete all realm keys.
  51. * @param $name
  52. * Variable name to delete. NULL to delete all variables for that realm, key
  53. */
  54. function variable_store_delete_all($realm, $key, $name = NULL) {
  55. _variable_store_reset();
  56. $query = db_delete('variable_store');
  57. if (isset($realm)) {
  58. $query->condition('realm', $realm);
  59. }
  60. if (isset($key)) {
  61. $query->condition('realm_key', $key);
  62. }
  63. if (isset($name)) {
  64. $query->condition('name', $name);
  65. }
  66. return $query->execute();
  67. }
  68. /**
  69. * List all variable names from a realm.
  70. *
  71. * @param $realm
  72. * Realm name to list. NULL to list all realms.
  73. * @param $key
  74. * Realm key to list. NULL to list all realm keys.
  75. *
  76. * @return array
  77. * List of variable names.
  78. */
  79. function variable_store_list_all($realm, $key = NULL) {
  80. $query = db_select('variable_store', 'vs')
  81. ->fields('vs', array('name'))
  82. ->distinct();
  83. if ($realm) {
  84. $query->condition('realm', $realm);
  85. }
  86. if ($key) {
  87. $query->condition('realm_key', $key);
  88. }
  89. return $query->execute()->fetchCol();
  90. }
  91. /**
  92. * Load realm from db store
  93. */
  94. function _variable_store_load($realm, $key) {
  95. $cacheid = 'variable:' . $realm . ':' . $key;
  96. if ($cached = cache_get($cacheid, 'cache_bootstrap')) {
  97. $variables = $cached->data;
  98. }
  99. else {
  100. $result = db_select('variable_store', 's')
  101. ->fields('s', array('name', 'value', 'serialized'))
  102. ->condition('realm', $realm)
  103. ->condition('realm_key', $key)
  104. ->execute();
  105. $variables = array();
  106. foreach ($result as $variable) {
  107. $variables[$variable->name] = $variable->serialized ? unserialize($variable->value) : $variable->value;
  108. }
  109. cache_set($cacheid, $variables, 'cache_bootstrap');
  110. }
  111. return $variables;
  112. }
  113. /**
  114. * Reset caches and static variables.
  115. */
  116. function _variable_store_reset() {
  117. $store = &drupal_static('variable_store', array());
  118. foreach ($store as $realm => &$realm_store) {
  119. foreach (array_keys($realm_store) as $key) {
  120. $realm_store[$key] = NULL;
  121. }
  122. }
  123. cache_clear_all('variable:', 'cache_bootstrap', TRUE);
  124. }
  125. /**
  126. * Set variable value
  127. */
  128. function variable_store_set($realm, $key, $name, $value, $rebuild = TRUE) {
  129. $store = &variable_store($realm, $key);
  130. $serialize = !is_int($value) && !is_string($value);
  131. db_merge('variable_store')
  132. ->key(array('realm' => $realm, 'realm_key' => $key, 'name' => $name))
  133. ->fields(array('value' => $serialize ? serialize($value) : $value, 'serialized' => $serialize ? 1 : 0))
  134. ->execute();
  135. cache_clear_all('variable:' . $realm . ':' . $key, 'cache_bootstrap');
  136. $store[$name] = $value;
  137. }