You are here

public function Importer::getChunkedNames in Tome 8

Gets chunked arrays of content names to import.

Chunks should be imported synchronously, but within each chunk any amount of concurrency should be acceptable.

Return value

array An array of arrays of content names to import.

Overrides ImporterInterface::getChunkedNames

File

modules/tome_sync/src/Importer.php, line 109

Class

Importer
Handles importing of content and file entities.

Namespace

Drupal\tome_sync

Code

public function getChunkedNames() {
  $graph = [];
  $index = $this
    ->getContentIndex();
  if (!$index) {
    throw new \Exception('No index file was found. Check that the content export directory is writable and that content JSON is in the directory.');
  }
  $names = $this->contentStorage
    ->listAll();
  foreach ($index as $name => $edges) {
    if (!in_array($name, $names, TRUE)) {
      continue;
    }
    $graph[$name]['edges'] = [];
    foreach ($edges as $edge) {
      if (in_array($edge, $names, TRUE)) {
        $graph[$name]['edges'][$edge] = TRUE;
      }
    }
  }
  $graph_object = new Graph($graph);
  $graph = $graph_object
    ->searchAndSort();
  uasort($graph, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
  $graph = array_reverse($graph);

  // Now we need to chunk the graph into parts we can do concurrently.
  // This is overkill for small sites, but for large migrations we need to
  // import as quickly as possible.
  $all_imported = [];
  $chunked_graph = [];
  while ($graph) {
    $chunk = [];
    foreach ($graph as $i => $node) {
      $edges = array_keys($node['edges']);
      if (count(array_intersect($edges, $all_imported)) === count($edges)) {
        $chunk[] = $i;
        unset($graph[$i]);
      }
    }
    $all_imported = array_merge($all_imported, $chunk);
    if (empty($chunk)) {
      throw new \Exception('Unable to build the content graph, probably due to circular dependencies. Here is the list of entities to review: ' . implode(', ', array_keys($graph)));
    }
    $chunked_graph[] = $chunk;
  }
  return $chunked_graph;
}