You are here

class IdAuditor in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/migrate/src/Audit/IdAuditor.php \Drupal\migrate\Audit\IdAuditor
  2. 10 core/modules/migrate/src/Audit/IdAuditor.php \Drupal\migrate\Audit\IdAuditor

Audits migrations that create content entities in the destination system.

Hierarchy

Expanded class hierarchy of IdAuditor

3 files declare their use of IdAuditor
IdConflictForm.php in core/modules/migrate_drupal_ui/src/Form/IdConflictForm.php
MigrateDrupal6AuditIdsTest.php in core/modules/migrate_drupal/tests/src/Kernel/d6/MigrateDrupal6AuditIdsTest.php
MigrateDrupal7AuditIdsTest.php in core/modules/migrate_drupal/tests/src/Kernel/d7/MigrateDrupal7AuditIdsTest.php

File

core/modules/migrate/src/Audit/IdAuditor.php, line 12

Namespace

Drupal\migrate\Audit
View source
class IdAuditor implements AuditorInterface {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function audit(MigrationInterface $migration) {

    // If the migration does not opt into auditing, it passes.
    if (!$migration
      ->isAuditable()) {
      return AuditResult::pass($migration);
    }
    $interface = HighestIdInterface::class;
    $destination = $migration
      ->getDestinationPlugin();
    if (!$destination instanceof HighestIdInterface) {
      throw new AuditException($migration, "Destination does not implement {$interface}");
    }
    $id_map = $migration
      ->getIdMap();
    if (!$id_map instanceof HighestIdInterface) {
      throw new AuditException($migration, "ID map does not implement {$interface}");
    }
    if ($destination
      ->getHighestId() > $id_map
      ->getHighestId() || $destination instanceof EntityContentComplete && !$this
      ->auditEntityComplete($migration)) {
      return AuditResult::fail($migration, [
        $this
          ->t('The destination system contains data which was not created by a migration.'),
      ]);
    }
    return AuditResult::pass($migration);
  }

  /**
   * {@inheritdoc}
   */
  public function auditMultiple(array $migrations) {
    $conflicts = [];
    foreach ($migrations as $migration) {
      $migration_id = $migration
        ->getPluginId();
      $conflicts[$migration_id] = $this
        ->audit($migration);
    }
    ksort($conflicts);
    return $conflicts;
  }

  /**
   * Audits an EntityComplete migration.
   *
   * @param \Drupal\migrate\Plugin\MigrationInterface $migration
   *   The migration to audit.
   *
   * @return bool
   *   TRUE if the audit passes and FALSE if not.
   *
   * @todo Refactor in https://www.drupal.org/project/drupal/issues/3061676 or
   *   https://www.drupal.org/project/drupal/issues/3091004
   */
  private function auditEntityComplete(MigrationInterface $migration) {
    $map_table = $migration
      ->getIdMap()
      ->mapTableName();
    $database = \Drupal::database();
    if (!$database
      ->schema()
      ->tableExists($map_table)) {
      throw new \InvalidArgumentException();
    }
    $query = $database
      ->select($map_table, 'map')
      ->fields('map', [
      'destid2',
    ])
      ->range(0, 1)
      ->orderBy('destid2', 'DESC');
    $max = (int) $query
      ->execute()
      ->fetchField();

    // Make a migration based on node_complete but with an entity_revision
    // destination.
    $revision_migration = $migration
      ->getPluginDefinition();
    $revision_migration['id'] = $migration
      ->getPluginId() . '-revision';
    $revision_migration['destination']['plugin'] = 'entity_revision:node';
    $revision_migration = \Drupal::service('plugin.manager.migration')
      ->createStubMigration($revision_migration);

    // Get the highest node revision ID.
    $destination = $revision_migration
      ->getDestinationPlugin();
    $highest = $destination
      ->getHighestId();
    return $max <= $highest;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
IdAuditor::audit public function Audits a migration. Overrides AuditorInterface::audit
IdAuditor::auditEntityComplete private function Audits an EntityComplete migration.
IdAuditor::auditMultiple public function Audits a set of migrations. Overrides AuditorInterface::auditMultiple
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.