views_plugin_cache.inc

  1. 3.x plugins/views_plugin_cache.inc
  2. 2.x plugins/views_plugin_cache.inc

Definition of views_plugin_cache.

File

plugins/views_plugin_cache.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_cache.
  5. */
  6. /**
  7. * @defgroup views_cache_plugins Views cache plugins
  8. * @{
  9. * @todo.
  10. *
  11. * @see hook_views_plugins()
  12. */
  13. /**
  14. * The base plugin to handle caching.
  15. */
  16. class views_plugin_cache extends views_plugin {
  17. /**
  18. * Contains all data that should be written/read from cache.
  19. */
  20. var $storage = array();
  21. /**
  22. * What table to store data in.
  23. */
  24. var $table = 'cache_views_data';
  25. /**
  26. * Initialize the plugin.
  27. *
  28. * @param $view
  29. * The view object.
  30. * @param $display
  31. * The display handler.
  32. */
  33. function init(&$view, &$display) {
  34. $this->view = &$view;
  35. $this->display = &$display;
  36. if (is_object($display->handler)) {
  37. $options = $display->handler->get_option('cache');
  38. // Overlay incoming options on top of defaults
  39. $this->unpack_options($this->options, $options);
  40. }
  41. }
  42. /**
  43. * Return a string to display as the clickable title for the
  44. * access control.
  45. */
  46. function summary_title() {
  47. return t('Unknown');
  48. }
  49. /**
  50. * Determine the expiration time of the cache type, or NULL if no expire.
  51. *
  52. * Plugins must override this to implement expiration.
  53. *
  54. * @param $type
  55. * The cache type, either 'query', 'result' or 'output'.
  56. */
  57. function cache_expire($type) { }
  58. /**
  59. * Determine expiration time in the cache table of the cache type
  60. * or CACHE_PERMANENT if item shouldn't be removed automatically from cache.
  61. *
  62. * Plugins must override this to implement expiration in the cache table.
  63. *
  64. * @param $type
  65. * The cache type, either 'query', 'result' or 'output'.
  66. */
  67. function cache_set_expire($type) {
  68. return CACHE_PERMANENT;
  69. }
  70. /**
  71. * Save data to the cache.
  72. *
  73. * A plugin should override this to provide specialized caching behavior.
  74. */
  75. function cache_set($type) {
  76. switch ($type) {
  77. case 'query':
  78. // Not supported currently, but this is certainly where we'd put it.
  79. break;
  80. case 'results':
  81. $data = array(
  82. 'result' => $this->view->result,
  83. 'total_rows' => isset($this->view->total_rows) ? $this->view->total_rows : 0,
  84. 'current_page' => $this->view->get_current_page(),
  85. );
  86. cache_set($this->get_results_key(), $data, $this->table, $this->cache_set_expire($type));
  87. break;
  88. case 'output':
  89. $this->gather_headers();
  90. $this->storage['output'] = $this->view->display_handler->output;
  91. cache_set($this->get_output_key(), $this->storage, $this->table, $this->cache_set_expire($type));
  92. break;
  93. }
  94. }
  95. /**
  96. * Retrieve data from the cache.
  97. *
  98. * A plugin should override this to provide specialized caching behavior.
  99. */
  100. function cache_get($type) {
  101. $cutoff = $this->cache_expire($type);
  102. switch ($type) {
  103. case 'query':
  104. // Not supported currently, but this is certainly where we'd put it.
  105. return FALSE;
  106. case 'results':
  107. // Values to set: $view->result, $view->total_rows, $view->execute_time,
  108. // $view->current_page.
  109. if ($cache = cache_get($this->get_results_key(), $this->table)) {
  110. if (!$cutoff || $cache->created > $cutoff) {
  111. $this->view->result = $cache->data['result'];
  112. $this->view->total_rows = $cache->data['total_rows'];
  113. $this->view->set_current_page($cache->data['current_page']);
  114. $this->view->execute_time = 0;
  115. return TRUE;
  116. }
  117. }
  118. return FALSE;
  119. case 'output':
  120. if ($cache = cache_get($this->get_output_key(), $this->table)) {
  121. if (!$cutoff || $cache->created > $cutoff) {
  122. $this->storage = $cache->data;
  123. $this->view->display_handler->output = $cache->data['output'];
  124. $this->restore_headers();
  125. return TRUE;
  126. }
  127. }
  128. return FALSE;
  129. }
  130. }
  131. /**
  132. * Clear out cached data for a view.
  133. *
  134. * We're just going to nuke anything related to the view, regardless of display,
  135. * to be sure that we catch everything. Maybe that's a bad idea.
  136. */
  137. function cache_flush() {
  138. cache_clear_all($this->view->name . ':', $this->table, TRUE);
  139. }
  140. /**
  141. * Post process any rendered data.
  142. *
  143. * This can be valuable to be able to cache a view and still have some level of
  144. * dynamic output. In an ideal world, the actual output will include HTML
  145. * comment based tokens, and then the post process can replace those tokens.
  146. *
  147. * Example usage. If it is known that the view is a node view and that the
  148. * primary field will be a nid, you can do something like this:
  149. *
  150. * <!--post-FIELD-NID-->
  151. *
  152. * And then in the post render, create an array with the text that should
  153. * go there:
  154. *
  155. * strtr($output, array('<!--post-FIELD-1-->', 'output for FIELD of nid 1');
  156. *
  157. * All of the cached result data will be available in $view->result, as well,
  158. * so all ids used in the query should be discoverable.
  159. */
  160. function post_render(&$output) { }
  161. /**
  162. * Start caching javascript, css and other out of band info.
  163. *
  164. * This takes a snapshot of the current system state so that we don't
  165. * duplicate it. Later on, when gather_headers() is run, this information
  166. * will be removed so that we don't hold onto it.
  167. */
  168. function cache_start() {
  169. $this->storage['head'] = drupal_add_html_head();
  170. $this->storage['css'] = drupal_add_css();
  171. $this->storage['js'] = drupal_add_js();
  172. $this->storage['headers'] = drupal_get_http_header();
  173. }
  174. /**
  175. * Gather out of band data, compare it to what we started with and store the difference.
  176. */
  177. function gather_headers() {
  178. // Simple replacement for head
  179. if (isset($this->storage['head'])) {
  180. $this->storage['head'] = str_replace($this->storage['head'], '', drupal_add_html_head());
  181. }
  182. else {
  183. $this->storage['head'] = '';
  184. }
  185. // Check if the advanced mapping function of D 7.23 is available.
  186. $array_mapping_func = function_exists('drupal_array_diff_assoc_recursive') ? 'drupal_array_diff_assoc_recursive' : 'array_diff_assoc';
  187. // Slightly less simple for CSS:
  188. $css = drupal_add_css();
  189. $css_start = isset($this->storage['css']) ? $this->storage['css'] : array();
  190. $this->storage['css'] = $array_mapping_func($css, $css_start);
  191. // Get javascript after/before views renders.
  192. $js = drupal_add_js();
  193. $js_start = isset($this->storage['js']) ? $this->storage['js'] : array();
  194. // If there are any differences between the old and the new javascript then
  195. // store them to be added later.
  196. $this->storage['js'] = $array_mapping_func($js, $js_start);
  197. // Special case the settings key and get the difference of the data.
  198. $settings = isset($js['settings']['data']) ? $js['settings']['data'] : array();
  199. $settings_start = isset($js_start['settings']['data']) ? $js_start['settings']['data'] : array();
  200. $this->storage['js']['settings'] = $array_mapping_func($settings, $settings_start);
  201. // Get difference of HTTP headers.
  202. $this->storage['headers'] = $array_mapping_func(drupal_get_http_header(), $this->storage['headers']);
  203. }
  204. /**
  205. * Restore out of band data saved to cache. Copied from Panels.
  206. */
  207. function restore_headers() {
  208. if (!empty($this->storage['head'])) {
  209. drupal_add_html_head($this->storage['head']);
  210. }
  211. if (!empty($this->storage['css'])) {
  212. foreach ($this->storage['css'] as $args) {
  213. drupal_add_css($args['data'], $args);
  214. }
  215. }
  216. if (!empty($this->storage['js'])) {
  217. foreach ($this->storage['js'] as $key => $args) {
  218. if ($key !== 'settings') {
  219. drupal_add_js($args['data'], $args);
  220. }
  221. else {
  222. foreach ($args as $setting) {
  223. drupal_add_js($setting, 'setting');
  224. }
  225. }
  226. }
  227. }
  228. if (!empty($this->storage['headers'])) {
  229. foreach ($this->storage['headers'] as $name => $value) {
  230. drupal_add_http_header($name, $value);
  231. }
  232. }
  233. }
  234. function get_results_key() {
  235. global $user;
  236. if (!isset($this->_results_key)) {
  237. $build_info = $this->view->build_info;
  238. $query_plugin = $this->view->display_handler->get_plugin('query');
  239. foreach (array('query','count_query') as $index) {
  240. // If the default query back-end is used generate SQL query strings from
  241. // the query objects.
  242. if ($build_info[$index] instanceof SelectQueryInterface) {
  243. $query = clone $build_info[$index];
  244. $query->preExecute();
  245. $build_info[$index] = (string) $query;
  246. }
  247. }
  248. $key_data = array(
  249. 'build_info' => $build_info,
  250. 'roles' => array_keys($user->roles),
  251. 'super-user' => $user->uid == 1, // special caching for super user.
  252. 'language' => $GLOBALS['language']->language,
  253. 'base_url' => $GLOBALS['base_url'],
  254. );
  255. foreach (array('exposed_info', 'page', 'sort', 'order', 'items_per_page', 'offset') as $key) {
  256. if (isset($_GET[$key])) {
  257. $key_data[$key] = $_GET[$key];
  258. }
  259. }
  260. $this->_results_key = $this->view->name . ':' . $this->display->id . ':results:' . md5(serialize($key_data));
  261. }
  262. return $this->_results_key;
  263. }
  264. function get_output_key() {
  265. global $user;
  266. if (!isset($this->_output_key)) {
  267. $key_data = array(
  268. 'result' => $this->view->result,
  269. 'roles' => array_keys($user->roles),
  270. 'super-user' => $user->uid == 1, // special caching for super user.
  271. 'theme' => $GLOBALS['theme'],
  272. 'language' => $GLOBALS['language']->language,
  273. 'base_url' => $GLOBALS['base_url'],
  274. );
  275. $this->_output_key = $this->view->name . ':' . $this->display->id . ':output:' . md5(serialize($key_data));
  276. }
  277. return $this->_output_key;
  278. }
  279. }
  280. /**
  281. * @}
  282. */