Project

General

Profile

Paste
Download (6.14 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / includes / cache.inc @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Load Views' data so that it knows what is available to build queries from.
6
 */
7

    
8
/**
9
 * Fetch Views' data from the cache.
10
 *
11
 * @param string $table
12
 * @param bool $move
13
 *   Under certain circumstances it makes sense to not get the moved table, but
14
 *   the old one. One example is views_get_handler.
15
 * @param bool $reset
16
 */
17
function _views_fetch_data($table = NULL, $move = TRUE, $reset = FALSE) {
18
  $cache = &drupal_static(__FUNCTION__ . '_cache');
19
  $recursion_protection = &drupal_static(__FUNCTION__ . '_recursion_protected');
20
  $fully_loaded = &drupal_static(__FUNCTION__ . '_fully_loaded');
21
  if ($reset) {
22
    $cache = NULL;
23
    $fully_loaded = FALSE;
24
  }
25
  if ($table) {
26
    if (!isset($cache[$table])) {
27
      $cid = 'views_data:' . $table;
28
      if ($data = views_cache_get($cid, TRUE)) {
29
        $cache[$table] = $data->data;
30
      }
31
      else {
32
        if (!$fully_loaded) {
33
          // Try to load the full views cache.
34
          if ($data = views_cache_get('views_data', TRUE)) {
35
            $cache = $data->data;
36
          }
37
          else {
38
            // No cache entry, rebuild.
39
            $cache = _views_fetch_data_build();
40
          }
41
          $fully_loaded = TRUE;
42
        }
43

    
44
        // Write back a cache for this table.
45
        if (isset($cache[$table])) {
46
          views_cache_set($cid, $cache[$table], TRUE);
47
        }
48
        else {
49
          // If there is still no information about that table, it is missing.
50
          // Write an empty array to avoid repeated rebuilds.
51
          views_cache_set($cid, array(), TRUE);
52
        }
53
      }
54
    }
55
    if (isset($cache[$table])) {
56
      if (isset($cache[$table]['moved to']) && $move) {
57
        $moved_table = $cache[$table]['moved to'];
58
        if (!empty($recursion_protection[$table])) {
59
          // recursion detected!
60
          return NULL;
61
        }
62
        $recursion_protection[$table] = TRUE;
63
        $data = _views_fetch_data($moved_table);
64
        $recursion_protection = array();
65
        return $data;
66
      }
67
      return $cache[$table];
68
    }
69
  }
70
  else {
71
    if (!$fully_loaded) {
72
      if ($data = views_cache_get('views_data', TRUE)) {
73
        $cache = $data->data;
74
      }
75
      else {
76
        // No cache entry, rebuild.
77
        $cache = _views_fetch_data_build();
78
      }
79
      $fully_loaded = TRUE;
80
    }
81
    return $cache;
82
  }
83
  // Return an empty array if there is no match.
84
  return array();
85
}
86

    
87
/**
88
 * Build and set the views data cache if empty.
89
 */
90
function _views_fetch_data_build() {
91
  views_include_handlers();
92
  $cache = module_invoke_all('views_data');
93
  foreach (module_implements('views_data_alter') as $module) {
94
    $function = $module . '_views_data_alter';
95
    $function($cache);
96
  }
97
  _views_data_process_entity_types($cache);
98

    
99
  // Keep a record with all data.
100
  views_cache_set('views_data', $cache, TRUE);
101
  return $cache;
102
}
103

    
104
/**
105
 * Links tables having an 'entity type' specified to the respective generic
106
 * entity-type tables.
107
 */
108
function _views_data_process_entity_types(&$data) {
109
  foreach ($data as $table_name => $table_info) {
110
    // Add in a join from the entity-table if an entity-type is given.
111
    if (!empty($table_info['table']['entity type'])) {
112
      $entity_table = 'views_entity_' . $table_info['table']['entity type'];
113

    
114
      $data[$entity_table]['table']['join'][$table_name] = array(
115
        'left_table' => $table_name,
116
      );
117
      $data[$entity_table]['table']['entity type'] = $table_info['table']['entity type'];
118
      // Copy over the default table group if we have none yet.
119
      if (!empty($table_info['table']['group']) && empty($data[$entity_table]['table']['group'])) {
120
        $data[$entity_table]['table']['group'] = $table_info['table']['group'];
121
      }
122
    }
123
  }
124
}
125

    
126
/**
127
 * Fetch the plugin data from cache.
128
 */
129
function _views_fetch_plugin_data($type = NULL, $plugin = NULL, $reset = FALSE) {
130
  static $cache = NULL;
131
  if (!isset($cache) || $reset) {
132
    // Load necessary code once.
133
    if (!isset($cache)) {
134
      views_include('plugins');
135
      views_include_handlers();
136
    }
137
    // Because plugin data contains translated strings, and as such can be
138
    // expensive to build, the results are cached per language.
139
    global $language;
140
    $cache_key = 'views:plugin_data:' . $language->language;
141
    if (!$reset) {
142
      if ($cache = cache_get($cache_key)) {
143
        $cache = $cache->data;
144
      }
145
    }
146
    // If not available in the cache, build it and cache it.
147
    if (!$cache || $reset) {
148
      $cache = views_discover_plugins();
149
      cache_set($cache_key, $cache);
150
    }
151
  }
152

    
153
  if (!$type && !$plugin) {
154
    return $cache;
155
  }
156
  elseif (!$plugin) {
157
    // Not in the if above so the else below won't run.
158
    if (isset($cache[$type])) {
159
      return $cache[$type];
160
    }
161
  }
162
  elseif (isset($cache[$type][$plugin])) {
163
    return $cache[$type][$plugin];
164
  }
165

    
166
  // Return an empty array if there is no match.
167
  return array();
168
}
169

    
170
/**
171
 * Set a cached item in the views cache.
172
 *
173
 * This is just a convenience wrapper around cache_set().
174
 *
175
 * @param string $cid
176
 *   The cache ID of the data to store.
177
 * @param mixed $data
178
 *   The data to store in the cache. Complex data types will be automatically
179
 *   serialized before insertion. Strings will be stored as plain text and not
180
 *   serialized.
181
 * @param bool $use_language
182
 *   If TRUE, the data will be cached specific to the currently active language.
183
 */
184
function views_cache_set($cid, $data, $use_language = FALSE) {
185
  global $language;
186

    
187
  if (variable_get('views_skip_cache', FALSE)) {
188
    return;
189
  }
190
  if ($use_language) {
191
    $cid .= ':' . $language->language;
192
  }
193

    
194
  cache_set($cid, $data, 'cache_views');
195
}
196

    
197
/**
198
 * Return data from the persistent views cache.
199
 *
200
 * This is just a convenience wrapper around cache_get().
201
 *
202
 * @param int $cid
203
 *   The cache ID of the data to retrieve.
204
 * @param bool $use_language
205
 *   If TRUE, the data will be requested specific to the currently active
206
 *   language.
207
 *
208
 * @return stdClass|bool
209
 *   The cache or FALSE on failure.
210
 */
211
function views_cache_get($cid, $use_language = FALSE) {
212
  global $language;
213

    
214
  if (variable_get('views_skip_cache', FALSE)) {
215
    return FALSE;
216
  }
217
  if ($use_language) {
218
    $cid .= ':' . $language->language;
219
  }
220

    
221
  return cache_get($cid, 'cache_views');
222
}