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

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