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().
variable_drush_help Implementation of hook_drush_help().

File

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