/**
* If entity type identified by $entityName defines an ID column, creates a mapping between WordPress ID and VPID
* for all entities (db rows) of such type.
*
* @param string $entityName E.g., "post"
*/
private function createVpidsForEntitiesOfType($entityName)
{
if (!$this->dbSchema->getEntityInfo($entityName)->usesGeneratedVpids) {
return;
}
$idColumnName = $this->dbSchema->getEntityInfo($entityName)->idColumnName;
$tableName = $this->dbSchema->getTableName($entityName);
$prefixedTableName = $this->dbSchema->getPrefixedTableName($entityName);
$entities = $this->database->get_results("SELECT * FROM {$prefixedTableName}", ARRAY_A);
$entities = $this->replaceForeignKeysWithReferencesInAllEntities($entityName, $entities);
$storage = $this->storageFactory->getStorage($entityName);
$entities = array_filter($entities, function ($entity) use($storage) {
return $storage->shouldBeSaved($entity);
});
$chunks = array_chunk($entities, 1000);
foreach ($chunks as $entitiesInChunk) {
$wordpressIds = array_column($entitiesInChunk, $idColumnName);
$idPairs = [];
foreach ($wordpressIds as $id) {
$id = intval($id);
if (!isset($this->idCache[$entityName], $this->idCache[$entityName][$id])) {
$this->idCache[$entityName][$id] = IdUtil::newId();
}
$idPairs[$id] = $this->idCache[$entityName][$id];
}
$sqlValues = join(', ', ArrayUtils::map(function ($vpId, $id) use($tableName) {
return "('{$tableName}', {$id}, UNHEX('{$vpId}'))";
}, $idPairs));
$query = "INSERT INTO {$this->database->vp_id} (`table`, id, vp_id) VALUES {$sqlValues}";
$this->database->query($query);
$this->checkTimeout();
}
}