Projet

Général

Profil

Paste
Télécharger (8,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / webform.drush.inc @ be58a50c

1
<?php
2

    
3
/**
4
 * @file
5
 * Implementation of hook_drush_command().
6
 */
7

    
8
function webform_drush_command() {
9
  return array(
10
    'webform-export' => array(
11
      'description' => 'Exports webform data to a file.',
12
      'arguments' => array(
13
        'nid' => 'The node ID of the webform you want to export (required)',
14
      ),
15
      'options' => array(
16
        'file' => 'The file path to export to (defaults to print to stdout)',
17
        'format' => 'The exporter format to use. Out-of-the-box this may be "delimited" or "excel".',
18
        'delimiter' => 'Delimiter between columns (defaults to site-wide setting). This option may need to be wrapped in quotes. i.e. --delimter="\t".',
19
        'components' => 'Comma-separated list of component IDs or form keys to include.' . "\n" .
20
                        '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".',
21
        'header-keys' => 'Integer -1 for none, 0 for label (default) or 1 for form key.',
22
        'select-keys' => 'Integer 0 or 1 value. Set to 1 to print select list values by their form keys instead of labels.',
23
        'select-format' => 'Set to "separate" (default) or "compact" to determine how select list values are exported.',
24
        'range-type' => 'Range of submissions to export: "all", "new", "latest", "range" (by sid, default if start is supplied), "range-serial", or "range-date".',
25
        'range-latest' => 'Integer specifying the latest X submissions will be downloaded. Used if "range-type" is "latest" or no other range options are provided.',
26
        'range-start' => 'The submission ID, serial number, or start date at which to start exporting.',
27
        'range-end' => 'The submission ID, serial number, or end date at which to end exporting.',
28
        'completion-type' => 'Submissions to be included: "finished", "draft" or "all" (default).',
29
        '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.',
30
      ),
31
      'aliases' => array('wfx'),
32
    ),
33
    'webform-clear' => array(
34
      'description' => 'Clear a webform by deleting all its submissions.',
35
      'arguments' => array(
36
        'nid' => 'The node ID of the webform you want to clear (required)',
37
      ),
38
      'options' => array(
39
        '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.',
40
      ),
41
    ),
42
  );
43
}
44

    
45
/**
46
 * Exports a webform via drush, useful for large data dumps that would otherwise
47
 * time out due to memory consumption.
48
 *
49
 * @param int $nid
50
 *   Node ID of the webform that we want to export.
51
 */
52
function drush_webform_export($nid = FALSE) {
53
  if (!$nid) {
54
    return drush_set_error('The node ID of the webform you want to export is required.');
55
  }
56
  $node = node_load($nid);
57
  if (!$node) {
58
    return drush_set_error(dt('Node !nid was not found.', array('!nid' => $nid)));
59
  }
60

    
61
  module_load_include('inc', 'webform', 'includes/webform.submissions');
62
  module_load_include('inc', 'webform', 'includes/webform.export');
63
  module_load_include('inc', 'webform', 'includes/webform.components');
64
  module_load_include('inc', 'webform', 'includes/webform.report');
65

    
66

    
67
  // Pull in options from drush to override the defaults.
68
  $format = drush_get_option('format', 'delimited');
69
  $options = webform_results_download_default_options($node, $format);
70
  foreach ($options as $option_name => $option_value) {
71
    $options[$option_name] = drush_get_option(str_replace('_', '-', $option_name), $option_value);
72
  }
73
  $options['components'] = is_array($options['components']) ? $options['components'] : explode(',', $options['components']);
74

    
75
  // Map form keys to cids.
76
  $form_keys = array();
77
  foreach ($node->webform['components'] as $cid => $component) {
78
    $form_keys[$component['form_key']] = $cid;
79
  }
80
  foreach ($options['components'] as $key => &$component) {
81
    if (isset($form_keys[$component])) {
82
      $component = $form_keys[$component];
83
    }
84
  }
85
  unset($component); // Drop PHP reference.
86

    
87
  // Get the range options.
88
  unset($options['range']['range_type']);
89
  foreach (drush_get_merged_prefixed_options('range-') as $option_name => $option_value) {
90
    if ($option_name == 'type' && in_array($option_value, array('all', 'new', 'latest', 'range', 'range-serial', 'range-date'))) {
91
      $options['range']['range_type'] = str_replace('-', '_', $option_value);
92
    }
93
    elseif (in_array($option_name, array('start', 'end', 'latest')) && is_numeric($option_value)) {
94
      $options['range'][$option_name] = $option_value;
95
    }
96
    elseif (in_array($option_name, array('start', 'end')) && strtotime($option_value)) {
97
      $options['range']['range_type'] = 'range_date';
98
      $options['range'][$option_name . '_date'] = $option_value;
99
    }
100
    else {
101
      return drush_set_error(dt('Unsupported range option or argument: !opt=!val',
102
                                array('!opt' => "range-$option_name", '!val' => $option_value)));
103
    }
104
  }
105
  
106
  // Determine the range type based on provided input, if not explicitly set.
107
  if (empty($options['range']['range_type'])) {
108
    $options['range']['range_type'] = isset($options['range']['start'])
109
                                        ? 'range'
110
                                        : (isset($options['range']['latest'])
111
                                           ? 'latest'
112
                                           : 'all');
113
  }
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

    
136
  // Get the preferred completion type
137
  $options['range']['completion_type'] = drush_get_option('completion-type', NULL); 
138
  if (isset($options['range']['completion_type']) && !in_array($options['range']['completion_type'], array('finished', 'draft', 'all'))) {
139
    return drush_set_error('Unsupported completion-type. The available options are "finished", "draft", or "all".');
140
  }
141

    
142
  // Set the export options.
143
  $options['range']['batch_size'] = drush_get_option('batch-size', 10000);
144
  $options['file_name'] = drush_get_option('file', tempnam(variable_get('file_directory_temp', file_directory_temp()), 'webform_'));
145

    
146
  $batch = webform_results_export_batch($node, $format, $options);
147
  batch_set($batch);
148
  drush_backend_batch_process();
149

    
150
  // If no filename was specified, print the file and delete it.
151
  if (drush_get_option('file', FALSE) === FALSE) {
152
    drush_print(file_get_contents($options['file_name']));  // The @ makes it silent.
153
    @unlink($options['file_name']);  // Clean up, the @ makes it silent.
154
  }
155
}
156

    
157
/**
158
 * Clears a webform via drush, useful for webforms with many submissions that
159
 * would otherwise fail due to time out due or memory consumption.
160
 *
161
 * @param int $nid
162
 *   Node ID of the webform to clear.
163
 */
164
function drush_webform_clear($nid = FALSE) {
165
  if (!$nid) {
166
    return drush_set_error('The node ID of the webform to be cleared is required.');
167
  }
168
  $node = node_load($nid);
169
  if (!$node) {
170
    return drush_set_error(dt('Node !nid was not found.', array('!nid' => $nid)));
171
  }
172
  if (!drush_confirm(dt('Clear submissions from webform "@title"?', array('@title' => $node->title)))) {
173
    return drush_set_error('webform-clear cancelled.');
174
  }
175

    
176
  //module_load_include('inc', 'webform', 'includes/webform.submissions');
177
  //module_load_include('inc', 'webform', 'includes/webform.components');
178
  module_load_include('inc', 'webform', 'includes/webform.report');
179

    
180

    
181
  // Pull in option from drush to override the default.
182
  $batch_size = drush_get_option('batch-size', 10000);
183
  $count = 0;
184
  while ($deleted = webform_results_clear($nid, $batch_size)) {
185
    $count += $deleted;
186
  }
187
  // Alas, there is no drush version of format_plural, so use the ugly "(s)".
188
  drush_log(dt('@count submission(s) in webform "@title" cleared.', array('@count' => $count, '@title' => $node->title)), 'ok');
189
}
190

    
191
/**
192
 * Implements hook_drush_sql_sync_sanitize.
193
 */
194
function webform_drush_sql_sync_sanitize($source) {
195
  drush_sql_register_post_sync_op('webform_submitted_data',
196
    dt('Delete all data submitted to webforms (depending on the site config, may contain sensitive data).'),
197
    "TRUNCATE webform_submitted_data; TRUNCATE webform_submissions;");
198
}