Projet

Général

Profil

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

root / htmltest / sites / all / modules / views / includes / cache.inc @ a5572547

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
      $data = views_cache_get($cid, TRUE);
27
      if (!empty($data->data)) {
28
        $cache[$table] = $data->data;
29
      }
30
      else {
31
        if (!$fully_loaded) {
32
          // No cache entry, rebuild.
33
          $cache = _views_fetch_data_build();
34
          $fully_loaded = TRUE;
35
        }
36
      }
37
    }
38
    if (isset($cache[$table])) {
39
      if (isset($cache[$table]['moved to']) && $move) {
40
        $moved_table = $cache[$table]['moved to'];
41
        if (!empty($recursion_protection[$table])) {
42
          // recursion detected!
43
          return NULL;
44
        }
45
        $recursion_protection[$table] = TRUE;
46
        $data = _views_fetch_data($moved_table);
47
        $recursion_protection = array();
48
        return $data;
49
      }
50
      return $cache[$table];
51
    }
52
  }
53
  else {
54
    if (!$fully_loaded) {
55
      $data = views_cache_get('views_data', TRUE);
56
      if (!empty($data->data)) {
57
        $cache = $data->data;
58
      }
59

    
60
      if (empty($cache)) {
61
        $cache = _views_fetch_data_build();
62
      }
63
      $fully_loaded = TRUE;
64
    }
65
    return $cache;
66
  }
67
  // Return an empty array if there is no match.
68
  return array();
69
}
70

    
71
/**
72
 * Build and set the views data cache if empty.
73
 */
74
function _views_fetch_data_build() {
75
  views_include_handlers();
76
  $cache = module_invoke_all('views_data');
77
  foreach (module_implements('views_data_alter') as $module) {
78
    $function = $module . '_views_data_alter';
79
    $function($cache);
80
  }
81
  _views_data_process_entity_types($cache);
82

    
83
  // Keep a record with all data.
84
  views_cache_set('views_data', $cache, TRUE);
85
  // Save data in seperate cache entries.
86
  foreach ($cache as $key => $data) {
87
    $cid = 'views_data:' . $key;
88
    views_cache_set($cid, $data, TRUE);
89
  }
90
  return $cache;
91
}
92

    
93
/**
94
 * Links tables having an 'entity type' specified to the respective generic entity-type tables.
95
 */
96
function _views_data_process_entity_types(&$data) {
97
  foreach ($data as $table_name => $table_info) {
98
    // Add in a join from the entity-table if an entity-type is given.
99
    if (!empty($table_info['table']['entity type'])) {
100
      $entity_table = 'views_entity_' . $table_info['table']['entity type'];
101

    
102
      $data[$entity_table]['table']['join'][$table_name] = array(
103
        'left_table' => $table_name,
104
      );
105
      $data[$entity_table]['table']['entity type'] = $table_info['table']['entity type'];
106
      // Copy over the default table group if we have none yet.
107
      if (!empty($table_info['table']['group']) && empty($data[$entity_table]['table']['group'])) {
108
        $data[$entity_table]['table']['group'] = $table_info['table']['group'];
109
      }
110
    }
111
  }
112
}
113

    
114
/**
115
 * Fetch the plugin data from cache.
116
 */
117
function _views_fetch_plugin_data($type = NULL, $plugin = NULL, $reset = FALSE) {
118
  static $cache = NULL;
119
  if (!isset($cache) || $reset) {
120
    $start = microtime(TRUE);
121
    views_include('plugins');
122
    views_include_handlers();
123

    
124
    $cache = views_discover_plugins();
125

    
126
  }
127

    
128
  if (!$type && !$plugin) {
129
    return $cache;
130
  }
131
  elseif (!$plugin) {
132
    // Not in the if above so the else below won't run
133
    if (isset($cache[$type])) {
134
      return $cache[$type];
135
    }
136
  }
137
  elseif (isset($cache[$type][$plugin])) {
138
    return $cache[$type][$plugin];
139
  }
140

    
141
  // Return an empty array if there is no match.
142
  return array();
143
}
144

    
145
/**
146
 * Set a cached item in the views cache.
147
 *
148
 * This is just a convenience wrapper around cache_set().
149
 *
150
 * @param $cid
151
 *   The cache ID of the data to store.
152
 * @param $data
153
 *   The data to store in the cache. Complex data types will be automatically serialized before insertion.
154
 *   Strings will be stored as plain text and not serialized.
155
 * @param $use_language
156
 *   If TRUE, the data will be cached specific to the currently active language.
157
 */
158
function views_cache_set($cid, $data, $use_language = FALSE) {
159
  global $language;
160

    
161
  if (variable_get('views_skip_cache', FALSE)) {
162
    return;
163
  }
164
  if ($use_language) {
165
    $cid .= ':' . $language->language;
166
  }
167

    
168
  cache_set($cid, $data, 'cache_views');
169
}
170

    
171
/**
172
 * Return data from the persistent views cache.
173
 *
174
 * This is just a convenience wrapper around cache_get().
175
 *
176
 * @param int $cid
177
 *   The cache ID of the data to retrieve.
178
 * @param bool $use_language
179
 *   If TRUE, the data will be requested specific to the currently active language.
180
 *
181
 * @return stdClass|bool
182
 *   The cache or FALSE on failure.
183
 */
184
function views_cache_get($cid, $use_language = FALSE) {
185
  global $language;
186

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

    
194
  return cache_get($cid, 'cache_views');
195
}