View source
<?php
namespace Drupal\migrate_tools;
use Drupal\migrate\MigrateMessage;
use Drupal\migrate\MigrateMessageInterface;
use Drupal\migrate\Plugin\MigrationInterface;
class MigrateBatchExecutable extends MigrateExecutable {
const BATCH_IMPORT = 1;
protected $updateExistingRows = 0;
protected $checkDependencies = 0;
protected $syncSource = FALSE;
protected $batchContext = [];
protected $migrationPluginManager;
public function __construct(MigrationInterface $migration, MigrateMessageInterface $message, array $options = []) {
if (isset($options['update'])) {
$this->updateExistingRows = $options['update'];
}
if (isset($options['force'])) {
$this->checkDependencies = $options['force'];
}
if (isset($options['sync'])) {
$this->syncSource = $options['sync'];
}
parent::__construct($migration, $message, $options);
$this->migrationPluginManager = \Drupal::getContainer()
->get('plugin.manager.migration');
}
public function setBatchContext(&$context) {
$this->batchContext =& $context;
}
public function &getBatchContext() {
return $this->batchContext;
}
public function batchImport() {
$operations = $this
->batchOperations([
$this->migration,
], 'import', [
'limit' => $this->itemLimit,
'update' => $this->updateExistingRows,
'force' => $this->checkDependencies,
'sync' => $this->syncSource,
]);
if (count($operations) > 0) {
$batch = [
'operations' => $operations,
'title' => $this
->t('Migrating %migrate', [
'%migrate' => $this->migration
->label(),
]),
'init_message' => $this
->t('Start migrating %migrate', [
'%migrate' => $this->migration
->label(),
]),
'progress_message' => $this
->t('Migrating %migrate', [
'%migrate' => $this->migration
->label(),
]),
'error_message' => $this
->t('An error occurred while migrating %migrate.', [
'%migrate' => $this->migration
->label(),
]),
'finished' => '\\Drupal\\migrate_tools\\MigrateBatchExecutable::batchFinishedImport',
];
batch_set($batch);
}
}
protected function batchOperations(array $migrations, $operation, array $options = []) {
$operations = [];
foreach ($migrations as $id => $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']);
$operations = array_merge($operations, $this
->batchOperations($required_migrations, $operation, [
'limit' => 0,
'update' => $options['update'],
'force' => $options['force'],
'sync' => $options['sync'],
]));
}
}
$operations[] = [
'\\Drupal\\migrate_tools\\MigrateBatchExecutable::batchProcessImport',
[
$migration
->id(),
$options,
],
];
}
return $operations;
}
public static function batchProcessImport($migration_id, array $options, &$context) {
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;
}
$message = new MigrateMessage();
$migration = \Drupal::getContainer()
->get('plugin.manager.migration')
->createInstance($migration_id, isset($options['configuration']) ? $options['configuration'] : []);
unset($options['configuration']);
if (!empty($options['limit']) && isset($context['results'][$migration
->id()]['@numitems'])) {
$options['limit'] = $options['limit'] - $context['results'][$migration
->id()]['@numitems'];
}
$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(),
];
}
$context['sandbox']['batch_counter'] = 0;
$executable
->setBatchContext($context);
$result = $executable
->import();
$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(),
];
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'] = (double) $context['sandbox']['counter'] / (double) $context['sandbox']['total'];
$context['message'] = t('Importing %migration (@percent%).', [
'%migration' => $migration
->label(),
'@percent' => (int) ($context['finished'] * 100),
]);
}
}
}
public static function batchFinishedImport($success, array $results, array $operations) {
if ($success) {
foreach ($results as $migration_id => $result) {
$singular_message = "Processed 1 item (@created created, @updated updated, @failures failed, @ignored ignored) - done with '@name'";
$plural_message = "Processed @numitems items (@created created, @updated updated, @failures failed, @ignored ignored) - done with '@name'";
\Drupal::messenger()
->addStatus(\Drupal::translation()
->formatPlural($result['@numitems'], $singular_message, $plural_message, $result));
}
}
}
public function checkStatus() {
$status = parent::checkStatus();
if ($status == MigrationInterface::RESULT_COMPLETED) {
$context = $this
->getBatchContext();
if (!empty($context['sandbox']) && $context['sandbox']['operation'] == MigrateBatchExecutable::BATCH_IMPORT) {
$context['sandbox']['batch_counter']++;
if ($context['sandbox']['batch_counter'] >= $context['sandbox']['batch_limit']) {
$status = MigrationInterface::RESULT_INCOMPLETE;
}
}
}
return $status;
}
public function calculateBatchLimit($context) {
return ceil($context['sandbox']['total'] / 100);
}
}