5 * Send scrubbed usage data to drush. Omits arguments and option values in order
6 * to assure that no sensitive data is shared. See http://drupal.org/node/1246738.
9 use Drush\Log\LogLevel;
12 * To send usage data, add the following to a .drushrc.php file:
13 * $options['drush_usage_log'] = TRUE;
14 * $options['drush_usage_send'] = TRUE;
15 * $options['drush_usage_size'] = 51200;
18 function usage_drush_command() {
19 $disclaimer = 'Usage statistics contain the Drush command name and the Drush option names, but no arguments or option values.';
20 $items['usage-show'] = array(
21 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
22 'description' => 'Show Drush usage information that has been logged but not sent. ' . $disclaimer,
25 'drush usage-show' => 'Show cached usage statistics.',
26 '$options[\'drush_usage_log\'] = TRUE;' => 'Specify in a .drushrc.php file that usage information should be logged locally in a usage statistics file.',
28 'aliases' => array('ushow'),
30 $items['usage-send'] = array(
31 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
33 'description' => 'Send anonymous Drush usage information to statistics logging site. ' . $disclaimer,
35 'drush usage-send' => 'Immediately send cached usage statistics.',
36 '$options[\'drush_usage_send\'] = TRUE;' => 'Specify in a .drushrc.php file that usage information should be sent.',
37 '$options[\'drush_usage_size\'] = 10240;' => 'Specify the frequency (file size) that usage information should be sent.',
39 'aliases' => array('usend'),
45 * Log and/or send usage data to Mongolab.
47 * An organization can implement own hook_drush_exit() to send data to a
50 function usage_drush_exit() {
51 // Ignore statistics for simulated commands. (n.b. in simulated mode, _drush_usage_mongolab will print rather than send statistics)
52 if (!drush_get_context('DRUSH_SIMULATE')) {
53 $file = _drush_usage_get_file();
54 if (drush_get_option('drush_usage_log', FALSE)) {
55 _drush_usage_log(drush_get_command(), $file);
57 if (drush_get_option('drush_usage_send', FALSE)) {
58 _drush_usage_mongolab($file, drush_get_option('drush_usage_size', 51200));
64 * Set option to send usage to Mongolab.
66 * See usage_drush_exit() for more information.
68 function drush_usage_send() {
69 $file = _drush_usage_get_file(TRUE);
71 drush_set_option('drush_usage_send', TRUE);
72 drush_set_option('drush_usage_size', 0);
73 drush_print(dt('To automatically send anonymous usage data, add the following to a .drushrc.php file: $options[\'drush_usage_send\'] = TRUE;'));
76 return drush_set_error('DRUSH_NO_USAGE_FILE', dt('No usage file; set $options[\'drush_usage_log\'] = TRUE; in a .drushrc.php file to enable.'));
81 * Displays usage file.
83 function drush_usage_show() {
84 $file = _drush_usage_get_file(TRUE);
86 $json = '[' . file_get_contents($file) . ']';
87 $usage_data = json_decode($json);
88 foreach ($usage_data as $item) {
90 $options = (array) $item->opt;
91 array_unshift($options, '');
92 drush_print($cmd . implode(' --', $options));
96 return drush_set_error('DRUSH_NO_USAGE_FILE', dt('No usage file; set $options[\'drush_usage_log\'] = TRUE; in a .drushrc.php file to enable.'));
101 * Returns path to usage file.
103 function _drush_usage_get_file($required = FALSE) {
104 $file = drush_directory_cache('usage') . '/usage.txt';
105 if (!file_exists($file) && $required) {
112 function _drush_usage_log($command, $file) {
113 $options = drush_get_command_options_extended($command);
115 $used = drush_get_merged_options();
116 $command_specific = array_intersect(array_keys($used), array_keys($options));
118 'date' => $_SERVER['REQUEST_TIME'],
119 'cmd' => $command['command'],
120 'opt' => $command_specific,
121 'major' => DRUSH_MAJOR_VERSION,
122 'minor' => DRUSH_MINOR_VERSION,
123 'os' => php_uname('s'),
124 'host' => md5(php_uname('n') . get_current_user()),
126 $prequel = (file_exists($file)) ? ",\n" : "";
127 if (file_put_contents($file, $prequel . json_encode($record), FILE_APPEND)) {
128 drush_log(dt('Logged command and option names to local cache.'), LogLevel::DEBUG);
131 drush_log(dt('Failed to log command and option names to local cache.'), LogLevel::DEBUG);
135 // We only send data periodically to save network traffic and delay. Files
136 // are sent once they grow over 50KB (configurable).
137 function _drush_usage_mongolab($file, $min_size_to_send) {
138 $json = '[' . file_get_contents($file) . ']';
139 if (filesize($file) > $min_size_to_send) {
140 $base = 'https://api.mongolab.com/api/1';
141 $apikey = '4eb95456e4b0bcd285d8135d'; // submitter account.
143 $collection = 'usage';
144 $action = "/databases/$database/collections/$collection";
145 $url = $base . $action . "?apiKey=$apikey";
146 $header = 'Content-Type: application/json';
147 if (!drush_shell_exec("wget -q -O - --no-check-certificate --timeout=20 --header=\"$header\" --post-data %s %s", $json, $url)) {
148 if (!drush_shell_exec("curl -s --connect-timeout 20 --header \"$header\" --data %s %s", $json, $url)) {
149 drush_log(dt('Drush usage statistics failed to post.'), LogLevel::DEBUG);
153 drush_log(dt('Drush usage statistics successfully posted.'), LogLevel::DEBUG);
154 // Empty the usage.txt file.