Projet

Général

Profil

Paste
Télécharger (6,65 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

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

    
8
function webform_drush_command() {
9
  $items = array();
10

    
11
  $items['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 field key.',
23
      'select-keys' => 'Integer 0 or 1 value. Set to 1 to print select list values by their field 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

    
35
  return $items;
36
}
37

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

    
54
  module_load_include('inc', 'webform', 'includes/webform.submissions');
55
  module_load_include('inc', 'webform', 'includes/webform.export');
56
  module_load_include('inc', 'webform', 'includes/webform.components');
57
  module_load_include('inc', 'webform', 'includes/webform.report');
58

    
59

    
60
  // Pull in options from drush to override the defaults.
61
  $format = drush_get_option('format', 'delimited');
62
  $options = webform_results_download_default_options($node, $format);
63
  foreach ($options as $option_name => $option_value) {
64
    $options[$option_name] = drush_get_option(str_replace('_', '-', $option_name), $option_value);
65
  }
66
  $options['components'] = is_array($options['components']) ? $options['components'] : explode(',', $options['components']);
67

    
68
  // Map form keys to cids.
69
  $form_keys = array();
70
  foreach ($node->webform['components'] as $cid => $component) {
71
    $form_keys[$component['form_key']] = $cid;
72
  }
73
  foreach ($options['components'] as $key => &$component) {
74
    if (isset($form_keys[$component])) {
75
      $component = $form_keys[$component];
76
    }
77
  }
78
  unset($component); // Drop PHP reference.
79

    
80
  // Get the range options.
81
  unset($options['range']['range_type']);
82
  foreach (drush_get_merged_prefixed_options('range-') as $option_name => $option_value) {
83
    if ($option_name == 'type' && in_array($option_value, array('all', 'new', 'latest', 'range', 'range-serial', 'range-date'))) {
84
      $options['range']['range_type'] = str_replace('-', '_', $option_value);
85
    }
86
    elseif (in_array($option_name, array('start', 'end', 'latest')) && is_numeric($option_value)) {
87
      $options['range'][$option_name] = $option_value;
88
    }
89
    elseif (in_array($option_name, array('start', 'end')) && strtotime($option_value)) {
90
      $options['range']['range_type'] = 'range_date';
91
      $options['range'][$option_name . '_date'] = $option_value;
92
    }
93
    else {
94
      return drush_set_error(dt('Unsupported range option or argument: !opt=!val',
95
                                array('!opt' => "range-$option_name", '!val' => $option_value)));
96
    }
97
  }
98
  
99
  // Determine the range type based on provided input, if not explicitly set.
100
  if (empty($options['range']['range_type'])) {
101
    $options['range']['range_type'] = isset($options['range']['start'])
102
                                        ? 'range'
103
                                        : (isset($options['range']['latest'])
104
                                           ? 'latest'
105
                                           : 'all');
106
  }
107
  
108
  // Set defaults for any missing range arguments.
109
  switch ($options['range']['range_type']) {
110
    case 'latest':
111
      if (empty($options['range']['latest'])) {
112
        drush_log('Argument range-latest defaulted to 100.', 'ok');
113
        $options['range']['latest'] = 100;
114
      }
115
      break;
116
    case 'range':
117
    case 'range_serial':
118
      if (empty($options['range']['start'])) {
119
        $options['range']['start'] = 1;
120
      }
121
      break;
122
    case 'range_date':
123
      if (empty($options['range']['start_date'])) {
124
        $options['range']['start_date'] = "1/1/1970";
125
      }
126
      break;
127
  }
128

    
129
  // Get the preferred completion type
130
  $options['range']['completion_type'] = drush_get_option('completion-type', NULL); 
131
  if (isset($options['range']['completion_type']) && !in_array($options['range']['completion_type'], array('finished', 'draft', 'all'))) {
132
    return drush_set_error('Unsupported completion-type. The available options are "finished", "draft", or "all".');
133
  }
134

    
135
  // Set the export options.
136
  $options['range']['batch_size'] = drush_get_option('batch-size', 10000);
137
  $options['file_name'] = drush_get_option('file', tempnam(variable_get('file_directory_temp', file_directory_temp()), 'webform_'));
138

    
139
  $batch = webform_results_export_batch($node, $format, $options);
140
  batch_set($batch);
141
  drush_backend_batch_process();
142

    
143
  // If no filename was specified, print the file and delete it.
144
  if (drush_get_option('file', FALSE) === FALSE) {
145
    drush_print(file_get_contents($options['file_name']));  // The @ makes it silent.
146
    @unlink($options['file_name']);  // Clean up, the @ makes it silent.
147
  }
148
}