Skip to content

Commit b787ccb

Browse files
adamwojsalongosz
andauthored
IBX-4477: Added LocationService::getSubtreeSize method (#360)
For more details see https://issues.ibexa.co/browse/IBX-4477 and ezsystems/ezplatform-kernel#360 * Added LocationService::getSubtreeSize API * Added ContentType::isContainer getter method for magic $isContainer property * Added Location::getPathString getter method for magic property * [Tests] Added integration test coverage for getSubtreeSize --------- Co-authored-by: Andrew Longosz <[email protected]>
1 parent c2c83cb commit b787ccb

File tree

14 files changed

+101
-2
lines changed

14 files changed

+101
-2
lines changed

eZ/Publish/API/Repository/LocationService.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ public function loadParentLocationsForDraftContent(VersionInfo $versionInfo, ?ar
125125
*/
126126
public function getLocationChildCount(Location $location): int;
127127

128+
/**
129+
* Return the subtree size of a given location.
130+
*
131+
* Warning! This method is not permission aware by design.
132+
*/
133+
public function getSubtreeSize(Location $location): int;
134+
128135
/**
129136
* Creates the new $location in the content repository for the given content.
130137
*

eZ/Publish/API/Repository/Tests/LocationServiceTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3471,6 +3471,23 @@ public function testMoveSubtreeKeepsContentHiddenOnChildren(): void
34713471
}
34723472
}
34733473

3474+
public function testGetSubtreeSize(): Location
3475+
{
3476+
$repository = $this->getRepository();
3477+
$locationService = $repository->getLocationService();
3478+
3479+
$folder = $this->createFolder(['eng-GB' => 'Parent Folder'], 2);
3480+
$location = $folder->getVersionInfo()->getContentInfo()->getMainLocation();
3481+
self::assertSame(1, $locationService->getSubtreeSize($location));
3482+
3483+
$this->createFolder(['eng-GB' => 'Child 1'], $location->id);
3484+
$this->createFolder(['eng-GB' => 'Child 2'], $location->id);
3485+
3486+
self::assertSame(3, $locationService->getSubtreeSize($location));
3487+
3488+
return $location;
3489+
}
3490+
34743491
/**
34753492
* Loads properties from all locations in the $location's subtree.
34763493
*

eZ/Publish/API/Repository/Values/Content/Location.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @property-read bool $explicitlyHidden Indicates that the Location entity has been explicitly marked as hidden.
2525
* @property-read string $remoteId a global unique id of the content object
2626
* @property-read int $parentLocationId the id of the parent location
27-
* @property-read string $pathString the path to this location e.g. /1/2/4/23 where 23 is current id.
27+
* @property-read string $pathString @deprecated use {@see Location::getPathString()} instead.
2828
* @property-read array $path Same as $pathString but as array, e.g. [ 1, 2, 4, 23 ]
2929
* @property-read int $depth Depth location has in the location tree
3030
* @property-read int $sortField Specifies which property the child locations should be sorted on. Valid values are found at {@link Location::SORT_FIELD_*}
@@ -239,4 +239,13 @@ public function getSortClauses(): array
239239

240240
return [$sortClause];
241241
}
242+
243+
/**
244+
* The path to the Location represented by the current instance,
245+
* e.g. /1/2/4/23 where 23 is current id.
246+
*/
247+
public function getPathString(): string
248+
{
249+
return $this->pathString;
250+
}
242251
}

eZ/Publish/API/Repository/Values/ContentType/ContentType.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* @property-read string $remoteId a global unique id of the content object
2828
* @property-read string $urlAliasSchema URL alias schema. If nothing is provided, $nameSchema will be used instead.
2929
* @property-read string $nameSchema The name schema.
30-
* @property-read bool $isContainer This flag hints to UIs if type may have children or not.
30+
* @property-read bool $isContainer @deprecated use strict getter {@see ContentType::isContainer} instead.
3131
* @property-read string $mainLanguageCode the main language of the content type names and description used for fallback.
3232
* @property-read bool $defaultAlwaysAvailable if an instance of a content type is created the always available flag is set by default this this value.
3333
* @property-read string[] $languageCodes array of language codes used by content type translations.
@@ -228,4 +228,9 @@ public function getFirstFieldDefinitionOfType(string $fieldTypeIdentifier): ?Fie
228228

229229
return null;
230230
}
231+
232+
public function isContainer(): bool
233+
{
234+
return $this->isContainer;
235+
}
231236
}

eZ/Publish/Core/Persistence/Cache/LocationHandler.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,15 @@ public function copySubtree($sourceId, $destinationParentId, $newOwnerId = null)
259259
return $this->persistenceHandler->locationHandler()->copySubtree($sourceId, $destinationParentId, $newOwnerId);
260260
}
261261

262+
public function getSubtreeSize(string $path): int
263+
{
264+
$this->logger->logCall(__METHOD__, [
265+
'path' => $path,
266+
]);
267+
268+
return $this->persistenceHandler->locationHandler()->getSubtreeSize($path);
269+
}
270+
262271
/**
263272
* {@inheritdoc}
264273
*/

eZ/Publish/Core/Persistence/Legacy/Content/Location/Gateway.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ abstract public function loadParentLocationsDataForDraftContent(int $contentId):
111111
*/
112112
abstract public function getSubtreeContent(int $sourceId, bool $onlyIds = false): array;
113113

114+
abstract public function getSubtreeSize(string $path): int;
115+
114116
/**
115117
* Returns data for the first level children of the location identified by given $locationId.
116118
*/

eZ/Publish/Core/Persistence/Legacy/Content/Location/Gateway/DoctrineDatabase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,21 @@ public function getSubtreeContent(int $sourceId, bool $onlyIds = false): array
237237
: $results;
238238
}
239239

240+
public function getSubtreeSize(string $path): int
241+
{
242+
$query = $this->createNodeQueryBuilder([$this->dbPlatform->getCountExpression('node_id')]);
243+
$query->andWhere(
244+
$query->expr()->like(
245+
't.path_string',
246+
$query->createPositionalParameter(
247+
$path . '%',
248+
)
249+
)
250+
);
251+
252+
return (int) $query->execute()->fetchOne();
253+
}
254+
240255
/**
241256
* Return constraint which limits the given $query to the subtree starting at $rootLocationId.
242257
*/

eZ/Publish/Core/Persistence/Legacy/Content/Location/Gateway/ExceptionConversion.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ public function getSubtreeContent(int $sourceId, bool $onlyIds = false): array
106106
}
107107
}
108108

109+
public function getSubtreeSize(string $path): int
110+
{
111+
try {
112+
return $this->innerGateway->getSubtreeSize($path);
113+
} catch (DBALException | PDOException $e) {
114+
throw DatabaseException::wrap($e);
115+
}
116+
}
117+
109118
public function getChildren(int $locationId): array
110119
{
111120
try {

eZ/Publish/Core/Persistence/Legacy/Content/Location/Handler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ public function copySubtree($sourceId, $destinationParentId, $newOwnerId = null)
346346
return $copiedSubtreeRootLocation;
347347
}
348348

349+
public function getSubtreeSize(string $path): int
350+
{
351+
return $this->locationGateway->getSubtreeSize($path);
352+
}
353+
349354
/**
350355
* Retrieves section ID of the location's content.
351356
*

eZ/Publish/Core/Repository/LocationService.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,13 @@ public function getLocationChildCount(APILocation $location): int
379379
return $this->count($filter);
380380
}
381381

382+
public function getSubtreeSize(APILocation $location): int
383+
{
384+
return $this->persistenceHandler->locationHandler()->getSubtreeSize(
385+
$location->getPathString()
386+
);
387+
}
388+
382389
protected function buildLocationChildrenFilter(APILocation $location): Filter
383390
{
384391
$filter = new Filter();

0 commit comments

Comments
 (0)