Commit e1ef4b5c authored by Blanca Esqueda's avatar Blanca Esqueda Committed by Blanca.Esqueda
Browse files

Issue #3128436 by mkalkbrenner, lobodacyril, guiu.rocafort.ferrer,...

Issue #3128436 by mkalkbrenner, lobodacyril, guiu.rocafort.ferrer, Blanca.Esqueda: Imports of circular entity references lead to infinite loop
parent 4ec40201
Loading
Loading
Loading
Loading
+2 −2
Changes for src/ContentSyncManager.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -80,8 +80,8 @@ class ContentSyncManager implements ContentSyncManagerInterface {
      $decoded_entities[$file] = $decoded_entity;
    }
    if (!empty($decoded_entities)) {
      $resolver = new ImportQueueResolver();
      $queue = $resolver->resolve($decoded_entities);
      $resolver = new ImportQueueResolver($decoded_entities);
      $queue = $resolver->resolve();
    }
    return $queue;
  }
+1 −1
Changes for src/DependencyResolver/ContentSyncResolverInterface.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -4,5 +4,5 @@ namespace Drupal\content_sync\DependencyResolver;

interface ContentSyncResolverInterface {

  public function resolve(array $normalized_entities, $visited = []);
  public function resolve();
}
+79 −54
Changes for src/DependencyResolver/ImportQueueResolver.php: 79 added lines, 54 removed lines.
Original line number Diff line number Diff line
@@ -12,53 +12,87 @@ use Drupal\Core\Serialization\Yaml;
class ImportQueueResolver implements ContentSyncResolverInterface {

  /**
   * Builds a graph placing the deepest vertexes at the first place.
   * Constructs an ImportQueueResolver object.
   *
   * @param array $visited
   *   Array of vertexes to return.
   * @param array $identifiers
   *   Array of entity identifiers to process.
   * @param array $normalized_entities
   *   Parsed entities to import.
   * @param Normalized entities $normalized_entities
   *   The normalized data.
   */
  protected function depthFirstSearch(array &$visited, array $identifiers, array $normalized_entities) {
    foreach ($identifiers as $identifier) {

      // Get a decoded entity. FALSE means no need to import.
      try {
        $entity = $this->getEntity($identifier, $normalized_entities);
      } catch (\Exception $e) {
        $entity = FALSE;
        $visited['Missing'][$identifier][] = $e->getMessage();
  public function __construct(array $normalized_entities) {
    $this->normalizedEntities = $normalized_entities;
    $this->rebuild = [];  // Entities with references to ancestors.
    $this->q = [];  // Queue variable.
  }

      // Process dependencies first.
      if (!empty($entity['_content_sync']['entity_dependencies'])) {
        foreach ($entity['_content_sync']['entity_dependencies'] as $ref_entity_type_id => $references) {
          $this->depthFirstSearch($visited, $references, $normalized_entities);
  /**
   * Creates a queue.
   *
   * @return array
   *   Queue to be processed within a batch process.
   */
  public function resolve() {
    $ancestors = [];
    foreach ($this->normalizedEntities as $identifier => $entity) {
      $this->processEntity($identifier, $entity, $ancestors);
    }
    return array_reverse(array_merge($this->q, $this->rebuild));
  }

      // Process translations' dependencies if any.
      if (!empty($entity["_translations"])) {
        foreach ($entity["_translations"] as $translation) {
          if (!empty($translation['_content_sync']['entity_dependencies'])) {
            foreach ($translation['_content_sync']['entity_dependencies'] as $ref_entity_type_id => $references) {
              $this->depthFirstSearch($visited, $references, $normalized_entities);
            }
  /**
   * Get entities dependencies - it considers redundancies.
   */
  public function processEntity( string $identifier, array $entity, array $ancestors) {
    $q = array_column( $this->q, 'identifier');
    if (in_array($identifier, $q)) {
      return;
    }
    $ancestors[] = $identifier;
    $dependencies = $this->getDependencies($entity);
    foreach($dependencies as $dependency){
      $dependency_entity = $this->fetchEntity($dependency);
      if (in_array($dependency ,$ancestors)) {
        $this->rebuild[] = ['identifier' => $identifier,
                            'entity_type_id' => $entity['_content_sync']['entity_type'],
                            'decoded_entity' => $entity ];
      } else {
        $this->processEntity($dependency, $dependency_entity, $ancestors);
      }
    }
    $this->q[] = ['identifier' => $identifier,
                  'entity_type_id' => $entity['_content_sync']['entity_type'],
                  'decoded_entity' => $entity ];
  }

  /**
   * Get dependencies of a given entity.
   *
   * @return array
   *   Dependencies of an entity.
   */
  public function getDependencies($entity) {
    $dependencies = [];
    if ( !empty($entity['_content_sync']['entity_dependencies']) ) {
      foreach ($entity['_content_sync']['entity_dependencies'] as $ref_entity_type_id => $references) {
        $dependencies = array_merge($dependencies, $references);
      }
    }

      if (!isset($visited[$identifier]) && $entity) {
        list($entity_type_id, $bundle, $uuid) = explode('.', $identifier);
        $visited[$identifier] = [
          'entity_type_id' => $entity_type_id,
          'decoded_entity' => $entity,
        ];
    return $dependencies;
  }

  /**
   * Fetch an entity - normalized data.
   *
   * @return array
   *   Entity normalized data.
   */
  public function fetchEntity($identifier) {
    try {
      $entity = $this->getEntity($identifier, $this->normalizedEntities);
    } catch (\Exception $e) {
      $entity = FALSE;
      // TODO: notice/log of what entity is missing.
      // TODO: should the import of the parent entity abort?
    }
    return $entity;
  }

  /**
@@ -77,8 +111,9 @@ class ImportQueueResolver implements ContentSyncResolverInterface {
  protected function getEntity($identifier, $normalized_entities) {
    if (!empty($normalized_entities[$identifier])) {
      $entity = $normalized_entities[$identifier];
    }
    else {
      return $entity;
    } else {
      // Check the entity in the content directory.
      list($entity_type_id, $bundle, $uuid) = explode('.', $identifier);
      $file_path = content_sync_get_content_directory('sync') . "/entities/" . $entity_type_id . "/" . $bundle . "/" . $identifier . ".yml";
      $raw_entity = file_get_contents($file_path);
@@ -88,6 +123,14 @@ class ImportQueueResolver implements ContentSyncResolverInterface {

      $entity = Yaml::decode($raw_entity);
    }

    // TODO: else if Check the entity exists in the snapshot

    // TODO: else if Check if the entity exist in the site.

    // TODO: better notice about missing dependency
    //       - should the parent import be aborted or not?

    return $entity;
  }

@@ -104,22 +147,4 @@ class ImportQueueResolver implements ContentSyncResolverInterface {
      ':name' => $identifier])->fetchField();
  }

  /**
   * Creates a queue.
   *
   * @param array $normalized_entities
   *   Parsed entities to import.
   *
   * @return array
   *   Queue to be processed within a batch process.
   */
  public function resolve(array $normalized_entities, $visited = []) {
    $visited = [];
    foreach ($normalized_entities as $identifier => $entity) {
      $this->depthFirstSearch($visited, [$identifier], $normalized_entities);
    }
    // Reverse the array to adjust it to an array_pop-driven iterator.
    return array_reverse($visited);
  }

}