Ejemplo n.º 1
0
 public function identifyEntity($entityName, $data, $id)
 {
     if ($this->schemaInfo->getEntityInfo($entityName)->usesGeneratedVpids) {
         $data['vp_id'] = IdUtil::newId();
         $this->saveId($entityName, $id, $data['vp_id']);
     }
     $data[$this->schemaInfo->getEntityInfo($entityName)->idColumnName] = $id;
     $data = $this->fillId($entityName, $data, $id);
     return $data;
 }
Ejemplo n.º 2
0
/**
 * Creates random GUID that is not based on URL
 *
 * @param array $data Sanitized post data
 * @param array $postarr Raw post data
 * @return array
 */
function vp_generate_post_guid($data, $postarr)
{
    if (!VersionPress::isActive()) {
        return $data;
    }
    if (empty($postarr['ID'])) {
        // it's insert not update
        $protocol = is_ssl() ? 'https://' : 'http://';
        $data['guid'] = $protocol . IdUtil::newUuid();
    }
    return $data;
}
Ejemplo n.º 3
0
 /**
  * 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;
 }
Ejemplo n.º 4
0
 public static function prepareTermMeta($vpId = null, $termVpId = null, $key = null, $value = null)
 {
     if ($vpId === null) {
         $vpId = IdUtil::newId();
     }
     $termmeta = ['vp_id' => $vpId];
     if ($termVpId !== null) {
         $termmeta['vp_term_id'] = $termVpId;
     }
     if ($key !== null) {
         $termmeta['meta_key'] = $key;
     }
     if ($value !== null) {
         $termmeta['meta_value'] = $value;
     }
     return $termmeta;
 }
Ejemplo n.º 5
0
 private function restoreIdsInString($stringWithVpids)
 {
     $stringWithIds = preg_replace_callback(IdUtil::getRegexMatchingId(), function ($match) {
         return $this->getIdForVpid($match[0]) ?: self::UNKNOWN_VPID_MARK;
     }, $stringWithVpids);
     return is_numeric($stringWithIds) ? intval($stringWithIds) : $stringWithIds;
 }
Ejemplo n.º 6
0
 /**
  * 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();
     }
 }