|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | +namespace OCA\DAV\Command; |
| 9 | + |
| 10 | +use InvalidArgumentException; |
| 11 | +use OCA\DAV\CalDAV\Export\ExportService; |
| 12 | +use OCP\Calendar\CalendarExportOptions; |
| 13 | +use OCP\Calendar\ICalendarExport; |
| 14 | +use OCP\Calendar\IManager; |
| 15 | +use OCP\IUserManager; |
| 16 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 17 | +use Symfony\Component\Console\Command\Command; |
| 18 | +use Symfony\Component\Console\Input\InputArgument; |
| 19 | +use Symfony\Component\Console\Input\InputInterface; |
| 20 | +use Symfony\Component\Console\Input\InputOption; |
| 21 | +use Symfony\Component\Console\Output\OutputInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * Calendar Export Command |
| 25 | + * |
| 26 | + * Used to export data from supported calendars to disk or stdout |
| 27 | + */ |
| 28 | +#[AsCommand( |
| 29 | + name: 'calendar:export', |
| 30 | + description: 'Export calendar data from supported calendars to disk or stdout', |
| 31 | + hidden: false |
| 32 | +)] |
| 33 | +class ExportCalendar extends Command { |
| 34 | + public function __construct( |
| 35 | + private IUserManager $userManager, |
| 36 | + private IManager $calendarManager, |
| 37 | + private ExportService $exportService, |
| 38 | + ) { |
| 39 | + parent::__construct(); |
| 40 | + } |
| 41 | + |
| 42 | + protected function configure(): void { |
| 43 | + $this->setName('calendar:export') |
| 44 | + ->setDescription('Export calendar data from supported calendars to disk or stdout') |
| 45 | + ->addArgument('uid', InputArgument::REQUIRED, 'Id of system user') |
| 46 | + ->addArgument('uri', InputArgument::REQUIRED, 'Uri of calendar') |
| 47 | + ->addOption('format', null, InputOption::VALUE_REQUIRED, 'Format of output (ical, jcal, xcal) defaults to ical', 'ical') |
| 48 | + ->addOption('location', null, InputOption::VALUE_REQUIRED, 'Location of where to write the output. defaults to stdout'); |
| 49 | + } |
| 50 | + |
| 51 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 52 | + $userId = $input->getArgument('uid'); |
| 53 | + $calendarId = $input->getArgument('uri'); |
| 54 | + $format = $input->getOption('format'); |
| 55 | + $location = $input->getOption('location'); |
| 56 | + |
| 57 | + if (!$this->userManager->userExists($userId)) { |
| 58 | + throw new InvalidArgumentException("User <$userId> not found."); |
| 59 | + } |
| 60 | + // retrieve calendar and evaluate if export is supported |
| 61 | + $calendars = $this->calendarManager->getCalendarsForPrincipal('principals/users/' . $userId, [$calendarId]); |
| 62 | + if ($calendars === []) { |
| 63 | + throw new InvalidArgumentException("Calendar <$calendarId> not found."); |
| 64 | + } |
| 65 | + $calendar = $calendars[0]; |
| 66 | + if (!$calendar instanceof ICalendarExport) { |
| 67 | + throw new InvalidArgumentException("Calendar <$calendarId> does not support exporting"); |
| 68 | + } |
| 69 | + // construct options object |
| 70 | + $options = new CalendarExportOptions(); |
| 71 | + // evaluate if provided format is supported |
| 72 | + if (!in_array($format, ExportService::FORMATS, true)) { |
| 73 | + throw new InvalidArgumentException("Format <$format> is not valid."); |
| 74 | + } |
| 75 | + $options->setFormat($format); |
| 76 | + // evaluate is a valid location was given and is usable otherwise output to stdout |
| 77 | + if ($location !== null) { |
| 78 | + $handle = fopen($location, 'wb'); |
| 79 | + if ($handle === false) { |
| 80 | + throw new InvalidArgumentException("Location <$location> is not valid. Can not open location for write operation."); |
| 81 | + } |
| 82 | + |
| 83 | + foreach ($this->exportService->export($calendar, $options) as $chunk) { |
| 84 | + fwrite($handle, $chunk); |
| 85 | + } |
| 86 | + fclose($handle); |
| 87 | + } else { |
| 88 | + foreach ($this->exportService->export($calendar, $options) as $chunk) { |
| 89 | + $output->writeln($chunk); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return self::SUCCESS; |
| 94 | + } |
| 95 | +} |
0 commit comments