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