cache.inc

  1. 3.x includes/cache.inc
  2. 2.x includes/cache.inc

Load Views' data so that it knows what is available to build queries from.

File

includes/cache.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Load Views' data so that it knows what is available to build queries from.
  5. */
  6. /**
  7. * Fetch Views' data from the cache
  8. *
  9. * @param $move
  10. * Under certain circumstances it makes sense to not get the moved table, but the old one.
  11. * One example is views_get_handler.
  12. */
  13. function _views_fetch_data($table = NULL, $move = TRUE, $reset = FALSE) {
  14. $cache = &drupal_static(__FUNCTION__ . '_cache');
  15. $recursion_protection = &drupal_static(__FUNCTION__ . '_recursion_protected');
  16. $fully_loaded = &drupal_static(__FUNCTION__ . '_fully_loaded');
  17. if ($reset) {
  18. $cache = NULL;
  19. $fully_loaded = FALSE;
  20. }
  21. if ($table) {
  22. if (!isset($cache[$table])) {
  23. $cid = 'views_data:' . $table;
  24. if ($data = views_cache_get($cid, TRUE)) {
  25. $cache[$table] = $data->data;
  26. }
  27. else {
  28. if (!$fully_loaded) {
  29. // Try to load the full views cache.
  30. if ($data = views_cache_get('views_data', TRUE)) {
  31. $cache = $data->data;
  32. }
  33. else {
  34. // No cache entry, rebuild.
  35. $cache = _views_fetch_data_build();
  36. }
  37. $fully_loaded = TRUE;
  38. }
  39. // Write back a cache for this table.
  40. if (isset($cache[$table])) {
  41. views_cache_set($cid, $cache[$table], TRUE);
  42. }
  43. else {
  44. // If there is still no information about that table, it is missing.
  45. // Write an empty array to avoid repeated rebuilds.
  46. views_cache_set($cid, array(), TRUE);
  47. }
  48. }
  49. }
  50. if (isset($cache[$table])) {
  51. if (isset($cache[$table]['moved to']) && $move) {
  52. $moved_table = $cache[$table]['moved to'];
  53. if (!empty($recursion_protection[$table])) {
  54. // recursion detected!
  55. return NULL;
  56. }
  57. $recursion_protection[$table] = TRUE;
  58. $data = _views_fetch_data($moved_table);
  59. $recursion_protection = array();
  60. return $data;
  61. }
  62. return $cache[$table];
  63. }
  64. }
  65. else {
  66. if (!$fully_loaded) {
  67. $data = views_cache_get('views_data', TRUE);
  68. if (!empty($data->data)) {
  69. $cache = $data->data;
  70. }
  71. if (empty($cache)) {
  72. $cache = _views_fetch_data_build();
  73. }
  74. $fully_loaded = TRUE;
  75. }
  76. return $cache;
  77. }
  78. // Return an empty array if there is no match.
  79. return array();
  80. }
  81. /**
  82. * Build and set the views data cache if empty.
  83. */
  84. function _views_fetch_data_build() {
  85. views_include_handlers();
  86. $cache = module_invoke_all('views_data');
  87. foreach (module_implements('views_data_alter') as $module) {
  88. $function = $module . '_views_data_alter';
  89. $function($cache);
  90. }
  91. _views_data_process_entity_types($cache);
  92. // Keep a record with all data.
  93. views_cache_set('views_data', $cache, TRUE);
  94. return $cache;
  95. }
  96. /**
  97. * Links tables having an 'entity type' specified to the respective generic entity-type tables.
  98. */
  99. function _views_data_process_entity_types(&$data) {
  100. foreach ($data as $table_name => $table_info) {
  101. // Add in a join from the entity-table if an entity-type is given.
  102. if (!empty($table_info['table']['entity type'])) {
  103. $entity_table = 'views_entity_' . $table_info['table']['entity type'];
  104. $data[$entity_table]['table']['join'][$table_name] = array(
  105. 'left_table' => $table_name,
  106. );
  107. $data[$entity_table]['table']['entity type'] = $table_info['table']['entity type'];
  108. // Copy over the default table group if we have none yet.
  109. if (!empty($table_info['table']['group']) && empty($data[$entity_table]['table']['group'])) {
  110. $data[$entity_table]['table']['group'] = $table_info['table']['group'];
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * Fetch the plugin data from cache.
  117. */
  118. function _views_fetch_plugin_data($type = NULL, $plugin = NULL, $reset = FALSE) {
  119. static $cache = NULL;
  120. if (!isset($cache) || $reset) {
  121. $start = microtime(TRUE);
  122. views_include('plugins');
  123. views_include_handlers();
  124. $cache = views_discover_plugins();
  125. }
  126. if (!$type && !$plugin) {
  127. return $cache;
  128. }
  129. elseif (!$plugin) {
  130. // Not in the if above so the else below won't run
  131. if (isset($cache[$type])) {
  132. return $cache[$type];
  133. }
  134. }
  135. elseif (isset($cache[$type][$plugin])) {
  136. return $cache[$type][$plugin];
  137. }
  138. // Return an empty array if there is no match.
  139. return array();
  140. }
  141. /**
  142. * Set a cached item in the views cache.
  143. *
  144. * This is just a convenience wrapper around cache_set().
  145. *
  146. * @param $cid
  147. * The cache ID of the data to store.
  148. * @param $data
  149. * The data to store in the cache. Complex data types will be automatically serialized before insertion.
  150. * Strings will be stored as plain text and not serialized.
  151. * @param $use_language
  152. * If TRUE, the data will be cached specific to the currently active language.
  153. */
  154. function views_cache_set($cid, $data, $use_language = FALSE) {
  155. global $language;
  156. if (variable_get('views_skip_cache', FALSE)) {
  157. return;
  158. }
  159. if ($use_language) {
  160. $cid .= ':' . $language->language;
  161. }
  162. cache_set($cid, $data, 'cache_views');
  163. }
  164. /**
  165. * Return data from the persistent views cache.
  166. *
  167. * This is just a convenience wrapper around cache_get().
  168. *
  169. * @param int $cid
  170. * The cache ID of the data to retrieve.
  171. * @param bool $use_language
  172. * If TRUE, the data will be requested specific to the currently active language.
  173. *
  174. * @return stdClass|bool
  175. * The cache or FALSE on failure.
  176. */
  177. function views_cache_get($cid, $use_language = FALSE) {
  178. global $language;
  179. if (variable_get('views_skip_cache', FALSE)) {
  180. return FALSE;
  181. }
  182. if ($use_language) {
  183. $cid .= ':' . $language->language;
  184. }
  185. return cache_get($cid, 'cache_views');
  186. }