-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathImportCalendar.php
More file actions
192 lines (182 loc) · 6.55 KB
/
Copy pathImportCalendar.php
File metadata and controls
192 lines (182 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?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\CalendarImpl;
use OCA\DAV\CalDAV\Import\ImportService;
use OCP\Calendar\CalendarImportOptions;
use OCP\Calendar\IManager;
use OCP\ITempManager;
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 Import Command
*
* Used to import data to supported calendars from disk or stdin
*/
#[AsCommand(
name: 'calendar:import',
description: 'Import calendar data to supported calendars from disk or stdin',
hidden: false
)]
class ImportCalendar extends Command {
public function __construct(
private IUserManager $userManager,
private IManager $calendarManager,
private ITempManager $tempManager,
private ImportService $importService,
) {
parent::__construct();
}
#[\Override]
protected function configure(): void {
$this->setName('calendar:import')
->setDescription('Import calendar data to supported calendars from disk or stdin')
->addArgument('uid', InputArgument::REQUIRED, 'Id of system user')
->addArgument('uri', InputArgument::REQUIRED, 'URI of calendar')
->addArgument('location', InputArgument::OPTIONAL, 'Location to read the input from, defaults to stdin.')
->addOption('format', null, InputOption::VALUE_REQUIRED, 'Format of input (ical, jcal, xcal) defaults to ical', 'ical')
->addOption('errors', null, InputOption::VALUE_REQUIRED, 'how to handle item errors (0 - continue, 1 - fail)')
->addOption('validation', null, InputOption::VALUE_REQUIRED, 'how to handle item validation (0 - no validation, 1 - validate and skip on issue, 2 - validate and fail on issue)')
->addOption('supersede', null, InputOption::VALUE_NONE, 'override/replace existing items')
->addOption('show-created', null, InputOption::VALUE_NONE, 'show all created items after processing')
->addOption('show-updated', null, InputOption::VALUE_NONE, 'show all updated items after processing')
->addOption('show-skipped', null, InputOption::VALUE_NONE, 'show all skipped items after processing')
->addOption('show-errors', null, InputOption::VALUE_NONE, 'show all errored items after processing');
}
#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int {
$userId = $input->getArgument('uid');
$calendarId = $input->getArgument('uri');
$location = $input->getArgument('location');
$format = $input->getOption('format');
$errors = is_numeric($input->getOption('errors')) ? (int)$input->getOption('errors') : null;
$validation = is_numeric($input->getOption('validation')) ? (int)$input->getOption('validation') : null;
$supersede = $input->getOption('supersede');
$showCreated = $input->getOption('show-created');
$showUpdated = $input->getOption('show-updated');
$showSkipped = $input->getOption('show-skipped');
$showErrors = $input->getOption('show-errors');
if (!$this->userManager->userExists($userId)) {
throw new InvalidArgumentException("User <$userId> not found.");
}
// retrieve calendar and evaluate if import is supported and writeable
$calendars = $this->calendarManager->getCalendarsForPrincipal('principals/users/' . $userId, [$calendarId]);
if ($calendars === []) {
throw new InvalidArgumentException("Calendar <$calendarId> not found");
}
$calendar = $calendars[0];
if (!$calendar instanceof CalendarImpl) {
throw new InvalidArgumentException("Calendar <$calendarId> doesn't support this function");
}
if (!$calendar->isWritable()) {
throw new InvalidArgumentException("Calendar <$calendarId> is not writeable");
}
if ($calendar->isDeleted()) {
throw new InvalidArgumentException("Calendar <$calendarId> is deleted");
}
// construct options object
$options = new CalendarImportOptions();
$options->setSupersede($supersede);
if ($errors !== null) {
$options->setErrors($errors);
}
if ($validation !== null) {
$options->setValidate($validation);
}
$options->setFormat($format);
// evaluate if a valid location was given and is usable otherwise default to stdin
$timeStarted = microtime(true);
if ($location !== null) {
$input = fopen($location, 'r');
if ($input === false) {
throw new InvalidArgumentException("Location <$location> is not valid. Cannot open location for read operation.");
}
try {
$outcome = $this->importService->import($input, $calendar, $options);
} finally {
fclose($input);
}
} else {
$input = fopen('php://stdin', 'r');
if ($input === false) {
throw new InvalidArgumentException('Cannot open stdin for read operation.');
}
try {
$tempPath = $this->tempManager->getTemporaryFile();
$tempFile = fopen($tempPath, 'w+');
while (!feof($input)) {
fwrite($tempFile, fread($input, 8192));
}
fseek($tempFile, 0);
$outcome = $this->importService->import($tempFile, $calendar, $options);
} finally {
fclose($input);
fclose($tempFile);
}
}
$timeFinished = microtime(true);
// summarize the outcome
$totalCreated = 0;
$totalUpdated = 0;
$totalSkipped = 0;
$totalErrors = 0;
if ($outcome !== []) {
if ($showCreated || $showUpdated || $showSkipped || $showErrors) {
$output->writeln('');
}
foreach ($outcome as $id => $result) {
if (isset($result['outcome'])) {
switch ($result['outcome']) {
case 'created':
$totalCreated++;
if ($showCreated) {
$output->writeln(['created: ' . $id]);
}
break;
case 'updated':
$totalUpdated++;
if ($showUpdated) {
$output->writeln(['updated: ' . $id]);
}
break;
case 'exists':
$totalSkipped++;
if ($showSkipped) {
$output->writeln(['skipped: ' . $id]);
}
break;
case 'error':
$totalErrors++;
if ($showErrors) {
$output->writeln(['errors: ' . $id]);
$output->writeln($result['errors']);
}
break;
}
}
}
}
$output->writeln([
'',
'Import Completed',
'================',
'Execution Time: ' . ($timeFinished - $timeStarted) . ' sec',
'Total Created: ' . $totalCreated,
'Total Updated: ' . $totalUpdated,
'Total Skipped: ' . $totalSkipped,
'Total Errors: ' . $totalErrors,
''
]);
return self::SUCCESS;
}
}