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_name_adjust
drush_variable_set Command callback. Set a variable.
variable_complete_variables List variables for completion.
variable_drush_command Implementation of hook_drush_command().
variable_variable_delete_complete Command argument complete callback.
variable_variable_get_complete Command argument complete callback.
variable_variable_set_complete Command argument complete callback.

File

commands/core/variable.drush.inc
View source
  1. <?php
  2. use Drush\Log\LogLevel;
  3. /**
  4. * Implementation of hook_drush_command().
  5. *
  6. * In this hook, you specify which commands your
  7. * drush module makes available, what it does and
  8. * description.
  9. *
  10. * Notice how this structure closely resembles how
  11. * you define menu hooks.
  12. *
  13. * @return
  14. * An associative array describing your command(s).
  15. */
  16. function variable_drush_command() {
  17. $items['variable-get'] = array(
  18. 'description' => 'Get a list of some or all site variables and values.',
  19. 'core' => array(6,7),
  20. 'arguments' => array(
  21. 'name' => 'A string to filter the variables by. Variables whose name contains the string will be listed.',
  22. ),
  23. 'examples' => array(
  24. 'drush vget' => 'List all variables and values.',
  25. 'drush vget user' => 'List all variables containing the string "user".',
  26. 'drush vget site_mail --exact' => 'Show only the value of the variable with the exact key "site_mail".',
  27. 'drush vget site_mail --exact --pipe' => 'Show only the variable with the exact key "site_mail" without changing the structure of the output.',
  28. ),
  29. 'options' => array(
  30. 'exact' => "Only get the one variable that exactly matches the specified name. Output will contain only the variable's value.",
  31. ),
  32. 'outputformat' => array(
  33. 'default' => 'yaml',
  34. 'pipe-format' => 'config',
  35. 'variable-name' => 'variables',
  36. 'table-metadata' => array(
  37. 'format' => 'var_export',
  38. ),
  39. ),
  40. 'aliases' => array('vget'),
  41. );
  42. $items['variable-set'] = array(
  43. 'description' => "Set a variable.",
  44. 'core' => array(6,7),
  45. 'arguments' => array(
  46. 'name' => 'The name of a variable or the first few letters of its name.',
  47. 'value' => 'The value to assign to the variable. Use \'-\' to read the object from STDIN.',
  48. ),
  49. 'required-arguments' => TRUE,
  50. 'options' => array(
  51. 'yes' => 'Skip confirmation if only one variable name matches.',
  52. 'always-set' => array('description' => 'Older synonym for --exact; deprecated.', 'hidden' => TRUE),
  53. 'exact' => 'The exact name of the variable to set has been provided; do not prompt for similarly-named variables.',
  54. 'format' => array(
  55. '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.',
  56. 'example-value' => 'boolean',
  57. ),
  58. ),
  59. 'examples' => array(
  60. 'drush vset --yes preprocess_css TRUE' => 'Set the preprocess_css variable to true. Skip confirmation if variable already exists.',
  61. '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.',
  62. 'drush vset pr TRUE' => 'Choose from a list of variables beginning with "pr" to set to (bool)true.',
  63. '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)',
  64. ),
  65. 'aliases' => array('vset'),
  66. );
  67. $items['variable-delete'] = array(
  68. 'core' => array(6,7),
  69. 'description' => "Delete a variable.",
  70. 'arguments' => array(
  71. 'name' => 'The name of a variable or the first few letters of its name.',
  72. ),
  73. 'required-arguments' => TRUE,
  74. 'options' => array(
  75. 'yes' => 'Skip confirmation if only one variable name matches.',
  76. 'exact' => 'Only delete the one variable that exactly matches the specified name.',
  77. ),
  78. 'examples' => array(
  79. 'drush vdel user_pictures' => 'Delete the user_pictures variable.',
  80. 'drush vdel u' => 'Choose from a list of variables beginning with "u" to delete.',
  81. 'drush vdel -y --exact maintenance_mode' => 'Bring the site back online, skipping confirmation. Variable is rewritten to site_offline for Drupal 6.',
  82. ),
  83. 'aliases' => array('vdel'),
  84. );
  85. return $items;
  86. }
  87. /**
  88. * Command argument complete callback.
  89. */
  90. function variable_variable_get_complete() {
  91. return variable_complete_variables();
  92. }
  93. /**
  94. * Command argument complete callback.
  95. */
  96. function variable_variable_set_complete() {
  97. return variable_complete_variables();
  98. }
  99. /**
  100. * Command argument complete callback.
  101. */
  102. function variable_variable_delete_complete() {
  103. return variable_complete_variables();
  104. }
  105. /**
  106. * List variables for completion.
  107. *
  108. * @return
  109. * Array of available variables.
  110. */
  111. function variable_complete_variables() {
  112. if (drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  113. global $conf;
  114. return array('values' => array_keys($conf));
  115. }
  116. }
  117. /**
  118. * Command callback.
  119. * List your site's variables.
  120. */
  121. function drush_variable_get() {
  122. global $conf;
  123. $exact = drush_get_option('exact', FALSE);
  124. $keys = array_keys($conf);
  125. if ($args = func_get_args()) {
  126. $args[0] = drush_variable_name_adjust($args[0]);
  127. if ($exact) {
  128. $keys = in_array($args[0], $keys) ? array($args[0]) : array();
  129. }
  130. $keys = preg_grep("/{$args[0]}/", $keys);
  131. }
  132. // In --exact mode, if --pipe is not set, then simplify the return type.
  133. if ($exact && !drush_get_context('DRUSH_PIPE')) {
  134. $key = reset($keys);
  135. $returns = isset($conf[$key]) ? $conf[$key] : FALSE;
  136. }
  137. else {
  138. foreach ($keys as $name) {
  139. $value = $conf[$name];
  140. $returns[$name] = $value;
  141. }
  142. }
  143. if (empty($keys)) {
  144. return drush_set_error('No matching variable found.');
  145. }
  146. else {
  147. return $returns;
  148. }
  149. }
  150. /**
  151. * Command callback.
  152. * Set a variable.
  153. */
  154. function drush_variable_set() {
  155. $args = func_get_args();
  156. $value = $args[1];
  157. if (!isset($value)) {
  158. return drush_set_error('DRUSH_VARIABLE_ERROR', dt('No value specified.'));
  159. }
  160. $args[0] = drush_variable_name_adjust($args[0]);
  161. $result = drush_variable_like($args[0]);
  162. $options[] = "$args[0] ". dt('(new variable)');
  163. $match = FALSE;
  164. while (!$match && $name = drush_db_result($result)) {
  165. if ($name == $args[0]) {
  166. $options[0] = $name;
  167. $match = TRUE;
  168. }
  169. else {
  170. $options[] = $name;
  171. }
  172. }
  173. if ($value == '-') {
  174. $value = stream_get_contents(STDIN);
  175. }
  176. // If the value is a string (usual case, unless we are called from code),
  177. // then format the input
  178. if (is_string($value)) {
  179. $value = drush_value_format($value, drush_get_option('format', 'auto'));
  180. }
  181. // Format the output for display
  182. if (is_array($value)) {
  183. $display = "\n" . var_export($value, TRUE);
  184. }
  185. elseif (is_integer($value)) {
  186. $display = $value;
  187. }
  188. elseif (is_bool($value)) {
  189. $display = $value ? "TRUE" : "FALSE";
  190. }
  191. else {
  192. $display = '"' . $value . '"';
  193. }
  194. // Check 'always-set' for compatibility with older scripts; --exact is preferred.
  195. $always_set = drush_get_option('always-set', FALSE) || drush_get_option('exact', FALSE);
  196. if ($always_set || count($options) == 1 || $match) {
  197. variable_set($args[0], $value);
  198. drush_log(dt('!name was set to !value.', array('!name' => $args[0], '!value' => $display)), LogLevel::SUCCESS);
  199. return '';
  200. }
  201. else {
  202. $choice = drush_choice($options, 'Enter a number to choose which variable to set.');
  203. if ($choice === FALSE) {
  204. return drush_user_abort();
  205. }
  206. $choice = $options[$choice];
  207. $choice = str_replace(' ' . dt('(new variable)'), '', $choice);
  208. drush_op('variable_set', $choice, $value);
  209. drush_log(dt('!name was set to !value', array('!name' => $choice, '!value' => $display)), LogLevel::SUCCESS);
  210. }
  211. }
  212. /**
  213. * Command callback.
  214. * Delete a variable.
  215. */
  216. function drush_variable_delete() {
  217. $args = func_get_args();
  218. $args[0] = drush_variable_name_adjust($args[0]);
  219. // Look for similar variable names.
  220. $result = drush_variable_like($args[0]);
  221. $options = array();
  222. while ($name = drush_db_result($result)) {
  223. $options[] = $name;
  224. }
  225. if (drush_get_option('exact', FALSE)) {
  226. $options = in_array($args[0], $options) ? array($args[0]) : array();
  227. }
  228. if (count($options) == 0) {
  229. drush_print(dt('!name not found.', array('!name' => $args[0])));
  230. return '';
  231. }
  232. if ((count($options) == 1) && drush_get_context('DRUSH_AFFIRMATIVE')) {
  233. drush_op('variable_del', $args[0]);
  234. drush_log(dt('!name was deleted.', array('!name' => $args[0])), LogLevel::SUCCESS);
  235. return '';
  236. }
  237. else {
  238. $choice = drush_choice($options, 'Enter a number to choose which variable to delete.');
  239. if ($choice !== FALSE) {
  240. $choice = $options[$choice];
  241. drush_op('variable_del', $choice);
  242. drush_log(dt('!choice was deleted.', array('!choice' => $choice)), LogLevel::SUCCESS);
  243. }
  244. }
  245. }
  246. // Query for similar variable names.
  247. function drush_variable_like($arg) {
  248. return drush_db_select('variable', 'name', 'name LIKE :keyword', array(':keyword' => $arg . '%'), NULL, NULL, 'name');
  249. }
  250. // Unify similar variable names across different versions of Drupal
  251. function drush_variable_name_adjust($arg) {
  252. if (($arg == 'maintenance_mode') && (drush_drupal_major_version() < 7)) {
  253. $arg = 'site_offline';
  254. }
  255. if (($arg == 'site_offline') && (drush_drupal_major_version() >= 7)) {
  256. $arg = 'maintenance_mode';
  257. }
  258. return $arg;
  259. }