source: sipes/modules_contrib/views/plugins/views_plugin_cache.inc @ 65dadeb

stableversion-3.0
Last change on this file since 65dadeb was 59029b2, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se actualizo la version del modulo views

  • Propiedad mode establecida a 100644
File size: 9.0 KB
Línea 
1<?php
2
3/**
4 * The base plugin to handle caching.
5 *
6 * @ingroup views_cache_plugins
7 */
8class views_plugin_cache extends views_plugin {
9  /**
10   * Contains all data that should be written/read from cache.
11   */
12  var $storage = array();
13
14  /**
15   * What table to store data in.
16   */
17  var $table = 'cache_views_data';
18
19  /**
20   * Initialize the plugin.
21   *
22   * @param $view
23   *   The view object.
24   * @param $display
25   *   The display handler.
26   */
27  function init(&$view, &$display) {
28    $this->view = &$view;
29    $this->display = &$display;
30
31    if (is_object($display->handler)) {
32      $options = $display->handler->get_option('cache');
33      // Overlay incoming options on top of defaults
34      $this->unpack_options($this->options, $options);
35    }
36  }
37
38  /**
39   * Return a string to display as the clickable title for the
40   * access control.
41   */
42  function summary_title() {
43    return t('Unknown');
44  }
45
46  /**
47   * Determine the expiration time of the cache type, or NULL if no expire.
48   *
49   * Plugins must override this to implement expiration.
50   *
51   * @param $type
52   *   The cache type, either 'query', 'result' or 'output'.
53   */
54   function cache_expire($type) { }
55
56   /**
57    * Determine expiration time in the cache table of the cache type
58    * or CACHE_PERMANENT if item shouldn't be removed automatically from cache.
59    *
60    * Plugins must override this to implement expiration in the cache table.
61    *
62    * @param $type
63    *   The cache type, either 'query', 'result' or 'output'.
64    */
65  function cache_set_expire($type) {
66    return CACHE_PERMANENT;
67  }
68
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        if ($this->get_results_key() !== FALSE) {
82          $data = array(
83            'result' => $this->view->result,
84            'total_rows' => $this->view->total_rows,
85            'current_page' => $this->view->get_current_page(),
86          );
87          cache_set($this->get_results_key(), $data, $this->table, $this->cache_set_expire($type));
88        }
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
99  /**
100   * Retrieve data from the cache.
101   *
102   * A plugin should override this to provide specialized caching behavior.
103   */
104  function cache_get($type) {
105    $cutoff = $this->cache_expire($type);
106    switch ($type) {
107      case 'query':
108        // Not supported currently, but this is certainly where we'd put it.
109        return FALSE;
110      case 'results':
111        // Values to set: $view->result, $view->total_rows, $view->execute_time,
112        // $view->current_page.
113        $results_key = $this->get_results_key();
114        if ($results_key === FALSE) {
115          return FALSE;
116        }
117        else if ($cache = cache_get($results_key, $this->table)) {
118          if (!$cutoff || $cache->created > $cutoff) {
119            $this->view->result = $cache->data['result'];
120            $this->view->total_rows = $cache->data['total_rows'];
121            $this->view->set_current_page($cache->data['current_page']);
122            $this->view->execute_time = 0;
123            return TRUE;
124          }
125        }
126        return FALSE;
127      case 'output':
128        if ($cache = cache_get($this->get_output_key(), $this->table)) {
129          if (!$cutoff || $cache->created > $cutoff) {
130            $this->storage = $cache->data;
131            $this->view->display_handler->output = $cache->data['output'];
132            $this->restore_headers();
133            return TRUE;
134          }
135        }
136        return FALSE;
137    }
138  }
139
140  /**
141   * Clear out cached data for a view.
142   *
143   * We're just going to nuke anything related to the view, regardless of display,
144   * to be sure that we catch everything. Maybe that's a bad idea.
145   */
146  function cache_flush() {
147    cache_clear_all($this->view->name . ':', $this->table, TRUE);
148  }
149
150  /**
151   * Post process any rendered data.
152   *
153   * This can be valuable to be able to cache a view and still have some level of
154   * dynamic output. In an ideal world, the actual output will include HTML
155   * comment based tokens, and then the post process can replace those tokens.
156   *
157   * Example usage. If it is known that the view is a node view and that the
158   * primary field will be a nid, you can do something like this:
159   *
160   * <!--post-FIELD-NID-->
161   *
162   * And then in the post render, create an array with the text that should
163   * go there:
164   *
165   * strtr($output, array('<!--post-FIELD-1-->', 'output for FIELD of nid 1');
166   *
167   * All of the cached result data will be available in $view->result, as well,
168   * so all ids used in the query should be discoverable.
169   */
170  function post_render(&$output) { }
171
172  /**
173   * Start caching javascript, css and other out of band info.
174   *
175   * This takes a snapshot of the current system state so that we don't
176   * duplicate it. Later on, when gather_headers() is run, this information
177   * will be removed so that we don't hold onto it.
178   */
179  function cache_start() {
180    $this->storage['head'] = drupal_set_html_head();
181    $this->storage['css'] = drupal_add_css();
182
183    foreach (array('header', 'footer') as $scope) {
184      $this->storage['js'][$scope] = drupal_add_js(NULL, NULL, $scope);
185    }
186  }
187
188  /**
189   * Gather out of band data, compare it to what we started with and store the difference.
190   */
191  function gather_headers() {
192    // Simple replacement for head
193    $this->storage['head'] = str_replace($this->storage['head'], '', drupal_set_html_head());
194
195    // Slightly less simple for CSS:
196    $css = drupal_add_css();
197    $start = $this->storage['css'];
198    $this->storage['css'] = array();
199
200    foreach ($css as $media => $medias) {
201      foreach ($medias as $type => $types) {
202        foreach ($types as $path => $preprocess) {
203          if (!isset($start[$media][$type][$path])) {
204            $this->storage['css'][] = array($path, $type, $media, $preprocess);
205          }
206        }
207      }
208    }
209
210    $js = array();
211    // A little less simple for js
212    foreach (array('header', 'footer') as $scope) {
213      $js[$scope] = drupal_add_js(NULL, NULL, $scope);
214    }
215
216    $start = $this->storage['js'];
217    $this->storage['js'] = array();
218
219    foreach ($js as $scope => $scopes) {
220      foreach ($scopes as $type => $types) {
221        foreach ($types as $id => $info) {
222          if (!isset($start[$scope][$type][$id])) {
223            switch ($type) {
224              case 'setting':
225                $this->storage['js'][] = array($info, $type, $scope);
226                break;
227
228              case 'inline':
229                $this->storage['js'][] = array($info['code'], $type, $scope, $info['defer']);
230                break;
231
232              default:
233                $this->storage['js'][] = array($id, $type, $scope, $info['defer'], $info['cache']);
234            }
235          }
236        }
237      }
238    }
239  }
240
241  /**
242   * Restore out of band data saved to cache. Copied from Panels.
243   */
244  function restore_headers() {
245    if (!empty($this->storage['head'])) {
246      drupal_set_html_head($this->storage['head']);
247    }
248    if (!empty($this->storage['css'])) {
249      foreach ($this->storage['css'] as $args) {
250        call_user_func_array('drupal_add_css', $args);
251      }
252    }
253    if (!empty($this->storage['js'])) {
254      foreach ($this->storage['js'] as $args) {
255        call_user_func_array('drupal_add_js', $args);
256      }
257    }
258  }
259
260  function get_results_key() {
261    global $user;
262
263    if (!isset($this->_results_key)) {
264      $cache_info = $this->view->query->get_cache_info();
265      if ($cache_info === FALSE) {
266        $this->_results_key = FALSE;
267      }
268      else {
269        $key_data = array(
270          'build_info' => $this->view->build_info,
271          'cache_info' => $this->view->query->get_cache_info(),
272          'roles' => array_keys($user->roles),
273          'super-user' => $user->uid == 1, // special caching for super user.
274          'language' => $GLOBALS['language'],
275        );
276        foreach (array('exposed_info', 'page', 'sort', 'order') as $key) {
277          if (isset($_GET[$key])) {
278            $key_data[$key] = $_GET[$key];
279          }
280        }
281
282        $this->_results_key = $this->view->name . ':' . $this->display->id . ':results:' . md5(serialize($key_data));
283      }
284    }
285
286    return $this->_results_key;
287  }
288
289  function get_output_key() {
290    global $user;
291    if (!isset($this->_output_key)) {
292      $key_data = array(
293        'result' => $this->view->result,
294        'roles' => array_keys($user->roles),
295        'super-user' => $user->uid == 1, // special caching for super user.
296        'theme' => $GLOBALS['theme'],
297        'language' => $GLOBALS['language'],
298      );
299
300      $this->_output_key = $this->view->name . ':' . $this->display->id . ':output:' . md5(serialize($key_data));
301    }
302
303    return $this->_output_key;
304  }
305
306}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.