Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-6017: Implemented loadVersionInfoListByContentInfo PAPI method #375

Merged
merged 6 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions eZ/Publish/API/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ public function loadVersionInfo(ContentInfo $contentInfo, ?int $versionNo = null
*/
public function loadVersionInfoById(int $contentId, ?int $versionNo = null): VersionInfo;

/**
* Bulk-load VersionInfo items by the list of ContentInfo Value Objects.
*
* @param array<\eZ\Publish\API\Repository\Values\Content\ContentInfo> $contentInfoList
*
* @return array<int, \eZ\Publish\API\Repository\Values\Content\VersionInfo> List of VersionInfo items with Content Ids as keys
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function loadVersionInfoListByContentInfo(array $contentInfoList): array;

/**
* Loads content in a version for the given content info object.
*
Expand Down
13 changes: 5 additions & 8 deletions eZ/Publish/Core/Helper/TranslationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,12 @@ public function getTranslatedContentName(Content $content, $forcedLanguage = nul

/**
* Returns content name, translated, from a VersionInfo object.
* By default this method uses prioritized languages, unless $forcedLanguage is provided.
*
* @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
* @param string $forcedLanguage
*
* @return string
* By default, this method uses prioritized languages, unless $forcedLanguage is provided.
*/
private function getTranslatedContentNameByVersionInfo(VersionInfo $versionInfo, $forcedLanguage = null)
{
public function getTranslatedContentNameByVersionInfo(
VersionInfo $versionInfo,
?string $forcedLanguage = null
): string {
foreach ($this->getLanguages($forcedLanguage) as $lang) {
$translatedName = $versionInfo->getName($lang);
if ($translatedName !== null) {
Expand Down
29 changes: 29 additions & 0 deletions eZ/Publish/Core/Persistence/Cache/ContentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,4 +580,33 @@ private function getCacheTagsForVersion(VersionInfo $versionInfo, array $tags =

return $getContentInfoTagsFn($contentInfo, $tags);
}

public function loadVersionInfoList(array $contentIds): array
{
return $this->getMultipleCacheValues(
$contentIds,
$this->cacheIdentifierGenerator->generateKey(
self::CONTENT_VERSION_INFO_IDENTIFIER,
[],
true
) . '-',
function (array $cacheMissIds): array {
return $this->persistenceHandler->contentHandler()->loadVersionInfoList($cacheMissIds);
},
function (VersionInfo $versionInfo): array {
return $this->getCacheTagsForVersion($versionInfo);
},
function (VersionInfo $versionInfo) {
return [
$this->cacheIdentifierGenerator->generateKey(
self::CONTENT_VERSION_INFO_IDENTIFIER,
[$versionInfo->contentInfo->id],
true
),
];
},
'',
['content' => $contentIds]
);
}
}
17 changes: 17 additions & 0 deletions eZ/Publish/Core/Persistence/Cache/Tests/ContentHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function providerForCachedLoadMethodsHit(): array
['loadVersionInfo', [2, 1], 'ibx-cvi-2-1', null, null, [['content_version_info', [2], true]], ['ibx-cvi-2'], $version],
['loadVersionInfo', [2], 'ibx-cvi-2', null, null, [['content_version_info', [2], true]], ['ibx-cvi-2'], $version],
['listVersions', [2], 'ibx-c-2-vl', null, null, [['content_version_list', [2], true]], ['ibx-c-2-vl'], [$version]],
['loadVersionInfoList', [[2]], 'ibx-cvi-2', null, null, [['content_version_info', [], true]], ['ibx-cvi'], [2 => $version], true],
];
}

Expand Down Expand Up @@ -300,6 +301,22 @@ public function providerForCachedLoadMethodsMiss(): array
['ibx-c-2-vl'],
[$version],
],
[
'loadVersionInfoList',
[[2]],
'ibx-cvi-2',
[
['content_version', [2, 1], false],
['content', [2], false],
],
['c-2-v-1', 'c-2'],
[
['content_version_info', [], true],
],
['ibx-cvi'],
[2 => $version],
true,
],
];
}

Expand Down
7 changes: 7 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Content/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,11 @@ abstract public function deleteTranslationFromVersion(
int $versionNo,
string $languageCode
): void;

/**
* @param array<int> $contentIds
*
* @throws \eZ\Publish\Core\Base\Exceptions\DatabaseException
*/
abstract public function loadVersionInfoList(array $contentIds): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1980,4 +1980,27 @@ private function setLanguageMaskForUpdateQuery(

return $query;
}

/**
* @throws \Doctrine\DBAL\Driver\Exception
* @throws \Doctrine\DBAL\Exception
*/
public function loadVersionInfoList(array $contentIds): array
{
$queryBuilder = $this->queryBuilder->createVersionInfoFindQueryBuilder();
$expr = $queryBuilder->expr();

$queryBuilder
->andWhere(
$expr->in(
'c.id',
$queryBuilder->createNamedParameter($contentIds, Connection::PARAM_INT_ARRAY)
)
)
->andWhere(
$expr->eq('v.version', 'c.current_version')
);

return $queryBuilder->execute()->fetchAllAssociative();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,13 @@ public function deleteTranslationFromVersion(
throw DatabaseException::wrap($e);
}
}

public function loadVersionInfoList(array $contentIds): array
{
try {
return $this->innerGateway->loadVersionInfoList($contentIds);
} catch (DBALException | PDOException $e) {
throw DatabaseException::wrap($e);
}
}
}
29 changes: 29 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Content/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -904,4 +904,33 @@ static function ($lang) use ($languageCode) {
// reload entire Version w/o removed Translation
return $this->load($contentId, $versionNo);
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function loadVersionInfoList(array $contentIds): array
{
$rows = $this->contentGateway->loadVersionInfoList($contentIds);
$mappedRows = array_map(
static function (array $row): array {
return [
'id' => $row['ezcontentobject_id'],
'version' => $row['ezcontentobject_version_version'],
];
},
$rows,
);

$versionInfoList = $this->mapper->extractVersionInfoListFromRows(
$rows,
$this->contentGateway->loadVersionedNameData($mappedRows)
);

$versionInfoListById = [];
foreach ($versionInfoList as $versionInfo) {
$versionInfoListById[$versionInfo->contentInfo->id] = $versionInfo;
}

return $versionInfoListById;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,63 @@ public function testSetStatus()
);
}

/**
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Handler::loadVersionInfoList
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function testLoadVersionInfoList(): void
{
$handler = $this->getContentHandler();
$gatewayMock = $this->getGatewayMock();
$mapperMock = $this->getMapperMock();

$contentIds = [2, 3];
$versionInfo1 = new VersionInfo([
'contentInfo' => new ContentInfo(['id' => 2]),
]);
$versionInfo2 = new VersionInfo([
'contentInfo' => new ContentInfo(['id' => 3]),
]);

$versionRows = [
['ezcontentobject_id' => 2, 'ezcontentobject_version_version' => 2],
['ezcontentobject_id' => 3, 'ezcontentobject_version_version' => 1],
];

$gatewayMock->expects(self::once())
->method('loadVersionInfoList')
->with($contentIds)
->willReturn($versionRows);

$nameDataRows = [
['ezcontentobject_name_contentobject_id' => 2, 'ezcontentobject_name_content_version' => 2],
['ezcontentobject_name_contentobject_id' => 3, 'ezcontentobject_name_content_version' => 1],
];

$gatewayMock->expects(self::once())
->method('loadVersionedNameData')
->with([['id' => 2, 'version' => 2], ['id' => 3, 'version' => 1]])
->willReturn($nameDataRows);

$mapperMock->expects(self::once())
->method('extractVersionInfoListFromRows')
->with($versionRows)
->willReturn([
$versionInfo1,
$versionInfo2,
]);

$expected = [
2 => $versionInfo1,
3 => $versionInfo2,
];

$result = $handler->loadVersionInfoList($contentIds);

self::assertEquals($expected, $result);
}

/**
* Returns the handler to test.
*
Expand Down
37 changes: 37 additions & 0 deletions eZ/Publish/Core/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,43 @@ public function loadVersionInfoById(int $contentId, ?int $versionNo = null): API
return $versionInfo;
}

public function loadVersionInfoListByContentInfo(array $contentInfoList): array
{
foreach ($contentInfoList as $idx => $contentInfo) {
if (!$contentInfo instanceof ContentInfo) {
throw new InvalidArgumentException(
'$contentInfoList',
sprintf(
'Element at position %d is not an instance of %s',
$idx,
$contentInfo
)
);
}
}

$contentIds = array_map(
static function (ContentInfo $contentInfo): int {
return $contentInfo->getId();
},
$contentInfoList
);

$persistenceVersionInfos = $this->persistenceHandler
->contentHandler()
->loadVersionInfoList($contentIds);

$versionInfoList = [];
foreach ($persistenceVersionInfos as $persistenceVersionInfo) {
$versionInfo = $this->contentDomainMapper->buildVersionInfoDomainObject($persistenceVersionInfo);
if ($this->permissionResolver->canUser('content', 'read', $versionInfo)) {
$versionInfoList[$versionInfo->getContentInfo()->getId()] = $versionInfo;
}
}

return $versionInfoList;
}

/**
* {@inheritdoc}
*/
Expand Down
5 changes: 5 additions & 0 deletions eZ/Publish/Core/Repository/SiteAccessAware/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public function loadVersionInfoById(int $contentId, ?int $versionNo = null): Ver
return $this->service->loadVersionInfoById($contentId, $versionNo);
}

public function loadVersionInfoListByContentInfo(array $contentInfoList): array
{
return $this->service->loadVersionInfoListByContentInfo($contentInfoList);
}

public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, ?int $versionNo = null, bool $useAlwaysAvailable = true): Content
{
return $this->service->loadContentByContentInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public function providerForPassTroughMethods(): array
['newContentMetadataUpdateStruct', [], $contentMetaStruct],
['newContentUpdateStruct', [], $contentUpdateStruct],
['validate', [$contentUpdateStruct, []], []],

['loadVersionInfoListByContentInfo', [[$contentInfo]], [$versionInfo]],
];
}

Expand Down
10 changes: 10 additions & 0 deletions eZ/Publish/SPI/Persistence/Content/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,14 @@ public function deleteTranslationFromContent($contentId, $languageCode);
* @return \eZ\Publish\SPI\Persistence\Content The Content Draft w/o removed Translation
*/
public function deleteTranslationFromDraft($contentId, $versionNo, $languageCode);

/**
* @param array<int> $contentIds
*
* @return array<\eZ\Publish\SPI\Persistence\Content\VersionInfo>
*
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
barw4 marked this conversation as resolved.
Show resolved Hide resolved
*/
public function loadVersionInfoList(array $contentIds): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public function loadVersionInfoById(
return $this->innerService->loadVersionInfoById($contentId, $versionNo);
}

public function loadVersionInfoListByContentInfo(array $contentInfoList): array
{
return $this->innerService->loadVersionInfoListByContentInfo($contentInfoList);
}

public function loadContentByContentInfo(
ContentInfo $contentInfo,
array $languages = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,25 @@ public function testNewContentUpdateStructDecorator()

$decoratedService->newContentUpdateStruct(...$parameters);
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function testLoadVersionInfoListByContentInfoDecorator(): void
{
$serviceMock = $this->createServiceMock();
$decoratedService = $this->createDecorator($serviceMock);

$argument = [$this->createMock(ContentInfo::class)];

$serviceMock
->expects(self::once())
->method('loadVersionInfoListByContentInfo')
->with($argument)
->willReturn([]);

$decoratedService->loadVersionInfoListByContentInfo($argument);
}
}
1 change: 1 addition & 0 deletions src/contracts/Test/IbexaTestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(static function (ContainerBuilder $container): void {
$container->setParameter('ibexa.core.test.resource_dir', self::getResourcesPath());
$container->setParameter('ezpublish.kernel.root_dir', dirname(__DIR__, 3));
});

$this->loadConfiguration($loader);
Expand Down
Loading
Loading