Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Various minor updates
Signed-off-by: Christopher Ng <[email protected]>
  • Loading branch information
Pytal committed Mar 2, 2022
commit 0cbb6d7ba9b5d3b2c79f2cf0bafe6145da0857f6
2 changes: 1 addition & 1 deletion apps/dav/lib/CalDAV/CalendarImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getKey() {
/**
* {@inheritDoc}
*/
public function getUri() {
public function getUri(): string {
return $this->calendarInfo['uri'];
}

Expand Down
66 changes: 29 additions & 37 deletions apps/dav/lib/UserMigration/CalendarMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ public function __construct(
$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());
Expand All @@ -115,7 +112,7 @@ private function getCalendarExportData(IUser $user, ICalendar $calendar): array
$calendarInfo = $this->calDavBackend->getCalendarById($calendarId);

if (empty($calendarInfo)) {
throw new CalendarMigratorException();
throw new CalendarMigratorException("Invalid info for calendar ID: $calendarId");
}

$uri = $calendarInfo['uri'];
Expand Down Expand Up @@ -176,8 +173,6 @@ private function getCalendarExports(IUser $user): array {
function (ICalendar $calendar) use ($user) {
try {
return $this->getCalendarExportData($user, $calendar);
} catch (CalendarMigratorException $e) {
throw new CalendarMigratorException();
} catch (InvalidCalendarException $e) {
// Allow this exception as invalid (e.g. deleted) calendars are not to be exported
return null;
Expand All @@ -194,7 +189,7 @@ private function getUniqueCalendarUri(IUser $user, string $initialCalendarUri):
? $initialCalendarUri
: CalendarMigrator::MIGRATED_URI_PREFIX . $initialCalendarUri;
} catch (StringsException $e) {
throw new CalendarMigratorException();
throw new CalendarMigratorException('Failed to get unique calendar URI', 0, $e);
}

$existingCalendarUris = array_map(
Expand All @@ -216,18 +211,14 @@ private function getUniqueCalendarUri(IUser $user, string $initialCalendarUri):
* {@inheritDoc}
*/
public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void {
$output->writeln('Exporting calendars…');
$output->writeln('Exporting calendars into ' . CalendarMigrator::EXPORT_ROOT . '…');

$userId = $user->getUID();

try {
$calendarExports = $this->getCalendarExports($user);
} catch (CalendarMigratorException $e) {
throw new CalendarMigratorException();
}
$calendarExports = $this->getCalendarExports($user);

if (empty($calendarExports)) {
$output->writeln("<info>User <$userId> has no calendars to export</info>");
$output->writeln("User <$userId> has no calendars to export");
}

/**
Expand All @@ -237,9 +228,10 @@ public function export(IUser $user, IExportDestination $exportDestination, Outpu
foreach ($calendarExports as ['name' => $name, 'vCalendar' => $vCalendar]) {
// 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;
$exportPath = CalendarMigrator::EXPORT_ROOT . $filename;

if ($exportDestination->addFileContents(CalendarMigrator::EXPORT_ROOT . $filename, $vCalendar->serialize()) === false) {
throw new CalendarMigratorException();
if ($exportDestination->addFileContents($exportPath, $vCalendar->serialize()) === false) {
throw new CalendarMigratorException('Could not export calendars');
}
}
}
Expand Down Expand Up @@ -420,40 +412,40 @@ public function import(IUser $user, IImportSource $importSource, OutputInterface
return;
}

$output->writeln('Importing calendars…');
$output->writeln('Importing calendars from ' . CalendarMigrator::EXPORT_ROOT . '…');

foreach ($importSource->getFolderListing(CalendarMigrator::EXPORT_ROOT) as $filename) {
$importPath = CalendarMigrator::EXPORT_ROOT . $filename;
try {
/** @var VCalendar $vCalendar */
$vCalendar = VObjectReader::read(
$importSource->getFileAsStream(CalendarMigrator::EXPORT_ROOT . $filename),
$importSource->getFileAsStream($importPath),
VObjectReader::OPTION_FORGIVING,
);
} catch (Throwable $e) {
throw new CalendarMigratorException();
throw new CalendarMigratorException("Failed to read file: \"$importPath\"", 0, $e);
}

$problems = $vCalendar->validate();
if (empty($problems)) {
$splitFilename = explode('_', $filename, 2);
if (count($splitFilename) !== 2) {
$output->writeln("<error>Invalid filename: \"$filename\" expected filename of the format: \"<calendar_name>_YYYY-MM-DD" . CalendarMigrator::FILENAME_EXT . "\"</error>");
throw new CalendarMigratorException();
}
[$initialCalendarUri, $suffix] = $splitFilename;

$this->importCalendar(
$user,
$filename,
$initialCalendarUri,
$vCalendar,
$output,
);
if (!empty($problems)) {
throw new CalendarMigratorException("Invalid calendar data contained in: \"$importPath\"");
}

$vCalendar->destroy();
} else {
throw new CalendarMigratorException("Invalid data contained in \"$filename\"");
$splitFilename = explode('_', $filename, 2);
if (count($splitFilename) !== 2) {
throw new CalendarMigratorException("Invalid filename: \"$filename\", expected filename of the format: \"<calendar_name>_YYYY-MM-DD" . CalendarMigrator::FILENAME_EXT . '"');
}
[$initialCalendarUri, $suffix] = $splitFilename;

$this->importCalendar(
$user,
$filename,
$initialCalendarUri,
$vCalendar,
$output,
);

$vCalendar->destroy();
}
}
}
2 changes: 1 addition & 1 deletion lib/public/Calendar/ICalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function getKey();
/**
* @since 24.0.0
*/
public function getUri();
public function getUri(): string;

/**
* In comparison to getKey() this function returns a human readable (maybe translated) name
Expand Down