Skip to content

Commit

Permalink
WIP: PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
Nattfarinn committed Nov 21, 2023
1 parent 3adc2af commit 3fde7dc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/lib/Persistence/Legacy/Content/FieldHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ protected function getEmptyField(FieldDefinition $fieldDefinition, $languageCode
public function createExistingFieldsInNewVersion(Content $content)
{
foreach ($content->fields as $field) {
if ($field->id === null) {
// Virtual field with default value, skip creating field as it has no id
continue;
}
$this->createExistingFieldInNewVersion($field, $content);
}
}
Expand Down
53 changes: 50 additions & 3 deletions src/lib/Persistence/Legacy/Content/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Ibexa\Contracts\Core\Persistence\Content\Language\Handler as LanguageHandler;
use Ibexa\Contracts\Core\Persistence\Content\Relation;
use Ibexa\Contracts\Core\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
use Ibexa\Contracts\Core\Persistence\Content\Type\FieldDefinition;
use Ibexa\Contracts\Core\Persistence\Content\Type\Handler as ContentTypeHandler;
use Ibexa\Contracts\Core\Persistence\Content\VersionInfo;
use Ibexa\Core\Base\Exceptions\NotFoundException;
use Ibexa\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry as Registry;
Expand All @@ -39,16 +41,22 @@ class Mapper
*/
protected $languageHandler;

private ContentTypeHandler $contentTypeHandler;

/**
* Creates a new mapper.
*
* @param \Ibexa\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry $converterRegistry
* @param \Ibexa\Contracts\Core\Persistence\Content\Language\Handler $languageHandler
*/
public function __construct(Registry $converterRegistry, LanguageHandler $languageHandler)
{
public function __construct(
Registry $converterRegistry,
LanguageHandler $languageHandler,
ContentTypeHandler $contentTypeHandler
) {
$this->converterRegistry = $converterRegistry;
$this->languageHandler = $languageHandler;
$this->contentTypeHandler = $contentTypeHandler;
}

/**
Expand Down Expand Up @@ -191,24 +199,34 @@ public function extractContentFromRows(array $rows, array $nameRows, $prefix = '
$contentInfos = [];
$versionInfos = [];
$fields = [];
$fieldDefinitions = [];

foreach ($rows as $row) {
$contentId = (int)$row["{$prefix}id"];
$contentTypeId = (int)$row["{$prefix}contentclass_id"];
if (!isset($contentInfos[$contentId])) {
$contentInfos[$contentId] = $this->extractContentInfoFromRow($row, $prefix);
}
if (!isset($versionInfos[$contentId])) {
$versionInfos[$contentId] = [];
}
if (!isset($fieldDefinitions[$contentId])) {
$contentType = $this->contentTypeHandler->load($contentTypeId);
foreach ($contentType->fieldDefinitions as $fieldDefinition) {
$fieldDefinitions[$contentId][$fieldDefinition->id] = $fieldDefinition;
}
}

$versionId = (int)$row['ezcontentobject_version_id'];
if (!isset($versionInfos[$contentId][$versionId])) {
$versionInfos[$contentId][$versionId] = $this->extractVersionInfoFromRow($row);
}

$fieldId = (int)$row['ezcontentobject_attribute_id'];
if (!isset($fields[$contentId][$versionId][$fieldId])) {
$fieldDefinitionId = (int)$row["ezcontentobject_attribute_contentclassattribute_id"];
if (!isset($fields[$contentId][$versionId][$fieldId]) && isset($fieldDefinitions[$contentId][$fieldDefinitionId])) {
$fields[$contentId][$versionId][$fieldId] = $this->extractFieldFromRow($row);
unset($fieldDefinitions[$contentId][$fieldDefinitionId]);
}
}

Expand All @@ -227,6 +245,15 @@ public function extractContentFromRows(array $rows, array $nameRows, $prefix = '
$content->versionInfo->names = $names;
$content->versionInfo->contentInfo = $contentInfo;
$content->fields = array_values($fields[$contentId][$versionId]);

foreach ($fieldDefinitions[$contentId] as $fieldDefinition) {
$languageCode = $this->determineEmptyFieldLanguageCode($content);
$content->fields[] = $this->createEmptyField(
$fieldDefinition,
$languageCode
);
}

$results[] = $content;
}
}
Expand Down Expand Up @@ -578,6 +605,26 @@ public function createRelationFromCreateStruct(RelationCreateStruct $struct)

return $relation;
}

private function createEmptyField(FieldDefinition $fieldDefinition, string $languageCode): Field
{
$field = new Field();
$field->fieldDefinitionId = $fieldDefinition->id;
$field->type = $fieldDefinition->fieldType;
$field->value = $fieldDefinition->defaultValue;
$field->languageCode = $languageCode;

return $field;
}

private function determineEmptyFieldLanguageCode(Content $content): string
{
foreach ($content->fields as $field) {
return $field->languageCode;
}

return $content->versionInfo->initialLanguageCode;;
}
}

class_alias(Mapper::class, 'eZ\Publish\Core\Persistence\Legacy\Content\Mapper');
1 change: 0 additions & 1 deletion src/lib/Persistence/Legacy/Content/Type/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,6 @@ public function publish($contentTypeId)

try {
$fromType = $this->load($contentTypeId, Type::STATUS_DEFINED);
$this->updateHandler->updateContentObjects($fromType, $toType);
$this->updateHandler->deleteOldType($fromType);
} catch (Exception\TypeNotFound $e) {
// If no old type is found, no updates are necessary to it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
arguments:
- '@Ibexa\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry'
- '@ibexa.spi.persistence.legacy.language.handler'
- '@ibexa.spi.persistence.legacy.content_type.handler'

Ibexa\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase.inner:
class: Ibexa\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase
Expand Down

0 comments on commit 3fde7dc

Please sign in to comment.