Returns EntityInfo for a given entity name (e.g., "post" or "comment")
public getEntityInfo ( $entityName ) : |
||
$entityName | ||
Результат |
/** * @param CommitMessage $commitMessage * @return ChangeInfoEnvelope */ public function parse(CommitMessage $commitMessage) { $fullBody = $commitMessage->getBody(); $splittedBodies = explode("\n\n", $fullBody); $lastBody = $splittedBodies[count($splittedBodies) - 1]; $changeInfoList = []; $version = null; $environment = null; if (self::containsVersion($lastBody)) { $version = self::extractTag(ChangeInfoEnvelope::VP_VERSION_TAG, $lastBody); $environment = self::extractTag(ChangeInfoEnvelope::VP_ENVIRONMENT_TAG, $lastBody); array_pop($splittedBodies); } if (!self::isTrackedChangeInfo($fullBody)) { return new ChangeInfoEnvelope([new UntrackedChangeInfo($commitMessage)], $version, $environment); } foreach ($splittedBodies as $body) { $partialCommitMessage = new CommitMessage("", $body); $actionTag = $partialCommitMessage->getVersionPressTag(TrackedChangeInfo::ACTION_TAG); list($scope, $action, $id) = array_pad(explode('/', $actionTag, 3), 3, null); $tags = $partialCommitMessage->getVersionPressTags(); unset($tags[TrackedChangeInfo::ACTION_TAG]); $actionsInfo = $this->actionsInfoProvider->getActionsInfo($scope); if ($this->dbSchema->isEntity($scope)) { $entityInfo = $this->dbSchema->getEntityInfo($scope); $changeInfoList[] = new EntityChangeInfo($entityInfo, $actionsInfo, $action, $id, $tags, []); } else { $changeInfoList[] = new TrackedChangeInfo($scope, $actionsInfo, $action, $id, $tags, []); } } return new ChangeInfoEnvelope($changeInfoList, $version, $environment); }
public function createEntityChangeInfo($entity, $entityName, $action, $customTags = [], $customFiles = []) { $entityInfo = $this->dbSchema->getEntityInfo($entityName); $vpid = $entity[$entityInfo->vpidColumnName]; $actionsInfo = $this->actionsInfoProvider->getActionsInfo($entityName); $automaticallySavedTags = $actionsInfo->getTags(); $tags = ChangeInfoUtils::extractTags($automaticallySavedTags, $entity, $entity); $tags = array_merge($tags, $customTags); return new EntityChangeInfo($entityInfo, $actionsInfo, $action, $vpid, $tags, $customFiles); }
private function fillId($entityName, $data, $id) { $idColumnName = $this->schemaInfo->getEntityInfo($entityName)->idColumnName; if (!isset($data[$idColumnName])) { $data[$idColumnName] = $id; } return $data; }
/** * @param $entityName * @return mixed */ private function getEntitiesFromDatabase($entityName) { if ($this->dbSchema->isChildEntity($entityName)) { $entityInfo = $this->dbSchema->getEntityInfo($entityName); $parentReference = $entityInfo->parentReference; return $this->database->get_results("SELECT * FROM {$this->dbSchema->getPrefixedTableName($entityName)} ORDER BY {$parentReference}", ARRAY_A); } return $this->database->get_results("SELECT * FROM {$this->dbSchema->getPrefixedTableName($entityName)}", ARRAY_A); }
/** * @test */ public function dbSchemaInfoMergesReferencesFromMultipleSources() { $schema1 = ['some-entity' => ['id' => 'some_column', 'value-references' => ['some_column@another_column' => ['some_value' => 'another-entity']]]]; $schema2 = ['some-entity' => ['value-references' => ['some_column@another_column' => ['another_value' => 'another-entity']]]]; $schemaFile1 = $this->createSchemaFile($schema1); $schemaFile2 = $this->createSchemaFile($schema2); $schemaInfo = new DbSchemaInfo([$schemaFile1, $schemaFile2], 'prefix_', PHP_INT_MAX); $entityInfo = $schemaInfo->getEntityInfo('some-entity'); $expectedValueReferences = ['some_column=some_value@another_column' => 'another-entity', 'some_column=another_value@another_column' => 'another-entity']; $this->assertSame($expectedValueReferences, $entityInfo->valueReferences); }
/** * Returns name of column referencing synchronized entity in the junction table. * Example: * We are synchronizing posts with M:N reference to the taxonomies. The reference is defined * as term_relationships.term_taxonomy_id => term_taxonomy. Name of column referencing term_taxonomy is obvious, * it's term_taxonomy_id. However we need also name of column referencing the post. We can find this name * in the definition of M:N references of term_taxonomy, where it's defined as term_relationships.object_id => post. * So in the end we are looking for M:N reference to post with same junction table (term_relationships). * * @param DbSchemaInfo $dbSchema * @param $sourceEntity * @param $targetEntity * @param $junctionTable * @return string */ private static function getSourceColumn(DbSchemaInfo $dbSchema, $sourceEntity, $targetEntity, $junctionTable) { $targetEntityMnReferences = $dbSchema->getEntityInfo($targetEntity)->mnReferences; foreach ($targetEntityMnReferences as $reference => $referencedEntity) { list($referencedTable, $referenceColumn) = explode(".", $reference); if ($referencedTable === $junctionTable && $referencedEntity === $sourceEntity) { return $referenceColumn; } } return null; }
private function fillParentId($metaEntityName, $where, $id) { $entityInfo = $this->dbSchemaInfo->getEntityInfo($metaEntityName); $parentReference = $entityInfo->parentReference; $parent = $entityInfo->references[$parentReference]; $vpIdTable = $this->dbSchemaInfo->getPrefixedTableName('vp_id'); $entityTable = $this->dbSchemaInfo->getPrefixedTableName($metaEntityName); $parentTable = $this->dbSchemaInfo->getTableName($parent); $idColumnName = $this->dbSchemaInfo->getEntityInfo($metaEntityName)->idColumnName; $where["vp_{$parentReference}"] = $this->database->get_var("SELECT HEX(vp_id) FROM {$vpIdTable} WHERE `table` = '{$parentTable}' AND ID = (SELECT {$parentReference} FROM {$entityTable} WHERE {$idColumnName} = {$id})"); return $where; }
private static function findExceedingEntities($entityName, $storageEntities, $dbEntities) { $exceedingEntities = []; $vpidColumnName = self::$schemaInfo->getEntityInfo($entityName)->vpidColumnName; $idColumnName = self::$schemaInfo->getEntityInfo($entityName)->idColumnName; foreach ($dbEntities as $dbEntity) { if (empty($dbEntity[$vpidColumnName])) { $exceedingEntities[] = $dbEntity[$idColumnName]; } elseif (!isset($storageEntities[$dbEntity[$vpidColumnName]])) { $exceedingEntities[] = $dbEntity[$vpidColumnName]; } } return $exceedingEntities; }
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; }
/** * @param $entities * @return array */ private function getExistingMnReferences($entities) { $entityInfo = $this->dbSchema->getEntityInfo($this->entityName); $mnReferences = $entityInfo->mnReferences; $referencesToFix = array(); foreach ($entities as $entity) { foreach ($mnReferences as $reference => $referencedEntity) { $vpReference = "vp_{$referencedEntity}"; if (!isset($entity[$vpReference]) || count($entity[$vpReference]) == 0) { continue; } foreach ($entity[$vpReference] as $referencedVpId) { $referencesToFix[$reference][] = array('vp_id' => $entity['vp_id'], 'referenced_vp_id' => $referencedVpId); } } } return $referencesToFix; }
/** * Returns true if there is any entity with reference to the passed one. * * @param $entityName * @param $entityId * @return bool */ private function existsSomeEntityWithReferenceTo($entityName, $entityId) { $entityNames = $this->dbSchemaInfo->getAllEntityNames(); foreach ($entityNames as $otherEntityName) { $otherEntityInfo = $this->dbSchemaInfo->getEntityInfo($otherEntityName); $otherEntityReferences = $otherEntityInfo->references; $otherEntityMnReferences = $otherEntityInfo->mnReferences; $otherEntityValueReferences = $otherEntityInfo->valueReferences; $allReferences = array_merge($otherEntityReferences, $otherEntityMnReferences, $otherEntityValueReferences); $reference = array_search($entityName, $allReferences); if ($reference === false) { // Other entity is not referencing $entityName continue; } $otherEntityStorage = $this->storageFactory->getStorage($otherEntityName); $possiblyReferencingEntities = $otherEntityStorage->loadAll(); if (isset($otherEntityReferences[$reference])) { // 1:N reference $vpReference = "vp_{$reference}"; foreach ($possiblyReferencingEntities as $possiblyReferencingEntity) { if (isset($possiblyReferencingEntity[$vpReference]) && $possiblyReferencingEntity[$vpReference] === $entityId) { return true; } } } elseif (isset($otherEntityMnReferences[$reference])) { // M:N reference $vpReference = "vp_{$otherEntityName}"; foreach ($possiblyReferencingEntities as $possiblyReferencingEntity) { if (isset($possiblyReferencingEntity[$vpReference]) && array_search($entityId, $possiblyReferencingEntity[$vpReference]) !== false) { return true; } } } elseif (isset($otherEntityValueReferences[$reference])) { // Value reference list($sourceColumn, $sourceValue, $valueColumn) = array_values(ReferenceUtils::getValueReferenceDetails($reference)); foreach ($possiblyReferencingEntities as $possiblyReferencingEntity) { if (isset($possiblyReferencingEntity[$sourceColumn]) && $possiblyReferencingEntity[$sourceColumn] == $sourceValue && isset($possiblyReferencingEntity[$valueColumn]) && $possiblyReferencingEntity[$valueColumn] === $entityId) { return true; } } } } return false; }
/** * Saves all already existing meta and M:N references for an entity that wasn't tracked yet * * @param array $data * @param string $entityName */ private function storeRelatedEntities($data, $entityName) { $id = $data[$this->dbSchemaInfo->getEntityInfo($entityName)->idColumnName]; foreach ($this->dbSchemaInfo->getAllEntityNames() as $referencedEntityName) { $entityInfo = $this->dbSchemaInfo->getEntityInfo($referencedEntityName); if ($this->dbSchemaInfo->isChildEntity($referencedEntityName) && $entityInfo->references[$entityInfo->parentReference] === $entityName) { $childEntities = $this->database->get_results("SELECT * FROM {$this->dbSchemaInfo->getPrefixedTableName($referencedEntityName)} WHERE `{$entityInfo->parentReference}` = '{$id}'", ARRAY_A); foreach ($childEntities as $childEntity) { $childEntity = $this->vpidRepository->replaceForeignKeysWithReferences($referencedEntityName, $childEntity); if (!$this->mirror->shouldBeSaved($referencedEntityName, $childEntity)) { continue; } $id = $childEntity[$entityInfo->idColumnName]; $vpid = $this->vpidRepository->getVpidForEntity($referencedEntityName, $id); if ($vpid) { $childEntity[$entityInfo->vpidColumnName] = $vpid; } else { $childEntity = $this->vpidRepository->identifyEntity($referencedEntityName, $childEntity, $childEntity[$entityInfo->idColumnName]); } $childEntity = $this->shortcodesReplacer->replaceShortcodesInEntity($referencedEntityName, $childEntity); $this->mirror->save($referencedEntityName, $childEntity); } } } foreach ($this->dbSchemaInfo->getAllMnReferences() as $mnReferenceDetails) { if ($mnReferenceDetails['source-entity'] === $entityName) { $junctionTable = $mnReferenceDetails['junction-table']; $prefixedJunctionTable = $this->dbSchemaInfo->getPrefixedTableName($junctionTable); $sourceColumn = $mnReferenceDetails['source-column']; $references = $this->database->get_results("SELECT * FROM `{$prefixedJunctionTable}` WHERE `{$sourceColumn}` = '{$id}'", ARRAY_A); foreach ($references as $reference) { $reference = $this->vpidRepository->replaceForeignKeysWithReferences($junctionTable, $reference); $this->mirror->save($junctionTable, $reference); } } } }
/** * Returns true if there is any entity with reference to the passed one. * * @param $entityName * @param $entityId * @return bool */ private function existsSomeEntityWithReferenceTo($entityName, $entityId) { $entityNames = $this->dbSchemaInfo->getAllEntityNames(); foreach ($entityNames as $otherEntityName) { $otherEntityInfo = $this->dbSchemaInfo->getEntityInfo($otherEntityName); $otherEntityReferences = $otherEntityInfo->references; $otherEntityMnReferences = $otherEntityInfo->mnReferences; $otherEntityValueReferences = $otherEntityInfo->valueReferences; $allReferences = array_merge($otherEntityReferences, $otherEntityMnReferences, $otherEntityValueReferences); foreach ($allReferences as $reference => $referencedEntity) { // if the target is dynamic, check it anyway - just to be sure if ($referencedEntity !== $entityName && $referencedEntity[0] !== '@') { continue; } $otherEntityStorage = $this->storageFactory->getStorage($otherEntityName); $possiblyReferencingEntities = $otherEntityStorage->loadAll(); if (isset($otherEntityReferences[$reference])) { // 1:N reference $vpReference = "vp_{$reference}"; foreach ($possiblyReferencingEntities as $possiblyReferencingEntity) { if (isset($possiblyReferencingEntity[$vpReference])) { $referencedVpidsString = $possiblyReferencingEntity[$vpReference]; preg_match_all(IdUtil::getRegexMatchingId(), $referencedVpidsString, $matches); if (ArrayUtils::any($matches[0], Comparators::equals($entityId))) { return true; } } } } elseif (isset($otherEntityMnReferences[$reference])) { // M:N reference $vpReference = "vp_{$otherEntityName}"; foreach ($possiblyReferencingEntities as $possiblyReferencingEntity) { if (isset($possiblyReferencingEntity[$vpReference]) && array_search($entityId, $possiblyReferencingEntity[$vpReference]) !== false) { return true; } } } elseif (isset($otherEntityValueReferences[$reference])) { // Value reference list($sourceColumn, $sourceValue, $valueColumn, $pathInStructure) = array_values(ReferenceUtils::getValueReferenceDetails($reference)); foreach ($possiblyReferencingEntities as $possiblyReferencingEntity) { if (isset($possiblyReferencingEntity[$sourceColumn]) && ($possiblyReferencingEntity[$sourceColumn] === $sourceValue || ReferenceUtils::valueMatchesWildcard($sourceValue, $possiblyReferencingEntity[$sourceColumn])) && isset($possiblyReferencingEntity[$valueColumn])) { if (is_numeric($possiblyReferencingEntity[$valueColumn]) && intval($possiblyReferencingEntity[$valueColumn]) === 0 || $possiblyReferencingEntity[$valueColumn] === '') { continue; } if ($pathInStructure) { $possiblyReferencingEntity[$valueColumn] = unserialize($possiblyReferencingEntity[$valueColumn]); $paths = ReferenceUtils::getMatchingPaths($possiblyReferencingEntity[$valueColumn], $pathInStructure); } else { $paths = [[]]; // root = the value itself } /** @var Cursor[] $cursors */ $cursors = array_map(function ($path) use(&$possiblyReferencingEntity, $valueColumn) { return new Cursor($possiblyReferencingEntity[$valueColumn], $path); }, $paths); foreach ($cursors as $cursor) { $vpidsString = $cursor->getValue(); preg_match_all(IdUtil::getRegexMatchingId(), $vpidsString, $matches); if (ArrayUtils::any($matches[0], Comparators::equals($entityId))) { return true; } } } } } } } return false; }
/** * @param $entityName * @return Synchronizer */ public function createSynchronizer($entityName) { return new Synchronizer($this->getStorage($entityName), $this->database, $this->dbSchema->getEntityInfo($entityName), $this->dbSchema, $this->vpidRepository, $this->urlReplacer, $this->shortcodesReplacer, $this->tableSchemaStorage); }