Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace usage of in_array() in MigrateExecutable::handleMissingSourceRows #5766

Open
wants to merge 2 commits into
base: 12.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/Drupal/Migrate/MigrateExecutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,22 @@ public function onPreImport(MigrateImportEvent $event): void
$this->initProgressBar($migration);
}

/**
* The array key to use to represent Source ID values.
*
* Helps speed up array searched by serializing the value for a key.
*
* @param array $source_id
* The Source ID array.
*
* @return string
* The array key to represent Source ID values.
*/
protected function getSourceIdKey(array $source_id): string
{
return implode('-', $source_id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how safe is this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the full extent of what is possible to appear in the $source_id array values, but from my investigation it is mostly entity IDs, revision IDs, and simple strings. Of which, most cases involve one or two values.

I am open to alternate ways of serializing the array values into a unique key.

}

/**
* Handles missing source rows after import.
*
Expand Down Expand Up @@ -242,7 +258,8 @@ protected function handleMissingSourceRows(MigrationInterface $migration): void
$destinationIds = [];
while ($idMap->valid()) {
$mapSourceId = $idMap->currentSource();
if (!in_array($mapSourceId, $this->allSourceIdValues)) {
$sourceIdKey = $this->getSourceIdKey($mapSourceId);
if (!isset($this->allSourceIdValues[$sourceIdKey])) {
$destinationIds[] = $idMap->currentDestination();
}
$idMap->next();
Expand Down Expand Up @@ -479,7 +496,8 @@ public function onPrepareRow(MigratePrepareRowEvent $event): void

// Collect all Source ID values so that we can handle missing source
// rows post import.
$this->allSourceIdValues[] = $sourceId;
$sourceIdKey = $this->getSourceIdKey($sourceId);
$this->allSourceIdValues[$sourceIdKey] = $sourceId;

if ($this->feedback && $this->counter && $this->counter % $this->feedback === 0) {
$this->importFeedbackMessage(false);
Expand Down