Skip to content

Commit

Permalink
Switching to bundle based exports
Browse files Browse the repository at this point in the history
  • Loading branch information
taylor-steve committed Jun 15, 2023
1 parent ceefaf8 commit 198edb3
Show file tree
Hide file tree
Showing 6 changed files with 404 additions and 302 deletions.
237 changes: 121 additions & 116 deletions modules/mukurtu_export/src/BatchExportExecutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,128 +9,133 @@

class BatchExportExecutable implements MukurtuExportExecutableInterface
{
use StringTranslationTrait;

/**
* The export entity source.
*
* @var \Drupal\mukurtu_export\MukurtuExporterSourceInterface
*/
protected $source;

protected $exporter;

public function __construct(MukurtuExporterSourceInterface $source, MukurtuExporterInterface $exporter)
{
$this->source = $source;
$this->exporter = $exporter;
use StringTranslationTrait;

/**
* The export entity source.
*
* @var \Drupal\mukurtu_export\MukurtuExporterSourceInterface
*/
protected $source;

protected $exporter;

public function __construct(MukurtuExporterSourceInterface $source, MukurtuExporterInterface $exporter)
{
$this->source = $source;
$this->exporter = $exporter;
}

/**
* {@inheritDoc}
*/
public function export()
{
$entities = $this->source->getEntities();

$operations = [
[sprintf('%s::%s', $this->exporter::class, 'exportSetup'), [$entities, $this->exporter->getConfiguration()]],
[sprintf('%s::%s', self::class, 'exportBatch'), [$this->exporter::class]],
[sprintf('%s::%s', self::class, 'package'), []],
[sprintf('%s::%s', $this->exporter::class, 'exportCompleted'), []],
];

$batch = [
'operations' => $operations,
'title' => $this->t('Exporting'),
'init_message' => $this->t('Initializing export'),
'progress_message' => $this->t('Exporting...'),
'error_message' => $this->t('Export failed with error.'),
'finished' => self::class . '::batchFinishedExport',
];

$_SESSION['mukurtu_export']['results'] = [];
batch_set($batch);
}

/**
* Run an export batch.
*
* @param string $exporter_class
* Class of the exporter that implements MukurtuExporterInterface.
* @param mixed $context
* The batch context.
*
* @return void
*/
public static function exportBatch($exporter_class, &$context)
{
call_user_func_array([$exporter_class, 'batchSetup'], [&$context]);
call_user_func_array([$exporter_class, 'batchExport'], [&$context]);
call_user_func_array([$exporter_class, 'batchCompleted'], [&$context]);
}

public static function package(&$context)
{
/** @var \Drupal\Core\File\FileSystemInterface $fs */
$fs = \Drupal::service('file_system');
$batchSize = 1;
$zip = new ZipArchive();
if (empty($context['sandbox']['deliverables'])) {
$context['sandbox']['deliverables'] = array_merge($context['results']['deliverables']['metadata'], $context['results']['deliverables']['files']);
$context['sandbox']['total'] = count($context['sandbox']['deliverables']);
$context['sandbox']['packaged'] = 0;
$zipName = 'export-' . date('m-d-Y_hia') . '.zip';
$context['sandbox']['download'] = "private://exports/{$context['results']['uid']}/$zipName";
}

/**
* {@inheritDoc}
*/
public function export()
{
$entities = $this->source->getEntities();

$operations = [
[sprintf('%s::%s', $this->exporter::class, 'exportSetup'), [$entities, []]],
[sprintf('%s::%s', self::class, 'exportBatch'), [$this->exporter::class]],
[sprintf('%s::%s', self::class, 'package'), []],
[sprintf('%s::%s', $this->exporter::class, 'exportCompleted'), []],
];

$batch = [
'operations' => $operations,
'title' => $this->t('Exporting'),
'init_message' => $this->t('Initializing export'),
'progress_message' => $this->t('Exporting...'),
'error_message' => $this->t('Export failed with error.'),
'finished' => self::class . '::batchFinishedExport',
];

$_SESSION['mukurtu_export']['results'] = [];
batch_set($batch);
// Open the Zip archive.
$zipPath = $fs->realpath($context['sandbox']['download']);
$zip->open($zipPath, ZipArchive::CREATE);

// Get the batch of files to zip and remove from the global list.
$filesBatch = array_slice($context['sandbox']['deliverables'], 0, $batchSize);
$context['sandbox']['deliverables'] = array_slice($context['sandbox']['deliverables'], $batchSize);

// Zip the files.
foreach ($filesBatch as $fileUri) {
$filePath = $fs->realpath($fileUri);
if ($filePath) {
$zip->addFile($filePath, basename($filePath));
}
$context['sandbox']['packaged']++;
}

/**
* Run an export batch.
*
* @param string $exporter_class
* Class of the exporter that implements MukurtuExporterInterface.
* @param mixed $context
* The batch context.
*
* @return void
*/
public static function exportBatch($exporter_class, &$context)
{
call_user_func_array([$exporter_class, 'batchSetup'], [&$context]);
call_user_func_array([$exporter_class, 'batchExport'], [&$context]);
call_user_func_array([$exporter_class, 'batchCompleted'], [&$context]);
}
$zip->close();

public static function package(&$context) {
/** @var \Drupal\Core\File\FileSystemInterface $fs */
$fs = \Drupal::service('file_system');
$batchSize = 1;
$zip = new ZipArchive();
if (empty($context['sandbox']['deliverables'])) {
$context['sandbox']['deliverables'] = $context['results']['deliverables']['metadata'] + $context['results']['deliverables']['files'];
$context['sandbox']['total'] = count($context['sandbox']['deliverables']);
$context['sandbox']['packaged'] = 0;
$zipName = 'export-' . date('m-d-Y_hia') . '.zip';
$context['sandbox']['download'] = "private://exports/{$context['results']['uid']}/$zipName";
}

$zipPath = $fs->realpath($context['sandbox']['download']);
$zip->open($zipPath, ZipArchive::CREATE);
$filesBatch = array_slice($context['sandbox']['deliverables'], 0, $batchSize);

foreach ($filesBatch as $delta => $fileUri) {
$filePath = $fs->realpath($fileUri);
if ($filePath) {
$zip->addFile($filePath, basename($filePath));
}
$context['sandbox']['packaged']++;
unset($context['sandbox']['deliverables'][$delta]);
}

$zip->close();

if (!empty($context['sandbox']['deliverables'])) {
$context['finished'] = $context['sandbox']['packaged']/ $context['sandbox']['total'];
} else {
$context['finished'] = 1;

// Add the final zip as a managed file for the exporting user.
$file = File::create([
'uri' => $context['sandbox']['download'],
'uid' => $context['results']['uid'],
]);
try {
$file->save();
$context['results']['download_fid'] = $file->id();
} catch (Exception $e) {

}
}
}
if (!empty($context['sandbox']['deliverables'])) {
$context['finished'] = $context['sandbox']['packaged'] / $context['sandbox']['total'];
} else {
$context['finished'] = 1;

// Add the final zip as a managed file for the exporting user.
$file = File::create([
'uri' => $context['sandbox']['download'],
'uid' => $context['results']['uid'],
]);
try {
$file->save();
$context['results']['download_fid'] = $file->id();
} catch (Exception $e) {

/**
* Finished callback for export batches.
*
* @param bool $success
* A boolean indicating whether the batch has completed successfully.
* @param array $results
* The value set in $context['results'] by callback_batch_operation().
* @param array $operations
* If $success is FALSE, contains the operations that remained unprocessed.
*/
public static function batchFinishedExport(bool $success, array $results, array $operations): void
{
$_SESSION['mukurtu_export']['results'] = $results;
$_SESSION['mukurtu_export']['download_fid'] = $results['download_fid'];
}
}
}

/**
* Finished callback for export batches.
*
* @param bool $success
* A boolean indicating whether the batch has completed successfully.
* @param array $results
* The value set in $context['results'] by callback_batch_operation().
* @param array $operations
* If $success is FALSE, contains the operations that remained unprocessed.
*/
public static function batchFinishedExport(bool $success, array $results, array $operations): void
{
$_SESSION['mukurtu_export']['results'] = $results;
$_SESSION['mukurtu_export']['download_fid'] = $results['download_fid'] ?? NULL;
}

}
1 change: 1 addition & 0 deletions modules/mukurtu_export/src/Form/ExportResultsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function getFormId()

public function submitNewExport(array &$form, FormStateInterface $form_state)
{
$this->reset();
$form_state->setRedirect('mukurtu_export.export_item_and_format_selection');
}

Expand Down
93 changes: 47 additions & 46 deletions modules/mukurtu_export/src/Form/ExportSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,51 @@
*/
class ExportSettingsForm extends ExportBaseForm
{
/**
* {@inheritdoc}
*/
public function getFormId()
{
return 'mukurtu_export_settings';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$form += $this->exporter->settingsForm($form, $form_state);

$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['back'] = [
'#type' => 'submit',
'#value' => $this->t('Back'),
'#button_type' => 'primary',
'#submit' => ['::submitBack'],
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Export'),
'#button_type' => 'primary',
];
return $form;
}

public function submitBack(array &$form, FormStateInterface $form_state)
{
$form_state->setRedirect('mukurtu_export.export_item_and_format_selection');
}

public function submitForm(array &$form, FormStateInterface $form_state)
{
$config = $this->exporter->getConfig($form, $form_state);
$this->setExporterConfig($config);

$this->executable->export();

$form_state->setRedirect('mukurtu_export.export_results');
}
/**
* {@inheritdoc}
*/
public function getFormId()
{
return 'mukurtu_export_settings';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$form += $this->exporter->settingsForm($form, $form_state);

$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['back'] = [
'#type' => 'submit',
'#value' => $this->t('Back'),
'#button_type' => 'primary',
'#submit' => ['::submitBack'],
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Export'),
'#button_type' => 'primary',
];
return $form;
}

public function submitBack(array &$form, FormStateInterface $form_state)
{
$form_state->setRedirect('mukurtu_export.export_item_and_format_selection');
}

public function submitForm(array &$form, FormStateInterface $form_state)
{
$settings = $this->exporter->getSettings($form, $form_state);
$this->exporter->setConfiguration(['settings' => $settings]);
$this->setExporterConfig($this->exporter->getConfiguration());

$this->executable->export();

$form_state->setRedirect('mukurtu_export.export_results');
}
}
Loading

0 comments on commit 198edb3

Please sign in to comment.