1
0

RemoveOrphanEventsAndContacts.php 4.1 KB

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