forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrush.inc
1834 lines (1694 loc) · 68.7 KB
/
drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* The drush API implementation and helpers.
*/
use Drush\Log\Logger;
use Drush\Log\LogLevel;
use Psr\Log\LoggerInterface;
/**
* @name Error status definitions
* @{
* Error code definitions for interpreting the current error status.
* @see drush_set_error(), drush_get_error(), drush_get_error_log(), drush_cmp_error()
*/
/** The command completed successfully. */
define('DRUSH_SUCCESS', 0);
/** The command could not be completed because the framework has specified errors that have occured. */
define('DRUSH_FRAMEWORK_ERROR', 1);
/** The command was aborted because the user chose to cancel it at some prompt.
This exit code is arbitrarily the same as EX_TEMPFAIL in sysexits.h, although
note that shell error codes are distinct from C exit codes, so this alignment
not meaningful. */
define('DRUSH_EXITCODE_USER_ABORT', 75);
/** The command that was executed resulted in an application error,
The most commom causes for this is invalid PHP or a broken SSH
pipe when using drush_backend_invoke in a distributed manner. */
define('DRUSH_APPLICATION_ERROR', 255);
/**
* @} End of "name Error status defintions".
*/
/**
* The number of bytes in a kilobyte. Copied from Drupal.
*/
define('DRUSH_KILOBYTE', 1024);
/**
* Default amount of time, in seconds, to cache downloads via
* drush_download_file(). One day is 86400 seconds.
*/
define('DRUSH_CACHE_LIFETIME_DEFAULT', 86400);
/**
* Include a file, selecting a version specific file if available.
*
* For example, if you pass the path "/var/drush" and the name
* "update" when bootstrapped on a Drupal 8 site it will first check for
* the presence of "/var/drush/update_8.inc" in include it if exists. If this
* file does NOT exist it will proceed and check for "/var/drush/update.inc".
* If neither file exists, it will return FALSE.
*
* @param $path
* The path you want to search.
* @param $name
* The file base name you want to include (not including a version suffix
* or extension).
* @param $version
* The version suffix you want to include (could be specific to the software
* or platform your are connecting to) - defaults to the current Drupal core
* major version.
* @param $extension
* The extension - defaults to ".inc".
*
* @return
* TRUE if the file was found and included.
*/
function drush_include($path, $name, $version = NULL, $extension = 'inc') {
$name = str_replace('-', '_', $name);
$version = ($version) ? $version : drush_drupal_major_version();
$file = sprintf("%s/%s_%s.%s", $path, $name, $version, $extension);
if (file_exists($file)) {
include_once($file);
return TRUE;
}
$file = sprintf("%s/%s.%s", $path, $name, $extension);
if (file_exists($file)) {
include_once($file);
return TRUE;
}
return drush_set_error('DRUSH_INCLUDE_NO_PATH', dt('Unable to include file !name!version!extension or !name!extension from !path.', array('!name' => $name, '!version' => $version, '!extension' => $extension)));
}
/**
* Provide a version-specific class instance.
*
* @param $class_name
* The name of the class to instantiate. Appends the Drupal
* major version number to the end of the class name before instantiation.
* @param $constructor_args
* An array of arguments to pass to the class constructor.
*
* Example wrapper class to instantiate a widget, called with the
* arguments for the WIDGET_CLASS constructor:
*
* function drush_WIDGET_CLASS_get_class($widgetName, $widgetStyle) {
* retrun drush_get_class('Widget_Class', func_get_args()));
* }
*/
function drush_get_class($class_name, $constructor_args = array(), $variations = array()) {
if (empty($variations)) {
$variations[] = drush_drupal_major_version();
}
$class_names = is_array($class_name) ? $class_name : array($class_name);
foreach ($class_names as $class_name) {
for ($i=count($variations); $i >= 0; $i--) {
$variant_class_name = $class_name . implode('', array_slice($variations, 0, $i));
if (class_exists($variant_class_name)) {
$reflectionClass = new ReflectionClass($variant_class_name);
return !empty($constructor_args) ? $reflectionClass->newInstanceArgs($constructor_args) : $reflectionClass->newInstanceArgs();
}
}
}
// Something bad happenned. TODO Exception?
return drush_set_error('DRUSH_GET_CLASS_ERROR', dt('Unable to load class !class', array('!class' => $class_name)));
}
/**
* Generate an .ini file. used by archive-dump."
*
* @param array $ini
* A two dimensional associative array where top level are sections and
* second level are key => value pairs.
*
* @return string
* .ini formatted text.
*/
function drush_export_ini($ini) {
$output = '';
foreach ($ini as $section => $pairs) {
if ($section) {
$output .= "[$section]\n";
}
foreach ($pairs as $k => $v) {
if ($v) {
$output .= "$k = \"$v\"\n";
}
}
}
return $output;
}
/**
* Generate code friendly to the Drupal .info format from a structured array.
* Mostly copied from http://drupalcode.org/viewvc/drupal/contributions/modules/features/features.export.inc.
*
* @param $info
* An array or single value to put in a module's .info file.
*
* @param boolean $integer_keys
* Use integer in keys.
*
* @param $parents
* Array of parent keys (internal use only).
*
* @return
* A code string ready to be written to a module's .info file.
*/
function drush_export_info($info, $integer_keys = FALSE, $parents = array()) {
$output = '';
if (is_array($info)) {
foreach ($info as $k => $v) {
$child = $parents;
$child[] = $k;
$output .= drush_export_info($v, $integer_keys, $child);
}
}
else if (!empty($info) && count($parents)) {
$line = array_shift($parents);
foreach ($parents as $key) {
$line .= (!$integer_keys && is_numeric($key)) ? "[]" : "[{$key}]";
}
$line .= " = \"{$info}\"\n";
return $line;
}
return $output;
}
/**
* Convert a csv string, or an array of items which
* may contain csv strings, into an array of items.
*
* @param $args
* A simple csv string; e.g. 'a,b,c'
* or a simple list of items; e.g. array('a','b','c')
* or some combination; e.g. array('a,b','c') or array('a,','b,','c,')
*
* @returns array
* A simple list of items (e.g. array('a','b','c')
*/
function _convert_csv_to_array($args) {
//
// Step 1: implode(',',$args) converts from, say, array('a,','b,','c,') to 'a,,b,,c,'
// Step 2: explode(',', ...) converts to array('a','','b','','c','')
// Step 3: array_filter(...) removes the empty items
// Step 4: array_map(...) trims extra whitespace from each item
// (handles csv strings with extra whitespace, e.g. 'a, b, c')
//
return array_map('trim', array_filter(explode(',', is_array($args) ? implode(',',$args) : $args)));
}
/**
* Convert a nested array into a flat array. Thows away
* the array keys, returning only the values.
*
* @param $args
* An array that may potentially be nested.
* e.g. array('a', array('b', 'c'))
*
* @returns array
* A simple list of items (e.g. array('a','b','c')
*/
function drush_flatten_array($a) {
$result = array();
if (!is_array($a)) {
return array($a);
}
foreach ($a as $value) {
$result = array_merge($result, drush_flatten_array($value));
}
return $result;
}
/**
* Get the available global options. Used by help command. Command files may
* modify this list using hook_drush_help_alter().
*
* @param boolean $brief
* Return a reduced set of important options. Used by help command.
*
* @return
* An associative array containing the option definition as the key,
* and a descriptive array for each of the available options. The array
* elements for each item are:
*
* - short-form: The shortcut form for specifying the key on the commandline.
* - propagate: backend invoke will use drush_get_option to propagate this
* option, when set, to any other Drush command that is called.
* - context: The drush context where the value of this item is cached. Used
* by backend invoke to propagate values set in code.
* - never-post: If TRUE, backend invoke will never POST this item's value
* on STDIN; it will always be sent as a commandline option.
* - never-propagate: If TRUE, backend invoke will never pass this item on
* to the subcommand being executed.
* - local-context-only: Backend invoke will only pass this value on for local calls.
* - merge: For options such as $options['shell-aliases'] that consist of an array
* of items, make a merged array that contains all of the values specified for
* all of the contexts (config files) where the option is defined. The value is stored in
* the specified 'context', or in a context named after the option itself if the
* context flag is not specified.
* IMPORTANT: When the merge flag is used, the option value must be obtained via
* drush_get_context('option') rather than drush_get_option('option').
* - merge-pathlist: For options such as --include and --config, make a merged list
* of options from all contexts; works like the 'merge' flag, but also handles string
* values separated by the PATH_SEPARATOR.
* - merge-associative: Like 'merge-pathlist', but key values are preserved.
* - propagate-cli-value: Used to tell backend invoke to include the value for
* this item as specified on the cli. This can either override 'context'
* (e.g., propagate --include from cli value instead of DRUSH_INCLUDE context),
* or for an independent global setting (e.g. --user)
* - description: The help text for this item. displayed by `drush help`.
*/
function drush_get_global_options($brief = FALSE) {
$options['root'] = array('short-form' => 'r', 'short-has-arg' => TRUE, 'never-post' => TRUE, 'description' => "Drupal root directory to use (default: current directory).", 'example-value' => 'path');
$options['uri'] = array('short-form' => 'l', 'short-has-arg' => TRUE, 'never-post' => TRUE, 'description' => 'URI of the drupal site to use (only needed in multisite environments or when running on an alternate port).', 'example-value' => 'http://example.com:8888');
$options['verbose'] = array('short-form' => 'v', 'context' => 'DRUSH_VERBOSE', 'description' => 'Display extra information about the command.', 'symfony-conflict' => TRUE);
$options['debug'] = array('short-form' => 'd', 'context' => 'DRUSH_DEBUG', 'description' => 'Display even more information, including internal messages.');
$options['yes'] = array('short-form' => 'y', 'context' => 'DRUSH_AFFIRMATIVE', 'description' => "Assume 'yes' as answer to all prompts.");
$options['no'] = array('short-form' => 'n', 'context' => 'DRUSH_NEGATIVE', 'description' => "Assume 'no' as answer to all prompts.");
$options['simulate'] = array('short-form' => 's', 'context' => 'DRUSH_SIMULATE', 'never-propagate' => TRUE, 'description' => "Simulate all relevant actions (don't actually change the system).");
$options['pipe'] = array('short-form' => 'p', 'hidden' => TRUE, 'description' => "Emit a compact representation of the command for scripting.");
$options['help'] = array('short-form' => 'h', 'description' => "This help system.");
if (!$brief) {
$options['version'] = array('description' => "Show drush version.", 'symfony-conflict' => TRUE);
$options['php'] = array('description' => "The absolute path to your PHP intepreter, if not 'php' in the path.", 'example-value' => '/path/to/file', 'never-propagate' => TRUE);
$options['interactive'] = array('short-form' => 'ia', 'description' => "Force interactive mode for commands run on multiple targets (e.g. `drush @site1,@site2 cc --ia`).", 'never-propagate' => TRUE);
$options['tty'] = array('hidden' => TRUE, 'description' => "Force allocation of tty for remote commands", 'never-propagate' => TRUE);
$options['quiet'] = array('short-form' => 'q', 'description' => 'Suppress non-error messages.');
$options['include'] = array('short-form' => 'i', 'short-has-arg' => TRUE, 'context' => 'DRUSH_INCLUDE', 'never-post' => TRUE, 'propagate-cli-value' => TRUE, 'merge-pathlist' => TRUE, 'description' => "A list of additional directory paths to search for drush commands.", 'example-value' => '/path/dir');
$options['exclude'] = array('propagate-cli-value' => TRUE, 'never-post' => TRUE, 'merge-pathlist' => TRUE, 'description' => "A list of files and directory paths to exclude from consideration when searching for drush commandfiles.", 'example-value' => '/path/dir');
$options['config'] = array('short-form' => 'c', 'short-has-arg' => TRUE, 'context' => 'DRUSH_CONFIG', 'never-post' => TRUE, 'propagate-cli-value' => TRUE, 'merge-pathlist' => TRUE, 'description' => "Specify an additional config file to load. See example.drushrc.php.", 'example-value' => '/path/file');
$options['user'] = array('short-form' => 'u', 'short-has-arg' => TRUE, 'propagate-cli-value' => TRUE, 'description' => "Specify a Drupal user to login with. May be a name or a number.", 'example-value' => 'name_or_number');
$options['backend'] = array('short-form' => 'b', 'never-propagate' => TRUE, 'description' => "Hide all output and return structured data.");
$options['choice'] = array('description' => "Provide an answer to a multiple-choice prompt.", 'example-value' => 'number');
$options['variables'] = array('description' => "Comma delimited list of name=value pairs. These values take precedence even over settings.php variable overrides.", 'example-value' => 'foo=bar,baz=yaz');
$options['search-depth'] = array('description' => "Control the depth that drush will search for alias files.", 'example-value' => 'number');
$options['ignored-modules'] = array('description' => "Exclude some modules from consideration when searching for drush command files.", 'example-value' => 'token,views');
$options['no-label'] = array('description' => "Remove the site label that drush includes in multi-site command output (e.g. `drush @site1,@site2 status`).");
$options['label-separator'] = array('description' => "Specify the separator to use in multi-site command output (e.g. `drush @sites pm-list --label-separator=',' --format=csv`).", 'example-value' => ',');
$options['nocolor'] = array('context' => 'DRUSH_NOCOLOR', 'propagate-cli-value' => TRUE, 'description' => "Suppress color highlighting on log messages.");
$options['show-passwords'] = array('description' => "Show database passwords in commands that display connection information.");
$options['show-invoke'] = array('description' => "Show all function names which could have been called for the current command. See drush_invoke().");
$options['watchdog'] = array('description' => "Control logging of Drupal's watchdog() to drush log. Recognized values are 'log', 'print', 'disabled'. Defaults to log. 'print' shows calls to admin but does not add them to the log.", 'example-value' => 'print');
$options['cache-default-class'] = array('description' => "A cache backend class that implements CacheInterface. Defaults to JSONCache.", 'example-value' => 'JSONCache');
$options['cache-class-<bin>'] = array('description' => "A cache backend class that implements CacheInterface to use for a specific cache bin.", 'example-value' => 'className');
$options['early'] = array('description' => "Include a file (with relative or full path) and call the drush_early_hook() function (where 'hook' is the filename). The function is called pre-bootstrap and offers an opportunity to alter the drush bootstrap environment or process (returning FALSE from the function will continue the bootstrap), or return output very rapidly (e.g. from caches). See includes/complete.inc for an example.");
$options['alias-path'] = array('context' => 'ALIAS_PATH', 'local-context-only' => TRUE, 'merge-pathlist' => TRUE, 'propagate-cli-value' => TRUE, 'description' => "Specifies the list of paths where drush will search for alias files.", 'example-value' => '/path/alias1:/path/alias2');
$options['backup-location'] = array('description' => "Specifies the directory where drush will store backups.", 'example-value' => '/path/to/dir');
$options['confirm-rollback'] = array('description' => 'Wait for confirmation before doing a rollback when something goes wrong.');
$options['complete-debug'] = array('hidden' => TRUE, 'description' => "Turn on debug mode forf completion code");
$options['php-options'] = array('description' => "Options to pass to `php` when running drush. Only effective when using the drush.launcher script.", 'never-propagate' => TRUE, 'example-value' => '-d error_reporting="E_ALL"');
$options['halt-on-error'] = array('propagate-cli-value' => TRUE, 'description' => "Controls error handling of recoverable errors (E_RECOVERABLE_ERROR). --halt-on-error=1: Execution is halted. --halt-on-error=0: Drush execution continues");
$options['deferred-sanitization'] = array('hidden' => TRUE, 'description' => "Defer calculating the sanitization operations until after the database has been copied. This is done automatically if the source database is remote.");
$options['remote-host'] = array('hidden' => TRUE, 'description' => 'Remote site to execute drush command on. Managed by site alias.', 'example-value' => 'http://example.com');
$options['remote-user'] = array('hidden' => TRUE, 'description' => 'User account to use with a remote drush command. Managed by site alias.', 'example-value' => 'www-data');
$options['remote-os'] = array('hidden' => TRUE, 'description' => 'The operating system used on the remote host. Managed by site alias.', 'example-value' => 'linux');
$options['site-list'] = array('hidden' => TRUE, 'description' => 'List of sites to run commands on. Managed by site alias.', 'example-value' => '@site1,@site2');
$options['reserve-margin'] = array('hidden' => TRUE, 'description' => 'Remove columns from formatted opions. Managed by multi-site command handling.', 'example-value' => 'number');
$options['strict'] = array('propagate' => TRUE, 'description' => 'Return an error on unrecognized options. --strict=0: Allow unrecognized options. --strict=2: Also return an error on any "warning" log messages. Optional. Default is 1.');
$options['command-specific'] = array('hidden' => TRUE, 'merge-associative' => TRUE, 'description' => 'Command-specific options.');
$options['site-aliases'] = array('hidden' => TRUE, 'merge-associative' => TRUE, 'description' => 'List of site aliases.');
$options['shell-aliases'] = array('hidden' => TRUE, 'merge' => TRUE, 'never-propagate' => TRUE, 'description' => 'List of shell aliases.');
$options['path-aliases'] = array('hidden' => TRUE, 'never-propagate' => TRUE, 'description' => 'Path aliases from site alias.');
$options['ssh-options'] = array('never-propagate' => TRUE, 'description' => 'A string of extra options that will be passed to the ssh command', 'example-value' => '-p 100');
$options['editor'] = array('never-propagate' => TRUE, 'description' => 'A string of bash which launches user\'s preferred text editor. Defaults to ${VISUAL-${EDITOR-vi}}.', 'example-value' => 'vi');
$options['bg'] = array('never-propagate' => TRUE, 'description' => 'Run editor in the background. Does not work with editors such as `vi` that run in the terminal. Supresses config-import at the end.');
$options['db-url'] = array('hidden' => TRUE, 'description' => 'A Drupal 6 style database URL. Used by various commands.', 'example-value' => 'mysql://root:pass@127.0.0.1/db');
$options['drush-coverage'] = array('hidden' => TRUE, 'never-post' => TRUE, 'propagate-cli-value' => TRUE, 'description' => 'File to save code coverage data into.');
$options['redirect-port'] = array('hidden' => TRUE, 'never-propagate' => TRUE, 'description' => 'Used by the user-login command to specify the redirect port on the local machine; it therefore would not do to pass this to the remote machines.');
$options['cache-clear'] = array('propagate' => TRUE, 'description' => 'If 0, Drush skips normal cache clearing; the caller should then clear if needed.', 'example-value' => '0', );
$options['local'] = array('propagate' => TRUE, 'description' => 'Don\'t look in global locations for commandfiles, config, and site aliases');
$command = array(
'options' => $options,
'#brief' => FALSE,
) + drush_command_defaults('global-options', 'global_options', __FILE__);
drush_command_invoke_all_ref('drush_help_alter', $command);
$options = $command['options'];
}
return $options;
}
/**
* Exits with a message. In general, you should use drush_set_error() instead of
* this function. That lets drush proceed with other tasks.
* TODO: Exit with a correct status code.
*/
function drush_die($msg = NULL, $status = NULL) {
die($msg ? "drush: $msg\n" : '');
}
/**
* Check to see if the provided line is a "#!/usr/bin/env drush"
* "shebang" script line.
*/
function _drush_is_drush_shebang_line($line) {
return ((substr($line,0,2) == '#!') && (strstr($line, 'drush') !== FALSE));
}
/**
* Check to see if the provided script file is a "#!/usr/bin/env drush"
* "shebang" script line.
*/
function _drush_is_drush_shebang_script($script_filename) {
$result = FALSE;
if (file_exists($script_filename)) {
$fp = fopen($script_filename, "r");
if ($fp !== FALSE) {
$line = fgets($fp);
$result = _drush_is_drush_shebang_line($line);
fclose($fp);
}
}
return $result;
}
/**
* @defgroup userinput Get input from the user.
* @{
*/
/**
* Asks the user a basic yes/no question.
*
* @param string $msg
* The question to ask.
* @param int $indent
* The number of spaces to indent the message.
*
* @return bool
* TRUE if the user enters "y" or FALSE if "n".
*/
function drush_confirm($msg, $indent = 0) {
drush_print_prompt((string)$msg . " (y/n): ", $indent);
// Automatically accept confirmations if the --yes argument was supplied.
if (drush_get_context('DRUSH_AFFIRMATIVE')) {
drush_print("y");
return TRUE;
}
// Automatically cancel confirmations if the --no argument was supplied.
elseif (drush_get_context('DRUSH_NEGATIVE')) {
drush_print("n");
return FALSE;
}
// See http://drupal.org/node/499758 before changing this.
$stdin = fopen("php://stdin","r");
while ($line = fgets($stdin)) {
$line = trim($line);
if ($line == 'y') {
return TRUE;
}
if ($line == 'n') {
return FALSE;
}
drush_print_prompt((string)$msg . " (y/n): ", $indent);
}
}
/**
* Ask the user to select an item from a list.
* From a provided associative array, drush_choice will
* display all of the questions, numbered from 1 to N,
* and return the item the user selected. "0" is always
* cancel; entering a blank line is also interpreted
* as cancelling.
*
* @param $options
* A list of questions to display to the user. The
* KEYS of the array are the result codes to return to the
* caller; the VALUES are the messages to display on
* each line. Special keys of the form '-- something --' can be
* provided as separator between choices groups. Separator keys
* don't alter the numbering.
* @param $prompt
* The message to display to the user prompting for input.
* @param $label
* Controls the display of each line. Defaults to
* '!value', which displays the value of each item
* in the $options array to the user. Use '!key' to
* display the key instead. In some instances, it may
* be useful to display both the key and the value; for
* example, if the key is a user id and the value is the
* user name, use '!value (uid=!key)'.
*/
function drush_choice($options, $prompt = 'Enter a number.', $label = '!value', $widths = array()) {
drush_print(dt($prompt));
// Preflight so that all rows will be padded out to the same number of columns
$array_pad = 0;
foreach ($options as $key => $option) {
if (is_array($option) && (count($option) > $array_pad)) {
$array_pad = count($option);
}
}
$rows[] = array_pad(array('[0]', ':', 'Cancel'), $array_pad + 2, '');
$selection_number = 0;
foreach ($options as $key => $option) {
if ((substr($key, 0, 3) == '-- ') && (substr($key, -3) == ' --')) {
$rows[] = array_pad(array('', '', $option), $array_pad + 2, '');
}
else {
$selection_number++;
$row = array("[$selection_number]", ':');
if (is_array($option)) {
$row = array_merge($row, $option);
}
else {
$row[] = dt($label, array('!number' => $selection_number, '!key' => $key, '!value' => $option));
}
$rows[] = $row;
$selection_list[$selection_number] = $key;
}
}
drush_print_table($rows, FALSE, $widths);
drush_print_pipe(array_keys($options));
// If the user specified --choice, then make an
// automatic selection. Cancel if the choice is
// not an available option.
if (($choice = drush_get_option('choice', FALSE)) !== FALSE) {
// First check to see if $choice is one of the symbolic options
if (array_key_exists($choice, $options)) {
return $choice;
}
// Next handle numeric selections
elseif (array_key_exists($choice, $selection_list)) {
return $selection_list[$choice];
}
return FALSE;
}
// If the user specified --no, then cancel; also avoid
// getting hung up waiting for user input in --pipe and
// backend modes. If none of these apply, then wait,
// for user input and return the selected result.
if (!drush_get_context('DRUSH_NEGATIVE') && !drush_get_context('DRUSH_AFFIRMATIVE') && !drush_get_context('DRUSH_PIPE')) {
while ($line = trim(fgets(STDIN))) {
if (array_key_exists($line, $selection_list)) {
return $selection_list[$line];
}
}
}
// We will allow --yes to confirm input if there is only
// one choice; otherwise, --yes will cancel to avoid ambiguity
if (drush_get_context('DRUSH_AFFIRMATIVE') && (count($options) == 1)) {
return $selection_list[1];
}
return FALSE;
}
/**
* Ask the user to select multiple items from a list.
* This is a wrapper around drush_choice, that repeats the selection process,
* allowing users to toggle a number of items in a list. The number of values
* that can be constrained by both min and max: the user will only be allowed
* finalize selection once the minimum number has been selected, and the oldest
* selected value will "drop off" the list, if they exceed the maximum number.
*
* @param $options
* Same as drush_choice() (see above).
* @param $defaults
* This can take 3 forms:
* - FALSE: (Default) All options are unselected by default.
* - TRUE: All options are selected by default.
* - Array of $options keys to be selected by default.
* @param $prompt
* Same as drush_choice() (see above).
* @param $label
* Same as drush_choice() (see above).
* @param $mark
* Controls how selected values are marked. Defaults to '!value (selected)'.
* @param $min
* Constraint on minimum number of selections. Defaults to zero. When fewer
* options than this are selected, no final options will be available.
* @param $max
* Constraint on minimum number of selections. Defaults to NULL (unlimited).
* If the a new selection causes this value to be exceeded, the oldest
* previously selected value is automatically unselected.
* @param $final_options
* An array of additional options in the same format as $options.
* When the minimum number of selections is met, this array is merged into the
* array of options. If the user selects one of these values and the
* selection process will complete (the key for the final option is included
* in the return value). If this is an empty array (default), then a built in
* final option of "Done" will be added to the available options (in this case
* no additional keys are added to the return value).
*/
function drush_choice_multiple($options, $defaults = FALSE, $prompt = 'Select some numbers.', $label = '!value', $mark = '!value (selected)', $min = 0, $max = NULL, $final_options = array()) {
$selections = array();
// Load default selections.
if (is_array($defaults)) {
$selections = $defaults;
}
elseif ($defaults === TRUE) {
$selections = array_keys($options);
}
$complete = FALSE;
$final_builtin = array();
if (empty($final_options)) {
$final_builtin['done'] = dt('Done');
}
$final_options_keys = array_keys($final_options);
while (TRUE) {
$current_options = $options;
// Mark selections.
foreach ($selections as $selection) {
$current_options[$selection] = dt($mark, array('!key' => $selection, '!value' => $options[$selection]));
}
// Add final options, if the minimum number of selections has been reached.
if (count($selections) >= $min) {
$current_options = array_merge($current_options, $final_options, $final_builtin);
}
$toggle = drush_choice($current_options, $prompt, $label);
if ($toggle === FALSE) {
return FALSE;
}
// Don't include the built in final option in the return value.
if (count($selections) >= $min && empty($final_options) && $toggle == 'done') {
return $selections;
}
// Toggle the selected value.
$item = array_search($toggle, $selections);
if ($item === FALSE) {
array_unshift($selections, $toggle);
}
else {
unset($selections[$item]);
}
// If the user selected one of the final options, return.
if (count($selections) >= $min && in_array($toggle, $final_options_keys)) {
return $selections;
}
// If the user selected too many options, drop the oldest selection.
if (isset($max) && count($selections) > $max) {
array_pop($selections);
}
}
}
/**
* Prompt the user for input
*
* The input can be anything that fits on a single line (not only y/n),
* so we can't use drush_confirm()
*
* @param $prompt
* The text which is displayed to the user.
* @param $default
* The default value of the input.
* @param $required
* If TRUE, user may continue even when no value is in the input.
* @param $password
* If TRUE, surpress printing of the input.
*
* @see drush_confirm()
*/
function drush_prompt($prompt, $default = NULL, $required = TRUE, $password = FALSE) {
if (isset($default)) {
$prompt .= " [" . $default . "]";
}
$prompt .= ": ";
drush_print_prompt($prompt);
if (drush_get_context('DRUSH_AFFIRMATIVE')) {
return $default;
}
$stdin = fopen('php://stdin', 'r');
if ($password) drush_shell_exec("stty -echo");
stream_set_blocking($stdin, TRUE);
while (($line = fgets($stdin)) !== FALSE) {
$line = trim($line);
if ($line === "") {
$line = $default;
}
if ($line || !$required) {
break;
}
drush_print_prompt($prompt);
}
fclose($stdin);
if ($password) {
drush_shell_exec("stty echo");
print "\n";
}
return $line;
}
/**
* @} End of "defgroup userinput".
*/
/**
* Calls a given function, passing through all arguments unchanged.
*
* This should be used when calling possibly mutative or destructive functions
* (e.g. unlink() and other file system functions) so that can be suppressed
* if the simulation mode is enabled.
*
* Important: Call @see drush_op_system() to execute a shell command,
* or @see drush_shell_exec() to execute a shell command and capture the
* shell output.
*
* @param $callable
* The name of the function. Any additional arguments are passed along.
* @return
* The return value of the function, or TRUE if simulation mode is enabled.
*
*/
function drush_op($callable) {
$args_printed = array();
$args = func_get_args();
array_shift($args); // Skip function name
foreach ($args as $arg) {
$args_printed[] = is_scalar($arg) ? $arg : (is_array($arg) ? 'Array' : 'Object');
}
if (!is_array($callable)) {
$callable_string = $callable;
}
else {
if (is_object($callable[0])) {
$callable_string = get_class($callable[0]) . '::' . $callable[1];
}
else {
$callable_string = implode('::', $callable);
}
}
// Special checking for drush_op('system')
if ($callable == 'system') {
drush_log(dt("Do not call drush_op('system'); use drush_op_system instead"), LogLevel::DEBUG);
}
if (drush_get_context('DRUSH_VERBOSE') || drush_get_context('DRUSH_SIMULATE')) {
drush_log(sprintf("Calling %s(%s)", $callable_string, implode(", ", $args_printed)), LogLevel::DEBUG);
}
if (drush_get_context('DRUSH_SIMULATE')) {
return TRUE;
}
return drush_call_user_func_array($callable, $args);
}
/**
* Mimic cufa but still call function directly. See http://drupal.org/node/329012#comment-1260752
*/
function drush_call_user_func_array($function, $args = array() ) {
if (is_array($function)) {
// $callable is a method so always use CUFA.
return call_user_func_array($function, $args);
}
switch (count($args)) {
case 0: return $function(); break;
case 1: return $function($args[0]); break;
case 2: return $function($args[0], $args[1]); break;
case 3: return $function($args[0], $args[1], $args[2]); break;
case 4: return $function($args[0], $args[1], $args[2], $args[3]); break;
case 5: return $function($args[0], $args[1], $args[2], $args[3], $args[4]); break;
case 6: return $function($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]); break;
case 7: return $function($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6]); break;
case 8: return $function($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7]); break;
case 9: return $function($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7], $args[8]); break;
default: return call_user_func_array($function,$args);
}
}
/**
* Download a file using wget, curl or file_get_contents, or via download cache.
*
* @param string $url
* The url of the file to download.
* @param string $destination
* The name of the file to be saved, which may include the full path.
* Optional, if omitted the filename will be extracted from the url and the
* file downloaded to the current working directory (Drupal root if
* bootstrapped).
* @param integer $cache_duration
* The acceptable age of a cached file. If cached file is too old, a fetch
* will occur and cache will be updated. Optional, if ommitted the file will
* be fetched directly.
*
* @return string
* The path to the downloaded file, or FALSE if the file could not be
* downloaded.
*/
function drush_download_file($url, $destination = FALSE, $cache_duration = 0) {
// Generate destination if omitted.
if (!$destination) {
$file = basename(current(explode('?', $url, 2)));
$destination = getcwd() . '/' . basename($file);
}
// Simply copy local files to the destination
if (!_drush_is_url($url)) {
return copy($url, $destination) ? $destination : FALSE;
}
if ($cache_duration !== 0 && $cache_file = drush_download_file_name($url)) {
// Check for cached, unexpired file.
if (file_exists($cache_file) && filectime($cache_file) > ($_SERVER['REQUEST_TIME']-$cache_duration)) {
drush_log(dt('!name retrieved from cache.', array('!name' => $cache_file)));
}
else {
if (_drush_download_file($url, $cache_file, TRUE)) {
// Cache was set just by downloading file to right location.
}
elseif (file_exists($cache_file)) {
drush_log(dt('!name retrieved from an expired cache since refresh failed.', array('!name' => $cache_file)), LogLevel::WARNING);
}
else {
$cache_file = FALSE;
}
}
if ($cache_file && copy($cache_file, $destination)) {
// Copy cached file to the destination
return $destination;
}
}
elseif ($return = _drush_download_file($url, $destination)) {
drush_register_file_for_deletion($return);
return $return;
}
// Unable to retrieve from cache nor download.
return FALSE;
}
/**
* Helper function to determine name of cached file.
*/
function drush_download_file_name($url) {
if ($cache_dir = drush_directory_cache('download')) {
$cache_name = str_replace(array(':', '/', '?', '=', '\\'), '-', $url);
return $cache_dir . "/" . $cache_name;
}
else {
return FALSE;
}
}
/**
* Check whether the given path is just a url or a local path
* @param string $url
* @return boolean
* TRUE if the path does not contain a schema:// part.
*/
function _drush_is_url($url) {
return parse_url($url, PHP_URL_SCHEME) !== NULL;
}
/**
* Download a file using wget, curl or file_get_contents. Does not use download
* cache.
*
* @param string $url
* The url of the file to download.
* @param string $destination
* The name of the file to be saved, which may include the full path.
* @param boolean $overwrite
* Overwrite any file thats already at the destination.
* @return string
* The path to the downloaded file, or FALSE if the file could not be
* downloaded.
*/
function _drush_download_file($url, $destination, $overwrite = TRUE) {
static $use_wget;
if ($use_wget === NULL) {
$use_wget = drush_shell_exec('wget --version');
}
$destination_tmp = drush_tempnam('download_file');
if ($use_wget) {
drush_shell_exec("wget -q --timeout=30 -O %s %s", $destination_tmp, $url);
}
else {
// Force TLS1+ as per https://github.com/drush-ops/drush/issues/894.
drush_shell_exec("curl --tlsv1 --fail -s -L --connect-timeout 30 -o %s %s", $destination_tmp, $url);
}
if (!drush_file_not_empty($destination_tmp) && $file = @file_get_contents($url)) {
@file_put_contents($destination_tmp, $file);
}
if (!drush_file_not_empty($destination_tmp)) {
// Download failed.
return FALSE;
}
drush_move_dir($destination_tmp, $destination, $overwrite);
return $destination;
}
/**
* Determines the MIME content type of the specified file.
*
* The power of this function depends on whether the PHP installation
* has either mime_content_type() or finfo installed -- if not, only tar,
* gz, zip and bzip2 types can be detected.
*
* If mime type can't be obtained, an error will be set.
*
* @return mixed
* The MIME content type of the file or FALSE.
*/
function drush_mime_content_type($filename) {
$content_type = drush_attempt_mime_content_type($filename);
if ($content_type) {
drush_log(dt('Mime type for !file is !mt', array('!file' => $filename, '!mt' => $content_type)), LogLevel::INFO);
return $content_type;
}
return drush_set_error('MIME_CONTENT_TYPE_UNKNOWN', dt('Unable to determine mime type for !file.', array('!file' => $filename)));
}
/**
* Works like drush_mime_content_type, but does not set an error
* if the type is unknown.
*/
function drush_attempt_mime_content_type($filename) {
$content_type = FALSE;
if (class_exists('finfo')) {
$finfo = new finfo(FILEINFO_MIME_TYPE);
$content_type = $finfo->file($filename);
if ($content_type == 'application/octet-stream') {
drush_log(dt('Mime type for !file is application/octet-stream.', array('!file' => $filename)), LogLevel::DEBUG);
$content_type = FALSE;
}
}
// If apache is configured in such a way that all files are considered
// octet-stream (e.g with mod_mime_magic and an http conf that's serving all
// archives as octet-stream for other reasons) we'll detect mime types on our
// own by examing the file's magic header bytes.
if (!$content_type) {
drush_log(dt('Examining !file headers.', array('!file' => $filename)), LogLevel::DEBUG);
if ($file = fopen($filename, 'rb')) {
$first = fread($file, 2);
fclose($file);
if ($first !== FALSE) {
// Interpret the two bytes as a little endian 16-bit unsigned int.
$data = unpack('v', $first);
switch ($data[1]) {
case 0x8b1f:
// First two bytes of gzip files are 0x1f, 0x8b (little-endian).
// See http://www.gzip.org/zlib/rfc-gzip.html#header-trailer
$content_type = 'application/x-gzip';
break;
case 0x4b50:
// First two bytes of zip files are 0x50, 0x4b ('PK') (little-endian).
// See http://en.wikipedia.org/wiki/Zip_(file_format)#File_headers
$content_type = 'application/zip';
break;
case 0x5a42:
// First two bytes of bzip2 files are 0x5a, 0x42 ('BZ') (big-endian).
// See http://en.wikipedia.org/wiki/Bzip2#File_format
$content_type = 'application/x-bzip2';
break;
default:
drush_log(dt('Unable to determine mime type from header bytes 0x!hex of !file.', array('!hex' => dechex($data[1]), '!file' => $filename,), LogLevel::DEBUG));
}
}
else {
drush_log(dt('Unable to read !file.', array('!file' => $filename)), LogLevel::WARNING);
}
}
else {
drush_log(dt('Unable to open !file.', array('!file' => $filename)), LogLevel::WARNING);
}
}
// 3. Lastly if above methods didn't work, try to guess the mime type from
// the file extension. This is useful if the file has no identificable magic
// header bytes (for example tarballs).
if (!$content_type) {
drush_log(dt('Examining !file extension.', array('!file' => $filename)), LogLevel::DEBUG);
// Remove querystring from the filename, if present.
$filename = basename(current(explode('?', $filename, 2)));
$extension_mimetype = array(
'.tar' => 'application/x-tar',
'.sql' => 'application/octet-stream',
);
foreach ($extension_mimetype as $extension => $ct) {
if (substr($filename, -strlen($extension)) === $extension) {
$content_type = $ct;
break;
}
}
}
return $content_type;
}
/**
* Check whether a file is a supported tarball.
*
* @return mixed
* The file content type if it's a tarball. FALSE otherwise.
*/
function drush_file_is_tarball($path) {
$content_type = drush_attempt_mime_content_type($path);
$supported = array(
'application/x-bzip2',
'application/x-gzip',
'application/x-tar',
'application/x-zip',
'application/zip',
);
if (in_array($content_type, $supported)) {
return $content_type;
}
return FALSE;
}
/**
* Extract a tarball.
*
* @param string $path
* Path to the archive to be extracted.
* @param string $destination
* The destination directory the tarball should be extracted into.
* Optional, if ommitted the tarball directory will be used as destination.
* @param boolean $listing
* If TRUE, a listing of the tar contents will be returned on success.
* @param string $tar_extra_options
* Extra options to be passed to the tar command.
*
* @return mixed
* TRUE on success, FALSE on fail. If $listing is TRUE, a file listing of the