Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
labudzinski committed Jun 6, 2024
2 parents e4fd087 + ecda0f4 commit 51d63b1
Show file tree
Hide file tree
Showing 20 changed files with 156 additions and 199 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"nelmio/api-doc-bundle": "^4.12",
"pimcore/admin-ui-classic-bundle": "^1.4.0",
"pimcore/data-hub": "^1.7.0",
"pimcore/elasticsearch-client": "^1.0",
"symfony/messenger": "^5.0 || ^6.0 || ^7.0",
"webmozart/assert": "^1.11",
"zircote/swagger-php": "^3.0 || ^4.7"
Expand Down
1 change: 0 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
SetList::DEAD_CODE,
SetList::PHP_82,
SetList::TYPE_DECLARATION,
SetList::NAMING,
LevelSetList::UP_TO_PHP_82,
]);

Expand Down
54 changes: 19 additions & 35 deletions src/Command/RebuildIndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
use Pimcore\Model\Asset\Folder;
use Pimcore\Model\DataObject;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\Version;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -50,32 +50,27 @@ protected function configure(): void
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws ClientResponseException
* @throws ServerResponseException
* @throws Exception
* @throws MissingParameterException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$endpointName = $input->getArgument('name');
$output->writeln('Starting index rebuilding for configuration: '.$endpointName);
$configuration = $this->dataHubConfigurationRepository->findOneByName($endpointName);
if ($configuration instanceof Configuration) {

try {
$configuration = $this->dataHubConfigurationRepository->findOneByName($endpointName);

if ($configuration instanceof Configuration) {
$configReader = new ConfigReader($configuration->getConfiguration());
$this->cleanAliases($configReader);

$configReader = new ConfigReader($configuration->getConfiguration());
$this->cleanAliases($configReader);

if ($configReader->isAssetIndexingEnabled()) {
$this->rebuildType(self::TYPE_ASSET, $endpointName, $output);
}
if ($configReader->isObjectIndexingEnabled()) {
$this->rebuildType(self::TYPE_OBJECT, $endpointName, $output);
}
if ($configReader->isAssetIndexingEnabled()) {
$this->rebuildType(self::TYPE_ASSET, $endpointName, $output);
}
if ($configReader->isObjectIndexingEnabled()) {
$this->rebuildType(self::TYPE_OBJECT, $endpointName, $output);
}
} catch (\Exception $e) {
throw $e;
}

$output->writeln('Peak usage: ' . memory_get_peak_usage() / 1024 / 1024 . ' MBs');
Expand Down Expand Up @@ -143,7 +138,7 @@ private function enqueueParentFolders(
$endpointName,
$indexName
);
} catch (\Exception $e) {
} catch (\Exception) {
}
$element = $element->getParent();
gc_collect_cycles();
Expand All @@ -160,24 +155,13 @@ public function getNewIndexName(string $index): string

private function getElement(int $id, string $type): Asset|DataObject
{
$asset = match($type){
'asset' => new Asset(),
'object' => new DataObject()
return match($type) {
'asset' => Asset::getById($id),
'object' => DataObject::getById($id),
};
$asset->getDao()->getById($id);

return $asset;
}

/**
* @param int $i
* @param int $batchSize
* @param string $sql
* @param Asset $asset
* @param string $type
* @param OutputInterface $output
* @param string $endpointName
* @return void
* @throws Exception
*/
private function doBatch(int $i, int $batchSize, string $sql, Asset $asset, string $type, OutputInterface $output, string $endpointName): void
Expand Down Expand Up @@ -209,7 +193,7 @@ private function doBatch(int $i, int $batchSize, string $sql, Asset $asset, stri
$result = null;
$element = null;
unset($result, $element);
$output->writeln('Usage: ' . memory_get_usage() / 1024 / 1024 . ' MBs');
$output->writeln('Usage: ' . Helper::formatMemory(memory_get_usage(true)));
}
$batchResults = null;
unset($batchResults);
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/BaseEndpointController.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ private function getFilter(): array
$filter = $this->getValidFilter($item, $filter);
}
}
} else if(is_json($rawData)) {
} elseif (is_json($rawData)) {
$filter = \GuzzleHttp\json_decode($rawData, true);
} else if (\is_string($rawData)) {
} elseif (\is_string($rawData)) {
$filter = $this->getValidFilter($rawData);
}
if(!empty($filter)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public function getStoragePath(ThumbnailInterface $thumb, int $id, string $filen
}

$fileExtension = $format;
if ('original' == $format) {
if ('original' === $format) {
$fileExtension = $fileExt;
} elseif ('pjpeg' === $format || 'jpeg' === $format) {
$fileExtension = 'jpg';
Expand Down
2 changes: 0 additions & 2 deletions src/Controller/ElementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,6 @@ public function unlock(AssetHelper $assetHelper, IndexPersistenceService $indexP
}

/**
* @param mixed $model
*
* @throws \Exception
*/
private function getAssetMetaData(AbstractModel $model, $result, ConfigReader $configReader): array
Expand Down
6 changes: 3 additions & 3 deletions src/Elasticsearch/EndpointAndIndexesConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public function createOrUpdate(ConfigReader $configReader): void
if ($configReader->isAssetIndexingEnabled()) {
try {
$this->handleAssetIndices($configReader);
} catch (ClientResponseException|MissingParameterException|ServerResponseException $e) {
} catch (ClientResponseException|MissingParameterException|ServerResponseException) {
}
}

if ($configReader->isObjectIndexingEnabled()) {
try {
$this->handleObjectIndices($configReader);
} catch (ClientResponseException|MissingParameterException|ServerResponseException $e) {
} catch (ClientResponseException|MissingParameterException|ServerResponseException) {
}
}

Expand Down Expand Up @@ -83,7 +83,7 @@ private function handleAssetIndices(ConfigReader $configReader): void
// Assets
$this->indexManager->createOrUpdateIndex(
$this->indexManager->getIndexName(IndexManager::INDEX_ASSET, $endpointName),
$this->assetMapping->generate($configReader->toArray())
$this->assetMapping->generate($configReader)
);
}

Expand Down
7 changes: 6 additions & 1 deletion src/Elasticsearch/Index/IndexPersistenceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,12 @@ public function update(ElementInterface $element, string $endpointName, string $
$configReader = new ConfigReader($configuration->getConfiguration());

if ($element instanceof AbstractObject) {
$body = $this->dataObjectProvider->getIndexData($element, $configReader);
if(in_array($element, $configReader->getObjectClassNames()) ||
in_array((new \ReflectionClass($this))->getShortName(), $configReader->getObjectClassNames())) {
$body = $this->dataObjectProvider->getIndexData($element, $configReader);
} else {
return [];
}
} elseif ($element instanceof Asset) {
$body = $this->assetProvider->getIndexData($element, $configReader);
} else {
Expand Down
26 changes: 12 additions & 14 deletions src/Elasticsearch/Mapping/AssetMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

final class AssetMapping extends DefaultMapping
{
public function extractMapping(): array
public function extractMapping(ConfigReader $config = null): array
{
$systemDataMapping = $this->createSystemAttributesMapping();
$systemDataMapping['properties']['checksum'] = ['type' => 'keyword'];
Expand Down Expand Up @@ -69,17 +69,15 @@ public function extractMapping(): array
]
];

if (!empty($this->config['thumbnails'])) {
foreach ($this->config['thumbnails'] as $thumbnail) {
$thumbnails[$thumbnail] = [
'type' => 'object',
'dynamic' => false,
'properties' => [
'path' => ['type' => 'keyword'],
'checksum' => ['type' => 'keyword']
]
];
}
foreach ($config->getAssetThumbnails() as $thumbnail) {
$thumbnails[$thumbnail] = [
'type' => 'object',
'dynamic' => false,
'properties' => [
'path' => ['type' => 'keyword'],
'checksum' => ['type' => 'keyword']
]
];
}

$data['binaryData'] = [
Expand All @@ -91,9 +89,9 @@ public function extractMapping(): array
return $data;
}

public function generate(array $config = []): array
public function generate(ConfigReader $config = null): array
{
$mappingProperties = $this->extractMapping();
$mappingProperties = $this->extractMapping($config);
$mappings = $this->mappingTemplate;
$mappings['properties'] = $mappingProperties;

Expand Down
116 changes: 58 additions & 58 deletions src/Elasticsearch/Mapping/DataObjectMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,81 +14,81 @@

final class DataObjectMapping extends DefaultMapping
{
public function generate(array $config = []): array
public function generate(array $objectClassConfig = []): array
{
if ([] === $config) {
if ([] === $objectClassConfig) {
throw new \RuntimeException('No DataObject class configuration provided.');
}

return array_merge($this->getCommonProperties(), [
'properties' => [
'data' => [
'dynamic' => 'true',
'properties' => $this->generateDataProperties($config),
],
'system' => [
'dynamic' => 'false',
'properties' => [
'id' => [
'type' => 'long',
],
'key' => [
'type' => 'keyword',
'fields' => [
'analyzed' => [
'type' => 'text',
'term_vector' => 'yes',
'analyzer' => 'datahub_ngram_analyzer',
'search_analyzer' => 'datahub_whitespace_analyzer',
],
$mappings = $this->mappingTemplate;
$mappings['properties'] = [
'data' => [
'dynamic' => 'true',
'properties' => $this->generateDataProperties($objectClassConfig),
],
'system' => [
'dynamic' => 'false',
'properties' => [
'id' => [
'type' => 'long',
],
'key' => [
'type' => 'keyword',
'fields' => [
'analyzed' => [
'type' => 'text',
'term_vector' => 'yes',
'analyzer' => 'datahub_ngram_analyzer',
'search_analyzer' => 'datahub_whitespace_analyzer',
],
],
'fullPath' => [
'type' => 'keyword',
'fields' => [
'analyzed' => [
'type' => 'text',
'term_vector' => 'yes',
'analyzer' => 'datahub_ngram_analyzer',
'search_analyzer' => 'datahub_whitespace_analyzer',
],
],
'fullPath' => [
'type' => 'keyword',
'fields' => [
'analyzed' => [
'type' => 'text',
'term_vector' => 'yes',
'analyzer' => 'datahub_ngram_analyzer',
'search_analyzer' => 'datahub_whitespace_analyzer',
],
],
'type' => [
'type' => 'constant_keyword',
],
'parentId' => [
'type' => 'keyword',
],
'hasChildren' => [
'type' => 'boolean',
],
'creationDate' => [
'type' => 'date',
],
'modificationDate' => [
'type' => 'date',
],
'subtype' => [
'type' => 'keyword',
],
],
'type' => [
'type' => 'constant_keyword',
],
'parentId' => [
'type' => 'keyword',
],
'hasChildren' => [
'type' => 'boolean',
],
'creationDate' => [
'type' => 'date',
],
'modificationDate' => [
'type' => 'date',
],
'subtype' => [
'type' => 'keyword',
],
],
],
]);
];

return $mappings;
}

/**
* Generates all data properties for the given DataObject class config.
*
* @param array<string, array|string> $config
*
* @return array<string, array>
*/
private function generateDataProperties(array $config): array
private function generateDataProperties(array $objectClassConfig): array
{
$properties = [];
$columnConfig = $config['columnConfig'] ?? [];
$columnConfig = $objectClassConfig['columnConfig'] ?? [];

foreach ($columnConfig as $column) {
if (true === $column['hidden']) {
Expand All @@ -104,13 +104,13 @@ private function generateDataProperties(array $config): array
/**
* Generates the property definition for a given field config.
*
* @param array<string, array|string> $config
* @param array<string, array|string> $objectClassConfig
*
* @return array<string, array|string>
*/
private function getPropertiesForFieldConfig(array $config): array
private function getPropertiesForFieldConfig(array $objectClassConfig): array
{
return match ($config['type']) {
return match ($objectClassConfig['type']) {
'hotspotimage', 'image' => array_merge($this->getImageProperties(), [
'dynamic' => 'false',
'type' => 'object',
Expand All @@ -120,7 +120,7 @@ private function getPropertiesForFieldConfig(array $config): array
'type' => 'nested',
]),
'numeric' => [
'type' => $config['layout']['integer'] ? 'integer' : 'float',
'type' => $objectClassConfig['layout']['integer'] ? 'integer' : 'float',
],
default => [
'type' => 'keyword',
Expand Down
Loading

0 comments on commit 51d63b1

Please sign in to comment.