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.
_drush_variable_format

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