private function containsTermChangeInfo($changeInfoList) { return ArrayUtils::any($changeInfoList, function ($changeInfo) { return $changeInfo instanceof TermChangeInfo; }); }
/** * 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; }
/** * Creates manual commit. Adds everything to stage. * * @param WP_REST_Request $request * @return WP_REST_Response|WP_Error */ public function commit(WP_REST_Request $request) { $currentUser = wp_get_current_user(); if ($currentUser->ID === 0) { return new WP_Error('error', 'You don\'t have permission to do this.', ['status' => 403]); } /** @noinspection PhpUndefinedFieldInspection */ $authorName = $currentUser->display_name; /** @noinspection PhpUndefinedFieldInspection */ $authorEmail = $currentUser->user_email; $this->gitRepository->stageAll(); $status = $this->gitRepository->getStatus(true); if (ArrayUtils::any($status, function ($fileStatus) { $vpdbName = basename(VP_VPDB_DIR); return Strings::contains($fileStatus[1], $vpdbName); })) { $this->updateDatabase($status); } $commitMessage = new CommitMessage($request['commit-message']); $changeInfoEnvelope = new ChangeInfoEnvelope([new UntrackedChangeInfo($commitMessage)]); $this->gitRepository->commit($changeInfoEnvelope->getCommitMessage(), $authorName, $authorEmail); return new WP_REST_Response(true); }
/** * Creates manual commit. Adds everything to stage. * * @param WP_REST_Request $request * @return WP_REST_Response|\WP_Error */ public function commit(WP_REST_Request $request) { $currentUser = wp_get_current_user(); if ($currentUser->ID === 0) { return new \WP_Error('error', 'You don\'t have permission to do this.', array('status' => 403)); } /** @noinspection PhpUndefinedFieldInspection */ $authorName = $currentUser->display_name; /** @noinspection PhpUndefinedFieldInspection */ $authorEmail = $currentUser->user_email; $this->gitRepository->stageAll(); $status = $this->gitRepository->getStatus(true); if (ArrayUtils::any($status, function ($fileStatus) { return Strings::contains($fileStatus[1], 'vpdb'); })) { $this->updateDatabase($status); } $this->gitRepository->commit($request['commit-message'], $authorName, $authorEmail); return new WP_REST_Response(true); }
private function containsTermChangeInfo($changeInfoList) { return ArrayUtils::any($changeInfoList, function ($changeInfo) { return ChangeInfoUtils::changeInfoRepresentsEntity($changeInfo, 'term'); }); }
/** * @param $entityName */ private static function assertEntitiesEqualDatabase($entityName) { $storage = self::$storageFactory->getStorage($entityName); $entityInfo = self::$schemaInfo->getEntityInfo($entityName); $allDbEntities = self::selectAll(self::$schemaInfo->getPrefixedTableName($entityName)); $idMap = self::getVpIdMap(); $allDbEntities = self::identifyEntities($entityName, $allDbEntities, $idMap); $allDbEntities = self::replaceForeignKeys($entityName, $allDbEntities, $idMap); $dbEntities = array_filter($allDbEntities, [$storage, 'shouldBeSaved']); $urlReplacer = new AbsoluteUrlReplacer(self::$testConfig->testSite->url); $storageEntities = array_map(function ($entity) use($urlReplacer) { return $urlReplacer->restore($entity); }, $storage->loadAll()); $countOfentitiesInDb = count($dbEntities); $countOfentitiesInStorage = count($storageEntities); if ($countOfentitiesInDb !== $countOfentitiesInStorage) { if ($countOfentitiesInStorage > $countOfentitiesInDb) { $problematicEntities = self::findMissingEntities($entityName, $storageEntities, $dbEntities); } else { $problematicEntities = self::findExceedingEntities($entityName, $storageEntities, $dbEntities); } throw new \PHPUnit_Framework_AssertionFailedError("Different count of synchronized entities ({$entityName}): DB = {$countOfentitiesInDb}, " . "storage = {$countOfentitiesInStorage}\nProblematic entities: " . join(", ", $problematicEntities)); } foreach ($dbEntities as $dbEntity) { $id = $dbEntity[$entityInfo->vpidColumnName]; $storageEntity = $storageEntities[$id]; $dbEntity = self::$shortcodesReplacer->replaceShortcodesInEntity($entityName, $dbEntity); foreach ($dbEntity as $column => $value) { if ($entityInfo->idColumnName === $column || isset($entityInfo->getIgnoredColumns()[$column])) { continue; } if (!isset($storageEntity[$column])) { throw new \PHPUnit_Framework_AssertionFailedError("{$entityName}[{$column}] with value = {$value}, ID = {$id} not found in storage"); } if (is_string($storageEntity[$column])) { $storageEntity[$column] = str_replace("\r\n", "\n", $storageEntity[$column]); } if (is_string($value)) { $value = str_replace("\r\n", "\n", $value); } if ($storageEntity[$column] != $value) { throw new \PHPUnit_Framework_AssertionFailedError("Different values ({$entityName}[{$column}]: {$id}): DB = {$value}, storage = {$storageEntity[$column]}"); } } } $missingReferences = []; $exceedingReferences = []; foreach ($entityInfo->mnReferences as $reference => $targetEntity) { if ($entityInfo->isVirtualReference($reference)) { continue; } $referenceDetails = ReferenceUtils::getMnReferenceDetails(self::$schemaInfo, $entityName, $reference); $sourceColumn = $referenceDetails['source-column']; $targetColumn = $referenceDetails['target-column']; $junctionTable = $referenceDetails['junction-table']; $prefixedJunctionTable = self::$schemaInfo->getPrefixedTableName($junctionTable); $prefixedVpIdTable = self::$schemaInfo->getPrefixedTableName('vp_id'); $sourceTable = self::$schemaInfo->getTableName($referenceDetails['source-entity']); $targetTable = self::$schemaInfo->getTableName($referenceDetails['target-entity']); $junctionTableContent = self::fetchAll("SELECT HEX(s_vp_id.vp_id), HEX(t_vp_id.vp_id) FROM {$prefixedJunctionTable} j\n JOIN {$prefixedVpIdTable} s_vp_id ON j.{$sourceColumn} = s_vp_id.id AND s_vp_id.`table`='{$sourceTable}'\n JOIN {$prefixedVpIdTable} t_vp_id ON j.{$targetColumn} = t_vp_id.id AND t_vp_id.`table` = '{$targetTable}'", MYSQLI_NUM); $checkedReferences = []; $missingReferences[$junctionTable] = []; foreach ($storageEntities as $storageEntity) { if (!isset($storageEntity["vp_{$targetEntity}"])) { continue; } foreach ($storageEntity["vp_{$targetEntity}"] as $referenceVpId) { if (!ArrayUtils::any($junctionTableContent, function ($junctionRow) use($storageEntity, $referenceVpId) { return $junctionRow[0] === $storageEntity['vp_id'] && $junctionRow[1] === $referenceVpId; })) { $missingReferences[$junctionTable][] = [$sourceColumn => $storageEntity['vp_id'], $targetColumn => $referenceVpId]; } $checkedReferences[] = [$storageEntity['vp_id'], $referenceVpId]; } } $exceedingReferences[$junctionTable] = array_map(function ($pair) use($sourceColumn, $targetColumn) { return [$sourceColumn => $pair[0], $targetColumn => $pair[1]]; }, array_filter($junctionTableContent, function ($pair) use($checkedReferences) { foreach ($checkedReferences as $reference) { if ($reference[0] === $pair[0] && $reference[1] === $pair[1]) { return false; } } return true; })); } self::reportResultOfMnReferenceCheck($missingReferences, "Missing"); self::reportResultOfMnReferenceCheck($exceedingReferences, "Exceeding"); }