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