123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- <?php
- class views_plugin_cache extends views_plugin {
-
- var $storage = array();
-
- var $table = 'cache_views_data';
-
- function init(&$view, &$display) {
- $this->view = &$view;
- $this->display = &$display;
- if (is_object($display->handler)) {
- $options = $display->handler->get_option('cache');
-
- $this->unpack_options($this->options, $options);
- }
- }
-
- function summary_title() {
- return t('Unknown');
- }
-
- function cache_expire($type) { }
-
- function cache_set_expire($type) {
- return CACHE_PERMANENT;
- }
-
- function cache_set($type) {
- switch ($type) {
- case 'query':
-
- break;
- case 'results':
- $data = array(
- 'result' => $this->view->result,
- 'total_rows' => isset($this->view->total_rows) ? $this->view->total_rows : 0,
- 'current_page' => $this->view->get_current_page(),
- );
- cache_set($this->get_results_key(), $data, $this->table, $this->cache_set_expire($type));
- break;
- case 'output':
- $this->gather_headers();
- $this->storage['output'] = $this->view->display_handler->output;
- cache_set($this->get_output_key(), $this->storage, $this->table, $this->cache_set_expire($type));
- break;
- }
- }
-
- function cache_get($type) {
- $cutoff = $this->cache_expire($type);
- switch ($type) {
- case 'query':
-
- return FALSE;
- case 'results':
-
-
- if ($cache = cache_get($this->get_results_key(), $this->table)) {
- if (!$cutoff || $cache->created > $cutoff) {
- $this->view->result = $cache->data['result'];
- $this->view->total_rows = $cache->data['total_rows'];
- $this->view->set_current_page($cache->data['current_page']);
- $this->view->execute_time = 0;
- return TRUE;
- }
- }
- return FALSE;
- case 'output':
- if ($cache = cache_get($this->get_output_key(), $this->table)) {
- if (!$cutoff || $cache->created > $cutoff) {
- $this->storage = $cache->data;
- $this->view->display_handler->output = $cache->data['output'];
- $this->restore_headers();
- return TRUE;
- }
- }
- return FALSE;
- }
- }
-
- function cache_flush() {
- cache_clear_all($this->view->name . ':', $this->table, TRUE);
- }
-
- function post_render(&$output) { }
-
- function cache_start() {
- $this->storage['head'] = drupal_add_html_head();
- $this->storage['css'] = drupal_add_css();
- $this->storage['js'] = drupal_add_js();
- $this->storage['headers'] = drupal_get_http_header();
- }
-
- function gather_headers() {
-
- if (isset($this->storage['head'])) {
- $this->storage['head'] = str_replace($this->storage['head'], '', drupal_add_html_head());
- }
- else {
- $this->storage['head'] = '';
- }
-
- $css = drupal_add_css();
- $css_start = isset($this->storage['css']) ? $this->storage['css'] : array();
- $this->storage['css'] = array_diff_assoc($css, $css_start);
-
- $js = drupal_add_js();
- $js_start = isset($this->storage['js']) ? $this->storage['js'] : array();
-
-
- $this->storage['js'] = array_diff_assoc($js, $js_start);
-
- $settings = isset($js['settings']['data']) ? $js['settings']['data'] : array();
- $settings_start = isset($js_start['settings']['data']) ? $js_start['settings']['data'] : array();
- $this->storage['js']['settings'] = array_diff_assoc($settings, $settings_start);
-
- $this->storage['headers'] = array_diff_assoc(drupal_get_http_header(), $this->storage['headers']);
- }
-
- function restore_headers() {
- if (!empty($this->storage['head'])) {
- drupal_add_html_head($this->storage['head']);
- }
- if (!empty($this->storage['css'])) {
- foreach ($this->storage['css'] as $args) {
- drupal_add_css($args['data'], $args);
- }
- }
- if (!empty($this->storage['js'])) {
- foreach ($this->storage['js'] as $key => $args) {
- if ($key !== 'settings') {
- drupal_add_js($args['data'], $args);
- }
- else {
- foreach ($args as $setting) {
- drupal_add_js($setting, 'setting');
- }
- }
- }
- }
- if (!empty($this->storage['headers'])) {
- foreach ($this->storage['headers'] as $name => $value) {
- drupal_add_http_header($name, $value);
- }
- }
- }
- function get_results_key() {
- global $user;
- if (!isset($this->_results_key)) {
- $build_info = $this->view->build_info;
- $query_plugin = $this->view->display_handler->get_plugin('query');
- foreach (array('query','count_query') as $index) {
-
-
- if ($build_info[$index] instanceof SelectQueryInterface) {
- $query = clone $build_info[$index];
- $query->preExecute();
- $build_info[$index] = (string) $query;
- }
- }
- $key_data = array(
- 'build_info' => $build_info,
- 'roles' => array_keys($user->roles),
- 'super-user' => $user->uid == 1,
- 'language' => $GLOBALS['language']->language,
- 'base_url' => $GLOBALS['base_url'],
- );
- foreach (array('exposed_info', 'page', 'sort', 'order', 'items_per_page', 'offset') as $key) {
- if (isset($_GET[$key])) {
- $key_data[$key] = $_GET[$key];
- }
- }
- $this->_results_key = $this->view->name . ':' . $this->display->id . ':results:' . md5(serialize($key_data));
- }
- return $this->_results_key;
- }
- function get_output_key() {
- global $user;
- if (!isset($this->_output_key)) {
- $key_data = array(
- 'result' => $this->view->result,
- 'roles' => array_keys($user->roles),
- 'super-user' => $user->uid == 1,
- 'theme' => $GLOBALS['theme'],
- 'language' => $GLOBALS['language']->language,
- 'base_url' => $GLOBALS['base_url'],
- );
- $this->_output_key = $this->view->name . ':' . $this->display->id . ':output:' . md5(serialize($key_data));
- }
- return $this->_output_key;
- }
- }
|