RemoveOrphanEventsAndContacts.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Thomas Citharel <nextcloud@tcit.fr>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\Migration;
  27. use OCA\DAV\CalDAV\CalDavBackend;
  28. use OCP\DB\QueryBuilder\IQueryBuilder;
  29. use OCP\IDBConnection;
  30. use OCP\Migration\IOutput;
  31. use OCP\Migration\IRepairStep;
  32. class RemoveOrphanEventsAndContacts implements IRepairStep {
  33. /** @var IDBConnection */
  34. private $connection;
  35. public function __construct(IDBConnection $connection) {
  36. $this->connection = $connection;
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function getName(): string {
  42. return 'Clean up orphan event and contact data';
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function run(IOutput $output) {
  48. $orphanItems = $this->removeOrphanChildren('calendarobjects', 'calendars', 'calendarid');
  49. $output->info(sprintf('%d events without a calendar have been cleaned up', $orphanItems));
  50. $orphanItems = $this->removeOrphanChildren('calendarobjects_props', 'calendarobjects', 'objectid');
  51. $output->info(sprintf('%d properties without an events have been cleaned up', $orphanItems));
  52. $orphanItems = $this->removeOrphanChildren('calendarchanges', 'calendars', 'calendarid');
  53. $output->info(sprintf('%d changes without a calendar have been cleaned up', $orphanItems));
  54. $orphanItems = $this->removeOrphanChildren('calendarobjects', 'calendarsubscriptions', 'calendarid');
  55. $output->info(sprintf('%d cached events without a calendar subscription have been cleaned up', $orphanItems));
  56. $orphanItems = $this->removeOrphanChildren('calendarchanges', 'calendarsubscriptions', 'calendarid');
  57. $output->info(sprintf('%d changes without a calendar subscription have been cleaned up', $orphanItems));
  58. $orphanItems = $this->removeOrphanChildren('cards', 'addressbooks', 'addressbookid');
  59. $output->info(sprintf('%d contacts without an addressbook have been cleaned up', $orphanItems));
  60. $orphanItems = $this->removeOrphanChildren('cards_properties', 'cards', 'cardid');
  61. $output->info(sprintf('%d properties without a contact have been cleaned up', $orphanItems));
  62. $orphanItems = $this->removeOrphanChildren('addressbookchanges', 'addressbooks', 'addressbookid');
  63. $output->info(sprintf('%d changes without an addressbook have been cleaned up', $orphanItems));
  64. }
  65. protected function removeOrphanChildren($childTable, $parentTable, $parentId): int {
  66. $qb = $this->connection->getQueryBuilder();
  67. $qb->select('c.id')
  68. ->from($childTable, 'c')
  69. ->leftJoin('c', $parentTable, 'p', $qb->expr()->eq('c.' . $parentId, 'p.id'))
  70. ->where($qb->expr()->isNull('p.id'));
  71. if (\in_array($parentTable, ['calendars', 'calendarsubscriptions'], true)) {
  72. $calendarType = $parentTable === 'calendarsubscriptions' ? CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION : CalDavBackend::CALENDAR_TYPE_CALENDAR;
  73. $qb->andWhere($qb->expr()->eq('c.calendartype', $qb->createNamedParameter($calendarType, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT));
  74. }
  75. $result = $qb->execute();
  76. $orphanItems = [];
  77. while ($row = $result->fetch()) {
  78. $orphanItems[] = (int) $row['id'];
  79. }
  80. $result->closeCursor();
  81. if (!empty($orphanItems)) {
  82. $qb->delete($childTable)
  83. ->where($qb->expr()->in('id', $qb->createParameter('ids')));
  84. $orphanItemsBatch = array_chunk($orphanItems, 200);
  85. foreach ($orphanItemsBatch as $items) {
  86. $qb->setParameter('ids', $items, IQueryBuilder::PARAM_INT_ARRAY);
  87. $qb->execute();
  88. }
  89. }
  90. return count($orphanItems);
  91. }
  92. }