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