View source
<?php
function views_data_export_drush_command() {
$items = array();
$items['views-data-export'] = array(
'aliases' => array(
'vde',
),
'description' => 'Fully executes a views_data_export display of a view and writes the output to file.',
'arguments' => array(
'view_name' => 'The name of the view',
'display_id' => 'The id of the views_data_export display to execute on the view',
'output_file' => 'The file to write the results to - will be overwritten if it already exists',
),
'options' => array(
'--format' => 'csv,doc,txt,xls or xml. These options are ignored if the display_id passed is a "views_data_export" display.',
'--separator' => 'csv only: What character separates the fields (default:,)',
'--trim-whitespace' => 'csv only: Trim whitespace from either side of fields (default:1)',
'--header-row' => 'csv only: Make the first row a row of headers (default:1)',
'--quote-values' => 'csv only: Surround each field in quotes (default:1)',
),
'examples' => array(
'drush views-data-export myviewname views_data_export_1 output.csv' => 'Export myviewname:views_data_export_1 and write the output to output.csv in the current directory',
),
'drupal dependencies' => array(
'views_data_export',
),
'core' => array(
'6',
),
);
return $items;
}
function views_data_export_drush_help($section) {
switch ($section) {
case 'drush:views-data-export':
return dt("This command may be used to fully execute a views_data_export display of a view, batched if need be, and write the output to a file.");
}
}
function drush_views_data_export_validate() {
static $already_run = FALSE;
if ($already_run) {
return;
}
$already_run = TRUE;
$args = drush_get_arguments();
array_shift($args);
if (count($args) !== 3) {
return drush_set_error('ARGUMENTS_REQUIRED', dt('All arguments are required.'));
}
if (!($view = views_get_view($args[0]))) {
return drush_set_error('VIEW_DOES_NOT_EXIST', dt('The view !view does not exist.', array(
'!view' => $args[0],
)));
}
if (!$view
->set_display($args[1])) {
return drush_set_error('VIEW_DOES_NOT_EXIST', dt('The view !view does not have the !display display.', array(
'!view' => $args[0],
'!display' => $args[1],
)));
}
else {
if ($view->current_display != $args[1]) {
drush_log(dt('Using different display from specified display: @display', array(
'@display' => $view->current_display,
)), 'notice');
}
drush_set_option('views_data_export_display_id', $view->current_display);
}
$format = drush_get_option('format');
$valid_formats = array(
'csv',
'doc',
'txt',
'xls',
'xml',
);
if (!empty($format) && !in_array($format, $valid_formats)) {
return drush_set_error('VIEWS_DATA_EXPORT_INVALID_OPTION', dt('The "--format" option is invalid, please supply one of the following: !formats', array(
'!formats' => implode(', ', $valid_formats),
)));
}
}
function drush_views_data_export($view_name, $display_id, $output_file) {
static $already_run = FALSE;
if ($already_run) {
return;
}
$already_run = TRUE;
$display_id = drush_get_option('views_data_export_display_id', 'default');
$view = views_get_view($view_name);
if ($view->display[$display_id]->display_plugin != 'views_data_export') {
$format = drush_get_option('format');
$settings = array();
switch ($format) {
case 'doc':
case 'xls':
case 'xml':
case 'txt':
$settings['display_options']['style_plugin'] = 'views_data_export_' . $format;
break;
case 'csv':
default:
$settings['display_options']['style_plugin'] = 'views_data_export_csv';
if ($separator = drush_get_option('separator')) {
$settings['display_options']['style_options']['separator'] = $separator;
}
if (!($trim = drush_get_option('trim-whitespace'))) {
$settings['display_options']['style_options']['trim'] = 0;
}
if (!($header = drush_get_option('header-row'))) {
$settings['display_options']['style_options']['header'] = 0;
}
if (!($quote = drush_get_option('quote-values'))) {
$settings['display_options']['style_options']['quote'] = 0;
}
}
$display_id = _drush_views_data_export_clone_display($view, $display_id, $settings);
}
$view
->set_display($display_id);
$options = array(
'output_file' => realpath(drush_get_context('DRUSH_OLDCWD', getcwd())) . '/' . $output_file,
);
_drush_views_data_export_override_batch($view_name, $display_id, $options);
$view
->execute_display($display_id);
}
function _drush_views_data_export_override_batch($view = NULL, $display = NULL, $options = TRUE) {
static $_views;
if (isset($view)) {
$_views[$view][$display] = $options;
}
return $_views;
}
function views_data_export_views_data_export_batch_alter(&$batch, &$final_destination, &$querystring) {
$new_batch = $batch;
$view_name = $new_batch['view_name'];
$display_id = $new_batch['display_id'];
$ok_to_override = _drush_views_data_export_override_batch();
if (!$ok_to_override[$view_name][$display_id]) {
return;
}
$options = $ok_to_override[$view_name][$display_id];
$new_batch['operations'][] = array(
'_drush_views_data_export_batch_finished',
array(
$batch['eid'],
$options['output_file'],
),
);
batch_set($new_batch);
$new_batch =& batch_get();
$new_batch['progressive'] = FALSE;
drush_backend_batch_process();
$batch = array();
}
function _drush_views_data_export_batch_finished($eid, $output_file, &$context) {
$export = views_data_export_get($eid);
$view = views_data_export_view_retrieve($eid);
$view
->set_display($export->view_display_id);
$view->display_handler->batched_execution_state = $export;
$view->display_handler
->remove_index();
$temp_file = $view->display_handler
->outputfile_path();
if (@drush_op('copy', $temp_file, $output_file)) {
drush_log("Data export saved to " . $output_file, 'success');
}
else {
drush_set_error('VIEWS_DATA_EXPORT_COPY_ERROR', dt("The file could not be copied to the selected destination"));
}
}
function _drush_views_data_export_clone_display(&$view, $display_id, $settings = array()) {
$new_display_id = _drush_views_data_export_generate_display_id($view, 'views_data_export');
$view->display[$new_display_id] = drupal_clone($view->display[$display_id]);
$default_settings = array(
'id' => $new_display_id,
'display_plugin' => 'views_data_export',
'position' => 99,
'display_options' => array(
'style_plugin' => 'views_data_export_csv',
'style_options' => array(
'attach_text' => 'CSV',
'provide_file' => 1,
'filename' => 'view-%view.csv',
'parent_sort' => 1,
'separator' => ',',
'quote' => 1,
'trim' => 1,
'header' => 1,
),
'use_batch' => 'batch',
'path' => '',
'displays' => array(
'default' => 'default',
),
),
);
$settings = array_replace_recursive($default_settings, $settings);
$view->display[$new_display_id] = (object) array_replace_recursive((array) $view->display[$new_display_id], $settings);
return $new_display_id;
}
function _drush_views_data_export_generate_display_id($view, $type) {
if ($type == 'default') {
return 'default';
}
$id = $type . '_1';
$count = 1;
while (!empty($view->display[$id])) {
$id = $type . '_' . ++$count;
}
return $id;
}
if (!function_exists('array_replace_recursive')) {
function array_replace_recursive($array, $array1) {
$arrays = func_get_args();
$original = array_shift($arrays);
foreach ($arrays as $array) {
foreach ($array as $key => $value) {
if (is_array($value)) {
$original[$key] = array_replace_recursive($original[$key], $array[$key]);
}
else {
$original[$key] = $value;
}
}
}
return $original;
}
}