Skip to content

Commit a70054e

Browse files
committed
IBX-8418: Remove drafts when trashing its parent or ancestor location
1 parent 7d40748 commit a70054e

File tree

8 files changed

+88
-0
lines changed

8 files changed

+88
-0
lines changed

src/contracts/Persistence/Content/Location/Handler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ public function create(CreateStruct $location);
215215
*/
216216
public function removeSubtree($locationId);
217217

218+
/**
219+
* Removes all draft contents that have no location assigned to them under the given parent location.
220+
*/
221+
public function deleteChildrenDrafts(int $locationId): void;
222+
218223
/**
219224
* Set section on all content objects in the subtree.
220225
* Only main locations will be updated.

src/lib/Persistence/Cache/LocationHandler.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,20 @@ public function removeSubtree($locationId)
415415
return $return;
416416
}
417417

418+
public function deleteChildrenDrafts(int $locationId): void
419+
{
420+
$this->logger->logCall(__METHOD__, ['location' => $locationId]);
421+
422+
$this->persistenceHandler->locationHandler()->deleteChildrenDrafts($locationId);
423+
424+
$this->cache->invalidateTags([
425+
$this->cacheIdentifierGenerator->generateTag(
426+
self::LOCATION_PATH_IDENTIFIER,
427+
[$locationId],
428+
),
429+
]);
430+
}
431+
418432
/**
419433
* {@inheritdoc}
420434
*/

src/lib/Persistence/Legacy/Content/Location/Gateway.php

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

114+
/**
115+
* Finds draft contents created under the given parent location.
116+
*
117+
* @return array<int>
118+
*/
119+
abstract public function getSubtreeChildrenDraftContentIds(int $sourceId): array;
120+
114121
abstract public function getSubtreeSize(string $path): int;
115122

116123
/**

src/lib/Persistence/Legacy/Content/Location/Gateway/DoctrineDatabase.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Doctrine\DBAL\FetchMode;
1111
use Doctrine\DBAL\ParameterType;
1212
use Doctrine\DBAL\Query\QueryBuilder;
13+
use Doctrine\DBAL\Result;
1314
use Ibexa\Contracts\Core\Persistence\Content\ContentInfo;
1415
use Ibexa\Contracts\Core\Persistence\Content\Location;
1516
use Ibexa\Contracts\Core\Persistence\Content\Location\CreateStruct;
@@ -237,6 +238,29 @@ public function getSubtreeContent(int $sourceId, bool $onlyIds = false): array
237238
: $results;
238239
}
239240

241+
/**
242+
* @return array<int>
243+
*
244+
* @throws \Doctrine\DBAL\Exception
245+
* @throws \Doctrine\DBAL\Driver\Exception
246+
*/
247+
public function getSubtreeChildrenDraftContentIds(int $sourceId): array
248+
{
249+
$query = $this->connection->createQueryBuilder();
250+
$query
251+
->select('contentobject_id')
252+
->from('eznode_assignment', 'n')
253+
->innerJoin('n', 'ezcontentobject', 'c', 'n.contentobject_id = c.id')
254+
->andWhere('n.parent_node = :parentNode')
255+
->andWhere('c.status = :status')
256+
->setParameter(':parentNode', $sourceId, ParameterType::INTEGER)
257+
->setParameter(':status', ContentInfo::STATUS_DRAFT, ParameterType::INTEGER);
258+
259+
$statement = $query->execute();
260+
261+
return $statement instanceof Result ? $statement->fetchFirstColumn() : [];
262+
}
263+
240264
public function getSubtreeSize(string $path): int
241265
{
242266
$query = $this->createNodeQueryBuilder([$this->dbPlatform->getCountExpression('node_id')]);

src/lib/Persistence/Legacy/Content/Location/Gateway/ExceptionConversion.php

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

109+
/**
110+
* @return array<int>
111+
*/
112+
public function getSubtreeChildrenDraftContentIds(int $sourceId): array
113+
{
114+
try {
115+
return $this->innerGateway->getSubtreeChildrenDraftContentIds($sourceId);
116+
} catch (PDOException $e) {
117+
throw DatabaseException::wrap($e);
118+
}
119+
}
120+
109121
public function getSubtreeSize(string $path): int
110122
{
111123
try {

src/lib/Persistence/Legacy/Content/Location/Handler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,11 @@ public function removeSubtree($locationId)
546546
$this->treeHandler->removeSubtree($locationId);
547547
}
548548

549+
public function deleteChildrenDrafts(int $locationId): void
550+
{
551+
$this->treeHandler->deleteChildrenDrafts($locationId);
552+
}
553+
549554
/**
550555
* Set section on all content objects in the subtree.
551556
*

src/lib/Persistence/Legacy/Content/TreeHandler.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,26 @@ public function removeSubtree($locationId)
215215
$this->locationGateway->deleteNodeAssignment($contentId);
216216
}
217217

218+
/**
219+
* Removes draft contents assigned to the given parent location and its descendant locations.
220+
*
221+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
222+
*/
223+
public function deleteChildrenDrafts(int $locationId): void
224+
{
225+
$subLocations = $this->locationGateway->getChildren($locationId);
226+
foreach ($subLocations as $subLocation) {
227+
$this->deleteChildrenDrafts($subLocation['node_id']);
228+
}
229+
230+
// Fetch child draft content ids
231+
$subtreeChildrenDraftIds = $this->locationGateway->getSubtreeChildrenDraftContentIds($locationId);
232+
233+
foreach ($subtreeChildrenDraftIds as $contentId) {
234+
$this->removeRawContent($contentId);
235+
}
236+
}
237+
218238
/**
219239
* Set section on all content objects in the subtree.
220240
*

src/lib/Repository/TrashService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public function trash(Location $location): ?APITrashItem
146146

147147
$this->repository->beginTransaction();
148148
try {
149+
$this->persistenceHandler->locationHandler()->deleteChildrenDrafts($location->id);
149150
$spiTrashItem = $this->persistenceHandler->trashHandler()->trashSubtree($location->id);
150151
$this->persistenceHandler->urlAliasHandler()->locationDeleted($location->id);
151152
$this->repository->commit();

0 commit comments

Comments
 (0)