Skip to content

Commit 04c5b01

Browse files
fixup! feat: Calendar Import
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
1 parent b09ec88 commit 04c5b01

File tree

7 files changed

+50
-56
lines changed

7 files changed

+50
-56
lines changed

apps/dav/appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
<command>OCA\DAV\Command\DeleteSubscription</command>
6666
<command>OCA\DAV\Command\ExportCalendar</command>
6767
<command>OCA\DAV\Command\FixCalendarSyncCommand</command>
68-
<command>OCA\DAV\Command\ImportCalendar</command>
6968
<command>OCA\DAV\Command\GetAbsenceCommand</command>
69+
<command>OCA\DAV\Command\ImportCalendar</command>
7070
<command>OCA\DAV\Command\ListAddressbooks</command>
7171
<command>OCA\DAV\Command\ListCalendarShares</command>
7272
<command>OCA\DAV\Command\ListCalendars</command>

apps/dav/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@
168168
'OCA\\DAV\\Command\\DeleteSubscription' => $baseDir . '/../lib/Command/DeleteSubscription.php',
169169
'OCA\\DAV\\Command\\ExportCalendar' => $baseDir . '/../lib/Command/ExportCalendar.php',
170170
'OCA\\DAV\\Command\\FixCalendarSyncCommand' => $baseDir . '/../lib/Command/FixCalendarSyncCommand.php',
171-
'OCA\\DAV\\Command\\ImportCalendar' => $baseDir . '/../lib/Command/ImportCalendar.php',
172171
'OCA\\DAV\\Command\\GetAbsenceCommand' => $baseDir . '/../lib/Command/GetAbsenceCommand.php',
172+
'OCA\\DAV\\Command\\ImportCalendar' => $baseDir . '/../lib/Command/ImportCalendar.php',
173173
'OCA\\DAV\\Command\\ListAddressbooks' => $baseDir . '/../lib/Command/ListAddressbooks.php',
174174
'OCA\\DAV\\Command\\ListCalendarShares' => $baseDir . '/../lib/Command/ListCalendarShares.php',
175175
'OCA\\DAV\\Command\\ListCalendars' => $baseDir . '/../lib/Command/ListCalendars.php',

apps/dav/composer/composer/autoload_static.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ class ComposerStaticInitDAV
183183
'OCA\\DAV\\Command\\DeleteSubscription' => __DIR__ . '/..' . '/../lib/Command/DeleteSubscription.php',
184184
'OCA\\DAV\\Command\\ExportCalendar' => __DIR__ . '/..' . '/../lib/Command/ExportCalendar.php',
185185
'OCA\\DAV\\Command\\FixCalendarSyncCommand' => __DIR__ . '/..' . '/../lib/Command/FixCalendarSyncCommand.php',
186-
'OCA\\DAV\\Command\\ImportCalendar' => __DIR__ . '/..' . '/../lib/Command/ImportCalendar.php',
187186
'OCA\\DAV\\Command\\GetAbsenceCommand' => __DIR__ . '/..' . '/../lib/Command/GetAbsenceCommand.php',
187+
'OCA\\DAV\\Command\\ImportCalendar' => __DIR__ . '/..' . '/../lib/Command/ImportCalendar.php',
188188
'OCA\\DAV\\Command\\ListAddressbooks' => __DIR__ . '/..' . '/../lib/Command/ListAddressbooks.php',
189189
'OCA\\DAV\\Command\\ListCalendarShares' => __DIR__ . '/..' . '/../lib/Command/ListCalendarShares.php',
190190
'OCA\\DAV\\Command\\ListCalendars' => __DIR__ . '/..' . '/../lib/Command/ListCalendars.php',

apps/dav/lib/CalDAV/Import/ImportService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
class ImportService {
2525

26+
/** @var resource */
2627
private $source;
2728

2829
public function __construct(
@@ -73,8 +74,7 @@ private function importText(): Generator {
7374
$sObjectSuffix = $importer::OBJECT_SUFFIX;
7475
// calendar properties
7576
foreach ($structure['VCALENDAR'] as $entry) {
76-
$sObjectPrefix .= $entry;
77-
if (substr($entry, -1) !== "\n" || substr($entry, -2) !== "\r\n") {
77+
if (!str_ends_with($entry, "\n") || !str_ends_with($entry, "\r\n")) {
7878
$sObjectPrefix .= PHP_EOL;
7979
}
8080
}
@@ -280,7 +280,7 @@ public function importProcess(CalendarImpl $calendar, CalendarImportOptions $opt
280280
$objectData
281281
);
282282
$outcome[$uid] = ['outcome' => 'created'];
283-
} elseif ($objectId !== null) {
283+
} else {
284284
[$cid, $oid] = explode('/', $objectId);
285285
if ($options->getSupersede()) {
286286
$this->backend->updateCalendarObject(

apps/dav/lib/CalDAV/Import/TextImporter.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,27 @@ class TextImporter {
1313

1414
public const OBJECT_PREFIX = 'BEGIN:VCALENDAR' . PHP_EOL;
1515
public const OBJECT_SUFFIX = PHP_EOL . 'END:VCALENDAR';
16-
protected const COMPONENT_TYPES = ['VEVENT', 'VTODO', 'VJOURNAL', 'VTIMEZONE'];
16+
private const COMPONENT_TYPES = ['VEVENT', 'VTODO', 'VJOURNAL', 'VTIMEZONE'];
1717

18-
protected bool $analyzed = false;
19-
protected array $structure = ['VCALENDAR' => [], 'VEVENT' => [], 'VTODO' => [], 'VJOURNAL' => [], 'VTIMEZONE' => []];
18+
private bool $analyzed = false;
19+
private array $structure = ['VCALENDAR' => [], 'VEVENT' => [], 'VTODO' => [], 'VJOURNAL' => [], 'VTIMEZONE' => []];
2020

21+
/**
22+
* @param resource $source
23+
*/
2124
public function __construct(
22-
protected $source,
25+
private $source,
2326
) {
24-
//Ensure that the $data var is of the right type
25-
if (!is_string($source) && (!is_resource($source) || get_resource_type($source) !== 'stream')) {
26-
throw new Exception('Source must be a string or a stream resource');
27+
// Ensure that source is a stream resource
28+
if (!is_resource($source) || get_resource_type($source) !== 'stream') {
29+
throw new Exception('Source must be a stream resource');
2730
}
2831
}
2932

3033
/**
3134
* Analyzes the source data and creates a structure of components
3235
*/
33-
protected function analyze() {
36+
private function analyze() {
3437
$componentStart = null;
3538
$componentEnd = null;
3639
$componentId = null;
@@ -46,8 +49,7 @@ protected function analyze() {
4649
if ($data === false || empty(trim($data))) {
4750
continue;
4851
}
49-
// check for withspace at the beginning of the line
50-
// lines with whitespace at the beginning are continuations of the pervious line
52+
// lines with whitespace at the beginning are continuations of the previous line
5153
if (ctype_space($data[0]) === false) {
5254
// detect the line TAG
5355
// detect the first occurrence of ':' or ';'

apps/dav/lib/CalDAV/Import/XmlImporter.php

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,35 @@ class XmlImporter {
1414

1515
public const OBJECT_PREFIX = '<?xml version="1.0" encoding="UTF-8"?><icalendar xmlns="urn:ietf:params:xml:ns:icalendar-2.0"><vcalendar><components>';
1616
public const OBJECT_SUFFIX = '</components></vcalendar></icalendar>';
17-
protected const COMPONENT_TYPES = ['VEVENT', 'VTODO', 'VJOURNAL', 'VTIMEZONE'];
17+
private const COMPONENT_TYPES = ['VEVENT', 'VTODO', 'VJOURNAL', 'VTIMEZONE'];
1818

19-
protected bool $analyzed = false;
20-
protected array $structure = ['VCALENDAR' => [], 'VEVENT' => [], 'VTODO' => [], 'VJOURNAL' => [], 'VTIMEZONE' => []];
21-
protected int $praseLevel = 0;
22-
protected array $prasePath = [];
23-
protected ?int $componentStart = null;
24-
protected ?int $componentEnd = null;
25-
protected int $componentLevel = 0;
26-
protected ?string $componentId = null;
27-
protected ?string $componentType = null;
28-
protected bool $componentIdProperty = false;
19+
private bool $analyzed = false;
20+
private array $structure = ['VCALENDAR' => [], 'VEVENT' => [], 'VTODO' => [], 'VJOURNAL' => [], 'VTIMEZONE' => []];
21+
private int $praseLevel = 0;
22+
private array $prasePath = [];
23+
private ?int $componentStart = null;
24+
private ?int $componentEnd = null;
25+
private int $componentLevel = 0;
26+
private ?string $componentId = null;
27+
private ?string $componentType = null;
28+
private bool $componentIdProperty = false;
2929

30+
/**
31+
* @param resource $source
32+
*/
3033
public function __construct(
31-
protected $source,
34+
private $source,
3235
) {
33-
// ensure that the source is of the right type
34-
if (!is_string($source) && (!is_resource($source) || get_resource_type($source) !== 'stream')) {
35-
throw new Exception('Source must be a string or a stream resource');
36+
// Ensure that source is a stream resource
37+
if (!is_resource($source) || get_resource_type($source) !== 'stream') {
38+
throw new Exception('Source must be a stream resource');
3639
}
3740
}
3841

3942
/**
4043
* Analyzes the source data and creates a structure of components
4144
*/
42-
protected function analyze() {
45+
private function analyze() {
4346
$this->praseLevel = 0;
4447
$this->prasePath = [];
4548
$this->componentStart = null;
@@ -53,21 +56,10 @@ protected function analyze() {
5356
xml_set_object($parser, $this);
5457
xml_set_element_handler($parser, $this->tagStart(...), $this->tagEnd(...));
5558
xml_set_default_handler($parser, $this->tagContents(...));
56-
// If the source is resource then prase if in chunks, otherwise just parse the full source
57-
if (is_resource($this->source)) {
58-
// iterate through the source data chuck by chunk to trigger the handlers
59-
@fseek($this->source, 0);
60-
while ($chunk = fread($this->source, 4096)) {
61-
if (!xml_parse($parser, $chunk, feof($this->source))) {
62-
throw new Exception(
63-
xml_error_string(xml_get_error_code($parser))
64-
. ' At line: '
65-
. xml_get_current_line_number($parser)
66-
);
67-
}
68-
}
69-
} else {
70-
if (!xml_parse($parser, $this->source, true)) {
59+
// iterate through the source data chuck by chunk to trigger the handlers
60+
@fseek($this->source, 0);
61+
while ($chunk = fread($this->source, 4096)) {
62+
if (!xml_parse($parser, $chunk, feof($this->source))) {
7163
throw new Exception(
7264
xml_error_string(xml_get_error_code($parser))
7365
. ' At line: '
@@ -82,7 +74,7 @@ protected function analyze() {
8274
/**
8375
* Handles start of tag events from the parser for all tags
8476
*/
85-
protected function tagStart(XMLParser $parser, string $tag, array $attributes): void {
77+
private function tagStart(XMLParser $parser, string $tag, array $attributes): void {
8678
// add the tag to the path tracker and increment depth the level
8779
$this->praseLevel++;
8880
$this->prasePath[$this->praseLevel] = $tag;
@@ -104,7 +96,7 @@ protected function tagStart(XMLParser $parser, string $tag, array $attributes):
10496
/**
10597
* Handles end of tag events from the parser for all tags
10698
*/
107-
protected function tagEnd(XMLParser $parser, string $tag): void {
99+
private function tagEnd(XMLParser $parser, string $tag): void {
108100
// if the end tag matched the component type or the component id property
109101
// then add the component to the structure
110102
if ($tag === 'UID' || $tag === 'TZID') {
@@ -142,7 +134,7 @@ protected function tagEnd(XMLParser $parser, string $tag): void {
142134
/**
143135
* Handles tag contents events from the parser for all tags
144136
*/
145-
protected function tagContents(XMLParser $parser, string $data): void {
137+
private function tagContents(XMLParser $parser, string $data): void {
146138
if ($this->componentIdProperty) {
147139
$this->componentId = $data;
148140
}

apps/dav/lib/Command/ImportCalendar.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function configure(): void {
4646
->setDescription('Import calendar data to supported calendars from disk or stdin')
4747
->addArgument('uid', InputArgument::REQUIRED, 'Id of system user')
4848
->addArgument('uri', InputArgument::REQUIRED, 'Uri of calendar')
49-
->addArgument('location', InputArgument::OPTIONAL, 'Location of where to write the input. defaults to stdin')
49+
->addArgument('location', InputArgument::OPTIONAL, 'Location to read the input from, defaults to stdin.')
5050
->addOption('format', null, InputOption::VALUE_REQUIRED, 'Format of input (ical, jcal, xcal) defaults to ical', 'ical')
5151
->addOption('errors', null, InputOption::VALUE_REQUIRED, 'how to handel item errors (0 - continue, 1 - fail)')
5252
->addOption('validation', null, InputOption::VALUE_REQUIRED, 'how to handel item validation (0 - no validation, 1 - validate and skip on issue, 2 - validate and fail on issue)')
@@ -176,11 +176,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
176176
'',
177177
'Import Completed',
178178
'================',
179-
'Execution Time: ' . (string)($timeFinished - $timeStarted) . ' sec',
180-
'Total Created: ' . (string)$totalCreated,
181-
'Total Updated: ' . (string)$totalUpdated,
182-
'Total Skipped: ' . (string)$totalSkipped,
183-
'Total Errors: ' . (string)$totalErrors,
179+
'Execution Time: ' . ($timeFinished - $timeStarted) . ' sec',
180+
'Total Created: ' . $totalCreated,
181+
'Total Updated: ' . $totalUpdated,
182+
'Total Skipped: ' . $totalSkipped,
183+
'Total Errors: ' . $totalErrors,
184184
''
185185
]);
186186

0 commit comments

Comments
 (0)