-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat: Calendar Export #51924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: Calendar Export #51924
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\DAV\CalDAV\Export; | ||
|
|
||
| use Generator; | ||
| use OCP\Calendar\CalendarExportOptions; | ||
| use OCP\Calendar\ICalendarExport; | ||
| use OCP\ServerVersion; | ||
| use Sabre\VObject\Component; | ||
| use Sabre\VObject\Writer; | ||
|
|
||
| /** | ||
| * Calendar Export Service | ||
| */ | ||
| class ExportService { | ||
|
|
||
| public const FORMATS = ['ical', 'jcal', 'xcal']; | ||
| private string $systemVersion; | ||
|
|
||
| public function __construct(ServerVersion $serverVersion) { | ||
| $this->systemVersion = $serverVersion->getVersionString(); | ||
| } | ||
|
|
||
| /** | ||
| * Generates serialized content stream for a calendar and objects based in selected format | ||
| * | ||
| * @return Generator<string> | ||
| */ | ||
| public function export(ICalendarExport $calendar, CalendarExportOptions $options): Generator { | ||
| // output start of serialized content based on selected format | ||
| yield $this->exportStart($options->getFormat()); | ||
| // iterate through each returned vCalendar entry | ||
| // extract each component except timezones, convert to appropriate format and output | ||
| // extract any timezones and save them but do not output | ||
| $timezones = []; | ||
| foreach ($calendar->export($options) as $entry) { | ||
| $consecutive = false; | ||
| foreach ($entry->getComponents() as $vComponent) { | ||
| if ($vComponent->name === 'VTIMEZONE') { | ||
| if (isset($vComponent->TZID) && !isset($timezones[$vComponent->TZID->getValue()])) { | ||
| $timezones[$vComponent->TZID->getValue()] = clone $vComponent; | ||
| } | ||
| } else { | ||
| yield $this->exportObject($vComponent, $options->getFormat(), $consecutive); | ||
| $consecutive = true; | ||
| } | ||
| } | ||
| } | ||
| // iterate through each saved vTimezone entry, convert to appropriate format and output | ||
| foreach ($timezones as $vComponent) { | ||
| yield $this->exportObject($vComponent, $options->getFormat(), $consecutive); | ||
| $consecutive = true; | ||
| } | ||
| // output end of serialized content based on selected format | ||
| yield $this->exportFinish($options->getFormat()); | ||
| } | ||
|
|
||
| /** | ||
| * Generates serialized content start based on selected format | ||
| */ | ||
| private function exportStart(string $format): string { | ||
| return match ($format) { | ||
| 'jcal' => '["vcalendar",[["version",{},"text","2.0"],["prodid",{},"text","-\/\/IDN nextcloud.com\/\/Calendar Export v' . $this->systemVersion . '\/\/EN"]],[', | ||
| 'xcal' => '<?xml version="1.0" encoding="UTF-8"?><icalendar xmlns="urn:ietf:params:xml:ns:icalendar-2.0"><vcalendar><properties><version><text>2.0</text></version><prodid><text>-//IDN nextcloud.com//Calendar Export v' . $this->systemVersion . '//EN</text></prodid></properties><components>', | ||
| default => "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//IDN nextcloud.com//Calendar Export v" . $this->systemVersion . "//EN\n" | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Generates serialized content end based on selected format | ||
| */ | ||
| private function exportFinish(string $format): string { | ||
| return match ($format) { | ||
| 'jcal' => ']]', | ||
| 'xcal' => '</components></vcalendar></icalendar>', | ||
| default => "END:VCALENDAR\n" | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Generates serialized content for a component based on selected format | ||
| */ | ||
| private function exportObject(Component $vobject, string $format, bool $consecutive): string { | ||
| return match ($format) { | ||
| 'jcal' => $consecutive ? ',' . Writer::writeJson($vobject) : Writer::writeJson($vobject), | ||
| 'xcal' => $this->exportObjectXml($vobject), | ||
| default => Writer::write($vobject) | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Generates serialized content for a component in xml format | ||
| */ | ||
| private function exportObjectXml(Component $vobject): string { | ||
| $writer = new \Sabre\Xml\Writer(); | ||
| $writer->openMemory(); | ||
| $writer->setIndent(false); | ||
| $vobject->xmlSerialize($writer); | ||
| return $writer->outputMemory(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\DAV\Command; | ||
|
|
||
| use InvalidArgumentException; | ||
| use OCA\DAV\CalDAV\Export\ExportService; | ||
| use OCP\Calendar\CalendarExportOptions; | ||
| use OCP\Calendar\ICalendarExport; | ||
| use OCP\Calendar\IManager; | ||
| use OCP\IUserManager; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| /** | ||
| * Calendar Export Command | ||
| * | ||
| * Used to export data from supported calendars to disk or stdout | ||
| */ | ||
| #[AsCommand( | ||
| name: 'calendar:export', | ||
kesselb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| description: 'Export calendar data from supported calendars to disk or stdout', | ||
| hidden: false | ||
| )] | ||
| class ExportCalendar extends Command { | ||
| public function __construct( | ||
| private IUserManager $userManager, | ||
| private IManager $calendarManager, | ||
| private ExportService $exportService, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function configure(): void { | ||
| $this->setName('calendar:export') | ||
SebastianKrupinski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ->setDescription('Export calendar data from supported calendars to disk or stdout') | ||
| ->addArgument('uid', InputArgument::REQUIRED, 'Id of system user') | ||
| ->addArgument('uri', InputArgument::REQUIRED, 'Uri of calendar') | ||
| ->addOption('format', null, InputOption::VALUE_REQUIRED, 'Format of output (ical, jcal, xcal) defaults to ical', 'ical') | ||
| ->addOption('location', null, InputOption::VALUE_REQUIRED, 'Location of where to write the output. defaults to stdout'); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int { | ||
| $userId = $input->getArgument('uid'); | ||
| $calendarId = $input->getArgument('uri'); | ||
| $format = $input->getOption('format'); | ||
| $location = $input->getOption('location'); | ||
|
|
||
| if (!$this->userManager->userExists($userId)) { | ||
| throw new InvalidArgumentException("User <$userId> not found."); | ||
| } | ||
| // retrieve calendar and evaluate if export is supported | ||
| $calendars = $this->calendarManager->getCalendarsForPrincipal('principals/users/' . $userId, [$calendarId]); | ||
| if ($calendars === []) { | ||
| throw new InvalidArgumentException("Calendar <$calendarId> not found."); | ||
| } | ||
| $calendar = $calendars[0]; | ||
| if (!$calendar instanceof ICalendarExport) { | ||
| throw new InvalidArgumentException("Calendar <$calendarId> does not support exporting"); | ||
| } | ||
| // construct options object | ||
| $options = new CalendarExportOptions(); | ||
| // evaluate if provided format is supported | ||
| if (!in_array($format, ExportService::FORMATS, true)) { | ||
| throw new InvalidArgumentException("Format <$format> is not valid."); | ||
| } | ||
| $options->setFormat($format); | ||
| // evaluate is a valid location was given and is usable otherwise output to stdout | ||
| if ($location !== null) { | ||
| $handle = fopen($location, 'wb'); | ||
| if ($handle === false) { | ||
| throw new InvalidArgumentException("Location <$location> is not valid. Can not open location for write operation."); | ||
| } | ||
|
|
||
| foreach ($this->exportService->export($calendar, $options) as $chunk) { | ||
| fwrite($handle, $chunk); | ||
| } | ||
| fclose($handle); | ||
| } else { | ||
| foreach ($this->exportService->export($calendar, $options) as $chunk) { | ||
| $output->writeln($chunk); | ||
SebastianKrupinski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.