variable.drush.inc

  1. 8.0.x commands/core/variable.drush.inc
  2. 6.x commands/core/variable.drush.inc
  3. 7.x commands/core/variable.drush.inc
  4. 3.x commands/core/variable.drush.inc
  5. 4.x commands/core/variable.drush.inc
  6. 5.x commands/core/variable.drush.inc
  7. master commands/core/variable.drush.inc

Functions

Namesort descending Description
drush_variable_delete Command callback. Delete a variable.
drush_variable_get Command callback. List your site's variables.
drush_variable_like
drush_variable_set Command callback. Set a variable.
variable_drush_command Implementation of hook_drush_command().

File

commands/core/variable.drush.inc
View source
  1. <?php
  2. /**
  3. * Implementation of hook_drush_command().
  4. *
  5. * In this hook, you specify which commands your
  6. * drush module makes available, what it does and
  7. * description.
  8. *
  9. * Notice how this structure closely resembles how
  10. * you define menu hooks.
  11. *
  12. * @return
  13. * An associative array describing your command(s).
  14. */
  15. function variable_drush_command() {
  16. $items['variable-get'] = array(
  17. 'description' => 'Get a list of some or all site variables and values.',
  18. 'arguments' => array(
  19. 'name' => 'A string to filter the variables by. Only variables beginning with the string will be listed.',
  20. ),
  21. 'examples' => array(
  22. 'drush vget' => 'List all variables and values.',
  23. 'drush vget user' => 'List all variables beginning with the string "user".',
  24. ),
  25. 'options' => array(
  26. 'pipe' => 'Use var_export() to emit executable PHP. Useful for pasting into code.',
  27. ),
  28. 'aliases' => array('vget'),
  29. );
  30. $items['variable-set'] = array(
  31. 'description' => "Set a variable.",
  32. 'arguments' => array(
  33. 'name' => 'The name of a variable or the first few letters of its name.',
  34. 'value' => 'The value to assign to the variable.',
  35. ),
  36. 'options' => array(
  37. 'yes' => 'Skip confirmation if only one variable name matches.',
  38. 'always-set' => 'Always skip confirmation.',
  39. ),
  40. 'examples' => array(
  41. 'drush vset --yes preprocess_css 1' => 'Set the preprocess_css variable to true. Skip confirmation if variable already exists.',
  42. 'drush vset --always-set site_offline 1' => 'Take the site offline; Skips confirmation even if site_offline variable does not exist.',
  43. 'drush vset pr 1' => 'Choose from a list of variables beginning with "pr" to set to true.',
  44. ),
  45. 'aliases' => array('vset'),
  46. );
  47. $items['variable-delete'] = array(
  48. 'description' => "Delete a variable.",
  49. 'arguments' => array(
  50. 'name' => 'The name of a variable or the first few letters of its name.',
  51. ),
  52. 'options' => array(
  53. 'yes' => 'Skip confirmation if only one variable name matches.',
  54. ),
  55. 'examples' => array(
  56. 'drush vdel user_pictures' => 'Delete the user_pictures variable.',
  57. 'drush vdel u' => 'Choose from a list of variables beginning with "u" to delete.',
  58. ),
  59. 'aliases' => array('vdel'),
  60. );
  61. return $items;
  62. }
  63. /**
  64. * Command callback.
  65. * List your site's variables.
  66. */
  67. function drush_variable_get() {
  68. global $conf;
  69. $found = FALSE;
  70. $keys = array_keys($conf);
  71. if ($args = func_get_args()) {
  72. $keys = preg_grep("/{$args[0]}/", $keys);
  73. }
  74. foreach ($keys as $name) {
  75. $value = $conf[$name];
  76. drush_print_pipe("\$variables['$name'] = ". var_export($value, TRUE). ";\n");
  77. $returns[$name] = $value;
  78. if ($value === TRUE) {
  79. $value = 'TRUE';
  80. }
  81. elseif ($value === FALSE) {
  82. $value = 'FALSE';
  83. }
  84. elseif (is_string($value)) {
  85. $value = '"' . $value . '"';
  86. }
  87. elseif (is_array($value) || is_object($value)) {
  88. $value = print_r($value, TRUE);
  89. }
  90. drush_print($name . ': ' . $value);
  91. $found = TRUE;
  92. }
  93. if (!$found) {
  94. return drush_set_error('No matching variable found.');
  95. }
  96. else {
  97. return $returns;
  98. }
  99. }
  100. /**
  101. * Command callback.
  102. * Set a variable.
  103. */
  104. function drush_variable_set() {
  105. $args = func_get_args();
  106. if (!isset($args[0])) {
  107. return drush_set_error('DRUSH_VARIABLE_ERROR', dt('No variable specified.'));
  108. }
  109. $value = $args[1];
  110. if (!isset($value)) {
  111. return drush_set_error('DRUSH_VARIABLE_ERROR', dt('No value specified.'));
  112. }
  113. $result = drush_variable_like($args[0]);
  114. $options[] = "$args[0] ". dt('(new variable)');
  115. $match = FALSE;
  116. while (!$match && $name = drush_db_result($result)) {
  117. if ($name == $args[0]) {
  118. $options[0] = $name;
  119. $match = TRUE;
  120. }
  121. else {
  122. $options[] = $name;
  123. }
  124. }
  125. if ((drush_get_option('always-set', FALSE)) || ((count($options) == 1 || $match) && drush_get_context('DRUSH_AFFIRMATIVE'))) {
  126. variable_set($args[0], $value);
  127. drush_log(dt('!name was set to !value.', array('!name' => $args[0], '!value' => $value)), 'success');
  128. return '';
  129. }
  130. else {
  131. $choice = drush_choice($options, 'Enter a number to choose which variable to set.');
  132. if ($choice !== FALSE) {
  133. $choice = $options[$choice];
  134. $choice = str_replace(' ' . dt('(new variable)'), '', $choice);
  135. drush_op('variable_set', $choice, $value);
  136. drush_log(dt('!name was set to !value', array('!name' => $choice, '!value' => $value)), 'success');
  137. }
  138. }
  139. }
  140. /**
  141. * Command callback.
  142. * Delete a variable.
  143. */
  144. function drush_variable_delete() {
  145. $args = func_get_args();
  146. if (!isset($args[0])) {
  147. drush_set_error('DRUSH_VARIABLE_ERROR', dt('No variable specified'));
  148. }
  149. // Look for similar variable names.
  150. $result = drush_variable_like($args[0]);
  151. $options = array();
  152. while ($name = drush_db_result($result)) {
  153. $options[] = $name;
  154. }
  155. if (count($options) == 0) {
  156. drush_print(dt('!name not found.', array('!name' => $args[0])));
  157. return '';
  158. }
  159. if ((count($options) == 1) && drush_get_context('DRUSH_AFFIRMATIVE')) {
  160. drush_op('variable_del', $args[0]);
  161. drush_log(dt('!name was deleted.', array('!name' => $args[0])), 'success');
  162. return '';
  163. }
  164. else {
  165. $choice = drush_choice($options, 'Enter a number to choose which variable to delete.');
  166. if ($choice !== FALSE) {
  167. $choice = $options[$choice];
  168. drush_op('variable_del', $choice);
  169. drush_log(dt('!choice was deleted.', array('!choice' => $choice)), 'success');
  170. }
  171. }
  172. }
  173. // Query for similar variable names.
  174. function drush_variable_like($arg) {
  175. return drush_db_select('variable', 'name', 'name LIKE :keyword', array(':keyword' => $arg . '%'), NULL, NULL, 'name');
  176. }