private function saveId($entityName, $id, $vpId) { $vpIdTableName = $this->schemaInfo->getPrefixedTableName('vp_id'); $tableName = $this->schemaInfo->getTableName($entityName); $query = "INSERT INTO {$vpIdTableName} (`vp_id`, `table`, `id`) VALUES (UNHEX('{$vpId}'), \"{$tableName}\", {$id})"; $this->database->query($query); }
private function deleteEntitiesWhichAreNotInStorage($entities) { if ($this->selectiveSynchronization) { $savedVpIds = array_map(function ($entity) { return $entity['vp_id']; }, $entities); $vpIdsToSynchronize = array_map(function ($entity) { return $entity['vp_id']; }, $this->entitiesToSynchronize); $sql = sprintf('SELECT id FROM %s WHERE `table` = "%s" ', $this->getPrefixedTableName('vp_id'), $this->dbSchema->getTableName($this->entityName)); $sql .= sprintf('AND HEX(vp_id) IN ("%s") ', join('", "', $vpIdsToSynchronize)); $sql .= sprintf('AND HEX(vp_id) NOT IN ("%s")', join('", "', $savedVpIds)); $ids = $this->database->get_col($sql); } else { $vpIdsUnhexed = array_map(function ($entity) { return 'UNHEX("' . $entity['vp_id'] . '")'; }, $entities); $ids = $this->database->get_col("SELECT id FROM {$this->getPrefixedTableName('vp_id')} " . "WHERE `table` = \"{$this->dbSchema->getTableName($this->entityName)}\"" . (count($vpIdsUnhexed) > 0 ? "AND vp_id NOT IN (" . join(",", $vpIdsUnhexed) . ")" : "")); } $this->deletedIds = $ids; if (count($ids) == 0) { return; } $idsString = join(',', $ids); $this->executeQuery("DELETE FROM {$this->getPrefixedTableName($this->entityName)} WHERE {$this->idColumnName} IN ({$idsString})"); $this->executeQuery("DELETE FROM {$this->getPrefixedTableName('vp_id')} WHERE `table` = \"{$this->dbSchema->getTableName($this->entityName)}\" AND id IN ({$idsString})"); }
/** * 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(); } }
/** * 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); $this->idCache[$entityName] = array(); foreach ($chunks as $entitiesInChunk) { $wordpressIds = ArrayUtils::column($entitiesInChunk, $idColumnName); $vpIds = array_map(array('VersionPress\\Utils\\IdUtil', 'newId'), $entitiesInChunk); $idPairs = array_combine($wordpressIds, $vpIds); $this->idCache[$entityName] = $this->idCache[$entityName] + $idPairs; // merge arrays with preserving keys $sqlValues = join(', ', ArrayUtils::map(function ($vpId, $id) use($tableName) { return "('{$tableName}', {$id}, UNHEX('{$vpId}'))"; }, $idPairs)); $query = "INSERT INTO {$this->getTableName('vp_id')} (`table`, id, vp_id) VALUES {$sqlValues}"; $this->database->query($query); $this->checkTimeout(); } }
private function fillParentId($metaEntityName, $entityInfo, $id) { $parentReference = $entityInfo->parentReference; $parent = $entityInfo->references[$parentReference]; $vpIdTable = $this->database->vp_id; $entityTable = $this->dbSchemaInfo->getPrefixedTableName($metaEntityName); $parentTable = $this->dbSchemaInfo->getTableName($parent); $idColumnName = $this->dbSchemaInfo->getEntityInfo($metaEntityName)->idColumnName; return $this->database->get_var("SELECT HEX(vp_id) FROM {$vpIdTable}\n WHERE `table` = '{$parentTable}'\n AND ID = (SELECT {$parentReference} FROM {$entityTable} WHERE {$idColumnName} = '{$id}')"); }
private function maybeRestoreReference($option) { $entityInfo = $this->dbSchema->getEntityInfo('option'); foreach ($entityInfo->valueReferences as $reference => $targetEntity) { $referenceDetails = ReferenceUtils::getValueReferenceDetails($reference); if ($option[$referenceDetails['source-column']] === $referenceDetails['source-value'] && isset($option[$referenceDetails['value-column']])) { $vpid = $option[$referenceDetails['value-column']]; $vpidTable = $this->dbSchema->getPrefixedTableName('vp_id'); $targetTable = $this->dbSchema->getTableName($targetEntity); $dbId = $this->database->get_var("SELECT id FROM {$vpidTable} WHERE `table`='{$targetTable}' AND vp_id=UNHEX('{$vpid}')"); $option[$referenceDetails['value-column']] = $dbId; } } return $option; }
private static function isInIdMap($idMap, $targetEntity, $id) { return isset($idMap[self::$schemaInfo->getTableName($targetEntity)]) && isset($idMap[self::$schemaInfo->getTableName($targetEntity)][$id]); }