From fee4c4e21d13a7bba8730307168da49b074d0450 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Mon, 21 Nov 2016 12:26:53 +0000 Subject: [PATCH] Issue #2687851 by quietone, mikeryan, heddn, xjm, catch: Refactor run() method on Migrate UI batch and remove the $operation parameter --- .../src/{ => Batch}/MigrateMessageCapture.php | 2 +- .../MigrateUpgradeImportBatch.php} | 103 +++++++----------- .../src/Form/MigrateUpgradeForm.php | 9 +- 3 files changed, 42 insertions(+), 72 deletions(-) rename core/modules/migrate_drupal_ui/src/{ => Batch}/MigrateMessageCapture.php (94%) rename core/modules/migrate_drupal_ui/src/{MigrateUpgradeRunBatch.php => Batch/MigrateUpgradeImportBatch.php} (74%) diff --git a/core/modules/migrate_drupal_ui/src/MigrateMessageCapture.php b/core/modules/migrate_drupal_ui/src/Batch/MigrateMessageCapture.php similarity index 94% rename from core/modules/migrate_drupal_ui/src/MigrateMessageCapture.php rename to core/modules/migrate_drupal_ui/src/Batch/MigrateMessageCapture.php index 72e1ace1568..efa0a57a414 100644 --- a/core/modules/migrate_drupal_ui/src/MigrateMessageCapture.php +++ b/core/modules/migrate_drupal_ui/src/Batch/MigrateMessageCapture.php @@ -1,6 +1,6 @@ addListener(MigrateEvents::POST_ROW_SAVE, [static::class, 'onPostRowSave']); - $event_dispatcher->addListener(MigrateEvents::MAP_SAVE, [static::class, 'onMapSave']); - $event_dispatcher->addListener(MigrateEvents::IDMAP_MESSAGE, [static::class, 'onIdMapMessage']); - } + $event_dispatcher->addListener(MigrateEvents::POST_ROW_SAVE, [static::class, 'onPostRowSave']); + $event_dispatcher->addListener(MigrateEvents::MAP_SAVE, [static::class, 'onMapSave']); + $event_dispatcher->addListener(MigrateEvents::IDMAP_MESSAGE, [static::class, 'onIdMapMessage']); + static::$maxExecTime = ini_get('max_execution_time'); if (static::$maxExecTime <= 0) { static::$maxExecTime = 60; @@ -98,7 +92,6 @@ class MigrateUpgradeRunBatch { $context['sandbox']['messages'] = []; $context['results']['failures'] = 0; $context['results']['successes'] = 0; - $context['results']['operation'] = $operation; } // Number processed in this batch. @@ -124,12 +117,10 @@ class MigrateUpgradeRunBatch { $migration_name = $migration->label() ? $migration->label() : $migration_id; try { - if ($operation == 'import') { - $migration_status = $executable->import(); - } + $migration_status = $executable->import(); } catch (\Exception $e) { - static::logger()->error($e->getMessage()); + \Drupal::logger('migrate_drupal_ui')->error($e->getMessage()); $migration_status = MigrationInterface::RESULT_FAILED; } @@ -137,13 +128,11 @@ class MigrateUpgradeRunBatch { case MigrationInterface::RESULT_COMPLETED: // Store the number processed in the sandbox. $context['sandbox']['num_processed'] += static::$numProcessed; - if ($operation == 'import') { - $message = new PluralTranslatableMarkup( - $context['sandbox']['num_processed'], 'Upgraded @migration (processed 1 item total)', 'Upgraded @migration (processed @count items total)', - ['@migration' => $migration_name]); - } + $message = new PluralTranslatableMarkup( + $context['sandbox']['num_processed'], 'Upgraded @migration (processed 1 item total)', 'Upgraded @migration (processed @count items total)', + ['@migration' => $migration_name]); $context['sandbox']['messages'][] = (string) $message; - static::logger()->notice($message); + \Drupal::logger('migrate_drupal_ui')->notice($message); $context['sandbox']['num_processed'] = 0; $context['results']['successes']++; break; @@ -162,12 +151,12 @@ class MigrateUpgradeRunBatch { case MigrationInterface::RESULT_FAILED: $context['sandbox']['messages'][] = (string) new TranslatableMarkup('Operation on @migration failed', ['@migration' => $migration_name]); $context['results']['failures']++; - static::logger()->error('Operation on @migration failed', ['@migration' => $migration_name]); + \Drupal::logger('migrate_drupal_ui')->error('Operation on @migration failed', ['@migration' => $migration_name]); break; case MigrationInterface::RESULT_SKIPPED: $context['sandbox']['messages'][] = (string) new TranslatableMarkup('Operation on @migration skipped due to unfulfilled dependencies', ['@migration' => $migration_name]); - static::logger()->error('Operation on @migration skipped due to unfulfilled dependencies', ['@migration' => $migration_name]); + \Drupal::logger('migrate_drupal_ui')->error('Operation on @migration skipped due to unfulfilled dependencies', ['@migration' => $migration_name]); break; case MigrationInterface::RESULT_DISABLED: @@ -184,7 +173,7 @@ class MigrateUpgradeRunBatch { // Add and log any captured messages. foreach (static::$messages->getMessages() as $message) { $context['sandbox']['messages'][] = (string) $message; - static::logger()->error($message); + \Drupal::logger('migrate_drupal_ui')->error($message); } // Only display the last MESSAGE_LENGTH messages, in reverse order. @@ -203,13 +192,11 @@ class MigrateUpgradeRunBatch { $migration_id = reset($context['sandbox']['migration_ids']); $migration = \Drupal::service('plugin.manager.migration')->createInstance($migration_id); $migration_name = $migration->label() ? $migration->label() : $migration_id; - if ($operation == 'import') { - $context['message'] = (string) new TranslatableMarkup('Currently upgrading @migration (@current of @max total tasks)', [ + $context['message'] = (string) new TranslatableMarkup('Currently upgrading @migration (@current of @max total tasks)', [ '@migration' => $migration_name, '@current' => $context['sandbox']['current'], '@max' => $context['sandbox']['max'], ]) . "
\n" . $context['message']; - } } } else { @@ -221,52 +208,36 @@ class MigrateUpgradeRunBatch { } /** - * Returns the logger using the migrate_drupal_ui channel. + * Callback executed when the Migrate Upgrade Import batch process completes. * - * @return \Psr\Log\LoggerInterface - * The logger instance. - */ - protected static function logger() { - return \Drupal::logger('migrate_drupal_ui'); - } - - /** - * Implements the Batch API finished method. + * @param bool $success + * TRUE if batch successfully completed. + * @param array $results + * Batch results. + * @param array $operations + * An array of methods run in the batch. + * @param string $elapsed + * The time to run the batch. */ public static function finished($success, $results, $operations, $elapsed) { - static::displayResults($results); - } - - /** - * Displays counts of success/failures on the migration upgrade complete page. - * - * @param array $results - * An array of result data built during the batch. - */ - protected static function displayResults($results) { $successes = $results['successes']; $failures = $results['failures']; // If we had any successes log that for the user. if ($successes > 0) { - if ($results['operation'] == 'import') { - drupal_set_message(new PluralTranslatableMarkup($successes, 'Completed 1 upgrade task successfully', 'Completed @count upgrade tasks successfully')); - } + drupal_set_message(\Drupal::translation() + ->formatPlural($successes, 'Completed 1 upgrade task successfully', 'Completed @count upgrade tasks successfully')); } - // If we had failures, log them and show the migration failed. if ($failures > 0) { - if ($results['operation'] == 'import') { - drupal_set_message(new PluralTranslatableMarkup($failures, '1 upgrade failed', '@count upgrades failed')); - drupal_set_message(new TranslatableMarkup('Upgrade process not completed'), 'error'); - } + drupal_set_message(\Drupal::translation() + ->formatPlural($failures, '1 upgrade failed', '@count upgrades failed')); + drupal_set_message(t('Upgrade process not completed'), 'error'); } else { - if ($results['operation'] == 'import') { - // Everything went off without a hitch. We may not have had successes - // but we didn't have failures so this is fine. - drupal_set_message(new TranslatableMarkup('Congratulations, you upgraded Drupal!')); - } + // Everything went off without a hitch. We may not have had successes + // but we didn't have failures so this is fine. + drupal_set_message(t('Congratulations, you upgraded Drupal!')); } if (\Drupal::moduleHandler()->moduleExists('dblog')) { @@ -335,7 +306,7 @@ class MigrateUpgradeRunBatch { $type = 'error'; } $source_id_string = implode(',', $event->getSourceIdValues()); - $message = new TranslatableMarkup('Source ID @source_id: @message', ['@source_id' => $source_id_string, '@message' => $event->getMessage()]); + $message = t('Source ID @source_id: @message', ['@source_id' => $source_id_string, '@message' => $event->getMessage()]); static::$messages->display($message, $type); } diff --git a/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php b/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php index 9ea659b1aa0..f90c993d7a4 100644 --- a/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php +++ b/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php @@ -9,7 +9,7 @@ use Drupal\Core\Render\RendererInterface; use Drupal\Core\State\StateInterface; use Drupal\Core\Url; use Drupal\migrate\Plugin\MigrationPluginManagerInterface; -use Drupal\migrate_drupal_ui\MigrateUpgradeRunBatch; +use Drupal\migrate_drupal_ui\Batch\MigrateUpgradeImportBatch; use Drupal\migrate_drupal\MigrationConfigurationTrait; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -1125,13 +1125,12 @@ class MigrateUpgradeForm extends ConfirmFormBase { 'progress_message' => '', 'operations' => [ [ - [MigrateUpgradeRunBatch::class, 'run'], - [array_keys($migrations), 'import', $config], + [MigrateUpgradeImportBatch::class, 'run'], + [array_keys($migrations), $config], ], ], 'finished' => [ - MigrateUpgradeRunBatch::class, - 'finished', + MigrateUpgradeImportBatch::class, 'finished', ], ]; batch_set($batch);