Projet

Général

Profil

Paste
Télécharger (5,58 ko) Statistiques
| Branche: | Révision:

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

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
      $data = views_cache_get('views_data', TRUE);
71
      if (!empty($data->data)) {
72
        $cache = $data->data;
73
      }
74

    
75
      if (empty($cache)) {
76
        $cache = _views_fetch_data_build();
77
      }
78
      $fully_loaded = TRUE;
79
    }
80
    return $cache;
81
  }
82
  // Return an empty array if there is no match.
83
  return array();
84
}
85

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

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

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

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

    
124
/**
125
 * Fetch the plugin data from cache.
126
 */
127
function _views_fetch_plugin_data($type = NULL, $plugin = NULL, $reset = FALSE) {
128
  static $cache = NULL;
129
  if (!isset($cache) || $reset) {
130
    $start = microtime(TRUE);
131
    views_include('plugins');
132
    views_include_handlers();
133

    
134
    $cache = views_discover_plugins();
135

    
136
  }
137

    
138
  if (!$type && !$plugin) {
139
    return $cache;
140
  }
141
  elseif (!$plugin) {
142
    // Not in the if above so the else below won't run
143
    if (isset($cache[$type])) {
144
      return $cache[$type];
145
    }
146
  }
147
  elseif (isset($cache[$type][$plugin])) {
148
    return $cache[$type][$plugin];
149
  }
150

    
151
  // Return an empty array if there is no match.
152
  return array();
153
}
154

    
155
/**
156
 * Set a cached item in the views cache.
157
 *
158
 * This is just a convenience wrapper around cache_set().
159
 *
160
 * @param $cid
161
 *   The cache ID of the data to store.
162
 * @param $data
163
 *   The data to store in the cache. Complex data types will be automatically serialized before insertion.
164
 *   Strings will be stored as plain text and not serialized.
165
 * @param $use_language
166
 *   If TRUE, the data will be cached specific to the currently active language.
167
 */
168
function views_cache_set($cid, $data, $use_language = FALSE) {
169
  global $language;
170

    
171
  if (variable_get('views_skip_cache', FALSE)) {
172
    return;
173
  }
174
  if ($use_language) {
175
    $cid .= ':' . $language->language;
176
  }
177

    
178
  cache_set($cid, $data, 'cache_views');
179
}
180

    
181
/**
182
 * Return data from the persistent views cache.
183
 *
184
 * This is just a convenience wrapper around cache_get().
185
 *
186
 * @param int $cid
187
 *   The cache ID of the data to retrieve.
188
 * @param bool $use_language
189
 *   If TRUE, the data will be requested specific to the currently active language.
190
 *
191
 * @return stdClass|bool
192
 *   The cache or FALSE on failure.
193
 */
194
function views_cache_get($cid, $use_language = FALSE) {
195
  global $language;
196

    
197
  if (variable_get('views_skip_cache', FALSE)) {
198
    return FALSE;
199
  }
200
  if ($use_language) {
201
    $cid .= ':' . $language->language;
202
  }
203

    
204
  return cache_get($cid, 'cache_views');
205
}