views_plugin_cache.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. public $storage = array();
  21. /**
  22. * What table to store data in.
  23. */
  24. public $table = 'cache_views_data';
  25. /**
  26. * Initialize the plugin.
  27. *
  28. * @param view $view
  29. * The view object.
  30. * @param object $display
  31. * The display handler.
  32. */
  33. public 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. public 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 string $type
  55. * The cache type, either 'query', 'result' or 'output'.
  56. */
  57. public function cache_expire($type) {
  58. }
  59. /**
  60. * Determine expiration time in the cache table of the cache type.
  61. *
  62. * Or CACHE_PERMANENT if item shouldn't be removed automatically from cache.
  63. *
  64. * Plugins must override this to implement expiration in the cache table.
  65. *
  66. * @param string $type
  67. * The cache type, either 'query', 'result' or 'output'.
  68. */
  69. public function cache_set_expire($type) {
  70. return CACHE_PERMANENT;
  71. }
  72. /**
  73. * Save data to the cache.
  74. *
  75. * A plugin should override this to provide specialized caching behavior.
  76. */
  77. public function cache_set($type) {
  78. switch ($type) {
  79. case 'query':
  80. // Not supported currently, but this is certainly where we'd put it.
  81. break;
  82. case 'results':
  83. $data = array(
  84. 'result' => $this->view->result,
  85. 'total_rows' => isset($this->view->total_rows) ? $this->view->total_rows : 0,
  86. 'current_page' => $this->view->get_current_page(),
  87. );
  88. cache_set($this->get_results_key(), $data, $this->table, $this->cache_set_expire($type));
  89. break;
  90. case 'output':
  91. $this->gather_headers();
  92. $this->storage['output'] = $this->view->display_handler->output;
  93. cache_set($this->get_output_key(), $this->storage, $this->table, $this->cache_set_expire($type));
  94. break;
  95. }
  96. }
  97. /**
  98. * Retrieve data from the cache.
  99. *
  100. * A plugin should override this to provide specialized caching behavior.
  101. */
  102. public function cache_get($type) {
  103. $cutoff = $this->cache_expire($type);
  104. switch ($type) {
  105. case 'query':
  106. // Not supported currently, but this is certainly where we'd put it.
  107. return FALSE;
  108. case 'results':
  109. // Values to set: $view->result, $view->total_rows, $view->execute_time,
  110. // $view->current_page.
  111. if ($cache = cache_get($this->get_results_key(), $this->table)) {
  112. if (!$cutoff || $cache->created > $cutoff) {
  113. $this->view->result = $cache->data['result'];
  114. $this->view->total_rows = $cache->data['total_rows'];
  115. $this->view->set_current_page($cache->data['current_page']);
  116. $this->view->execute_time = 0;
  117. return TRUE;
  118. }
  119. }
  120. return FALSE;
  121. case 'output':
  122. if ($cache = cache_get($this->get_output_key(), $this->table)) {
  123. if (!$cutoff || $cache->created > $cutoff) {
  124. $this->storage = $cache->data;
  125. $this->view->display_handler->output = $cache->data['output'];
  126. $this->restore_headers();
  127. return TRUE;
  128. }
  129. }
  130. return FALSE;
  131. }
  132. }
  133. /**
  134. * Clear out cached data for a view.
  135. *
  136. * We're just going to nuke anything related to the view, regardless of
  137. * display, to be sure that we catch everything. Maybe that's a bad idea.
  138. */
  139. public function cache_flush() {
  140. cache_clear_all($this->view->name . ':', $this->table, TRUE);
  141. }
  142. /**
  143. * Post process any rendered data.
  144. *
  145. * This can be valuable to be able to cache a view and still have some level
  146. * of dynamic output. In an ideal world, the actual output will include HTML
  147. * comment based tokens, and then the post process can replace those tokens.
  148. *
  149. * Example usage. If it is known that the view is a node view and that the
  150. * primary field will be a nid, you can do something like this:
  151. *
  152. * <!--post-FIELD-NID-->
  153. *
  154. * And then in the post render, create an array with the text that should
  155. * go there:
  156. *
  157. * strtr($output, array('<!--post-FIELD-1-->', 'output for FIELD of nid 1');
  158. *
  159. * All of the cached result data will be available in $view->result, as well,
  160. * so all ids used in the query should be discoverable.
  161. */
  162. public function post_render(&$output) {
  163. }
  164. /**
  165. * Start caching javascript, css and other out of band info.
  166. *
  167. * This takes a snapshot of the current system state so that we don't
  168. * duplicate it. Later on, when gather_headers() is run, this information
  169. * will be removed so that we don't hold onto it.
  170. */
  171. public function cache_start() {
  172. $this->storage['head'] = drupal_add_html_head();
  173. $this->storage['css'] = drupal_add_css();
  174. $this->storage['js'] = drupal_add_js();
  175. $this->storage['headers'] = drupal_get_http_header();
  176. }
  177. /**
  178. * Gather out of band data, compare it to the start data and store the diff.
  179. */
  180. public function gather_headers() {
  181. // Simple replacement for head.
  182. if (isset($this->storage['head'])) {
  183. $this->storage['head'] = str_replace($this->storage['head'], '', drupal_add_html_head());
  184. }
  185. else {
  186. $this->storage['head'] = '';
  187. }
  188. // Check if the advanced mapping function of D 7.23 is available.
  189. $array_mapping_func = function_exists('drupal_array_diff_assoc_recursive') ? 'drupal_array_diff_assoc_recursive' : 'array_diff_assoc';
  190. // Slightly less simple for CSS.
  191. $css = drupal_add_css();
  192. $css_start = isset($this->storage['css']) ? $this->storage['css'] : array();
  193. $this->storage['css'] = $this->assetDiff($css, $css_start, $array_mapping_func);
  194. // Get javascript after/before views renders.
  195. $js = drupal_add_js();
  196. $js_start = isset($this->storage['js']) ? $this->storage['js'] : array();
  197. // If there are any differences between the old and the new javascript then
  198. // store them to be added later.
  199. $this->storage['js'] = $this->assetDiff($js, $js_start, $array_mapping_func);
  200. // Special case the settings key and get the difference of the data.
  201. $settings = isset($js['settings']['data']) ? $js['settings']['data'] : array();
  202. $settings_start = isset($js_start['settings']['data']) ? $js_start['settings']['data'] : array();
  203. $this->storage['js']['settings'] = $array_mapping_func($settings, $settings_start);
  204. // Get difference of HTTP headers.
  205. $this->storage['headers'] = $array_mapping_func(drupal_get_http_header(), $this->storage['headers']);
  206. }
  207. /**
  208. * Computes the differences between two JS/CSS asset arrays.
  209. *
  210. * @param array $assets
  211. * The current asset array.
  212. * @param array $start_assets
  213. * The original asset array.
  214. * @param string $diff_function
  215. * The function that should be used for computing the diff.
  216. *
  217. * @return array
  218. * A CSS or JS asset array that contains all entries that are new/different
  219. * in $assets.
  220. */
  221. protected function assetDiff(array $assets, array $start_assets, $diff_function) {
  222. $diff = $diff_function($assets, $start_assets);
  223. // Cleanup the resulting array since drupal_array_diff_assoc_recursive() can
  224. // leave half populated arrays behind.
  225. foreach ($diff as $key => $entry) {
  226. // If only the weight was different we can remove this entry.
  227. if (count($entry) == 1 && isset($entry['weight'])) {
  228. unset($diff[$key]);
  229. }
  230. // If there are other differences we override with the latest entry.
  231. elseif ($entry != $assets[$key]) {
  232. $diff[$key] = $assets[$key];
  233. }
  234. }
  235. return $diff;
  236. }
  237. /**
  238. * Restore out of band data saved to cache. Copied from Panels.
  239. */
  240. public function restore_headers() {
  241. if (!empty($this->storage['head'])) {
  242. drupal_add_html_head($this->storage['head']);
  243. }
  244. if (!empty($this->storage['css'])) {
  245. foreach ($this->storage['css'] as $args) {
  246. drupal_add_css($args['data'], $args);
  247. }
  248. }
  249. if (!empty($this->storage['js'])) {
  250. foreach ($this->storage['js'] as $key => $args) {
  251. if ($key !== 'settings') {
  252. drupal_add_js($args['data'], $args);
  253. }
  254. else {
  255. foreach ($args as $setting) {
  256. drupal_add_js($setting, 'setting');
  257. }
  258. }
  259. }
  260. }
  261. if (!empty($this->storage['headers'])) {
  262. foreach ($this->storage['headers'] as $name => $value) {
  263. drupal_add_http_header($name, $value);
  264. }
  265. }
  266. }
  267. /**
  268. *
  269. */
  270. public function get_results_key() {
  271. if (!isset($this->_results_key)) {
  272. $key_data = array();
  273. foreach (array('exposed_info', 'page', 'sort', 'order', 'items_per_page', 'offset') as $key) {
  274. if (isset($_GET[$key])) {
  275. $key_data[$key] = $_GET[$key];
  276. }
  277. }
  278. $this->_results_key = $this->view->name . ':' . $this->display->id . ':results:' . $this->get_cache_key($key_data);
  279. }
  280. return $this->_results_key;
  281. }
  282. /**
  283. *
  284. */
  285. public function get_output_key() {
  286. if (!isset($this->_output_key)) {
  287. $key_data = array(
  288. 'result' => $this->view->result,
  289. 'theme' => $GLOBALS['theme'],
  290. );
  291. $this->_output_key = $this->view->name . ':' . $this->display->id . ':output:' . $this->get_cache_key($key_data);
  292. }
  293. return $this->_output_key;
  294. }
  295. /**
  296. * Returns cache key.
  297. *
  298. * @param array $key_data
  299. * Additional data for cache segmentation and/or overrides for default
  300. * segmentation.
  301. *
  302. * @return string
  303. */
  304. public function get_cache_key($key_data = array()) {
  305. global $user;
  306. $key_data += array(
  307. 'roles' => array_keys($user->roles),
  308. 'super-user' => $user->uid == 1,
  309. // special caching for super user.
  310. 'language' => $GLOBALS['language']->language,
  311. 'language_content' => $GLOBALS['language_content']->language,
  312. 'base_url' => $GLOBALS['base_url'],
  313. );
  314. if (empty($key_data['build_info'])) {
  315. $build_info = $this->view->build_info;
  316. foreach (array('query', 'count_query') as $index) {
  317. // If the default query back-end is used generate SQL query strings from
  318. // the query objects.
  319. if ($build_info[$index] instanceof SelectQueryInterface) {
  320. $query = clone $build_info[$index];
  321. $query->preExecute();
  322. $key_data['build_info'][$index] = array(
  323. 'sql' => (string) $query,
  324. 'arguments' => $query->getArguments(),
  325. );
  326. }
  327. }
  328. }
  329. return md5(serialize($key_data));
  330. }
  331. }
  332. /**
  333. * @}
  334. */