CalDAVRemoveEmptyValue.php 3.4 KB

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