Skip to content

Commit 91c12b9

Browse files
committed
[WIP] Export and import calendars
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent 0999803 commit 91c12b9

File tree

10 files changed

+638
-0
lines changed

10 files changed

+638
-0
lines changed

apps/dav/appinfo/info.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
<command>OCA\DAV\Command\SyncBirthdayCalendar</command>
5757
<command>OCA\DAV\Command\SyncSystemAddressBook</command>
5858
<command>OCA\DAV\Command\RemoveInvalidShares</command>
59+
<command>OCA\DAV\Command\ExportCalendars</command>
60+
<command>OCA\DAV\Command\ImportCalendar</command>
5961
</commands>
6062

6163
<settings>

apps/dav/composer/composer/autoload_classmap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@
124124
'OCA\\DAV\\Command\\CreateAddressBook' => $baseDir . '/../lib/Command/CreateAddressBook.php',
125125
'OCA\\DAV\\Command\\CreateCalendar' => $baseDir . '/../lib/Command/CreateCalendar.php',
126126
'OCA\\DAV\\Command\\DeleteCalendar' => $baseDir . '/../lib/Command/DeleteCalendar.php',
127+
'OCA\\DAV\\Command\\ExportCalendars' => $baseDir . '/../lib/Command/ExportCalendars.php',
128+
'OCA\\DAV\\Command\\ImportCalendar' => $baseDir . '/../lib/Command/ImportCalendar.php',
127129
'OCA\\DAV\\Command\\ListCalendars' => $baseDir . '/../lib/Command/ListCalendars.php',
128130
'OCA\\DAV\\Command\\MoveCalendar' => $baseDir . '/../lib/Command/MoveCalendar.php',
129131
'OCA\\DAV\\Command\\RemoveInvalidShares' => $baseDir . '/../lib/Command/RemoveInvalidShares.php',
@@ -298,4 +300,7 @@
298300
'OCA\\DAV\\Upload\\UploadFile' => $baseDir . '/../lib/Upload/UploadFile.php',
299301
'OCA\\DAV\\Upload\\UploadFolder' => $baseDir . '/../lib/Upload/UploadFolder.php',
300302
'OCA\\DAV\\Upload\\UploadHome' => $baseDir . '/../lib/Upload/UploadHome.php',
303+
'OCA\\DAV\\UserMigration\\CalendarMigrator' => $baseDir . '/../lib/UserMigration/CalendarMigrator.php',
304+
'OCA\\DAV\\UserMigration\\CalendarMigratorException' => $baseDir . '/../lib/UserMigration/CalendarMigratorException.php',
305+
'OCA\\DAV\\UserMigration\\InvalidCalendarException' => $baseDir . '/../lib/UserMigration/InvalidCalendarException.php',
301306
);

apps/dav/composer/composer/autoload_static.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ class ComposerStaticInitDAV
139139
'OCA\\DAV\\Command\\CreateAddressBook' => __DIR__ . '/..' . '/../lib/Command/CreateAddressBook.php',
140140
'OCA\\DAV\\Command\\CreateCalendar' => __DIR__ . '/..' . '/../lib/Command/CreateCalendar.php',
141141
'OCA\\DAV\\Command\\DeleteCalendar' => __DIR__ . '/..' . '/../lib/Command/DeleteCalendar.php',
142+
'OCA\\DAV\\Command\\ExportCalendars' => __DIR__ . '/..' . '/../lib/Command/ExportCalendars.php',
143+
'OCA\\DAV\\Command\\ImportCalendar' => __DIR__ . '/..' . '/../lib/Command/ImportCalendar.php',
142144
'OCA\\DAV\\Command\\ListCalendars' => __DIR__ . '/..' . '/../lib/Command/ListCalendars.php',
143145
'OCA\\DAV\\Command\\MoveCalendar' => __DIR__ . '/..' . '/../lib/Command/MoveCalendar.php',
144146
'OCA\\DAV\\Command\\RemoveInvalidShares' => __DIR__ . '/..' . '/../lib/Command/RemoveInvalidShares.php',
@@ -313,6 +315,9 @@ class ComposerStaticInitDAV
313315
'OCA\\DAV\\Upload\\UploadFile' => __DIR__ . '/..' . '/../lib/Upload/UploadFile.php',
314316
'OCA\\DAV\\Upload\\UploadFolder' => __DIR__ . '/..' . '/../lib/Upload/UploadFolder.php',
315317
'OCA\\DAV\\Upload\\UploadHome' => __DIR__ . '/..' . '/../lib/Upload/UploadHome.php',
318+
'OCA\\DAV\\UserMigration\\CalendarMigrator' => __DIR__ . '/..' . '/../lib/UserMigration/CalendarMigrator.php',
319+
'OCA\\DAV\\UserMigration\\CalendarMigratorException' => __DIR__ . '/..' . '/../lib/UserMigration/CalendarMigratorException.php',
320+
'OCA\\DAV\\UserMigration\\InvalidCalendarException' => __DIR__ . '/..' . '/../lib/UserMigration/InvalidCalendarException.php',
316321
);
317322

318323
public static function getInitializer(ClassLoader $loader)

apps/dav/lib/CalDAV/CalendarImpl.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ public function getKey() {
6969
return $this->calendarInfo['id'];
7070
}
7171

72+
/**
73+
* @inheritDoc
74+
*/
75+
public function getUri() {
76+
return $this->calendarInfo['uri'];
77+
}
78+
7279
/**
7380
* In comparison to getKey() this function returns a human readable (maybe translated) name
7481
* @return null|string
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2022 Christopher Ng <chrng8@gmail.com>
7+
*
8+
* @author Christopher Ng <chrng8@gmail.com>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\DAV\Command;
28+
29+
use OC\Core\Command\Base;
30+
use OCA\DAV\UserMigration\CalendarMigrator;
31+
use OCA\DAV\UserMigration\CalendarMigratorException;
32+
use OCP\IUser;
33+
use OCP\IUserManager;
34+
use Symfony\Component\Console\Input\InputArgument;
35+
use Symfony\Component\Console\Input\InputInterface;
36+
use Symfony\Component\Console\Output\OutputInterface;
37+
38+
class ExportCalendars extends Base {
39+
40+
/** @var IUserManager */
41+
private $userManager;
42+
43+
/** @var CalendarMigrator */
44+
private $calendarMigrator;
45+
46+
public function __construct(
47+
IUserManager $userManager,
48+
CalendarMigrator $calendarMigrator
49+
) {
50+
parent::__construct();
51+
$this->userManager = $userManager;
52+
$this->calendarMigrator = $calendarMigrator;
53+
}
54+
55+
protected function configure() {
56+
$this
57+
->setName('dav:export-calendars')
58+
->setDescription('Export the calendars of a user')
59+
->addArgument(
60+
'user',
61+
InputArgument::REQUIRED,
62+
'User to export',
63+
);
64+
}
65+
66+
protected function execute(InputInterface $input, OutputInterface $output): int {
67+
$user = $this->userManager->get($input->getArgument('user'));
68+
69+
if (!$user instanceof IUser) {
70+
$output->writeln('<error>User ' . $input->getArgument('user') . ' does not exist</error>');
71+
return 1;
72+
}
73+
74+
try {
75+
$this->calendarMigrator->export($user, $output);
76+
} catch (CalendarMigratorException $e) {
77+
$output->writeln('<error>' . $e->getMessage() . '</error>');
78+
return $e->getCode() !== 0 ? $e->getCode() : 1;
79+
}
80+
81+
return 0;
82+
}
83+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2022 Christopher Ng <chrng8@gmail.com>
7+
*
8+
* @author Christopher Ng <chrng8@gmail.com>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\DAV\Command;
28+
29+
use OC\Core\Command\Base;
30+
use OCA\DAV\UserMigration\CalendarMigrator;
31+
use OCA\DAV\UserMigration\CalendarMigratorException;
32+
use OCP\IUser;
33+
use OCP\IUserManager;
34+
use Symfony\Component\Console\Input\InputArgument;
35+
use Symfony\Component\Console\Input\InputInterface;
36+
use Symfony\Component\Console\Output\OutputInterface;
37+
38+
class ImportCalendar extends Base {
39+
40+
/** @var IUserManager */
41+
private $userManager;
42+
43+
/** @var CalendarMigrator */
44+
private $calendarMigrator;
45+
46+
public function __construct(
47+
IUserManager $userManager,
48+
CalendarMigrator $calendarMigrator
49+
) {
50+
parent::__construct();
51+
$this->userManager = $userManager;
52+
$this->calendarMigrator = $calendarMigrator;
53+
}
54+
55+
protected function configure() {
56+
$this
57+
->setName('dav:import-calendar')
58+
->setDescription('Import a calendar to a user\'s account')
59+
->addArgument(
60+
'user',
61+
InputArgument::REQUIRED,
62+
'User to import the calendar for',
63+
)
64+
->addArgument(
65+
'path',
66+
InputArgument::REQUIRED,
67+
'Path to the *.ics file',
68+
);
69+
}
70+
71+
protected function execute(InputInterface $input, OutputInterface $output): int {
72+
$user = $this->userManager->get($input->getArgument('user'));
73+
74+
[
75+
'basename' => $filename,
76+
'dirname' => $srcDir,
77+
] = pathinfo($input->getArgument('path'));
78+
79+
80+
if (!$user instanceof IUser) {
81+
$output->writeln('<error>User ' . $input->getArgument('user') . ' does not exist</error>');
82+
return 1;
83+
}
84+
85+
try {
86+
$this->calendarMigrator->import($user, $srcDir, $filename, $output);
87+
} catch (CalendarMigratorException $e) {
88+
$output->writeln('<error>' . $e->getMessage() . '</error>');
89+
return $e->getCode() !== 0 ? $e->getCode() : 1;
90+
}
91+
92+
return 0;
93+
}
94+
}

0 commit comments

Comments
 (0)