EventComparisonService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2022 Anna Larch <anna.larch@gmx.net>
  5. *
  6. * @author 2022 Anna Larch <anna.larch@gmx.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\CalDAV;
  25. use OCA\DAV\CalDAV\Schedule\IMipService;
  26. use Sabre\VObject\Component\VCalendar;
  27. use Sabre\VObject\Component\VEvent;
  28. class EventComparisonService {
  29. /** @var string[] */
  30. private const EVENT_DIFF = [
  31. 'RECURRENCE-ID',
  32. 'RRULE',
  33. 'SEQUENCE',
  34. 'LAST-MODIFIED'
  35. ];
  36. /**
  37. * If found, remove the event from $eventsToFilter that
  38. * is identical to the passed $filterEvent
  39. * and return whether an identical event was found
  40. *
  41. * This function takes into account the SEQUENCE,
  42. * RRULE, RECURRENCE-ID and LAST-MODIFIED parameters
  43. *
  44. * @param VEvent $filterEvent
  45. * @param array $eventsToFilter
  46. * @return bool true if there was an identical event found and removed, false if there wasn't
  47. */
  48. private function removeIfUnchanged(VEvent $filterEvent, array &$eventsToFilter): bool {
  49. $filterEventData = [];
  50. foreach(self::EVENT_DIFF as $eventDiff) {
  51. $filterEventData[] = IMipService::readPropertyWithDefault($filterEvent, $eventDiff, '');
  52. }
  53. /** @var VEvent $component */
  54. foreach ($eventsToFilter as $k => $eventToFilter) {
  55. $eventToFilterData = [];
  56. foreach(self::EVENT_DIFF as $eventDiff) {
  57. $eventToFilterData[] = IMipService::readPropertyWithDefault($eventToFilter, $eventDiff, '');
  58. }
  59. // events are identical and can be removed
  60. if (empty(array_diff($filterEventData, $eventToFilterData))) {
  61. unset($eventsToFilter[$k]);
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. /**
  68. * Compare two VCalendars with each other and find all changed elements
  69. *
  70. * Returns an array of old and new events
  71. *
  72. * Old events are only detected if they are also changed
  73. * If there is no corresponding old event for a VEvent, it
  74. * has been newly created
  75. *
  76. * @param VCalendar $new
  77. * @param VCalendar|null $old
  78. * @return array<string, VEvent[]|null>
  79. */
  80. public function findModified(VCalendar $new, ?VCalendar $old): array {
  81. $newEventComponents = $new->getComponents();
  82. foreach ($newEventComponents as $k => $event) {
  83. if(!$event instanceof VEvent) {
  84. unset($newEventComponents[$k]);
  85. }
  86. }
  87. if(empty($old)) {
  88. return ['old' => null, 'new' => $newEventComponents];
  89. }
  90. $oldEventComponents = $old->getComponents();
  91. if(is_array($oldEventComponents) && !empty($oldEventComponents)) {
  92. foreach ($oldEventComponents as $k => $event) {
  93. if(!$event instanceof VEvent) {
  94. unset($oldEventComponents[$k]);
  95. continue;
  96. }
  97. if($this->removeIfUnchanged($event, $newEventComponents)) {
  98. unset($oldEventComponents[$k]);
  99. }
  100. }
  101. }
  102. return ['old' => array_values($oldEventComponents), 'new' => array_values($newEventComponents)];
  103. }
  104. }