Back to dev
[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 'callback' => 'drush_cache_command_get',
23 'outputformat' => array(
24 'default' => 'print-r',
25 'pipe-format' => 'var_export',
26 ),
27 'aliases' => array('cg'),
28 );
29 $items['cache-clear'] = array(
30 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
31 'description' => 'Clear a specific cache, or all drupal caches.',
32 'arguments' => array(
33 'type' => 'The particular cache to clear. Omit this argument to choose from available caches.',
34 ),
35 'callback' => 'drush_cache_command_clear',
36 'aliases' => array('cc'),
37 );
38 $items['cache-set'] = array(
39 'description' => 'Cache an object expressed in JSON or var_export() format.',
40 'arguments' => array(
41 'cid' => 'The id of the object to set.',
42 'data' => 'The object to set in the cache. Use \'-\' to read the object from STDIN.',
43 'bin' => 'Optional. The cache bin to store the object in.',
44 'expire' => 'Optional. CACHE_PERMANENT, CACHE_TEMPORARY, or a Unix timestamp.',
45 ),
46 'required-arguments' => 2,
47 'options' => array(
48 // Note that this is not an outputformat option.
49 'format' => 'Format to parse the object. Use "string" for string (default), and "json" for JSON.',
50 '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.',
51 ),
52 'callback' => 'drush_cache_command_set',
53 'aliases' => array('cs'),
54 );
55
56 return $items;
57 }
58
59 /**
60 * Command argument complete callback.
61 *
62 * @return
63 * Array of clear types.
64 */
65 function cache_cache_command_clear_complete() {
66 return array('values' => array_keys(drush_cache_clear_types(TRUE)));
67 }
68
69 /**
70 * Command callback for drush cache-clear.
71 */
72 function drush_cache_command_clear($type = NULL) {
73 $types = drush_cache_clear_types(drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL));
74
75 // Check if the provided type ($type) is a valid cache type.
76 if ($type && !key_exists($type, $types)) {
77 // If we haven't done a full bootstrap, provide a more
78 // specific message with instructions to the user on
79 // bootstrapping a Drupal site for more options.
80 if (!drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
81 $all_types = drush_cache_clear_types(TRUE);
82 if (key_exists($type, $all_types)) {
83 return drush_set_error(dt("'!type' cache requires a working Drupal site to operate on. Use the --root and --uri options, or a site @alias, or cd to a directory containing a Drupal settings.php file.", array('!type' => $type)));
84 }
85 else {
86 return drush_set_error(dt("'!type' cache is not a valid cache type. There may be more cache types available if you select a working Drupal site.", array('!type' => $type)));
87 }
88 }
89 return drush_set_error(dt("'!type' cache is not a valid cache type.", array('!type' => $type)));
90 }
91
92 if ($type) {
93 drush_op($types[$type]);
94 if ($type == 'all' && !drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
95 $type = 'drush';
96 }
97 }
98 else {
99 // Don't offer 'all' unless Drush has bootstrapped the Drupal site
100 if (!drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
101 unset($types['all']);
102 }
103 $type = drush_choice($types, 'Enter a number to choose which cache to clear.', '!key');
104 if ($type !== FALSE) {
105 call_user_func($types[$type]);
106 }
107 }
108 if ($type !== FALSE) {
109 $site_label = '';
110 if ($type != 'drush') {
111 $self_name = drush_sitealias_bootstrapped_site_name();
112 if (isset($self_name)) {
113 $site_label = dt(' in !name', array('!name' => $self_name));
114 }
115 }
116 drush_log(dt("'!name' cache was cleared!insitename", array('!name' => $type, '!insitename' => $site_label)), 'success');
117 }
118 }
119
120 /**
121 * Print an object returned from the cache.
122 *
123 * @param $cid
124 * The cache ID of the object to fetch.
125 * @param $bin
126 * Optional parameter to specify a specific bin to fetch from.
127 */
128 function drush_cache_command_get($cid = NULL, $bin = NULL) {
129 if (!$cid) {
130 return drush_set_error('DRUSH_CACHE_NOT_SPECIFIED', dt('You must specify a cache id to fetch.'));
131 }
132
133 if (!$bin) {
134 $bin = 'cache';
135 }
136
137 $result = cache_get($cid, $bin);
138 if (empty($result)) {
139 return drush_set_error('DRUSH_CACHE_OBJECT_NOT_FOUND', dt('The !cid object in the !bin cache bin was not found.', array('!cid' => $cid, '!bin' => $bin)));
140 }
141 return $result;
142 }
143
144 /**
145 * Set an object in the cache.
146 *
147 * @param $cid
148 * The cache ID of the object to fetch.
149 * @param $data
150 * The data to save to the cache, or '-' to read from STDIN.
151 * @param $bin
152 * Optional parameter to specify a specific bin to fetch from.
153 * @param $expire
154 * Optional parameter to specify the expiry of the cached object.
155 */
156 function drush_cache_command_set($cid = NULL, $data = '', $bin = NULL, $expire = CACHE_PERMANENT) {
157 if (!$bin) {
158 $bin = 'cache';
159 }
160
161 if ($data == '-') {
162 $data = stream_get_contents(STDIN);
163 }
164
165 // Now, we parse the object.
166 switch (drush_get_option('format', 'string')) {
167 case 'json':
168 $data = drush_json_decode($data);
169 break;
170 }
171
172 if (drush_get_option('cache-get')) {
173 $data = $data->data;
174 }
175
176 cache_set($cid, $data, $bin, $expire);
177 }
178
179 /**
180 * All types of caches available for clearing. Contrib commands can alter in their own.
181 */
182 function drush_cache_clear_types($include_bootstraped_types = FALSE) {
183 $types = array(
184 'all' => 'drush_cache_clear_both',
185 'drush' => 'drush_cache_clear_drush',
186 );
187 if ($include_bootstraped_types) {
188 $types += array(
189 'theme-registry' => 'drush_cache_clear_theme_registry',
190 'menu' => 'menu_rebuild',
191 'css-js' => 'drush_cache_clear_css_js',
192 'block' => 'drush_cache_clear_block',
193 'module-list' => 'drush_get_modules',
194 'theme-list' => 'drush_get_themes',
195 );
196 }
197
198 // D8 renamed menu_rebuild() (https://drupal.org/node/1561492).
199 if (drush_drupal_major_version() >= 8) {
200 $types['menu'] = 'menu_router_rebuild';
201 }
202
203 if (drush_drupal_major_version() >= 7) {
204 $types['registry'] = 'registry_update';
205 }
206 elseif (drush_drupal_major_version() == 6 && function_exists('module_exists') && module_exists('autoload')) {
207 // TODO: move this to autoload module.
208 $types['registry'] = 'autoload_registry_update';
209 }
210
211 // Include the appropriate environment engine, so callbacks can use core
212 // version specific cache clearing functions directly.
213 drush_include_engine('drupal', 'environment');
214
215 // Command files may customize $types as desired.
216 drush_command_invoke_all_ref('drush_cache_clear', $types);
217
218 return $types;
219 }
220
221 function drush_cache_clear_theme_registry() {
222 if (drush_drupal_major_version() >= 7) {
223 drupal_theme_rebuild();
224 }
225 else {
226 cache_clear_all('theme_registry', 'cache', TRUE);
227 }
228 }
229
230 function drush_cache_clear_css_js() {
231 _drupal_flush_css_js();
232 drupal_clear_css_cache();
233 drupal_clear_js_cache();
234 }
235
236 /**
237 * Clear the cache of the block output.
238 */
239 function drush_cache_clear_block() {
240 cache_clear_all(NULL, 'cache_block');
241 }
242
243 /**
244 * Clear caches internal to drush core.
245 */
246 function drush_cache_clear_drush() {
247 drush_cache_clear_all(NULL, 'default'); // commandfiles, etc.
248 drush_cache_clear_all(NULL, 'complete'); // completion
249 }
250
251 /**
252 * Clear caches internal to Drush core and Drupal.
253 */
254 function drush_cache_clear_both() {
255 drush_cache_clear_drush();
256 if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
257 drupal_flush_all_caches();
258 }
259 }