usage.drush.inc

  1. 8.0.x commands/core/usage.drush.inc
  2. 6.x commands/core/usage.drush.inc
  3. 7.x commands/core/usage.drush.inc
  4. 5.x commands/core/usage.drush.inc

Send scrubbed usage data to drush. Omits arguments and option values in order to assure that no sensitive data is shared. See http://drupal.org/node/1246738.

Functions

Namesort descending Description
drush_usage_send Set option to send usage to Mongolab.
drush_usage_show Displays usage file.
usage_drush_command
usage_drush_exit Log and/or send usage data to Mongolab.
_drush_usage_get_file Returns path to usage file.
_drush_usage_log
_drush_usage_mongolab

File

commands/core/usage.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Send scrubbed usage data to drush. Omits arguments and option values in order
  5. * to assure that no sensitive data is shared. See http://drupal.org/node/1246738.
  6. */
  7. use Drush\Log\LogLevel;
  8. /**
  9. * To send usage data, add the following to a .drushrc.php file:
  10. * $options['drush_usage_log'] = TRUE;
  11. * $options['drush_usage_send'] = TRUE;
  12. * $options['drush_usage_size'] = 51200;
  13. */
  14. function usage_drush_command() {
  15. $disclaimer = 'Usage statistics contain the Drush command name and the Drush option names, but no arguments or option values.';
  16. $items['usage-show'] = array(
  17. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  18. 'description' => 'Show Drush usage information that has been logged but not sent. ' . $disclaimer,
  19. 'examples' => array(
  20. 'drush usage-show' => 'Show cached usage statistics.',
  21. '$options[\'drush_usage_log\'] = TRUE;' => 'Specify in a .drushrc.php file that usage information should be logged locally in a usage statistics file.',
  22. ),
  23. 'aliases' => array('ushow'),
  24. );
  25. $items['usage-send'] = array(
  26. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  27. 'description' => 'Send anonymous Drush usage information to statistics logging site. ' . $disclaimer,
  28. 'examples' => array(
  29. 'drush usage-send' => 'Immediately send cached usage statistics.',
  30. '$options[\'drush_usage_send\'] = TRUE;' => 'Specify in a .drushrc.php file that usage information should be sent.',
  31. '$options[\'drush_usage_size\'] = 10240;' => 'Specify the frequency (file size) that usage information should be sent.',
  32. ),
  33. 'aliases' => array('usend'),
  34. );
  35. return $items;
  36. }
  37. /**
  38. * Log and/or send usage data to Mongolab.
  39. *
  40. * An organization can implement own hook_drush_exit() to send data to a
  41. * different endpoint.
  42. */
  43. function usage_drush_exit() {
  44. // Ignore statistics for simulated commands. (n.b. in simulated mode, _drush_usage_mongolab will print rather than send statistics)
  45. if (!drush_get_context('DRUSH_SIMULATE')) {
  46. $file = _drush_usage_get_file();
  47. if (drush_get_option('drush_usage_log', FALSE)) {
  48. _drush_usage_log(drush_get_command(), $file);
  49. }
  50. if (drush_get_option('drush_usage_send', FALSE)) {
  51. _drush_usage_mongolab($file, drush_get_option('drush_usage_size', 51200));
  52. }
  53. }
  54. }
  55. /**
  56. * Set option to send usage to Mongolab.
  57. *
  58. * See usage_drush_exit() for more information.
  59. */
  60. function drush_usage_send() {
  61. $file = _drush_usage_get_file(TRUE);
  62. if ($file) {
  63. drush_set_option('drush_usage_send', TRUE);
  64. drush_set_option('drush_usage_size', 0);
  65. drush_print(dt('To automatically send anonymous usage data, add the following to a .drushrc.php file: $options[\'drush_usage_send\'] = TRUE;'));
  66. }
  67. else {
  68. 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.'));
  69. }
  70. }
  71. /**
  72. * Displays usage file.
  73. */
  74. function drush_usage_show() {
  75. $file = _drush_usage_get_file(TRUE);
  76. if ($file) {
  77. $json = '[' . file_get_contents($file) . ']';
  78. $usage_data = json_decode($json);
  79. foreach ($usage_data as $item) {
  80. $cmd = $item->cmd;
  81. $options = (array) $item->opt;
  82. array_unshift($options, '');
  83. drush_print($cmd . implode(' --', $options));
  84. }
  85. }
  86. else {
  87. 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.'));
  88. }
  89. }
  90. /**
  91. * Returns path to usage file.
  92. */
  93. function _drush_usage_get_file($required = FALSE) {
  94. $file = drush_directory_cache('usage') . '/usage.txt';
  95. if (!file_exists($file) && $required) {
  96. return FALSE;
  97. }
  98. return $file;
  99. }
  100. function _drush_usage_log($command, $file) {
  101. drush_merge_engine_data($command);
  102. // Start out with just the options in the current command record.
  103. $options = _drush_get_command_options($command);
  104. // If 'allow-additional-options' contains a list of command names,
  105. // then union together all of the options from all of the commands.
  106. if (is_array($command['allow-additional-options'])) {
  107. $implemented = drush_get_commands();
  108. foreach ($command['allow-additional-options'] as $subcommand_name) {
  109. if (array_key_exists($subcommand_name, $implemented)) {
  110. $options = array_merge($options, _drush_get_command_options($implemented[$subcommand_name]));
  111. }
  112. }
  113. }
  114. $used = drush_get_merged_options();
  115. $command_specific = array_intersect(array_keys($used), array_keys($options));
  116. $record = array(
  117. 'date' => $_SERVER['REQUEST_TIME'],
  118. 'cmd' => $command['command'],
  119. 'opt' => $command_specific,
  120. 'major' => DRUSH_MAJOR_VERSION,
  121. 'minor' => DRUSH_MINOR_VERSION,
  122. 'os' => php_uname('s'),
  123. 'host' => md5(php_uname('n') . get_current_user()),
  124. );
  125. $prequel = (file_exists($file)) ? ",\n" : "";
  126. if (file_put_contents($file, $prequel . json_encode($record), FILE_APPEND)) {
  127. drush_log(dt('Logged command and option names to local cache.'), LogLevel::DEBUG);
  128. }
  129. else {
  130. drush_log(dt('Failed to log command and option names to local cache.'), LogLevel::DEBUG);
  131. }
  132. }
  133. // We only send data periodically to save network traffic and delay. Files
  134. // are sent once they grow over 50KB (configurable).
  135. function _drush_usage_mongolab($file, $min_size_to_send) {
  136. $json = '[' . file_get_contents($file) . ']';
  137. if (filesize($file) > $min_size_to_send) {
  138. $base = 'https://api.mongolab.com/api/1';
  139. $apikey = '4eb95456e4b0bcd285d8135d'; // submitter account.
  140. $database = 'usage';
  141. $collection = 'usage';
  142. $action = "/databases/$database/collections/$collection";
  143. $url = $base . $action . "?apiKey=$apikey";
  144. $header = 'Content-Type: application/json';
  145. if (!drush_shell_exec("wget -q -O - --no-check-certificate --timeout=20 --header=\"$header\" --post-data %s %s", $json, $url)) {
  146. if (!drush_shell_exec("curl -s --connect-timeout 20 --header \"$header\" --data %s %s", $json, $url)) {
  147. drush_log(dt('Drush usage statistics failed to post.'), LogLevel::DEBUG);
  148. return FALSE;
  149. }
  150. }
  151. drush_log(dt('Drush usage statistics successfully posted.'), LogLevel::DEBUG);
  152. // Empty the usage.txt file.
  153. unlink($file);
  154. return TRUE;
  155. }
  156. }