3 namespace Drupal\migrate\Plugin;
5 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6 use Drupal\Core\Plugin\PluginBase;
7 use Drupal\migrate\Exception\RequirementsException;
8 use Drupal\migrate\MigrateException;
9 use Drupal\migrate\MigrateSkipRowException;
10 use Drupal\Component\Utility\NestedArray;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Defines the Migration plugin.
16 * The migration process plugin represents one single migration and acts like a
17 * container for the information about a single migration such as the source,
18 * process and destination plugins.
20 class Migration extends PluginBase implements MigrationInterface, RequirementsInterface, ContainerFactoryPluginInterface {
23 * The migration ID (machine name).
30 * The human-readable label for the migration.
37 * The plugin ID for the row.
44 * The source configuration, with at least a 'plugin' key.
46 * Used to initialize the $sourcePlugin.
55 * @var \Drupal\migrate\Plugin\MigrateSourceInterface
57 protected $sourcePlugin;
60 * The configuration describing the process plugins.
62 * This is a strictly internal property and should not returned to calling
63 * code, use getProcess() instead.
67 protected $process = [];
70 * The cached process plugins.
74 protected $processPlugins = [];
77 * The destination configuration, with at least a 'plugin' key.
79 * Used to initialize $destinationPlugin.
83 protected $destination;
86 * The destination plugin.
88 * @var \Drupal\migrate\Plugin\MigrateDestinationInterface
90 protected $destinationPlugin;
93 * The identifier map data.
95 * Used to initialize $idMapPlugin.
99 protected $idMap = [];
102 * The identifier map.
104 * @var \Drupal\migrate\Plugin\MigrateIdMapInterface
106 protected $idMapPlugin;
109 * The source identifiers.
111 * An array of source identifiers: the keys are the name of the properties,
112 * the values are dependent on the ID map plugin.
116 protected $sourceIds = [];
119 * The destination identifiers.
121 * An array of destination identifiers: the keys are the name of the
122 * properties, the values are dependent on the ID map plugin.
126 protected $destinationIds = [];
129 * Specify value of source_row_status for current map row. Usually set by
130 * MigrateFieldHandler implementations.
134 protected $sourceRowStatus = MigrateIdMapInterface::STATUS_IMPORTED;
137 * Track time of last import if TRUE.
141 protected $trackLastImported = FALSE;
144 * These migrations must be already executed before this migration can run.
148 protected $requirements = [];
151 * An optional list of tags, used by the plugin manager for filtering.
155 protected $migration_tags = [];
158 * These migrations, if run, must be executed before this migration.
160 * These are different from the configuration dependencies. Migration
161 * dependencies are only used to store relationships between migrations.
163 * The migration_dependencies value is structured like this:
166 * 'required' => array(
167 * // An array of migration IDs that must be run before this migration.
169 * 'optional' => array(
170 * // An array of migration IDs that, if they exist, must be run before
178 protected $migration_dependencies = [];
181 * The migration's configuration dependencies.
183 * These store any dependencies on modules or other configuration (including
184 * other migrations) that must be available before the migration can be
187 * @see \Drupal\Core\Config\Entity\ConfigDependencyManager
191 protected $dependencies = [];
194 * The migration plugin manager for loading other migration plugins.
196 * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
198 protected $migrationPluginManager;
201 * The source plugin manager.
203 * @var \Drupal\migrate\Plugin\MigratePluginManager
205 protected $sourcePluginManager;
208 * Thep process plugin manager.
210 * @var \Drupal\migrate\Plugin\MigratePluginManager
212 protected $processPluginManager;
215 * The destination plugin manager.
217 * @var \Drupal\migrate\Plugin\MigrateDestinationPluginManager
219 protected $destinationPluginManager;
222 * The ID map plugin manager.
224 * @var \Drupal\migrate\Plugin\MigratePluginManager
226 protected $idMapPluginManager;
229 * Labels corresponding to each defined status.
233 protected $statusLabels = [
234 self::STATUS_IDLE => 'Idle',
235 self::STATUS_IMPORTING => 'Importing',
236 self::STATUS_ROLLING_BACK => 'Rolling back',
237 self::STATUS_STOPPING => 'Stopping',
238 self::STATUS_DISABLED => 'Disabled',
242 * Constructs a Migration.
244 * @param array $configuration
245 * Plugin configuration.
246 * @param string $plugin_id
248 * @param mixed $plugin_definition
249 * The plugin definition.
250 * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
251 * The migration plugin manager.
252 * @param \Drupal\migrate\Plugin\MigratePluginManagerInterface $source_plugin_manager
253 * The source migration plugin manager.
254 * @param \Drupal\migrate\Plugin\MigratePluginManagerInterface $process_plugin_manager
255 * The process migration plugin manager.
256 * @param \Drupal\migrate\Plugin\MigrateDestinationPluginManager $destination_plugin_manager
257 * The destination migration plugin manager.
258 * @param \Drupal\migrate\Plugin\MigratePluginManagerInterface $idmap_plugin_manager
259 * The ID map migration plugin manager.
261 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationPluginManagerInterface $migration_plugin_manager, MigratePluginManagerInterface $source_plugin_manager, MigratePluginManagerInterface $process_plugin_manager, MigrateDestinationPluginManager $destination_plugin_manager, MigratePluginManagerInterface $idmap_plugin_manager) {
262 parent::__construct($configuration, $plugin_id, $plugin_definition);
263 $this->migrationPluginManager = $migration_plugin_manager;
264 $this->sourcePluginManager = $source_plugin_manager;
265 $this->processPluginManager = $process_plugin_manager;
266 $this->destinationPluginManager = $destination_plugin_manager;
267 $this->idMapPluginManager = $idmap_plugin_manager;
269 foreach (NestedArray::mergeDeep($plugin_definition, $configuration) as $key => $value) {
270 $this->$key = $value;
277 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
282 $container->get('plugin.manager.migration'),
283 $container->get('plugin.manager.migrate.source'),
284 $container->get('plugin.manager.migrate.process'),
285 $container->get('plugin.manager.migrate.destination'),
286 $container->get('plugin.manager.migrate.id_map')
293 public function id() {
294 return $this->pluginId;
300 public function label() {
305 * Gets any arbitrary property's value.
307 * @param string $property
308 * The property to retrieve.
311 * The value for that property, or NULL if the property does not exist.
313 * @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.x. Use
314 * more specific getters instead.
316 public function get($property) {
317 return isset($this->$property) ? $this->$property : NULL;
321 * Retrieves the ID map plugin.
323 * @return \Drupal\migrate\Plugin\MigrateIdMapInterface
326 public function getIdMapPlugin() {
327 return $this->idMapPlugin;
333 public function getSourcePlugin() {
334 if (!isset($this->sourcePlugin)) {
335 $this->sourcePlugin = $this->sourcePluginManager->createInstance($this->source['plugin'], $this->source, $this);
337 return $this->sourcePlugin;
343 public function getProcessPlugins(array $process = NULL) {
344 if (!isset($process)) {
345 $process = $this->getProcess();
347 $index = serialize($process);
348 if (!isset($this->processPlugins[$index])) {
349 $this->processPlugins[$index] = [];
350 foreach ($this->getProcessNormalized($process) as $property => $configurations) {
351 $this->processPlugins[$index][$property] = [];
352 foreach ($configurations as $configuration) {
353 if (isset($configuration['source'])) {
354 $this->processPlugins[$index][$property][] = $this->processPluginManager->createInstance('get', $configuration, $this);
356 // Get is already handled.
357 if ($configuration['plugin'] != 'get') {
358 $this->processPlugins[$index][$property][] = $this->processPluginManager->createInstance($configuration['plugin'], $configuration, $this);
360 if (!$this->processPlugins[$index][$property]) {
361 throw new MigrateException("Invalid process configuration for $property");
366 return $this->processPlugins[$index];
370 * Resolve shorthands into a list of plugin configurations.
372 * @param array $process
373 * A process configuration array.
376 * The normalized process configuration.
378 protected function getProcessNormalized(array $process) {
379 $normalized_configurations = [];
380 foreach ($process as $destination => $configuration) {
381 if (is_string($configuration)) {
384 'source' => $configuration,
387 if (isset($configuration['plugin'])) {
388 $configuration = [$configuration];
390 $normalized_configurations[$destination] = $configuration;
392 return $normalized_configurations;
398 public function getDestinationPlugin($stub_being_requested = FALSE) {
399 if ($stub_being_requested && !empty($this->destination['no_stub'])) {
400 throw new MigrateSkipRowException();
402 if (!isset($this->destinationPlugin)) {
403 $this->destinationPlugin = $this->destinationPluginManager->createInstance($this->destination['plugin'], $this->destination, $this);
405 return $this->destinationPlugin;
411 public function getIdMap() {
412 if (!isset($this->idMapPlugin)) {
413 $configuration = $this->idMap;
414 $plugin = isset($configuration['plugin']) ? $configuration['plugin'] : 'sql';
415 $this->idMapPlugin = $this->idMapPluginManager->createInstance($plugin, $configuration, $this);
417 return $this->idMapPlugin;
423 public function checkRequirements() {
424 // Check whether the current migration source and destination plugin
425 // requirements are met or not.
426 if ($this->getSourcePlugin() instanceof RequirementsInterface) {
427 $this->getSourcePlugin()->checkRequirements();
429 if ($this->getDestinationPlugin() instanceof RequirementsInterface) {
430 $this->getDestinationPlugin()->checkRequirements();
433 if (empty($this->requirements)) {
434 // There are no requirements to check.
437 /** @var \Drupal\migrate\Plugin\MigrationInterface[] $required_migrations */
438 $required_migrations = $this->getMigrationPluginManager()->createInstances($this->requirements);
440 $missing_migrations = array_diff($this->requirements, array_keys($required_migrations));
441 // Check if the dependencies are in good shape.
442 foreach ($required_migrations as $migration_id => $required_migration) {
443 if (!$required_migration->allRowsProcessed()) {
444 $missing_migrations[] = $migration_id;
447 if ($missing_migrations) {
448 throw new RequirementsException('Missing migrations ' . implode(', ', $missing_migrations) . '.', ['requirements' => $missing_migrations]);
453 * Gets the migration plugin manager.
455 * @return \Drupal\migrate\Plugin\MigratePluginManager
456 * The plugin manager.
458 protected function getMigrationPluginManager() {
459 return $this->migrationPluginManager;
465 public function setStatus($status) {
466 \Drupal::keyValue('migrate_status')->set($this->id(), $status);
472 public function getStatus() {
473 return \Drupal::keyValue('migrate_status')->get($this->id(), static::STATUS_IDLE);
479 public function getStatusLabel() {
480 $status = $this->getStatus();
481 if (isset($this->statusLabels[$status])) {
482 return $this->statusLabels[$status];
492 public function getInterruptionResult() {
493 return \Drupal::keyValue('migrate_interruption_result')->get($this->id(), static::RESULT_INCOMPLETE);
499 public function clearInterruptionResult() {
500 \Drupal::keyValue('migrate_interruption_result')->delete($this->id());
506 public function interruptMigration($result) {
507 $this->setStatus(MigrationInterface::STATUS_STOPPING);
508 \Drupal::keyValue('migrate_interruption_result')->set($this->id(), $result);
514 public function allRowsProcessed() {
515 $source_count = $this->getSourcePlugin()->count();
516 // If the source is uncountable, we have no way of knowing if it's
517 // complete, so stipulate that it is.
518 if ($source_count < 0) {
521 $processed_count = $this->getIdMap()->processedCount();
522 // We don't use == because in some circumstances (like unresolved stubs
523 // being created), the processed count may be higher than the available
525 return $source_count <= $processed_count;
531 public function set($property_name, $value) {
532 if ($property_name == 'source') {
533 // Invalidate the source plugin.
534 unset($this->sourcePlugin);
536 elseif ($property_name === 'destination') {
537 // Invalidate the destination plugin.
538 unset($this->destinationPlugin);
540 $this->{$property_name} = $value;
548 public function getProcess() {
549 return $this->getProcessNormalized($this->process);
555 public function setProcess(array $process) {
556 $this->process = $process;
563 public function setProcessOfProperty($property, $process_of_property) {
564 $this->process[$property] = $process_of_property;
571 public function mergeProcessOfProperty($property, array $process_of_property) {
572 // If we already have a process value then merge the incoming process array
573 //otherwise simply set it.
574 $current_process = $this->getProcess();
575 if (isset($current_process[$property])) {
576 $this->process = NestedArray::mergeDeepArray([$current_process, $this->getProcessNormalized([$property => $process_of_property])], TRUE);
579 $this->setProcessOfProperty($property, $process_of_property);
588 public function isTrackLastImported() {
589 return $this->trackLastImported;
595 public function setTrackLastImported($track_last_imported) {
596 $this->trackLastImported = (bool) $track_last_imported;
603 public function getMigrationDependencies() {
604 $this->migration_dependencies = ($this->migration_dependencies ?: []) + ['required' => [], 'optional' => []];
605 $this->migration_dependencies['optional'] = array_unique(array_merge($this->migration_dependencies['optional'], $this->findMigrationDependencies($this->process)));
606 return $this->migration_dependencies;
610 * Find migration dependencies from the migration and the iterator plugins.
615 protected function findMigrationDependencies($process) {
617 foreach ($this->getProcessNormalized($process) as $process_pipeline) {
618 foreach ($process_pipeline as $plugin_configuration) {
619 if ($plugin_configuration['plugin'] == 'migration') {
620 $return = array_merge($return, (array) $plugin_configuration['migration']);
622 if ($plugin_configuration['plugin'] == 'iterator') {
623 $return = array_merge($return, $this->findMigrationDependencies($plugin_configuration['process']));
633 public function getPluginDefinition() {
635 // While normal plugins do not change their definitions on the fly, this
636 // one does so accommodate for that.
637 foreach (parent::getPluginDefinition() as $key => $value) {
638 $definition[$key] = isset($this->$key) ? $this->$key : $value;
646 public function getDestinationConfiguration() {
647 return $this->destination;
653 public function getSourceConfiguration() {
654 return $this->source;
660 public function getTrackLastImported() {
661 return $this->trackLastImported;
667 public function getDestinationIds() {
668 return $this->destinationIds;
674 public function getMigrationTags() {
675 return $this->migration_tags;