Skip to content
Closed
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
5 changes: 4 additions & 1 deletion eZ/Publish/API/Repository/LocationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,11 @@ public function getLocationChildCount(Location $location): int;
* Return the subtree size of a given location.
*
* Warning! This method is not permission aware by design.
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
* @param int|null $limit Optional limit to the number of locations to count. (Can be used to limit the number of locations counted in large subtrees.)
*/
public function getSubtreeSize(Location $location): int;
public function getSubtreeSize(Location $location, ?int $limit = null): int;

/**
* Creates the new $location in the content repository for the given content.
Expand Down
36 changes: 36 additions & 0 deletions eZ/Publish/API/Repository/Tests/LocationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3552,6 +3552,42 @@ public function testGetSubtreeSize(): Location
return $location;
}

public function testGetSubtreeSizeWithLimit(): Location
{
$repository = $this->getRepository();
$locationService = $repository->getLocationService();

$folder = $this->createFolder(['eng-GB' => 'Parent Folder'], 2);
$location = $folder->getVersionInfo()->getContentInfo()->getMainLocation();
self::assertSame(1, $locationService->getSubtreeSize($location));

for ($i = 1; $i <= 10; ++$i) {
$this->createFolder(['eng-GB' => 'Child ' . $i], $location->id);
}

self::assertSame(3, $locationService->getSubtreeSize($location, 3));

return $location;
}

public function testGetSubtreeSizeWithInvalidLimitHasNoEffect(): Location
{
$repository = $this->getRepository();
$locationService = $repository->getLocationService();

$folder = $this->createFolder(['eng-GB' => 'Parent Folder'], 2);
$location = $folder->getVersionInfo()->getContentInfo()->getMainLocation();
self::assertSame(1, $locationService->getSubtreeSize($location));

for ($i = 1; $i <= 10; ++$i) {
$this->createFolder(['eng-GB' => 'Child ' . $i], $location->id);
}

self::assertSame(11, $locationService->getSubtreeSize($location, -2));

return $location;
}

/**
* Loads properties from all locations in the $location's subtree.
*
Expand Down
4 changes: 2 additions & 2 deletions eZ/Publish/Core/Persistence/Cache/LocationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,13 @@ public function copySubtree($sourceId, $destinationParentId, $newOwnerId = null)
return $this->persistenceHandler->locationHandler()->copySubtree($sourceId, $destinationParentId, $newOwnerId);
}

public function getSubtreeSize(string $path): int
public function getSubtreeSize(string $path, ?int $limit = null): int
{
$this->logger->logCall(__METHOD__, [
'path' => $path,
]);

return $this->persistenceHandler->locationHandler()->getSubtreeSize($path);
return $this->persistenceHandler->locationHandler()->getSubtreeSize($path, $limit);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ abstract public function loadParentLocationsDataForDraftContent(int $contentId):
*/
abstract public function getSubtreeContent(int $sourceId, bool $onlyIds = false): array;

abstract public function getSubtreeSize(string $path): int;
abstract public function getSubtreeSize(string $path, ?int $limit = null): int;

/**
* Returns data for the first level children of the location identified by given $locationId.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,32 @@ public function getSubtreeContent(int $sourceId, bool $onlyIds = false): array
: $results;
}

public function getSubtreeSize(string $path): int
public function getSubtreeSize(string $path, ?int $limit = null): int
{
$query = $this->createNodeQueryBuilder([$this->dbPlatform->getCountExpression('node_id')]);
$useLimit = $limit !== null && $limit > 0;

$query = $this->createNodeQueryBuilder([$useLimit ? 'node_id' : $this->dbPlatform->getCountExpression('node_id')]);
$query->andWhere(
$query->expr()->like(
't.path_string',
$query->createPositionalParameter(
$path . '%',
)
),
)
);

if ($useLimit) {
$query->setMaxResults($limit);
$outerQuery = $this
->connection
->createQueryBuilder()
->select($this->dbPlatform->getCountExpression('*'))
->from('(' . $query->getSQL() . ')', 't')
->setParameters($query->getParameters());

return (int) $outerQuery->execute()->fetchOne();
}

return (int) $query->execute()->fetchOne();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ public function getSubtreeContent(int $sourceId, bool $onlyIds = false): array
}
}

public function getSubtreeSize(string $path): int
public function getSubtreeSize(string $path, ?int $limit = null): int
{
try {
return $this->innerGateway->getSubtreeSize($path);
return $this->innerGateway->getSubtreeSize($path, $limit);
} catch (DBALException | PDOException $e) {
throw DatabaseException::wrap($e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ public function copySubtree($sourceId, $destinationParentId, $newOwnerId = null)
return $copiedSubtreeRootLocation;
}

public function getSubtreeSize(string $path): int
public function getSubtreeSize(string $path, ?int $limit = null): int
{
return $this->locationGateway->getSubtreeSize($path);
return $this->locationGateway->getSubtreeSize($path, $limit);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions eZ/Publish/Core/Repository/LocationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,11 @@ public function getLocationChildCount(APILocation $location): int
return $this->count($filter);
}

public function getSubtreeSize(APILocation $location): int
public function getSubtreeSize(APILocation $location, ?int $limit = null): int
{
return $this->persistenceHandler->locationHandler()->getSubtreeSize(
$location->getPathString()
$location->getPathString(),
$limit
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ public function getLocationChildCount(Location $location): int
return $this->service->getLocationChildCount($location);
}

public function getSubtreeSize(Location $location): int
public function getSubtreeSize(Location $location, ?int $limit = null): int
{
return $this->service->getSubtreeSize($location);
return $this->service->getSubtreeSize($location, $limit);
}

public function createLocation(ContentInfo $contentInfo, LocationCreateStruct $locationCreateStruct): Location
Expand Down
2 changes: 1 addition & 1 deletion eZ/Publish/SPI/Persistence/Content/Location/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function loadParentLocationsForDraftContent($contentId);
*/
public function copySubtree($sourceId, $destinationParentId);

public function getSubtreeSize(string $path): int;
public function getSubtreeSize(string $path, ?int $limit = null): int;

/**
* Moves location identified by $sourceId into new parent identified by $destinationParentId.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public function getLocationChildCount(Location $location): int
return $this->innerService->getLocationChildCount($location);
}

public function getSubtreeSize(Location $location): int
public function getSubtreeSize(Location $location, ?int $limit = null): int
{
return $this->innerService->getSubtreeSize($location);
return $this->innerService->getSubtreeSize($location, $limit);
}

public function createLocation(
Expand Down
Loading