function synchronize($task, $entitiesToSynchronize = null) { $this->maybeInit($entitiesToSynchronize); $options = $this->options; if (count($options) > 0) { $syncQuery = "INSERT INTO {$this->tableName} (option_name, option_value, autoload) VALUES "; foreach ($options as $optionName => $option) { $option = $this->urlReplacer->restore($option); $option = $this->maybeRestoreReference($option); if (!isset($option['autoload'])) { $option['autoload'] = 'yes'; } // default value if (!isset($option['option_value'])) { $option['option_value'] = ''; } $syncQuery .= "(\"{$optionName}\", \"" . $this->database->_real_escape($option['option_value']) . "\", \"{$option['autoload']}\"),"; } $syncQuery[strlen($syncQuery) - 1] = " "; // strip last comma $syncQuery .= " ON DUPLICATE KEY UPDATE option_value = VALUES(option_value), autoload = VALUES(autoload);"; $this->database->query($syncQuery); } $ignoredOptionNames = ArrayUtils::column($options, 'option_name'); $ignoredOptionNames = array_merge($ignoredOptionNames, OptionStorage::$optionsBlacklist); $deleteSql = "DELETE FROM {$this->tableName} WHERE option_name NOT IN (\"" . join('", "', $ignoredOptionNames) . "\") OR option_name NOT LIKE '_%'"; if ($entitiesToSynchronize) { $synchronizedOptions = ArrayUtils::column($entitiesToSynchronize, 'vp_id'); $deleteSql = "DELETE FROM {$this->tableName} WHERE option_name NOT IN (\"" . join('", "', $ignoredOptionNames) . "\") AND option_name IN (\"" . join('", "', $synchronizedOptions) . "\")"; } $this->database->query($deleteSql); return array(); }
private function getIdsForVpIds($referencesToUpdate) { if (count($referencesToUpdate) === 0) { return array(array(0, 0)); } $vpIdTable = $this->dbSchema->getPrefixedTableName('vp_id'); $vpIds = array_map(function ($vpId) { return 'UNHEX("' . $vpId . '")'; }, $referencesToUpdate); $vpIdsRestriction = join(', ', $vpIds); $result = $this->database->get_results("SELECT HEX(vp_id), id FROM {$vpIdTable} WHERE vp_id IN ({$vpIdsRestriction})", ARRAY_N); $result[] = array(0, 0); return array_combine(ArrayUtils::column($result, 0), ArrayUtils::column($result, 1)); }
/** * 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 loadAllFromFiles($entityFiles) { $entities = array_map(array($this, 'deserializeEntity'), array_map('file_get_contents', $entityFiles)); $vpIds = ArrayUtils::column($entities, $this->entityInfo->vpidColumnName); return array_combine($vpIds, $entities); }