cache.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. if ($data = views_cache_get('views_data', TRUE)) {
  68. $cache = $data->data;
  69. }
  70. else {
  71. // No cache entry, rebuild.
  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. // Load necessary code once.
  122. if (!isset($cache)) {
  123. views_include('plugins');
  124. views_include_handlers();
  125. }
  126. // Because plugin data contains translated strings, and as such can be
  127. // expensive to build, the results are cached per language.
  128. global $language;
  129. $cache_key = 'views:plugin_data:' . $language->language;
  130. if (!$reset) {
  131. if ($cache = cache_get($cache_key)) {
  132. $cache = $cache->data;
  133. }
  134. }
  135. // If not available in the cache, build it and cache it.
  136. if (!$cache) {
  137. $cache = views_discover_plugins();
  138. cache_set($cache_key, $cache);
  139. }
  140. }
  141. if (!$type && !$plugin) {
  142. return $cache;
  143. }
  144. elseif (!$plugin) {
  145. // Not in the if above so the else below won't run
  146. if (isset($cache[$type])) {
  147. return $cache[$type];
  148. }
  149. }
  150. elseif (isset($cache[$type][$plugin])) {
  151. return $cache[$type][$plugin];
  152. }
  153. // Return an empty array if there is no match.
  154. return array();
  155. }
  156. /**
  157. * Set a cached item in the views cache.
  158. *
  159. * This is just a convenience wrapper around cache_set().
  160. *
  161. * @param $cid
  162. * The cache ID of the data to store.
  163. * @param $data
  164. * The data to store in the cache. Complex data types will be automatically serialized before insertion.
  165. * Strings will be stored as plain text and not serialized.
  166. * @param $use_language
  167. * If TRUE, the data will be cached specific to the currently active language.
  168. */
  169. function views_cache_set($cid, $data, $use_language = FALSE) {
  170. global $language;
  171. if (variable_get('views_skip_cache', FALSE)) {
  172. return;
  173. }
  174. if ($use_language) {
  175. $cid .= ':' . $language->language;
  176. }
  177. cache_set($cid, $data, 'cache_views');
  178. }
  179. /**
  180. * Return data from the persistent views cache.
  181. *
  182. * This is just a convenience wrapper around cache_get().
  183. *
  184. * @param int $cid
  185. * The cache ID of the data to retrieve.
  186. * @param bool $use_language
  187. * If TRUE, the data will be requested specific to the currently active language.
  188. *
  189. * @return stdClass|bool
  190. * The cache or FALSE on failure.
  191. */
  192. function views_cache_get($cid, $use_language = FALSE) {
  193. global $language;
  194. if (variable_get('views_skip_cache', FALSE)) {
  195. return FALSE;
  196. }
  197. if ($use_language) {
  198. $cid .= ':' . $language->language;
  199. }
  200. return cache_get($cid, 'cache_views');
  201. }