00001 <?php
00002
00008 class views_plugin_cache extends views_plugin {
00012 var $storage = array();
00013
00017 var $table = 'cache_views_data';
00018
00027 function init(&$view, &$display) {
00028 $this->view = &$view;
00029 $this->display = &$display;
00030
00031 if (is_object($display->handler)) {
00032 $options = $display->handler->get_option('cache');
00033
00034 $this->unpack_options($this->options, $options);
00035 }
00036 }
00037
00042 function summary_title() {
00043 return t('Unknown');
00044 }
00045
00054 function cache_expire($type) { }
00055
00065 function cache_set_expire($type) {
00066 return CACHE_PERMANENT;
00067 }
00068
00069
00075 function cache_set($type) {
00076 switch ($type) {
00077 case 'query':
00078
00079 break;
00080 case 'results':
00081 $data = array(
00082 'result' => $this->view->result,
00083 'total_rows' => isset($this->view->total_rows) ? $this->view->total_rows : 0,
00084 'current_page' => $this->view->get_current_page(),
00085 );
00086 cache_set($this->get_results_key(), $data, $this->table, $this->cache_set_expire($type));
00087 break;
00088 case 'output':
00089 $this->gather_headers();
00090 $this->storage['output'] = $this->view->display_handler->output;
00091 cache_set($this->get_output_key(), $this->storage, $this->table, $this->cache_set_expire($type));
00092 break;
00093 }
00094 }
00095
00096
00102 function cache_get($type) {
00103 $cutoff = $this->cache_expire($type);
00104 switch ($type) {
00105 case 'query':
00106
00107 return FALSE;
00108 case 'results':
00109
00110
00111 if ($cache = cache_get($this->get_results_key(), $this->table)) {
00112 if (!$cutoff || $cache->created > $cutoff) {
00113 $this->view->result = $cache->data['result'];
00114 $this->view->total_rows = $cache->data['total_rows'];
00115 $this->view->set_current_page($cache->data['current_page']);
00116 $this->view->execute_time = 0;
00117 return TRUE;
00118 }
00119 }
00120 return FALSE;
00121 case 'output':
00122 if ($cache = cache_get($this->get_output_key(), $this->table)) {
00123 if (!$cutoff || $cache->created > $cutoff) {
00124 $this->storage = $cache->data;
00125 $this->view->display_handler->output = $cache->data['output'];
00126 $this->restore_headers();
00127 return TRUE;
00128 }
00129 }
00130 return FALSE;
00131 }
00132 }
00133
00140 function cache_flush() {
00141 cache_clear_all($this->view->name . ':', $this->table, TRUE);
00142 }
00143
00164 function post_render(&$output) { }
00165
00173 function cache_start() {
00174 $this->storage['head'] = drupal_add_html_head();
00175 $this->storage['css'] = drupal_add_css();
00176 $this->storage['js'] = drupal_add_js();
00177 }
00178
00182 function gather_headers() {
00183
00184 if (isset($this->storage['head'])) {
00185 $this->storage['head'] = str_replace($this->storage['head'], '', drupal_add_html_head());
00186 }
00187 else {
00188 $this->storage['head'] = '';
00189 }
00190
00191
00192 $css = drupal_add_css();
00193 $start = isset($this->storage['css']) ? $this->storage['css'] : array();
00194 $this->storage['css'] = array();
00195
00196 foreach ($css as $file => $data) {
00197 if (!isset($this->storage['css'][$file])) {
00198 $this->storage['css'][$file] = $data;
00199 }
00200 }
00201
00202
00203 $js = drupal_add_js();
00204
00205
00206 $start = isset($this->storage['js']) ? $this->storage['js'] : array();
00207 $this->storage['js'] = array();
00208
00209
00210
00211 if ($diff = array_diff_assoc($js, $start)) {
00212 $this->storage['js'] = $diff;
00213 }
00214
00215
00216 if ($settings_diff = array_diff_assoc($js['settings']['data'], $start['settings']['data'])) {
00217 $this->storage['js']['settings'] = $settings_diff;
00218 }
00219 }
00220
00224 function restore_headers() {
00225 if (!empty($this->storage['head'])) {
00226 drupal_add_html_head($this->storage['head']);
00227 }
00228 if (!empty($this->storage['css'])) {
00229 foreach ($this->storage['css'] as $args) {
00230 drupal_add_css($args['data'], $args);
00231 }
00232 }
00233 if (!empty($this->storage['js'])) {
00234 foreach ($this->storage['js'] as $key => $args) {
00235 if ($key != 'settings') {
00236 drupal_add_js($args['data'], $args);
00237 }
00238 else {
00239 foreach ($args as $setting) {
00240 drupal_add_js($setting, 'setting');
00241 }
00242 }
00243 }
00244 }
00245 }
00246
00247 function get_results_key() {
00248 global $user;
00249
00250 if (!isset($this->_results_key)) {
00251
00252 $build_info = $this->view->build_info;
00253
00254 $query_plugin = $this->view->display_handler->get_plugin('query');
00255
00256 foreach (array('query','count_query') as $index) {
00257
00258
00259 if ($build_info[$index] instanceof SelectQueryInterface) {
00260 $query = clone $build_info[$index];
00261 $query->preExecute();
00262 $build_info[$index] = (string)$query;
00263 }
00264 }
00265 $key_data = array(
00266 'build_info' => $build_info,
00267 'roles' => array_keys($user->roles),
00268 'super-user' => $user->uid == 1,
00269 'language' => $GLOBALS['language']->language,
00270 );
00271 foreach (array('exposed_info', 'page', 'sort', 'order') as $key) {
00272 if (isset($_GET[$key])) {
00273 $key_data[$key] = $_GET[$key];
00274 }
00275 }
00276
00277 $this->_results_key = $this->view->name . ':' . $this->display->id . ':results:' . md5(serialize($key_data));
00278 }
00279
00280 return $this->_results_key;
00281 }
00282
00283 function get_output_key() {
00284 global $user;
00285 if (!isset($this->_output_key)) {
00286 $key_data = array(
00287 'result' => $this->view->result,
00288 'roles' => array_keys($user->roles),
00289 'super-user' => $user->uid == 1,
00290 'theme' => $GLOBALS['theme'],
00291 'language' => $GLOBALS['language']->language,
00292 );
00293
00294 $this->_output_key = $this->view->name . ':' . $this->display->id . ':output:' . md5(serialize($key_data));
00295 }
00296
00297 return $this->_output_key;
00298 }
00299
00300 }