webform.drush.inc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * @file
  4. * Functions relating to Drush integration.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function webform_drush_command() {
  10. return array(
  11. 'webform-export' => array(
  12. 'description' => 'Exports webform data to a file.',
  13. 'arguments' => array(
  14. 'nid' => 'The node ID of the webform you want to export (required)',
  15. ),
  16. 'options' => array(
  17. 'file' => 'The file path to export to (defaults to print to stdout)',
  18. 'format' => 'The exporter format to use. Out-of-the-box this may be "delimited" or "excel".',
  19. 'delimiter' => 'Delimiter between columns (defaults to site-wide setting). This option may need to be wrapped in quotes. i.e. --delimter="\t".',
  20. 'components' => 'Comma-separated list of component IDs or form keys to include.' . "\n" .
  21. 'May also include "webform_serial", "webform_sid", "webform_time", "webform_complete_time", "webform_modified_time", "webform_draft", "webform_ip_address", "webform_uid", and "webform_username".',
  22. 'header-keys' => 'Integer -1 for none, 0 for label (default) or 1 for form key.',
  23. 'select-keys' => 'Integer 0 or 1 value. Set to 1 to print select list values by their form keys instead of labels.',
  24. 'select-format' => 'Set to "separate" (default) or "compact" to determine how select list values are exported.',
  25. 'range-type' => 'Range of submissions to export: "all", "new", "latest", "range" (by sid, default if start is supplied), "range-serial", or "range-date".',
  26. 'range-latest' => 'Integer specifying the latest X submissions will be downloaded. Used if "range-type" is "latest" or no other range options are provided.',
  27. 'range-start' => 'The submission ID, serial number, or start date at which to start exporting.',
  28. 'range-end' => 'The submission ID, serial number, or end date at which to end exporting.',
  29. 'completion-type' => 'Submissions to be included: "finished", "draft" or "all" (default).',
  30. 'batch-size' => 'The size of batches in rows (default 10000). If encountering out of memory errors, set this number lower to export fewer submissions per batch.',
  31. ),
  32. 'aliases' => array('wfx'),
  33. ),
  34. 'webform-clear' => array(
  35. 'description' => 'Clear a webform by deleting all its submissions.',
  36. 'arguments' => array(
  37. 'nid' => 'The node ID of the webform you want to clear (required)',
  38. ),
  39. 'options' => array(
  40. 'batch-size' => 'The size of batches in rows (default 10000). If encountering out of memory errors, set this number lower to export fewer submissions per batch.',
  41. ),
  42. ),
  43. );
  44. }
  45. /**
  46. * Exports a webform via drush.
  47. *
  48. * This is useful for large data dumps that would otherwise time out due to
  49. * memory consumption.
  50. *
  51. * @param bool|int $nid
  52. * Node ID of the webform that we want to export.
  53. *
  54. * @return false|null
  55. * The value returned from drush_set_error() or NULL if that function is not
  56. * called.
  57. */
  58. function drush_webform_export($nid = FALSE) {
  59. if (!$nid) {
  60. return drush_set_error('The node ID of the webform you want to export is required.');
  61. }
  62. $node = node_load($nid);
  63. if (!$node) {
  64. return drush_set_error(dt('Node !nid was not found.', array('!nid' => $nid)));
  65. }
  66. module_load_include('inc', 'webform', 'includes/webform.submissions');
  67. module_load_include('inc', 'webform', 'includes/webform.export');
  68. module_load_include('inc', 'webform', 'includes/webform.components');
  69. module_load_include('inc', 'webform', 'includes/webform.report');
  70. // Pull in options from drush to override the defaults.
  71. $format = drush_get_option('format', 'delimited');
  72. $options = webform_results_download_default_options($node, $format);
  73. foreach ($options as $option_name => $option_value) {
  74. $options[$option_name] = drush_get_option(str_replace('_', '-', $option_name), $option_value);
  75. }
  76. $options['components'] = is_array($options['components']) ? $options['components'] : explode(',', $options['components']);
  77. // Map form keys to cids.
  78. $form_keys = array();
  79. foreach ($node->webform['components'] as $cid => $component) {
  80. $form_keys[$component['form_key']] = $cid;
  81. }
  82. foreach ($options['components'] as $key => &$component) {
  83. if (isset($form_keys[$component])) {
  84. $component = $form_keys[$component];
  85. }
  86. }
  87. // Drop PHP reference.
  88. unset($component);
  89. // Get the range options.
  90. unset($options['range']['range_type']);
  91. foreach (drush_get_merged_prefixed_options('range-') as $option_name => $option_value) {
  92. if ($option_name == 'type' && in_array($option_value, array('all', 'new', 'latest', 'range', 'range-serial', 'range-date'))) {
  93. $options['range']['range_type'] = str_replace('-', '_', $option_value);
  94. }
  95. elseif (in_array($option_name, array('start', 'end', 'latest')) && is_numeric($option_value)) {
  96. $options['range'][$option_name] = $option_value;
  97. }
  98. elseif (in_array($option_name, array('start', 'end')) && strtotime($option_value)) {
  99. $options['range']['range_type'] = 'range_date';
  100. $options['range'][$option_name . '_date'] = $option_value;
  101. }
  102. else {
  103. return drush_set_error(dt('Unsupported range option or argument: !opt=!val',
  104. array('!opt' => "range-$option_name", '!val' => $option_value)));
  105. }
  106. }
  107. // Determine the range type based on provided input, if not explicitly set.
  108. if (empty($options['range']['range_type'])) {
  109. $options['range']['range_type'] = isset($options['range']['start'])
  110. ? 'range'
  111. : (isset($options['range']['latest'])
  112. ? 'latest'
  113. : 'all');
  114. }
  115. // Set defaults for any missing range arguments.
  116. switch ($options['range']['range_type']) {
  117. case 'latest':
  118. if (empty($options['range']['latest'])) {
  119. drush_log('Argument range-latest defaulted to 100.', 'ok');
  120. $options['range']['latest'] = 100;
  121. }
  122. break;
  123. case 'range':
  124. case 'range_serial':
  125. if (empty($options['range']['start'])) {
  126. $options['range']['start'] = 1;
  127. }
  128. break;
  129. case 'range_date':
  130. if (empty($options['range']['start_date'])) {
  131. $options['range']['start_date'] = "1/1/1970";
  132. }
  133. break;
  134. }
  135. // Get the preferred completion type.
  136. $options['range']['completion_type'] = drush_get_option('completion-type', NULL);
  137. if (isset($options['range']['completion_type']) && !in_array($options['range']['completion_type'], array('finished', 'draft', 'all'))) {
  138. return drush_set_error('Unsupported completion-type. The available options are "finished", "draft", or "all".');
  139. }
  140. // Set the export options.
  141. $options['range']['batch_size'] = drush_get_option('batch-size', 10000);
  142. $options['file_name'] = drush_get_option('file', tempnam(variable_get('file_directory_temp', file_directory_temp()), 'webform_'));
  143. $batch = webform_results_export_batch($node, $format, $options);
  144. batch_set($batch);
  145. drush_backend_batch_process();
  146. // If no filename was specified, print the file and delete it.
  147. if (drush_get_option('file', FALSE) === FALSE) {
  148. // The @ makes it silent.
  149. drush_print(file_get_contents($options['file_name']));
  150. // Clean up, the @ makes it silent.
  151. @unlink($options['file_name']);
  152. }
  153. }
  154. /**
  155. * Clears a webform via drush.
  156. *
  157. * This is useful for webforms with many submissions that would otherwise fail
  158. * due to time out due or memory consumption.
  159. *
  160. * @param int $nid
  161. * Node ID of the webform to clear.
  162. */
  163. function drush_webform_clear($nid = FALSE) {
  164. if (!$nid) {
  165. return drush_set_error('The node ID of the webform to be cleared is required.');
  166. }
  167. $node = node_load($nid);
  168. if (!$node) {
  169. return drush_set_error(dt('Node !nid was not found.', array('!nid' => $nid)));
  170. }
  171. if (!drush_confirm(dt('Clear submissions from webform "@title"?', array('@title' => $node->title)))) {
  172. return drush_set_error('webform-clear cancelled.');
  173. }
  174. // @code
  175. // module_load_include('inc', 'webform', 'includes/webform.submissions');
  176. // module_load_include('inc', 'webform', 'includes/webform.components');
  177. // @endcode
  178. module_load_include('inc', 'webform', 'includes/webform.report');
  179. // Pull in option from drush to override the default.
  180. $batch_size = drush_get_option('batch-size', 10000);
  181. $count = 0;
  182. while ($deleted = webform_results_clear($nid, $batch_size)) {
  183. $count += $deleted;
  184. }
  185. // Alas, there is no drush version of format_plural, so use the ugly "(s)".
  186. drush_log(dt('@count submission(s) in webform "@title" cleared.', array('@count' => $count, '@title' => $node->title)), 'ok');
  187. }
  188. /**
  189. * Implements hook_drush_sql_sync_sanitize().
  190. */
  191. function webform_drush_sql_sync_sanitize($source) {
  192. // Fetch list of all table.
  193. $all_tables = drush_sql_get_class()->listTables();
  194. $tables_to_truncate = array('webform_submitted_data', 'webform_submissions');
  195. $truncate_webform_tables_query = array();
  196. foreach ($tables_to_truncate as $table) {
  197. if (in_array($table, $all_tables, TRUE)) {
  198. $truncate_webform_tables_query[] = 'TRUNCATE ' . $table . ';';
  199. }
  200. }
  201. drush_sql_register_post_sync_op('webform_submitted_data',
  202. dt('Delete all data submitted to webforms (depending on the site config, may contain sensitive data).'),
  203. implode(' ', $truncate_webform_tables_query));
  204. }