cache.inc 6.1 KB

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