Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request ezsystems#1198 from ezsystems/impl-EZP-23941-fix-c…
…ontent-updater

EZP-23941: Move field map to storage 1/2
  • Loading branch information
pspanja committed Feb 25, 2015
2 parents f7db9f7 + 36a9cb3 commit 6b86efc
Show file tree
Hide file tree
Showing 16 changed files with 733 additions and 368 deletions.
9 changes: 9 additions & 0 deletions eZ/Publish/API/Repository/Tests/ContentTypeServiceTest.php
Expand Up @@ -1967,6 +1967,15 @@ public function testAddFieldDefinitionAddsFieldToContentAdded( array $data )
isset( $contentVersion2Draft->fields["byline"] ),
"New field was not added to draft version."
);

$this->assertEquals(
$contentVersion1Archived->getField( "byline" )->id,
$contentVersion1Published->getField( "byline" )->id
);
$this->assertEquals(
$contentVersion1Published->getField( "byline" )->id,
$contentVersion2Draft->getField( "byline" )->id
);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Content/Gateway.php
Expand Up @@ -388,4 +388,13 @@ abstract public function deleteRelation( $relationId, $type );
* @return int ID the inserted ID
*/
abstract public function insertRelation( RelationCreateStruct $createStruct );

/**
* Returns all Content IDs for a given $contentTypeId.
*
* @param int $contentTypeId
*
* @return int[]
*/
abstract public function getContentIdsByContentTypeId( $contentTypeId );
}
Expand Up @@ -1820,4 +1820,30 @@ public function deleteRelation( $relationId, $type )
// No match, do nothing
}
}

/**
* Returns all Content IDs for a given $contentTypeId.
*
* @param int $contentTypeId
*
* @return int[]
*/
public function getContentIdsByContentTypeId( $contentTypeId )
{
$query = $this->dbHandler->createSelectQuery();
$query
->select( $this->dbHandler->quoteColumn( "id" ) )
->from( $this->dbHandler->quoteTable( "ezcontentobject" ) )
->where(
$query->expr->eq(
$this->dbHandler->quoteColumn( "contentclass_id" ),
$query->bindValue( $contentTypeId, null, PDO::PARAM_INT )
)
);

$statement = $query->prepare();
$statement->execute();

return $statement->fetchAll( PDO::FETCH_COLUMN );
}
}
Expand Up @@ -886,4 +886,27 @@ public function insertRelation( RelationCreateStruct $struct )
throw new RuntimeException( 'Database error', 0, $e );
}
}

/**
* Returns all Content IDs for a given $contentTypeId.
*
* @param int $contentTypeId
*
* @return int[]
*/
public function getContentIdsByContentTypeId( $contentTypeId )
{
try
{
return $this->innerGateway->getContentIdsByContentTypeId( $contentTypeId );
}
catch ( DBALException $e )
{
throw new RuntimeException( 'Database error', 0, $e );
}
catch ( PDOException $e )
{
throw new RuntimeException( 'Database error', 0, $e );
}
}
}
39 changes: 10 additions & 29 deletions eZ/Publish/Core/Persistence/Legacy/Content/Type/ContentUpdater.php
Expand Up @@ -38,13 +38,6 @@ class ContentUpdater
*/
protected $converterRegistry;

/**
* Search handler
*
* @var \eZ\Publish\Core\Search\Legacy\Content\Handler
*/
protected $searchHandler;

/**
* Storage handler
*
Expand All @@ -60,20 +53,17 @@ class ContentUpdater
/**
* Creates a new content updater
*
* @param \eZ\Publish\SPI\Search\Content\Handler $searchHandler
* @param \eZ\Publish\Core\Persistence\Legacy\Content\Gateway $contentGateway
* @param \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry $converterRegistry
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageHandler $storageHandler
* @param \eZ\Publish\Core\Persistence\Legacy\Content\Mapper $contentMapper
*/
public function __construct(
SearchHandler $searchHandler,
ContentGateway $contentGateway,
Registry $converterRegistry,
StorageHandler $storageHandler,
ContentMapper $contentMapper )
{
$this->searchHandler = $searchHandler;
$this->contentGateway = $contentGateway;
$this->converterRegistry = $converterRegistry;
$this->storageHandler = $storageHandler;
Expand Down Expand Up @@ -152,11 +142,16 @@ protected function hasFieldDefinition( Type $type, FieldDefinition $fieldDef )
*/
public function applyUpdates( $contentTypeId, array $actions )
{
foreach ( $this->loadContentObjects( $contentTypeId ) as $contentInfo )
if ( empty( $actions ) )
{
return;
}

foreach ( $this->getContentIdsByContentTypeId( $contentTypeId ) as $contentId )
{
foreach ( $actions as $action )
{
$action->apply( $contentInfo );
$action->apply( $contentId );
}
}
}
Expand All @@ -166,24 +161,10 @@ public function applyUpdates( $contentTypeId, array $actions )
*
* @param mixed $contentTypeId
*
* @return \eZ\Publish\SPI\Persistence\Content\ContentInfo[]
* @return int[]
*/
protected function loadContentObjects( $contentTypeId )
protected function getContentIdsByContentTypeId( $contentTypeId )
{
$result = $this->searchHandler->findContent(
new Query(
array(
'filter' => new Criterion\ContentTypeId( $contentTypeId )
)
)
);

$contentInfo = array();
foreach ( $result->searchHits as $hit )
{
$contentInfo[] = $hit->valueObject;
}

return $contentInfo;
return $this->contentGateway->getContentIdsByContentTypeId( $contentTypeId );
}
}
Expand Up @@ -9,7 +9,6 @@

namespace eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater;

use eZ\Publish\SPI\Persistence\Content\ContentInfo;
use eZ\Publish\Core\Persistence\Legacy\Content\Gateway as ContentGateway;

/**
Expand Down Expand Up @@ -37,9 +36,7 @@ public function __construct( ContentGateway $contentGateway )
/**
* Applies the action to the given $content
*
* @param \eZ\Publish\SPI\Persistence\Content\ContentInfo $contentInfo
*
* @return void
* @param int $contentId
*/
abstract public function apply( ContentInfo $contentInfo );
abstract public function apply( $contentId );
}
Expand Up @@ -11,7 +11,6 @@

use eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action;
use eZ\Publish\SPI\Persistence\Content;
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
use eZ\Publish\SPI\Persistence\Content\Field;
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
Expand Down Expand Up @@ -65,7 +64,8 @@ public function __construct(
FieldDefinition $fieldDef,
Converter $converter,
StorageHandler $storageHandler,
ContentMapper $contentMapper)
ContentMapper $contentMapper
)
{
$this->contentGateway = $contentGateway;
$this->fieldDefinition = $fieldDef;
Expand All @@ -77,43 +77,66 @@ public function __construct(
/**
* Applies the action to the given $content
*
* @param \eZ\Publish\SPI\Persistence\Content\ContentInfo $contentInfo
* @param int $contentId
*/
public function apply( ContentInfo $contentInfo )
public function apply( $contentId )
{
$languageCodeSet = array();
$versionNumbers = $this->contentGateway->listVersionNumbers( $contentInfo->id );
$versionNumbers = $this->contentGateway->listVersionNumbers( $contentId );
$languageCodeToFieldId = array();

$contentRows = $this->contentGateway->load( $contentInfo->id, $contentInfo->currentVersionNo );
$contentList = $this->contentMapper->extractContentFromRows( $contentRows );
$content = $contentList[0];

foreach ( $content->fields as $field )
foreach ( $versionNumbers as $versionNo )
{
if ( isset( $languageCodeSet[$field->languageCode] ) )
{
continue;
}
$contentRows = $this->contentGateway->load( $contentId, $versionNo );
$contentList = $this->contentMapper->extractContentFromRows( $contentRows );
$content = $contentList[0];
$languageCodeSet = array();

$languageCodeSet[$field->languageCode] = true;

foreach ( $versionNumbers as $versionNo )
// Each subsequent Content version can have additional language(s)
foreach ( $content->fields as $field )
{
$this->insertField(
$languageCode = $field->languageCode;

// Add once for each language per version
if ( isset( $languageCodeSet[$languageCode] ) )
{
continue;
}

$languageCodeSet[$languageCode] = true;

// Check if field was already inserted for current language code,
// in that case we need to preserve its ID across versions
if ( isset( $languageCodeToFieldId[$languageCode] ) )
{
$fieldId = $languageCodeToFieldId[$languageCode];
}
else
{
$fieldId = null;
}

$languageCodeToFieldId[$languageCode] = $this->insertField(
$content,
$this->createField( $versionNo, $field->languageCode )
$this->createField(
$fieldId,
$versionNo,
$languageCode
)
);
}
}
}

/**
* Inserts given $field and appends it to the given $content field collection.
* Inserts given $field to the internal and external storage.
*
* If $field->id is null, creating new field id will be created.
* Otherwise it will be inserted for the given $content version, reusing existing Field id.
*
* @param \eZ\Publish\SPI\Persistence\Content $content
* @param \eZ\Publish\SPI\Persistence\Content\Field $field
*
* @return void
* @return int The ID of the field that was inserted
*/
protected function insertField( Content $content, Field $field )
{
Expand All @@ -123,11 +146,24 @@ protected function insertField( Content $content, Field $field )
$storageValue
);

$field->id = $this->contentGateway->insertNewField(
$content,
$field,
$storageValue
);
if ( isset( $field->id ) )
{
// Insert with existing Field id and given Content version number
$this->contentGateway->insertExistingField(
$content,
$field,
$storageValue
);
}
else
{
// Insert with creating new Field id and given Content version number
$field->id = $this->contentGateway->insertNewField(
$content,
$field,
$storageValue
);
}

// If the storage handler returns true, it means that $field value has been modified
// So we need to update it in order to store those modifications
Expand All @@ -140,38 +176,30 @@ protected function insertField( Content $content, Field $field )
$storageValue
);

if ( $this->fieldDefinition->isTranslatable )
{
$this->contentGateway->updateField(
$field,
$storageValue
);
}
else
{
$this->contentGateway->updateNonTranslatableField(
$field,
$storageValue,
$content->versionInfo->contentInfo->id
);
}
$this->contentGateway->updateField(
$field,
$storageValue
);
}

$content->fields[] = $field;
return $field->id;
}

/**
* Creates new Field value object, setting given parameters and default value
* for a field definition the action is constructed for.
*
*
* @param null|int $id
* @param int $versionNo
* @param string $languageCode
*
* @return \eZ\Publish\SPI\Persistence\Content\Field
*/
protected function createField( $versionNo, $languageCode )
protected function createField( $id, $versionNo, $languageCode )
{
$field = new Field();

$field->id = $id;
$field->fieldDefinitionId = $this->fieldDefinition->id;
$field->type = $this->fieldDefinition->fieldType;
$field->value = clone $this->fieldDefinition->defaultValue;
Expand Down

0 comments on commit 6b86efc

Please sign in to comment.