CalDAVRemoveEmptyValue.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /** @var IDBConnection */
  16. private $db;
  17. /** @var CalDavBackend */
  18. private $calDavBackend;
  19. private LoggerInterface $logger;
  20. public function __construct(IDBConnection $db, CalDavBackend $calDavBackend, LoggerInterface $logger) {
  21. $this->db = $db;
  22. $this->calDavBackend = $calDavBackend;
  23. $this->logger = $logger;
  24. }
  25. public function getName() {
  26. return 'Fix broken values of calendar objects';
  27. }
  28. public function run(IOutput $output) {
  29. $pattern = ';VALUE=:';
  30. $count = $warnings = 0;
  31. $objects = $this->getInvalidObjects($pattern);
  32. $output->startProgress(count($objects));
  33. foreach ($objects as $row) {
  34. $calObject = $this->calDavBackend->getCalendarObject((int)$row['calendarid'], $row['uri']);
  35. $data = preg_replace('/' . $pattern . '/', ':', $calObject['calendardata']);
  36. if ($data !== $calObject['calendardata']) {
  37. $output->advance();
  38. try {
  39. $this->calDavBackend->getDenormalizedData($data);
  40. } catch (InvalidDataException $e) {
  41. $this->logger->info('Calendar object for calendar {cal} with uri {uri} still invalid', [
  42. 'app' => 'dav',
  43. 'cal' => (int)$row['calendarid'],
  44. 'uri' => $row['uri'],
  45. ]);
  46. $warnings++;
  47. continue;
  48. }
  49. $this->calDavBackend->updateCalendarObject((int)$row['calendarid'], $row['uri'], $data);
  50. $count++;
  51. }
  52. }
  53. $output->finishProgress();
  54. if ($warnings > 0) {
  55. $output->warning(sprintf('%d events could not be updated, see log file for more information', $warnings));
  56. }
  57. if ($count > 0) {
  58. $output->info(sprintf('Updated %d events', $count));
  59. }
  60. }
  61. protected function getInvalidObjects($pattern) {
  62. if ($this->db->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) {
  63. $rows = [];
  64. $chunkSize = 500;
  65. $query = $this->db->getQueryBuilder();
  66. $query->select($query->func()->count('*', 'num_entries'))
  67. ->from('calendarobjects');
  68. $result = $query->execute();
  69. $count = $result->fetchOne();
  70. $result->closeCursor();
  71. $numChunks = ceil($count / $chunkSize);
  72. $query = $this->db->getQueryBuilder();
  73. $query->select(['calendarid', 'uri', 'calendardata'])
  74. ->from('calendarobjects')
  75. ->setMaxResults($chunkSize);
  76. for ($chunk = 0; $chunk < $numChunks; $chunk++) {
  77. $query->setFirstResult($chunk * $chunkSize);
  78. $result = $query->execute();
  79. while ($row = $result->fetch()) {
  80. if (mb_strpos($row['calendardata'], $pattern) !== false) {
  81. unset($row['calendardata']);
  82. $rows[] = $row;
  83. }
  84. }
  85. $result->closeCursor();
  86. }
  87. return $rows;
  88. }
  89. $query = $this->db->getQueryBuilder();
  90. $query->select(['calendarid', 'uri'])
  91. ->from('calendarobjects')
  92. ->where($query->expr()->like(
  93. 'calendardata',
  94. $query->createNamedParameter(
  95. '%' . $this->db->escapeLikeParameter($pattern) . '%',
  96. IQueryBuilder::PARAM_STR
  97. ),
  98. IQueryBuilder::PARAM_STR
  99. ));
  100. $result = $query->execute();
  101. $rows = $result->fetchAll();
  102. $result->closeCursor();
  103. return $rows;
  104. }
  105. }