+++ /dev/null
-<?php
-
-declare(strict_types=1);
-
-/**
- * @copyright 2022 Christopher Ng <chrng8@gmail.com>
- *
- * @author Christopher Ng <chrng8@gmail.com>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace OCA\DAV\Command;
-
-use OC\Core\Command\Base;
-use OCA\DAV\UserMigration\CalendarMigrator;
-use OCA\DAV\UserMigration\CalendarMigratorException;
-use OCP\IUser;
-use OCP\IUserManager;
-use Symfony\Component\Console\Input\InputArgument;
-use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Output\OutputInterface;
-
-class ExportCalendars extends Base {
-
- /** @var IUserManager */
- private $userManager;
-
- /** @var CalendarMigrator */
- private $calendarMigrator;
-
- public function __construct(
- IUserManager $userManager,
- CalendarMigrator $calendarMigrator
- ) {
- parent::__construct();
- $this->userManager = $userManager;
- $this->calendarMigrator = $calendarMigrator;
- }
-
- protected function configure() {
- $this
- ->setName('dav:export-calendars')
- ->setDescription('Export the calendars of a user')
- ->addArgument(
- 'user',
- InputArgument::REQUIRED,
- 'User to export',
- );
- }
-
- protected function execute(InputInterface $input, OutputInterface $output): int {
- $user = $this->userManager->get($input->getArgument('user'));
-
- if (!$user instanceof IUser) {
- $output->writeln('<error>User ' . $input->getArgument('user') . ' does not exist</error>');
- return 1;
- }
-
- try {
- $this->calendarMigrator->export($user, $output);
- } catch (CalendarMigratorException $e) {
- $output->writeln('<error>' . $e->getMessage() . '</error>');
- return $e->getCode() !== 0 ? (int)$e->getCode() : 1;
- }
-
- return 0;
- }
-}
+++ /dev/null
-<?php
-
-declare(strict_types=1);
-
-/**
- * @copyright 2022 Christopher Ng <chrng8@gmail.com>
- *
- * @author Christopher Ng <chrng8@gmail.com>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace OCA\DAV\Command;
-
-use OC\Core\Command\Base;
-use OCA\DAV\UserMigration\CalendarMigrator;
-use OCA\DAV\UserMigration\CalendarMigratorException;
-use OCP\IUser;
-use OCP\IUserManager;
-use Symfony\Component\Console\Input\InputArgument;
-use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Output\OutputInterface;
-
-class ImportCalendar extends Base {
-
- /** @var IUserManager */
- private $userManager;
-
- /** @var CalendarMigrator */
- private $calendarMigrator;
-
- public function __construct(
- IUserManager $userManager,
- CalendarMigrator $calendarMigrator
- ) {
- parent::__construct();
- $this->userManager = $userManager;
- $this->calendarMigrator = $calendarMigrator;
- }
-
- protected function configure() {
- $this
- ->setName('dav:import-calendar')
- ->setDescription('Import a calendar to a user\'s account')
- ->addArgument(
- 'user',
- InputArgument::REQUIRED,
- 'User to import the calendar for',
- )
- ->addArgument(
- 'path',
- InputArgument::REQUIRED,
- 'Path to the *.ics file',
- );
- }
-
- protected function execute(InputInterface $input, OutputInterface $output): int {
- $user = $this->userManager->get($input->getArgument('user'));
-
- [
- 'basename' => $filename,
- 'dirname' => $srcDir,
- ] = pathinfo($input->getArgument('path'));
-
-
- if (!$user instanceof IUser) {
- $output->writeln('<error>User ' . $input->getArgument('user') . ' does not exist</error>');
- return 1;
- }
-
- try {
- $this->calendarMigrator->import($user, $srcDir, $filename, $output);
- } catch (CalendarMigratorException $e) {
- $output->writeln('<error>' . $e->getMessage() . '</error>');
- return $e->getCode() !== 0 ? (int)$e->getCode() : 1;
- }
-
- return 0;
- }
-}
namespace OCA\DAV\UserMigration;
-use function Safe\fopen;
use function Safe\substr;
-use OC\Files\Filesystem;
-use OC\Files\View;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\ICSExportPlugin\ICSExportPlugin;
use OCA\DAV\CalDAV\Plugin as CalDAVPlugin;
use OCP\Defaults;
use OCP\IL10N;
use OCP\IUser;
+use OCP\UserMigration\IExportDestination;
+use OCP\UserMigration\IImportSource;
+use OCP\UserMigration\IMigrator;
+use OCP\UserMigration\TMigratorBasicVersionHandling;
use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\Version as SabreDavVersion;
use Sabre\VObject\Component as VObjectComponent;
use Safe\Exceptions\FilesystemException;
use Symfony\Component\Console\Output\OutputInterface;
-class CalendarMigrator {
+class CalendarMigrator implements IMigrator {
+
+ use TMigratorBasicVersionHandling;
private CalDavBackend $calDavBackend;
private SabreDavServer $sabreDavServer;
- public const USERS_URI_ROOT = 'principals/users/';
+ private const USERS_URI_ROOT = 'principals/users/';
+
+ private const FILENAME_EXT = '.ics';
- public const FILENAME_EXT = '.ics';
+ private const MIGRATED_URI_PREFIX = 'migrated-';
- public const MIGRATED_URI_PREFIX = 'migrated-';
+ private const EXPORT_ROOT = 'calendars/';
public function __construct(
CalDavBackend $calDavBackend,
$this->defaults = $defaults;
$this->l10n = $l10n;
+ // Override trait property
+ $this->mandatory = true;
+
$root = new RootCollection();
$this->sabreDavServer = new SabreDavServer(new CachingTree($root));
$this->sabreDavServer->addPlugin(new CalDAVPlugin());
}
- public function getPrincipalUri(IUser $user): string {
+ private function getPrincipalUri(IUser $user): string {
return CalendarMigrator::USERS_URI_ROOT . $user->getUID();
}
* @throws CalendarMigratorException
* @throws InvalidCalendarException
*/
- public function getCalendarExportData(IUser $user, ICalendar $calendar): array {
+ private function getCalendarExportData(IUser $user, ICalendar $calendar): array {
$userId = $user->getUID();
$calendarId = $calendar->getKey();
$calendarInfo = $this->calDavBackend->getCalendarById($calendarId);
*
* @throws CalendarMigratorException
*/
- public function getCalendarExports(IUser $user): array {
+ private function getCalendarExports(IUser $user): array {
$principalUri = $this->getPrincipalUri($user);
return array_values(array_filter(array_map(
)));
}
- public function getUniqueCalendarUri(IUser $user, string $initialCalendarUri): string {
+ private function getUniqueCalendarUri(IUser $user, string $initialCalendarUri): string {
$principalUri = $this->getPrincipalUri($user);
$initialCalendarUri = substr($initialCalendarUri, 0, strlen(CalendarMigrator::MIGRATED_URI_PREFIX)) === CalendarMigrator::MIGRATED_URI_PREFIX
? $initialCalendarUri
}
/**
- * @throws CalendarMigratorException
+ * {@inheritDoc}
*/
- protected function writeExport(IUser $user, string $data, string $destDir, string $filename, OutputInterface $output): void {
- $userId = $user->getUID();
-
- \OC::$server->getUserFolder($userId);
- Filesystem::initMountPoints($userId);
-
- $view = new View();
+ public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void {
+ $output->writeln("Exporting calendars…");
- if ($view->file_put_contents("$destDir/$filename", $data) === false) {
- throw new CalendarMigratorException('Could not export calendar');
- }
-
- $output->writeln("<info>✅ Exported calendar of <$userId> into $destDir/$filename</info>");
- }
-
- public function export(IUser $user, OutputInterface $output): void {
$userId = $user->getUID();
try {
// Set filename to sanitized calendar name appended with the date
$filename = preg_replace('/[^a-zA-Z0-9-_ ]/um', '', $name) . '_' . date('Y-m-d') . CalendarMigrator::FILENAME_EXT;
- $this->writeExport(
- $user,
- $vCalendar->serialize(),
- // TESTING directory does not automatically get created so just write to user directory, this will be put in a zip with all other user_migration data
- // "/$userId/export/$appId",
- "/$userId",
- $filename,
- $output,
- );
+ if ($exportDestination->addFileContents(CalendarMigrator::EXPORT_ROOT . $filename, $vCalendar->serialize()) === false) {
+ throw new CalendarMigratorException();
+ }
}
}
/**
- * Return an associative array mapping Time Zone ID to VTimeZone component
- *
* @return array<string, VTimeZone>
*/
- public function getCalendarTimezones(VCalendar $vCalendar): array {
+ private function getCalendarTimezones(VCalendar $vCalendar): array {
/** @var VTimeZone[] $calendarTimezones */
$calendarTimezones = array_values(array_filter(
$vCalendar->getComponents(),
/**
* @return VTimeZone[]
*/
- public function getTimezonesForComponent(VCalendar $vCalendar, VObjectComponent $component): array {
+ private function getTimezonesForComponent(VCalendar $vCalendar, VObjectComponent $component): array {
$componentTimezoneIds = [];
foreach ($component->children() as $child) {
)));
}
- public function sanitizeComponent(VObjectComponent $component): VObjectComponent {
+ private function sanitizeComponent(VObjectComponent $component): VObjectComponent {
// Operate on the component clone to prevent mutation of the original
$componentClone = clone $component;
/**
* @return VObjectComponent[]
*/
- public function getRequiredImportComponents(VCalendar $vCalendar, VObjectComponent $component): array {
+ private function getRequiredImportComponents(VCalendar $vCalendar, VObjectComponent $component): array {
$component = $this->sanitizeComponent($component);
/** @var array<int, VTimeZone> $timezoneComponents */
$timezoneComponents = $this->getTimezonesForComponent($vCalendar, $component);
];
}
- public function initCalendarObject(): VCalendar {
+ private function initCalendarObject(): VCalendar {
$vCalendarObject = new VCalendar();
$vCalendarObject->PRODID = $this->sabreDavServer::$exposeVersion
? '-//SabreDAV//SabreDAV ' . SabreDavVersion::VERSION . '//EN'
return $vCalendarObject;
}
- public function importCalendarObject(int $calendarId, VCalendar $vCalendarObject): void {
+ private function importCalendarObject(int $calendarId, VCalendar $vCalendarObject): void {
try {
$this->calDavBackend->createCalendarObject(
$calendarId,
/**
* @throws CalendarMigratorException
*/
- public function importCalendar(IUser $user, string $filename, string $initialCalendarUri, VCalendar $vCalendar): void {
+ private function importCalendar(IUser $user, string $filename, string $initialCalendarUri, VCalendar $vCalendar): void {
$principalUri = $this->getPrincipalUri($user);
$calendarUri = $this->getUniqueCalendarUri($user, $initialCalendarUri);
}
/**
+ * {@inheritDoc}
+ *
* @throws FilesystemException
* @throws CalendarMigratorException
*/
- public function import(IUser $user, string $srcDir, string $filename, OutputInterface $output): void {
- $userId = $user->getUID();
-
- try {
- /** @var VCalendar $vCalendar */
- $vCalendar = VObjectReader::read(
- fopen("$srcDir/$filename", 'r'),
- VObjectReader::OPTION_FORGIVING,
- );
- } catch (FilesystemException $e) {
- throw new FilesystemException("Failed to read file: \"$srcDir/$filename\"");
+ public function import(IUser $user, IImportSource $importSource, OutputInterface $output): void {
+ if ($importSource->getMigratorVersion(static::class) === null) {
+ $output->writeln('No version for ' . static::class . ', skipping import…');
+ return;
}
- $problems = $vCalendar->validate();
- if (empty($problems)) {
- $splitFilename = explode('_', $filename, 2);
- if (count($splitFilename) !== 2) {
- $output->writeln("<error>Invalid filename, filename must be of the format: \"<calendar_name>_YYYY-MM-DD" . CalendarMigrator::FILENAME_EXT . "\"</error>");
- throw new CalendarMigratorException();
+ $output->writeln("Importing calendars…");
+
+ foreach ($importSource->getFolderListing(CalendarMigrator::EXPORT_ROOT) as $filename) {
+ try {
+ /** @var VCalendar $vCalendar */
+ $vCalendar = VObjectReader::read(
+ $importSource->getFileAsStream(CalendarMigrator::EXPORT_ROOT . $filename),
+ VObjectReader::OPTION_FORGIVING,
+ );
+ } catch (FilesystemException $e) {
+ throw new FilesystemException("Failed to read file: \"$filename\"");
}
- [$initialCalendarUri, $suffix] = $splitFilename;
- $this->importCalendar(
- $user,
- $filename,
- $initialCalendarUri,
- $vCalendar,
- );
+ $problems = $vCalendar->validate();
+ if (empty($problems)) {
+ $splitFilename = explode('_', $filename, 2);
+ if (count($splitFilename) !== 2) {
+ $output->writeln("<error>Invalid filename, expected filename of the format: \"<calendar_name>_YYYY-MM-DD" . CalendarMigrator::FILENAME_EXT . "\"</error>");
+ throw new CalendarMigratorException();
+ }
+ [$initialCalendarUri, $suffix] = $splitFilename;
- $vCalendar->destroy();
+ $this->importCalendar(
+ $user,
+ $filename,
+ $initialCalendarUri,
+ $vCalendar,
+ );
- $output->writeln("<info>✅ Imported calendar \"$filename\" into account of <$userId></info>");
- } else {
- throw new CalendarMigratorException("Invalid data contained in \"$srcDir/$filename\"");
+ $vCalendar->destroy();
+ } else {
+ throw new CalendarMigratorException("Invalid data contained in \"$filename\"");
+ }
}
}
}