diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6d973d21763bd88a6457307011420815bce35375..61509dc2f475b1e9e79ab8238c6189e285e11573 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -29,3 +29,4 @@ include: # _CURL_TEMPLATES_REF: 'main' variables: _SHOW_ENVIRONMENT_VARIABLES: '1' + _TARGET_CORE: $CORE_STABLE diff --git a/src/Form/MigrateSourceUiForm.php b/src/Form/MigrateSourceUiForm.php index 9a719a180d274b63d993d8ac4f84f2d5503405fd..134ae652b16733affd8f914534a93ed00846248a 100644 --- a/src/Form/MigrateSourceUiForm.php +++ b/src/Form/MigrateSourceUiForm.php @@ -8,13 +8,13 @@ use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\migrate\Plugin\Migration; use Drupal\migrate\Plugin\MigrationInterface; +use Drupal\migrate\Plugin\MigrationPluginManager; use Drupal\migrate_plus\Plugin\migrate_plus\data_parser\Json; use Drupal\migrate_plus\Plugin\migrate_plus\data_parser\Xml; use Drupal\migrate_source_csv\Plugin\migrate\source\CSV; use Drupal\migrate_source_ui\StubMigrationMessage; -use Drupal\migrate_source_ui\MigrateBatchExecutable; +use Drupal\migrate_tools\MigrateBatchExecutable; use Symfony\Component\DependencyInjection\ContainerInterface; -use Drupal\migrate\Plugin\MigrationPluginManager; /** * Contribute form. @@ -179,16 +179,28 @@ class MigrateSourceUiForm extends FormBase { $this->messenger()->addWarning($this->t('Migration @id reset to Idle', ['@id' => $migration_id])); } + $executable = new MigrateBatchExecutable($migration, new StubMigrationMessage(), $this->getBatchOptions($form, $form_state)); + $executable->batchImport(); + } + + /** + * Prepares an array of migrate batch options. + */ + protected function getBatchOptions($form, FormStateInterface $form_state) { $options = [ - 'file_path' => $form_state->getValue('file_path'), + 'configuration' => [ + 'source' => [ + 'path' => $form_state->getValue('file_path'), + ], + ], ]; + // Force updates or not. if ($form_state->getValue('update_existing_records')) { $options['update'] = 1; } - $executable = new MigrateBatchExecutable($migration, new StubMigrationMessage(), $options); - $executable->batchImport(); + return $options; } /** diff --git a/src/MigrateBatchExecutable.php b/src/MigrateBatchExecutable.php deleted file mode 100644 index 1e395878ee2111e03d68d119abbafa3304ec26e6..0000000000000000000000000000000000000000 --- a/src/MigrateBatchExecutable.php +++ /dev/null @@ -1,155 +0,0 @@ -<?php - -namespace Drupal\migrate_source_ui; - -use Drupal\migrate\MigrateMessageInterface; -use Drupal\migrate\Plugin\MigrationInterface; -use Drupal\migrate_tools\MigrateBatchExecutable as BaseMigrateBatchExecutable; - -/** - * Defines a migrate executable class for batch migrations through UI. - */ -class MigrateBatchExecutable extends BaseMigrateBatchExecutable { - - /** - * The file path. - * - * @var string - */ - protected $filePath; - - /** - * {@inheritdoc} - */ - public function __construct(MigrationInterface $migration, MigrateMessageInterface $message, array $options = []) { - parent::__construct($migration, $message, $options); - $this->filePath = $options['file_path']; - } - - /** - * Helper to generate the batch operations for importing migrations. - * - * @param \Drupal\migrate\Plugin\MigrationInterface[] $migrations - * The migrations. - * @param string $operation - * The batch operation to perform. - * @param array $options - * The migration options. - * - * @return array - * The batch operations to perform. - */ - protected function batchOperations(array $migrations, string $operation, array $options = []): array { - $operations = []; - foreach ($migrations as $migration) { - - if (!empty($options['update'])) { - $migration->getIdMap()->prepareUpdate(); - } - if (!empty($options['force'])) { - $migration->set('requirements', []); - } - else { - $dependencies = $migration->getMigrationDependencies(); - if (!empty($dependencies['required'])) { - $required_migrations = $this->migrationPluginManager->createInstances($dependencies['required']); - // For dependent migrations will need to be migrate all items. - $operations += $this->batchOperations($required_migrations, $operation, [ - 'limit' => 0, - 'update' => $options['update'], - 'force' => $options['force'], - ]); - } - } - - $operations[] = [ - [get_class($this), 'batchProcessImport'], - [$migration->id(), $options + ['file_path' => $this->filePath]], - ]; - } - - return $operations; - } - - /** - * Batch 'operation' callback. - * - * @param string $migration_id - * The migration id. - * @param array $options - * The batch executable options. - * @param array|\DrushBatchContext $context - * The sandbox context. - */ - public static function batchProcessImport(string $migration_id, array $options, &$context): void { - if (empty($context['sandbox'])) { - $context['finished'] = 0; - $context['sandbox'] = []; - $context['sandbox']['total'] = 0; - $context['sandbox']['counter'] = 0; - $context['sandbox']['batch_limit'] = 0; - $context['sandbox']['operation'] = MigrateBatchExecutable::BATCH_IMPORT; - } - - // Prepare the migration executable. - $message = new StubMigrationMessage(); - $definition = \Drupal::getContainer()->get('plugin.manager.migration')->getDefinition($migration_id); - // Override the file path. - $definition['source']['path'] = $options['file_path']; - /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */ - $migration = \Drupal::getContainer()->get('plugin.manager.migration')->createStubMigration($definition); - - $executable = new MigrateBatchExecutable($migration, $message, $options); - - if (empty($context['sandbox']['total'])) { - $context['sandbox']['total'] = $executable->getSource()->count(); - $context['sandbox']['batch_limit'] = $executable->calculateBatchLimit($context); - $context['results'][$migration->id()] = [ - '@numitems' => 0, - '@created' => 0, - '@updated' => 0, - '@failures' => 0, - '@ignored' => 0, - '@name' => $migration->id(), - ]; - } - - // Every iteration, we reset out batch counter. - $context['sandbox']['batch_counter'] = 0; - - // Make sure we know our batch context. - $executable->setBatchContext($context); - - // Do the import. - $result = $executable->import(); - - // Store the result; will need to combine the results of all our iterations. - $context['results'][$migration->id()] = [ - '@numitems' => $context['results'][$migration->id()]['@numitems'] + $executable->getProcessedCount(), - '@created' => $context['results'][$migration->id()]['@created'] + $executable->getCreatedCount(), - '@updated' => $context['results'][$migration->id()]['@updated'] + $executable->getUpdatedCount(), - '@failures' => $context['results'][$migration->id()]['@failures'] + $executable->getFailedCount(), - '@ignored' => $context['results'][$migration->id()]['@ignored'] + $executable->getIgnoredCount(), - '@name' => $migration->id(), - ]; - - // Do some housekeeping. - if ( - $result != MigrationInterface::RESULT_INCOMPLETE - ) { - $context['finished'] = 1; - } - else { - $context['sandbox']['counter'] = $context['results'][$migration->id()]['@numitems']; - if ($context['sandbox']['counter'] <= $context['sandbox']['total']) { - $context['finished'] = ((float) $context['sandbox']['counter'] / (float) $context['sandbox']['total']); - $context['message'] = t('Importing %migration (@percent%).', [ - '%migration' => $migration->label(), - '@percent' => (int) ($context['finished'] * 100), - ]); - } - } - - } - -}