Projet

Général

Profil

Paste
Télécharger (6,05 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / includes / cache.inc @ 6f57d8c7

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

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

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

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

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

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

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

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

    
163
  // Return an empty array if there is no match.
164
  return array();
165
}
166

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

    
183
  if (variable_get('views_skip_cache', FALSE)) {
184
    return;
185
  }
186
  if ($use_language) {
187
    $cid .= ':' . $language->language;
188
  }
189

    
190
  cache_set($cid, $data, 'cache_views');
191
}
192

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

    
209
  if (variable_get('views_skip_cache', FALSE)) {
210
    return FALSE;
211
  }
212
  if ($use_language) {
213
    $cid .= ':' . $language->language;
214
  }
215

    
216
  return cache_get($cid, 'cache_views');
217
}