1
0

RemoveDeletedUsersCalendarSubscriptions.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Thomas Citharel <nextcloud@tcit.fr>
  5. *
  6. * @author Thomas Citharel <nextcloud@tcit.fr>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Migration;
  25. use OCP\DB\Exception;
  26. use OCP\IDBConnection;
  27. use OCP\IUserManager;
  28. use OCP\Migration\IOutput;
  29. use OCP\Migration\IRepairStep;
  30. class RemoveDeletedUsersCalendarSubscriptions implements IRepairStep {
  31. /** @var IDBConnection */
  32. private $connection;
  33. /** @var IUserManager */
  34. private $userManager;
  35. /** @var int */
  36. private $progress = 0;
  37. /** @var int[] */
  38. private $orphanSubscriptionIds = [];
  39. private const SUBSCRIPTIONS_CHUNK_SIZE = 1000;
  40. public function __construct(IDBConnection $connection, IUserManager $userManager) {
  41. $this->connection = $connection;
  42. $this->userManager = $userManager;
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function getName(): string {
  48. return 'Clean up old calendar subscriptions from deleted users that were not cleaned-up';
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function run(IOutput $output) {
  54. $nbSubscriptions = $this->countSubscriptions();
  55. $output->startProgress($nbSubscriptions);
  56. while ($this->progress < $nbSubscriptions) {
  57. $this->checkSubscriptions();
  58. $this->progress += self::SUBSCRIPTIONS_CHUNK_SIZE;
  59. $output->advance(min(self::SUBSCRIPTIONS_CHUNK_SIZE, $nbSubscriptions));
  60. }
  61. $output->finishProgress();
  62. $this->deleteOrphanSubscriptions();
  63. $output->info(sprintf('%d calendar subscriptions without an user have been cleaned up', count($this->orphanSubscriptionIds)));
  64. }
  65. /**
  66. * @throws Exception
  67. */
  68. private function countSubscriptions(): int {
  69. $qb = $this->connection->getQueryBuilder();
  70. $query = $qb->select($qb->func()->count('*'))
  71. ->from('calendarsubscriptions');
  72. $result = $query->execute();
  73. $count = $result->fetchOne();
  74. $result->closeCursor();
  75. if ($count !== false) {
  76. $count = (int)$count;
  77. } else {
  78. $count = 0;
  79. }
  80. return $count;
  81. }
  82. /**
  83. * @throws Exception
  84. */
  85. private function checkSubscriptions(): void {
  86. $qb = $this->connection->getQueryBuilder();
  87. $query = $qb->selectDistinct(['id', 'principaluri'])
  88. ->from('calendarsubscriptions')
  89. ->setMaxResults(self::SUBSCRIPTIONS_CHUNK_SIZE)
  90. ->setFirstResult($this->progress);
  91. $result = $query->execute();
  92. while ($row = $result->fetch()) {
  93. $username = $this->getPrincipal($row['principaluri']);
  94. if (!$this->userManager->userExists($username)) {
  95. $this->orphanSubscriptionIds[] = (int) $row['id'];
  96. }
  97. }
  98. $result->closeCursor();
  99. }
  100. /**
  101. * @throws Exception
  102. */
  103. private function deleteOrphanSubscriptions(): void {
  104. foreach ($this->orphanSubscriptionIds as $orphanSubscriptionID) {
  105. $this->deleteOrphanSubscription($orphanSubscriptionID);
  106. }
  107. }
  108. /**
  109. * @throws Exception
  110. */
  111. private function deleteOrphanSubscription(int $orphanSubscriptionID): void {
  112. $qb = $this->connection->getQueryBuilder();
  113. $qb->delete('calendarsubscriptions')
  114. ->where($qb->expr()->eq('id', $qb->createNamedParameter($orphanSubscriptionID)))
  115. ->executeStatement();
  116. }
  117. private function getPrincipal(string $principalUri): string {
  118. $uri = explode('/', $principalUri);
  119. return array_pop($uri);
  120. }
  121. }