CalDAVRemoveEmptyValue.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * @copyright 2017 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\Migration;
  26. use Doctrine\DBAL\Platforms\OraclePlatform;
  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. use Psr\Log\LoggerInterface;
  33. use Sabre\VObject\InvalidDataException;
  34. class CalDAVRemoveEmptyValue implements IRepairStep {
  35. /** @var IDBConnection */
  36. private $db;
  37. /** @var CalDavBackend */
  38. private $calDavBackend;
  39. private LoggerInterface $logger;
  40. public function __construct(IDBConnection $db, CalDavBackend $calDavBackend, LoggerInterface $logger) {
  41. $this->db = $db;
  42. $this->calDavBackend = $calDavBackend;
  43. $this->logger = $logger;
  44. }
  45. public function getName() {
  46. return 'Fix broken values of calendar objects';
  47. }
  48. public function run(IOutput $output) {
  49. $pattern = ';VALUE=:';
  50. $count = $warnings = 0;
  51. $objects = $this->getInvalidObjects($pattern);
  52. $output->startProgress(count($objects));
  53. foreach ($objects as $row) {
  54. $calObject = $this->calDavBackend->getCalendarObject((int)$row['calendarid'], $row['uri']);
  55. $data = preg_replace('/' . $pattern . '/', ':', $calObject['calendardata']);
  56. if ($data !== $calObject['calendardata']) {
  57. $output->advance();
  58. try {
  59. $this->calDavBackend->getDenormalizedData($data);
  60. } catch (InvalidDataException $e) {
  61. $this->logger->info('Calendar object for calendar {cal} with uri {uri} still invalid', [
  62. 'app' => 'dav',
  63. 'cal' => (int)$row['calendarid'],
  64. 'uri' => $row['uri'],
  65. ]);
  66. $warnings++;
  67. continue;
  68. }
  69. $this->calDavBackend->updateCalendarObject((int)$row['calendarid'], $row['uri'], $data);
  70. $count++;
  71. }
  72. }
  73. $output->finishProgress();
  74. if ($warnings > 0) {
  75. $output->warning(sprintf('%d events could not be updated, see log file for more information', $warnings));
  76. }
  77. if ($count > 0) {
  78. $output->info(sprintf('Updated %d events', $count));
  79. }
  80. }
  81. protected function getInvalidObjects($pattern) {
  82. if ($this->db->getDatabasePlatform() instanceof OraclePlatform) {
  83. $rows = [];
  84. $chunkSize = 500;
  85. $query = $this->db->getQueryBuilder();
  86. $query->select($query->func()->count('*', 'num_entries'))
  87. ->from('calendarobjects');
  88. $result = $query->execute();
  89. $count = $result->fetchOne();
  90. $result->closeCursor();
  91. $numChunks = ceil($count / $chunkSize);
  92. $query = $this->db->getQueryBuilder();
  93. $query->select(['calendarid', 'uri', 'calendardata'])
  94. ->from('calendarobjects')
  95. ->setMaxResults($chunkSize);
  96. for ($chunk = 0; $chunk < $numChunks; $chunk++) {
  97. $query->setFirstResult($chunk * $chunkSize);
  98. $result = $query->execute();
  99. while ($row = $result->fetch()) {
  100. if (mb_strpos($row['calendardata'], $pattern) !== false) {
  101. unset($row['calendardata']);
  102. $rows[] = $row;
  103. }
  104. }
  105. $result->closeCursor();
  106. }
  107. return $rows;
  108. }
  109. $query = $this->db->getQueryBuilder();
  110. $query->select(['calendarid', 'uri'])
  111. ->from('calendarobjects')
  112. ->where($query->expr()->like(
  113. 'calendardata',
  114. $query->createNamedParameter(
  115. '%' . $this->db->escapeLikeParameter($pattern) . '%',
  116. IQueryBuilder::PARAM_STR
  117. ),
  118. IQueryBuilder::PARAM_STR
  119. ));
  120. $result = $query->execute();
  121. $rows = $result->fetchAll();
  122. $result->closeCursor();
  123. return $rows;
  124. }
  125. }