/**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     // Create a mock migration. This will be injected into the source plugin
     // under test.
     $this->migration = $this->prophesize(MigrationInterface::class);
     $this->migration->id()->willReturn($this->randomMachineName(16));
     // Prophesize a useless ID map plugin and an empty set of destination IDs.
     // Calling code can override these prophecies later and set up different
     // behaviors.
     $this->migration->getIdMap()->willReturn($this->prophesize(MigrateIdMapInterface::class)->reveal());
     $this->migration->getDestinationIds()->willReturn([]);
 }
 /**
  * {@inheritdoc}
  */
 public function prepareRow(Row $row)
 {
     $result = TRUE;
     try {
         $result_hook = $this->getModuleHandler()->invokeAll('migrate_prepare_row', array($row, $this, $this->migration));
         $result_named_hook = $this->getModuleHandler()->invokeAll('migrate_' . $this->migration->id() . '_prepare_row', array($row, $this, $this->migration));
         // We will skip if any hook returned FALSE.
         $skip = $result_hook && in_array(FALSE, $result_hook) || $result_named_hook && in_array(FALSE, $result_named_hook);
         $save_to_map = TRUE;
     } catch (MigrateSkipRowException $e) {
         $skip = TRUE;
         $save_to_map = $e->getSaveToMap();
     }
     // We're explicitly skipping this row - keep track in the map table.
     if ($skip) {
         // Make sure we replace any previous messages for this item with any
         // new ones.
         if ($save_to_map) {
             $this->idMap->saveIdMapping($row, array(), MigrateIdMapInterface::STATUS_IGNORED);
             $this->currentRow = NULL;
             $this->currentSourceIds = NULL;
         }
         $result = FALSE;
     } elseif ($this->trackChanges) {
         // When tracking changed data, We want to quietly skip (rather than
         // "ignore") rows with changes. The caller needs to make that decision,
         // so we need to provide them with the necessary information (before and
         // after hashes).
         $row->rehash();
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function rollback()
 {
     // Only begin the rollback operation if the migration is currently idle.
     if ($this->migration->getStatus() !== MigrationInterface::STATUS_IDLE) {
         $this->message->display($this->t('Migration @id is busy with another operation: @status', ['@id' => $this->migration->id(), '@status' => $this->t($this->migration->getStatusLabel())]), 'error');
         return MigrationInterface::RESULT_FAILED;
     }
     // Announce that rollback is about to happen.
     $this->getEventDispatcher()->dispatch(MigrateEvents::PRE_ROLLBACK, new MigrateRollbackEvent($this->migration));
     // Optimistically assume things are going to work out; if not, $return will be
     // updated to some other status.
     $return = MigrationInterface::RESULT_COMPLETED;
     $this->migration->setStatus(MigrationInterface::STATUS_ROLLING_BACK);
     $id_map = $this->migration->getIdMap();
     $destination = $this->migration->getDestinationPlugin();
     // Loop through each row in the map, and try to roll it back.
     foreach ($id_map as $map_row) {
         $destination_key = $id_map->currentDestination();
         if ($destination_key) {
             $map_row = $id_map->getRowByDestination($destination_key);
             if ($map_row['rollback_action'] == MigrateIdMapInterface::ROLLBACK_DELETE) {
                 $this->getEventDispatcher()->dispatch(MigrateEvents::PRE_ROW_DELETE, new MigrateRowDeleteEvent($this->migration, $destination_key));
                 $destination->rollback($destination_key);
                 $this->getEventDispatcher()->dispatch(MigrateEvents::POST_ROW_DELETE, new MigrateRowDeleteEvent($this->migration, $destination_key));
             }
             // We're now done with this row, so remove it from the map.
             $id_map->deleteDestination($destination_key);
         } else {
             // If there is no destination key the import probably failed and we can
             // remove the row without further action.
             $source_key = $id_map->currentSource();
             $id_map->delete($source_key);
         }
         // Check for memory exhaustion.
         if (($return = $this->checkStatus()) != MigrationInterface::RESULT_COMPLETED) {
             break;
         }
         // If anyone has requested we stop, return the requested result.
         if ($this->migration->getStatus() == MigrationInterface::STATUS_STOPPING) {
             $return = $this->migration->getInterruptionResult();
             $this->migration->clearInterruptionResult();
             break;
         }
     }
     // If rollback completed successfully, reset the high water mark.
     if ($return == MigrationInterface::RESULT_COMPLETED) {
         $this->migration->saveHighWater(NULL);
     }
     // Notify modules that rollback attempt was complete.
     $this->getEventDispatcher()->dispatch(MigrateEvents::POST_ROLLBACK, new MigrateRollbackEvent($this->migration));
     $this->migration->setStatus(MigrationInterface::STATUS_IDLE);
     return $return;
 }
Example #4
0
 /**
  * Initialize the plugin.
  */
 protected function init()
 {
     if (!$this->initialized) {
         $this->initialized = TRUE;
         // Default generated table names, limited to 63 characters.
         $machine_name = str_replace(':', '__', $this->migration->id());
         $prefix_length = strlen($this->getDatabase()->tablePrefix());
         $this->mapTableName = 'migrate_map_' . Unicode::strtolower($machine_name);
         $this->mapTableName = Unicode::substr($this->mapTableName, 0, 63 - $prefix_length);
         $this->messageTableName = 'migrate_message_' . Unicode::strtolower($machine_name);
         $this->messageTableName = Unicode::substr($this->messageTableName, 0, 63 - $prefix_length);
         $this->ensureTables();
     }
 }
 /**
  * {@inheritdoc}
  */
 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL)
 {
     $migration_configuration['migration'][] = $migration->id();
     return new static($configuration, $plugin_id, $plugin_definition, $container->get('plugin.manager.migrate.process')->createInstance('migration', $migration_configuration, $migration), $container->get('plugin.manager.menu.link'), $container->get('entity.manager')->getStorage('menu_link_content'));
 }
Example #6
0
/**
 * Allows adding data to a row before processing it.
 *
 * For example, filter module used to store filter format settings in the
 * variables table which now needs to be inside the filter format config
 * file. So, it needs to be added here.
 *
 * hook_migrate_MIGRATION_ID_prepare_row() is also available.
 *
 * @ingroup migration
 */
function hook_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration)
{
    if ($migration->id() == 'd6_filter_formats') {
        $value = $source->getDatabase()->query('SELECT value FROM {variable} WHERE name = :name', array(':name' => 'mymodule_filter_foo_' . $row->getSourceProperty('format')))->fetchField();
        if ($value) {
            $row->setSourceProperty('settings:mymodule:foo', unserialize($value));
        }
    }
}
Example #7
0
 /**
  * Save the new high water mark.
  *
  * @param int $high_water
  *   The high water timestamp.
  */
 protected function saveHighWater($high_water)
 {
     $this->getHighWaterStorage()->set($this->migration->id(), $high_water);
 }