function drush_views_data_export in Views data export 7.4
Same name and namespace in other branches
- 6.3 views_data_export.drush.inc \drush_views_data_export()
- 6 views_data_export.drush.inc \drush_views_data_export()
- 6.2 views_data_export.drush.inc \drush_views_data_export()
- 7 views_data_export.drush.inc \drush_views_data_export()
- 7.3 views_data_export.drush.inc \drush_views_data_export()
Drush command callback to export a views data to a file.
See also
drush_views_data_export_validate().
views_data_export_views_data_export_batch_alter().
File
- ./
views_data_export.drush.inc, line 106
Code
function drush_views_data_export($view_name, $display_id, $output_file) {
// Because of a bug in the way that Drush 4 computes the name of functions to
// call from a Drush command, we may end up getting called twice, so we just
// don't do anything on subsequent invocations.
static $already_run = FALSE;
if ($already_run) {
return;
}
$already_run = TRUE;
// Set the display to the one that we computed earlier.
$display_id = drush_get_option('views_data_export_display_id', 'default');
$view = views_get_view($view_name);
// If the given display_id is not views_data_alter then
// we programatically clone it to a views_data_alter display
// and then execute that one instead
if ($view->display[$display_id]->display_plugin != 'views_data_export') {
//drush_log("Display '$display_id' is not views_data_export. Making one that is and executing that instead =).", 'success');
$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);
// We execute the view normally, and take advantage
// of an alter function to interject later and batch it ourselves
$options = array();
// Let's see if the given $output_file is a absolute path.
if (strpos($output_file, '/') === 0) {
$options['output_file'] = $output_file;
}
else {
$options['output_file'] = realpath(drush_get_context('DRUSH_OLDCWD', getcwd())) . '/' . $output_file;
}
if ($view->display_handler
->is_batched()) {
// This is a batched export, and needs to be handled as such.
_drush_views_data_export_override_batch($view_name, $display_id, $options);
$arguments = drush_get_option('arguments', '');
$arguments = explode(',', $arguments);
$view
->execute_display($display_id, $arguments);
}
else {
// This export isn't batched.
ob_start();
$view
->execute_display($display_id);
// Get the results, and clean the output buffer.
$result = ob_get_contents();
// Clean the buffer.
ob_end_clean();
// Save the results to file.
// Copy file over
if (file_put_contents($options['output_file'], $result)) {
drush_log("Data export saved to " . $options['output_file'], 'success');
}
else {
drush_set_error('VIEWS_DATA_EXPORT_COPY_ERROR', dt("The file could not be copied to the selected destination"));
}
}
}