[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/views/plugins/ -> views_plugin_cache.inc (source)

   1  <?php
   2  
   3  /**
   4   * The base plugin to handle caching.
   5   *
   6   * @ingroup views_cache_plugins
   7   */
   8  class 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      $this->options = array();
  31  
  32      if (is_object($display->handler)) {
  33        // Note: The below is read only.
  34        $this->options = $display->handler->get_option('cache');
  35      }
  36    }
  37  
  38    /**
  39     * Retrieve the default options when this is a new access
  40     * control plugin
  41     */
  42    function option_defaults(&$options) { }
  43  
  44    /**
  45     * Return a string to display as the clickable title for the
  46     * access control.
  47     */
  48    function summary_title() {
  49      return t('Unknown');
  50    }
  51  
  52    /**
  53     * Determine the expiration time of the cache type, or NULL if no expire.
  54     *
  55     * Plugins must override this to implement expiration.
  56     *
  57     * @param $type
  58     *   The cache type, either 'query', 'result' or 'output'.
  59     */
  60     function cache_expire($type) { }
  61     
  62     /**
  63      * Determine expiration time in the cache table of the cache type
  64      * or CACHE_PERMANENT if item shouldn't be removed automatically from cache.
  65      *
  66      * Plugins must override this to implement expiration in the cache table.
  67      *
  68      * @param $type
  69      *   The cache type, either 'query', 'result' or 'output'.
  70      */
  71    function cache_set_expire($type) {
  72      return CACHE_PERMANENT;
  73    }
  74  
  75  
  76    /**
  77     * Save data to the cache.
  78     *
  79     * A plugin should override this to provide specialized caching behavior.
  80     */
  81    function cache_set($type) {
  82      switch ($type) {
  83        case 'query':
  84          // Not supported currently, but this is certainly where we'd put it.
  85          break;
  86        case 'results':
  87          $data = array(
  88            'result' => $this->view->result,
  89            'total_rows' => $this->view->total_rows,
  90            'pager' => $this->view->pager,
  91          );
  92          cache_set($this->get_results_key(), $data, $this->table, $this->cache_set_expire($type));
  93          break;
  94        case 'output':
  95          $this->gather_headers();
  96          $this->storage['output'] = $this->view->display_handler->output;
  97          cache_set($this->get_output_key(), $this->storage, $this->table, $this->cache_set_expire($type));
  98          break;
  99      }
 100    }
 101  
 102  
 103    /**
 104     * Retrieve data from the cache.
 105     *
 106     * A plugin should override this to provide specialized caching behavior.
 107     */
 108    function cache_get($type) {
 109      $cutoff = $this->cache_expire($type);
 110      switch ($type) {
 111        case 'query':
 112          // Not supported currently, but this is certainly where we'd put it.
 113          return FALSE;
 114        case 'results':
 115          // Values to set: $view->result, $view->total_rows, $view->execute_time,
 116          // $view->pager['current_page'].
 117          if ($cache = cache_get($this->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->pager = $cache->data['pager'];
 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        $key_data = array(
 265          'build_info' => $this->view->build_info,
 266          'roles' => array_keys($user->roles),
 267          'super-user' => $user->uid == 1, // special caching for super user.
 268          'language' => $GLOBALS['language'],
 269        );
 270        foreach (array('exposed_info', 'page', 'sort', 'order') as $key) {
 271          if (isset($_GET[$key])) {
 272            $key_data[$key] = $_GET[$key];
 273          }
 274        }
 275  
 276        $this->_results_key = $this->view->name . ':' . $this->display->id . ':results:' . md5(serialize($key_data));
 277      }
 278  
 279      return $this->_results_key;
 280    }
 281  
 282    function get_output_key() {
 283      global $user;
 284      if (!isset($this->_output_key)) {
 285        $key_data = array(
 286          'result' => $this->view->result,
 287          'roles' => array_keys($user->roles),
 288          'super-user' => $user->uid == 1, // special caching for super user.
 289          'theme' => $GLOBALS['theme'],
 290          'language' => $GLOBALS['language'],
 291        );
 292  
 293        $this->_output_key = $this->view->name . ':' . $this->display->id . ':output:' . md5(serialize($key_data));
 294      }
 295  
 296      return $this->_output_key;
 297    }
 298  
 299  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7