You are here

function drush_webform_export in Webform 7.4

Same name and namespace in other branches
  1. 8.5 drush/webform.drush.inc \drush_webform_export()
  2. 6.x drush/webform.drush.inc \drush_webform_export()

Exports a webform via drush.

This is useful for large data dumps that would otherwise time out due to memory consumption.

Parameters

bool|int $nid: Node ID of the webform that we want to export.

Return value

false|null The value returned from drush_set_error() or NULL if that function is not called.

File

./webform.drush.inc, line 61
Functions relating to Drush integration.

Code

function drush_webform_export($nid = FALSE) {
  if (!$nid) {
    return drush_set_error('The node ID of the webform you want to export is required.');
  }
  $node = node_load($nid);
  if (!$node) {
    return drush_set_error(dt('Node !nid was not found.', array(
      '!nid' => $nid,
    )));
  }
  module_load_include('inc', 'webform', 'includes/webform.submissions');
  module_load_include('inc', 'webform', 'includes/webform.export');
  module_load_include('inc', 'webform', 'includes/webform.components');
  module_load_include('inc', 'webform', 'includes/webform.report');

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

  // Map form keys to cids.
  $form_keys = array();
  foreach ($node->webform['components'] as $cid => $component) {
    $form_keys[$component['form_key']] = $cid;
  }
  foreach ($options['components'] as $key => &$component) {
    if (isset($form_keys[$component])) {
      $component = $form_keys[$component];
    }
  }

  // Drop PHP reference.
  unset($component);

  // Get the range options.
  unset($options['range']['range_type']);
  foreach (drush_get_merged_prefixed_options('range-') as $option_name => $option_value) {
    if ($option_name == 'type' && in_array($option_value, array(
      'all',
      'new',
      'latest',
      'range',
      'range-serial',
      'range-date',
    ))) {
      $options['range']['range_type'] = str_replace('-', '_', $option_value);
    }
    elseif (in_array($option_name, array(
      'start',
      'end',
      'latest',
    )) && is_numeric($option_value)) {
      $options['range'][$option_name] = $option_value;
    }
    elseif (in_array($option_name, array(
      'start',
      'end',
    )) && strtotime($option_value)) {
      $options['range']['range_type'] = 'range_date';
      $options['range'][$option_name . '_date'] = $option_value;
    }
    else {
      return drush_set_error(dt('Unsupported range option or argument: !opt=!val', array(
        '!opt' => "range-{$option_name}",
        '!val' => $option_value,
      )));
    }
  }

  // Determine the range type based on provided input, if not explicitly set.
  if (empty($options['range']['range_type'])) {
    $options['range']['range_type'] = isset($options['range']['start']) ? 'range' : (isset($options['range']['latest']) ? 'latest' : 'all');
  }

  // Set defaults for any missing range arguments.
  switch ($options['range']['range_type']) {
    case 'latest':
      if (empty($options['range']['latest'])) {
        drush_log('Argument range-latest defaulted to 100.', 'ok');
        $options['range']['latest'] = 100;
      }
      break;
    case 'range':
    case 'range_serial':
      if (empty($options['range']['start'])) {
        $options['range']['start'] = 1;
      }
      break;
    case 'range_date':
      if (empty($options['range']['start_date'])) {
        $options['range']['start_date'] = "1/1/1970";
      }
      break;
  }

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

  // Set the export options.
  $options['range']['batch_size'] = drush_get_option('batch-size', 10000);
  $options['file_name'] = drush_get_option('file', tempnam(variable_get('file_directory_temp', file_directory_temp()), 'webform_'));
  $batch = webform_results_export_batch($node, $format, $options);
  batch_set($batch);
  drush_backend_batch_process();

  // If no filename was specified, print the file and delete it.
  if (drush_get_option('file', FALSE) === FALSE) {

    // The @ makes it silent.
    drush_print(file_get_contents($options['file_name']));

    // Clean up, the @ makes it silent.
    @unlink($options['file_name']);
  }
}