4 * Implementation of hook_drush_command().
6 function cache_drush_command() {
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.',
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.',
18 'cid' => 'The id of the object to fetch.',
19 'bin' => 'Optional. The cache bin to fetch from.',
21 'required-arguments' => 1,
23 'format' => 'Format to output the object. Use "print_r" for print_r (default), "export" for var_export, and "json" for JSON.',
25 'callback' => 'drush_cache_command_get',
26 'aliases' => array('cg'),
28 $items['cache-clear'] = array(
29 'bootstrap' => DRUSH_BOOTSTRAP_MAX
,
30 'description' => 'Clear a specific cache, or all drupal caches.',
32 'type' => 'The particular cache to clear. Omit this argument to choose from available caches.',
34 'callback' => 'drush_cache_command_clear',
35 'aliases' => array('cc'),
37 $items['cache-set'] = array(
38 'description' => 'Cache an object expressed in JSON or var_export() format.',
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.',
45 'required-arguments' => 2,
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.',
50 'callback' => 'drush_cache_command_set',
51 'aliases' => array('cs'),
58 * Command argument complete callback.
61 * Array of clear types.
63 function cache_cache_command_clear_complete() {
64 return array('values' => array_keys(drush_cache_clear_types()));
68 * Command callback for drush cache-clear.
70 function drush_cache_command_clear($type = NULL
) {
71 $types = drush_cache_clear_types();
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)));
79 drush_op($types[$type]);
80 if ($type == 'all' && !drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL
)) {
85 // Don't offer 'all' unless Drush has bootstrapped the Drupal site
86 if (!drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL
)) {
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]);
94 if ($type !== FALSE
) {
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));
102 drush_log(dt("'!name' cache was cleared!insitename", array('!name' => $type, '!insitename' => $site_label)), 'success');
107 * Print an object returned from the cache.
110 * The cache ID of the object to fetch.
112 * Optional parameter to specify a specific bin to fetch from.
114 function drush_cache_command_get($cid = NULL
, $bin = NULL
) {
116 drush_log(dt('You must specify a cache id to fetch.'), 'error');
124 $result = cache_get($cid, $bin);
125 if (!empty($result)) {
126 drush_print(drush_format($result));
129 drush_log(dt('The !cid object in the !bin cache bin was not found.', array('!cid' => $cid, '!bin' => $bin)), 'error');
134 * Set an object in the cache.
137 * The cache ID of the object to fetch.
139 * The data to save to the cache, or '-' to read from STDIN.
141 * Optional parameter to specify a specific bin to fetch from.
143 * Optional parameter to specify the expiry of the cached object.
145 function drush_cache_command_set($cid = NULL
, $data = '', $bin = NULL
, $expire = CACHE_PERMANENT
) {
151 $data = stream_get_contents(STDIN
);
154 // Now, we parse the object.
155 switch (drush_get_option('format', 'string')) {
157 $data = drush_json_decode($data);
161 if (drush_get_option('cache-get')) {
165 cache_set($cid, $data, $bin, $expire);
168 function drush_cache_clear_types() {
170 'all' => 'drush_cache_clear_both',
171 'drush' => 'drush_cache_clear_drush',
173 if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL
)) {
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',
182 if (count(module_implements('node_grants'))) {
183 $types['nodeaccess'] = 'node_access_rebuild';
187 if (drush_drupal_major_version() >= 7) {
188 $types['registry'] = 'registry_update';
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';
195 // Include the appropriate environment engine, so callbacks can use core
196 // version specific cache clearing functions directly.
197 drush_include_engine('drupal', 'environment');
199 // Command files may customize $types as desired.
200 drush_command_invoke_all_ref('drush_cache_clear', $types);
205 function drush_cache_clear_theme_registry() {
206 drush_db_delete('cache', 'cid LIKE :theme_registry', array(':theme_registry' => 'theme_registry%'));
209 function drush_cache_clear_css_js() {
210 _drupal_flush_css_js();
211 drupal_clear_css_cache();
212 drupal_clear_js_cache();
216 * Clear the cache of the block output.
218 function drush_cache_clear_block() {
219 cache_clear_all(NULL
, 'cache_block');
223 * Clear caches internal to drush core.
225 function drush_cache_clear_drush() {
226 drush_cache_clear_all(NULL
, 'default'); // commandfiles, etc.
227 drush_cache_clear_all(NULL
, 'complete'); // completion
231 * Clear caches internal to Drush core and Drupal.
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();