Issue #1592706: Fix profile variants check
[project/drush.git] / commands / core / cache.drush.inc
1 <?php
2
3 /**
4 * Implementation of hook_drush_command().
5 */
6 function cache_drush_command() {
7 $items = array();
8
9 // We specify command callbacks here because the defaults would collide with
10 // the drush cache api functions.
11 $items['cache-get'] = array(
12 'description' => 'Fetch a cached object and display it.',
13 'examples' => array(
14 'drush cache-get schema' => 'Display the data for the cache id "schema" from the "cache" bin.',
15 'drush cache-get update_available_releases update' => 'Display the data for the cache id "update_available_releases" from the "update" bin.',
16 ),
17 'arguments' => array(
18 'cid' => 'The id of the object to fetch.',
19 'bin' => 'Optional. The cache bin to fetch from.',
20 ),
21 'required-arguments' => 1,
22 'options' => array(
23 'format' => 'Format to output the object. Use "print_r" for print_r (default), "export" for var_export, and "json" for JSON.',
24 ),
25 'callback' => 'drush_cache_command_get',
26 'aliases' => array('cg'),
27 );
28 $items['cache-clear'] = array(
29 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
30 'description' => 'Clear a specific cache, or all drupal caches.',
31 'arguments' => array(
32 'type' => 'The particular cache to clear. Omit this argument to choose from available caches.',
33 ),
34 'callback' => 'drush_cache_command_clear',
35 'aliases' => array('cc'),
36 );
37 $items['cache-set'] = array(
38 'description' => 'Cache an object expressed in JSON or var_export() format.',
39 'arguments' => array(
40 'cid' => 'The id of the object to set.',
41 'data' => 'The object to set in the cache. Use \'-\' to read the object from STDIN.',
42 'bin' => 'Optional. The cache bin to store the object in.',
43 'expire' => 'Optional. CACHE_PERMANENT, CACHE_TEMPORARY, or a Unix timestamp.',
44 ),
45 'required-arguments' => 2,
46 'options' => array(
47 'format' => 'Format to parse the object. Use "string" for string (default), and "json" for JSON.',
48 'cache-get' => 'If the object is the result a previous fetch from the cache, only store the value in the "data" property of the object in the cache.',
49 ),
50 'callback' => 'drush_cache_command_set',
51 'aliases' => array('cs'),
52 );
53
54 return $items;
55 }
56
57 /**
58 * Command argument complete callback.
59 *
60 * @return
61 * Array of clear types.
62 */
63 function cache_cache_command_clear_complete() {
64 return array('values' => array_keys(drush_cache_clear_types()));
65 }
66
67 /**
68 * Command callback for drush cache-clear.
69 */
70 function drush_cache_command_clear($type = NULL) {
71 $types = drush_cache_clear_types();
72
73 // Check if the provided type ($type) is a valid cache type.
74 if ($type && !key_exists($type, $types)) {
75 return drush_set_error(dt("'!type' cache is not a valid cache type", array('!type' => $type)));
76 }
77
78 if ($type) {
79 drush_op($types[$type]);
80 if ($type == 'all' && !drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
81 $type = 'drush';
82 }
83 }
84 else {
85 // Don't offer 'all' unless Drush has bootstrapped the Drupal site
86 if (!drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
87 unset($types['all']);
88 }
89 $type = drush_choice($types, 'Enter a number to choose which cache to clear.', '!key');
90 if ($type !== FALSE) {
91 call_user_func($types[$type]);
92 }
93 }
94 if ($type !== FALSE) {
95 $site_label = '';
96 if ($type != 'drush') {
97 $self_name = drush_sitealias_bootstrapped_site_name();
98 if (isset($self_name)) {
99 $site_label = dt(' in !name', array('!name' => $self_name));
100 }
101 }
102 drush_log(dt("'!name' cache was cleared!insitename", array('!name' => $type, '!insitename' => $site_label)), 'success');
103 }
104 }
105
106 /**
107 * Print an object returned from the cache.
108 *
109 * @param $cid
110 * The cache ID of the object to fetch.
111 * @param $bin
112 * Optional parameter to specify a specific bin to fetch from.
113 */
114 function drush_cache_command_get($cid = NULL, $bin = NULL) {
115 if (!$cid) {
116 drush_log(dt('You must specify a cache id to fetch.'), 'error');
117 return;
118 }
119
120 if (!$bin) {
121 $bin = 'cache';
122 }
123
124 $result = cache_get($cid, $bin);
125 if (!empty($result)) {
126 drush_print(drush_format($result));
127 }
128 else {
129 drush_log(dt('The !cid object in the !bin cache bin was not found.', array('!cid' => $cid, '!bin' => $bin)), 'error');
130 }
131 }
132
133 /**
134 * Set an object in the cache.
135 *
136 * @param $cid
137 * The cache ID of the object to fetch.
138 * @param $data
139 * The data to save to the cache, or '-' to read from STDIN.
140 * @param $bin
141 * Optional parameter to specify a specific bin to fetch from.
142 * @param $expire
143 * Optional parameter to specify the expiry of the cached object.
144 */
145 function drush_cache_command_set($cid = NULL, $data = '', $bin = NULL, $expire = CACHE_PERMANENT) {
146 if (!$bin) {
147 $bin = 'cache';
148 }
149
150 if ($data == '-') {
151 $data = stream_get_contents(STDIN);
152 }
153
154 // Now, we parse the object.
155 switch (drush_get_option('format', 'string')) {
156 case 'json':
157 $data = drush_json_decode($data);
158 break;
159 }
160
161 if (drush_get_option('cache-get')) {
162 $data = $data->data;
163 }
164
165 cache_set($cid, $data, $bin, $expire);
166 }
167
168 function drush_cache_clear_types() {
169 $types = array(
170 'all' => 'drush_cache_clear_both',
171 'drush' => 'drush_cache_clear_drush',
172 );
173 if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
174 $types += array(
175 'theme-registry' => 'drush_cache_clear_theme_registry',
176 'menu' => 'menu_rebuild',
177 'css-js' => 'drush_cache_clear_css_js',
178 'block' => 'drush_cache_clear_block',
179 'module-list' => 'drush_get_modules',
180 'theme-list' => 'drush_get_themes',
181 );
182 if (count(module_implements('node_grants'))) {
183 $types['nodeaccess'] = 'node_access_rebuild';
184 }
185 }
186
187 if (drush_drupal_major_version() >= 7) {
188 $types['registry'] = 'registry_update';
189 }
190 elseif (drush_drupal_major_version() == 6 && function_exists('module_exists') && module_exists('autoload')) {
191 // TODO: move this to autoload module.
192 $types['registry'] = 'autoload_registry_update';
193 }
194
195 // Include the appropriate environment engine, so callbacks can use core
196 // version specific cache clearing functions directly.
197 drush_include_engine('drupal', 'environment');
198
199 // Command files may customize $types as desired.
200 drush_command_invoke_all_ref('drush_cache_clear', $types);
201
202 return $types;
203 }
204
205 function drush_cache_clear_theme_registry() {
206 drush_db_delete('cache', 'cid LIKE :theme_registry', array(':theme_registry' => 'theme_registry%'));
207 }
208
209 function drush_cache_clear_css_js() {
210 _drupal_flush_css_js();
211 drupal_clear_css_cache();
212 drupal_clear_js_cache();
213 }
214
215 /**
216 * Clear the cache of the block output.
217 */
218 function drush_cache_clear_block() {
219 cache_clear_all(NULL, 'cache_block');
220 }
221
222 /**
223 * Clear caches internal to drush core.
224 */
225 function drush_cache_clear_drush() {
226 drush_cache_clear_all(NULL, 'default'); // commandfiles, etc.
227 drush_cache_clear_all(NULL, 'complete'); // completion
228 }
229
230 /**
231 * Clear caches internal to Drush core and Drupal.
232 */
233 function drush_cache_clear_both() {
234 drush_cache_clear_drush();
235 if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
236 drupal_flush_all_caches();
237 }
238 }