3 use Drush\Log\LogLevel;
6 * Implementation of hook_drush_command().
8 * In this hook, you specify which commands your
9 * drush module makes available, what it does and
12 * Notice how this structure closely resembles how
13 * you define menu hooks.
16 * An associative array describing your command(s).
18 function variable_drush_command() {
19 $items['variable-get'] = array(
20 'description' => 'Get a list of some or all site variables and values.',
23 'name' => 'A string to filter the variables by. Variables whose name contains the string will be listed.',
26 'drush vget' => 'List all variables and values.',
27 'drush vget user' => 'List all variables containing the string "user".',
28 'drush vget site_mail --exact' => 'Show only the value of the variable with the exact key "site_mail".',
29 'drush vget site_mail --exact --pipe' => 'Show only the variable with the exact key "site_mail" without changing the structure of the output.',
32 'exact' => "Only get the one variable that exactly matches the specified name. Output will contain only the variable's value.",
34 'outputformat' => array(
36 'pipe-format' => 'config',
37 'variable-name' => 'variables',
38 'table-metadata' => array(
39 'format' => 'var_export',
42 'aliases' => array('vget'),
44 $items['variable-set'] = array(
45 'description' => "Set a variable.",
48 'name' => 'The name of a variable or the first few letters of its name.',
49 'value' => 'The value to assign to the variable. Use \'-\' to read the object from STDIN.',
51 'required-arguments' => TRUE,
53 'yes' => 'Skip confirmation if only one variable name matches.',
54 'always-set' => array('description' => 'Older synonym for --exact; deprecated.', 'hidden' => TRUE),
55 'exact' => 'The exact name of the variable to set has been provided; do not prompt for similarly-named variables.',
57 'description' => 'Type for the value. Use "auto" to detect format from value. Other recognized values are string, integer float, or boolean for corresponding primitive type, or json, yaml for complex types.',
58 'example-value' => 'boolean',
62 'drush vset --yes preprocess_css TRUE' => 'Set the preprocess_css variable to true. Skip confirmation if variable already exists.',
63 'drush vset --exact maintenance_mode 1' => 'Take the site offline; skips confirmation even if maintenance_mode variable does not exist. Variable is rewritten to site_offline for Drupal 6.',
64 'drush vset pr TRUE' => 'Choose from a list of variables beginning with "pr" to set to (bool)true.',
65 'php -r "print json_encode(array(\'drupal\', \'simpletest\'));" | drush vset --format=json project_dependency_excluded_dependencies -'=> 'Set a variable to a complex value (e.g. array)',
67 'aliases' => array('vset'),
69 $items['variable-delete'] = array(
71 'description' => "Delete a variable.",
73 'name' => 'The name of a variable or the first few letters of its name.',
75 'required-arguments' => TRUE,
77 'yes' => 'Skip confirmation if only one variable name matches.',
78 'exact' => 'Only delete the one variable that exactly matches the specified name.',
81 'drush vdel user_pictures' => 'Delete the user_pictures variable.',
82 'drush vdel u' => 'Choose from a list of variables beginning with "u" to delete.',
83 'drush vdel -y --exact maintenance_mode' => 'Bring the site back online, skipping confirmation. Variable is rewritten to site_offline for Drupal 6.',
85 'aliases' => array('vdel'),
92 * Command argument complete callback.
94 function variable_variable_get_complete() {
95 return variable_complete_variables();
99 * Command argument complete callback.
101 function variable_variable_set_complete() {
102 return variable_complete_variables();
106 * Command argument complete callback.
108 function variable_variable_delete_complete() {
109 return variable_complete_variables();
113 * List variables for completion.
116 * Array of available variables.
118 function variable_complete_variables() {
119 if (drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
121 return array('values' => array_keys($conf));
127 * List your site's variables.
129 function drush_variable_get() {
131 $exact = drush_get_option('exact', FALSE);
133 $keys = array_keys($conf);
134 if ($args = func_get_args()) {
135 $args[0] = drush_variable_name_adjust($args[0]);
137 $keys = in_array($args[0], $keys) ? array($args[0]) : array();
139 $keys = preg_grep("/{$args[0]}/", $keys);
142 // In --exact mode, if --pipe is not set, then simplify the return type.
143 if ($exact && !drush_get_context('DRUSH_PIPE')) {
145 $returns = isset($conf[$key]) ? $conf[$key] : FALSE;
148 foreach ($keys as $name) {
149 $value = $conf[$name];
150 $returns[$name] = $value;
154 return drush_set_error('No matching variable found.');
165 function drush_variable_set() {
166 $args = func_get_args();
168 if (!isset($value)) {
169 return drush_set_error('DRUSH_VARIABLE_ERROR', dt('No value specified.'));
172 $args[0] = drush_variable_name_adjust($args[0]);
173 $result = drush_variable_like($args[0]);
175 $options[] = "$args[0] ". dt('(new variable)');
177 while (!$match && $name = drush_db_result($result)) {
178 if ($name == $args[0]) {
188 $value = stream_get_contents(STDIN);
191 // If the value is a string (usual case, unless we are called from code),
192 // then format the input
193 if (is_string($value)) {
194 $value = drush_value_format($value, drush_get_option('format', 'auto'));
197 // Format the output for display
198 if (is_array($value)) {
199 $display = "\n" . var_export($value, TRUE);
201 elseif (is_integer($value)) {
204 elseif (is_bool($value)) {
205 $display = $value ? "TRUE" : "FALSE";
208 $display = '"' . $value . '"';
211 // Check 'always-set' for compatibility with older scripts; --exact is preferred.
212 $always_set = drush_get_option('always-set', FALSE) || drush_get_option('exact', FALSE);
214 if ($always_set || count($options) == 1 || $match) {
215 variable_set($args[0], $value);
216 drush_log(dt('!name was set to !value.', array('!name' => $args[0], '!value' => $display)), LogLevel::SUCCESS);
220 $choice = drush_choice($options, 'Enter a number to choose which variable to set.');
221 if ($choice === FALSE) {
222 return drush_user_abort();
224 $choice = $options[$choice];
225 $choice = str_replace(' ' . dt('(new variable)'), '', $choice);
226 drush_op('variable_set', $choice, $value);
227 drush_log(dt('!name was set to !value', array('!name' => $choice, '!value' => $display)), LogLevel::SUCCESS);
235 function drush_variable_delete() {
236 $args = func_get_args();
237 $args[0] = drush_variable_name_adjust($args[0]);
238 // Look for similar variable names.
239 $result = drush_variable_like($args[0]);
242 while ($name = drush_db_result($result)) {
245 if (drush_get_option('exact', FALSE)) {
246 $options = in_array($args[0], $options) ? array($args[0]) : array();
249 if (count($options) == 0) {
250 drush_print(dt('!name not found.', array('!name' => $args[0])));
254 if ((count($options) == 1) && drush_get_context('DRUSH_AFFIRMATIVE')) {
255 drush_op('variable_del', $args[0]);
256 drush_log(dt('!name was deleted.', array('!name' => $args[0])), LogLevel::SUCCESS);
260 $choice = drush_choice($options, 'Enter a number to choose which variable to delete.');
261 if ($choice !== FALSE) {
262 $choice = $options[$choice];
263 drush_op('variable_del', $choice);
264 drush_log(dt('!choice was deleted.', array('!choice' => $choice)), LogLevel::SUCCESS);
269 // Query for similar variable names.
270 function drush_variable_like($arg) {
271 return drush_db_select('variable', 'name', 'name LIKE :keyword', array(':keyword' => $arg . '%'), NULL, NULL, 'name');
274 // Unify similar variable names across different versions of Drupal
275 function drush_variable_name_adjust($arg) {
276 if (($arg == 'maintenance_mode') && (drush_drupal_major_version() < 7)) {
277 $arg = 'site_offline';
279 if (($arg == 'site_offline') && (drush_drupal_major_version() >= 7)) {
280 $arg = 'maintenance_mode';