CalDAVRemoveEmptyValue.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @copyright 2017 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\DAV\Migration;
  24. use OCA\DAV\CalDAV\CalDavBackend;
  25. use OCP\DB\QueryBuilder\IQueryBuilder;
  26. use OCP\IDBConnection;
  27. use OCP\ILogger;
  28. use OCP\Migration\IOutput;
  29. use OCP\Migration\IRepairStep;
  30. use Sabre\VObject\InvalidDataException;
  31. class CalDAVRemoveEmptyValue implements IRepairStep {
  32. /** @var IDBConnection */
  33. private $db;
  34. /** @var CalDavBackend */
  35. private $calDavBackend;
  36. /** @var ILogger */
  37. private $logger;
  38. /**
  39. * @param IDBConnection $db
  40. * @param CalDavBackend $calDavBackend
  41. * @param ILogger $logger
  42. */
  43. public function __construct(IDBConnection $db, CalDavBackend $calDavBackend, ILogger $logger) {
  44. $this->db = $db;
  45. $this->calDavBackend = $calDavBackend;
  46. $this->logger = $logger;
  47. }
  48. public function getName() {
  49. return 'Fix broken values of calendar objects';
  50. }
  51. public function run(IOutput $output) {
  52. $pattern = ';VALUE=:';
  53. $count = $warnings = 0;
  54. $objects = $this->getInvalidObjects($pattern);
  55. $output->startProgress(count($objects));
  56. foreach ($objects as $row) {
  57. $calObject = $this->calDavBackend->getCalendarObject((int)$row['calendarid'], $row['uri']);
  58. $data = preg_replace('/' . $pattern . '/', ':', $calObject['calendardata']);
  59. if ($data !== $calObject['calendardata']) {
  60. $output->advance();
  61. try {
  62. $this->calDavBackend->getDenormalizedData($data);
  63. } catch (InvalidDataException $e) {
  64. $this->logger->info('Calendar object for calendar {cal} with uri {uri} still invalid', [
  65. 'app' => 'dav',
  66. 'cal' => (int)$row['calendarid'],
  67. 'uri' => $row['uri'],
  68. ]);
  69. $warnings++;
  70. continue;
  71. }
  72. $this->calDavBackend->updateCalendarObject((int)$row['calendarid'], $row['uri'], $data);
  73. $count++;
  74. }
  75. }
  76. $output->finishProgress();
  77. if ($warnings > 0) {
  78. $output->warning(sprintf('%d events could not be updated, see log file for more information', $warnings));
  79. }
  80. if ($count > 0) {
  81. $output->info(sprintf('Updated %d events', $count));
  82. }
  83. }
  84. protected function getInvalidObjects($pattern) {
  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->execute();
  97. $rows = $result->fetchAll();
  98. $result->closeCursor();
  99. return $rows;
  100. }
  101. }