1
0

CalDAVRemoveEmptyValue.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Migration;
  7. use OCA\DAV\CalDAV\CalDavBackend;
  8. use OCP\DB\QueryBuilder\IQueryBuilder;
  9. use OCP\IDBConnection;
  10. use OCP\Migration\IOutput;
  11. use OCP\Migration\IRepairStep;
  12. use Psr\Log\LoggerInterface;
  13. use Sabre\VObject\InvalidDataException;
  14. class CalDAVRemoveEmptyValue implements IRepairStep {
  15. public function __construct(
  16. private IDBConnection $db,
  17. private CalDavBackend $calDavBackend,
  18. private LoggerInterface $logger,
  19. ) {
  20. }
  21. public function getName() {
  22. return 'Fix broken values of calendar objects';
  23. }
  24. public function run(IOutput $output) {
  25. $pattern = ';VALUE=:';
  26. $count = $warnings = 0;
  27. $objects = $this->getInvalidObjects($pattern);
  28. $output->startProgress(count($objects));
  29. foreach ($objects as $row) {
  30. $calObject = $this->calDavBackend->getCalendarObject((int)$row['calendarid'], $row['uri']);
  31. $data = preg_replace('/' . $pattern . '/', ':', $calObject['calendardata']);
  32. if ($data !== $calObject['calendardata']) {
  33. $output->advance();
  34. try {
  35. $this->calDavBackend->getDenormalizedData($data);
  36. } catch (InvalidDataException $e) {
  37. $this->logger->info('Calendar object for calendar {cal} with uri {uri} still invalid', [
  38. 'app' => 'dav',
  39. 'cal' => (int)$row['calendarid'],
  40. 'uri' => $row['uri'],
  41. ]);
  42. $warnings++;
  43. continue;
  44. }
  45. $this->calDavBackend->updateCalendarObject((int)$row['calendarid'], $row['uri'], $data);
  46. $count++;
  47. }
  48. }
  49. $output->finishProgress();
  50. if ($warnings > 0) {
  51. $output->warning(sprintf('%d events could not be updated, see log file for more information', $warnings));
  52. }
  53. if ($count > 0) {
  54. $output->info(sprintf('Updated %d events', $count));
  55. }
  56. }
  57. protected function getInvalidObjects($pattern) {
  58. if ($this->db->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) {
  59. $rows = [];
  60. $chunkSize = 500;
  61. $query = $this->db->getQueryBuilder();
  62. $query->select($query->func()->count('*', 'num_entries'))
  63. ->from('calendarobjects');
  64. $result = $query->executeQuery();
  65. $count = $result->fetchOne();
  66. $result->closeCursor();
  67. $numChunks = ceil($count / $chunkSize);
  68. $query = $this->db->getQueryBuilder();
  69. $query->select(['calendarid', 'uri', 'calendardata'])
  70. ->from('calendarobjects')
  71. ->setMaxResults($chunkSize);
  72. for ($chunk = 0; $chunk < $numChunks; $chunk++) {
  73. $query->setFirstResult($chunk * $chunkSize);
  74. $result = $query->executeQuery();
  75. while ($row = $result->fetch()) {
  76. if (mb_strpos($row['calendardata'], $pattern) !== false) {
  77. unset($row['calendardata']);
  78. $rows[] = $row;
  79. }
  80. }
  81. $result->closeCursor();
  82. }
  83. return $rows;
  84. }
  85. $query = $this->db->getQueryBuilder();
  86. $query->select(['calendarid', 'uri'])
  87. ->from('calendarobjects')
  88. ->where($query->expr()->like(
  89. 'calendardata',
  90. $query->createNamedParameter(
  91. '%' . $this->db->escapeLikeParameter($pattern) . '%',
  92. IQueryBuilder::PARAM_STR
  93. ),
  94. IQueryBuilder::PARAM_STR
  95. ));
  96. $result = $query->executeQuery();
  97. $rows = $result->fetchAll();
  98. $result->closeCursor();
  99. return $rows;
  100. }
  101. }