From a76e879f52398e461580dd1e783c0a0677e4894a Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Wed, 6 Apr 2022 13:02:36 -0100 Subject: [PATCH] remove child shares Signed-off-by: Maxence Lange --- lib/Db/ShareWrapperRequest.php | 45 +++++++++++++++++++++++++++++ lib/Service/MaintenanceService.php | 8 +++++ lib/Service/ShareWrapperService.php | 9 ++++++ 3 files changed, 62 insertions(+) diff --git a/lib/Db/ShareWrapperRequest.php b/lib/Db/ShareWrapperRequest.php index e86623deb..aea644cd3 100644 --- a/lib/Db/ShareWrapperRequest.php +++ b/lib/Db/ShareWrapperRequest.php @@ -449,6 +449,51 @@ public function deleteFromCircle(string $circleId): void { $qb = $this->getShareDeleteSql(); $qb->andWhere($qb->exprLimit('share_with', $circleId)); + $ids = array_map( + function (ShareWrapper $share): string { + return $share->getId(); + }, + $this->getItemsFromRequest($qb) + ); + + $this->deleteSharesAndChild($ids); + } + + + public function removeOrphanShares(): void { + $qb = $this->getShareSelectSql(); + $expr = $qb->expr(); + $qb->leftJoin( + CoreQueryBuilder::SHARE, CoreRequestBuilder::TABLE_SHARE, 'p', + $expr->andX($expr->eq('p.id', CoreQueryBuilder::SHARE . '.parent')) + ); + + $qb->filterNull('parent'); + $qb->limitNull('id', false, 'p'); + + $ids = []; + $cursor = $qb->execute(); + while ($data = $cursor->fetch()) { + $ids[] = $data['id']; + } + $cursor->closeCursor(); + + $this->deleteSharesAndChild($ids); + } + + + /** + * @param array $ids + */ + private function deleteSharesAndChild(array $ids): void { + $qb = $this->getShareDeleteSql(); + $qb->andWhere( + $qb->expr()->orX( + $qb->exprLimitInArray('id', $ids), + $qb->exprLimitInArray('parent', $ids) + ) + ); + $qb->execute(); } } diff --git a/lib/Service/MaintenanceService.php b/lib/Service/MaintenanceService.php index e10c8081b..c6119d356 100644 --- a/lib/Service/MaintenanceService.php +++ b/lib/Service/MaintenanceService.php @@ -314,6 +314,14 @@ private function removeMembersWithNoCircles(): void { } + private function removeOrphanShares(): void { + $this->shareWrapperRequest->removeOrphanShares(); + } + + + /** + * @throws RequestBuilderException + */ private function removeDeprecatedShares(): void { $probe = new CircleProbe(); $probe->includePersonalCircles() diff --git a/lib/Service/ShareWrapperService.php b/lib/Service/ShareWrapperService.php index 0636c1e9f..750a6fcc6 100644 --- a/lib/Service/ShareWrapperService.php +++ b/lib/Service/ShareWrapperService.php @@ -110,6 +110,15 @@ public function deleteSharesToCircle(string $circleId) { } + /** + * @param string $circleId + */ + public function deleteAllSharesToCircle(string $circleId): void { + $this->cache->clear(''); + $this->shareWrapperRequest->deleteSharesToCircle($circleId, ''); + } + + /** * @param string $circleId * @param FederatedUser|null $shareRecipient