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 ascending Description
_drush_variable_format
variable_variable_set_complete Command argument complete callback.
variable_variable_get_complete Command argument complete callback.
variable_variable_delete_complete Command argument complete callback.
variable_drush_command Implementation of hook_drush_command().
variable_complete_variables List variables for completion.
drush_variable_set Command callback. Set a variable.
drush_variable_name_adjust
drush_variable_like
drush_variable_get Command callback. List your site's variables.
drush_variable_delete Command callback. Delete a variable.

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