Version 1
[yaffs-website] / vendor / drush / drush / commands / core / usage.drush.inc
1 <?php
2
3 /**
4  * @file
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.
7  */
8
9 use Drush\Log\LogLevel;
10
11 /**
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;
16 */
17
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,
23     'hidden' => TRUE,
24     'examples' => array(
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.',
27     ),
28     'aliases' => array('ushow'),
29   );
30   $items['usage-send'] = array(
31     'bootstrap' => DRUSH_BOOTSTRAP_NONE,
32     'hidden' => TRUE,
33     'description' => 'Send anonymous Drush usage information to statistics logging site.  ' . $disclaimer,
34     'examples' => array(
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.',
38     ),
39     'aliases' => array('usend'),
40   );
41   return $items;
42 }
43
44 /**
45  * Log and/or send usage data to Mongolab.
46  *
47  * An organization can implement own hook_drush_exit() to send data to a
48  * different endpoint.
49  */
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);
56     }
57     if (drush_get_option('drush_usage_send', FALSE)) {
58       _drush_usage_mongolab($file, drush_get_option('drush_usage_size', 51200));
59     }
60   }
61 }
62
63 /**
64  * Set option to send usage to Mongolab.
65  *
66  * See usage_drush_exit() for more information.
67  */
68 function drush_usage_send() {
69   $file = _drush_usage_get_file(TRUE);
70   if ($file) {
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;'));
74   }
75   else {
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.'));
77   }
78 }
79
80 /**
81  * Displays usage file.
82  */
83 function drush_usage_show() {
84   $file = _drush_usage_get_file(TRUE);
85   if ($file) {
86     $json = '[' . file_get_contents($file) . ']';
87     $usage_data = json_decode($json);
88     foreach ($usage_data as $item) {
89       $cmd = $item->cmd;
90       $options = (array) $item->opt;
91       array_unshift($options, '');
92       drush_print($cmd . implode(' --', $options));
93     }
94   }
95   else {
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.'));
97   }
98 }
99
100 /**
101  * Returns path to usage file.
102  */
103 function _drush_usage_get_file($required = FALSE) {
104   $file = drush_directory_cache('usage') . '/usage.txt';
105   if (!file_exists($file) && $required) {
106     return FALSE;
107
108   }
109   return $file;
110 }
111
112 function _drush_usage_log($command, $file) {
113   $options = drush_get_command_options_extended($command);
114
115   $used = drush_get_merged_options();
116   $command_specific = array_intersect(array_keys($used), array_keys($options));
117   $record = array(
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()),
125   );
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);
129   }
130   else {
131     drush_log(dt('Failed to log command and option names to local cache.'), LogLevel::DEBUG);
132   }
133 }
134
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.
142     $database = 'usage';
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);
150         return FALSE;
151       }
152     }
153     drush_log(dt('Drush usage statistics successfully posted.'), LogLevel::DEBUG);
154     // Empty the usage.txt file.
155     unlink($file);
156     return TRUE;
157   }
158 }