Skip to content

Commit 58f5de0

Browse files
authored
Merge pull request #31945 from nextcloud/fix/user_migration-use-exceptions
Adapt user_migration APIs to have information about failures
2 parents 964bc57 + 78c8e57 commit 58f5de0

File tree

7 files changed

+78
-63
lines changed

7 files changed

+78
-63
lines changed

apps/dav/lib/UserMigration/CalendarMigrator.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,20 @@ public function export(IUser $user, IExportDestination $exportDestination, Outpu
225225
$output->writeln('No calendars to export…');
226226
}
227227

228-
/**
229-
* @var string $name
230-
* @var VCalendar $vCalendar
231-
*/
232-
foreach ($calendarExports as ['name' => $name, 'vCalendar' => $vCalendar]) {
233-
// Set filename to sanitized calendar name appended with the date
234-
$filename = preg_replace('/[^a-zA-Z0-9-_ ]/um', '', $name) . '_' . date('Y-m-d') . CalendarMigrator::FILENAME_EXT;
235-
$exportPath = CalendarMigrator::EXPORT_ROOT . $filename;
236-
237-
if ($exportDestination->addFileContents($exportPath, $vCalendar->serialize()) === false) {
238-
throw new CalendarMigratorException('Could not export calendars');
228+
try {
229+
/**
230+
* @var string $name
231+
* @var VCalendar $vCalendar
232+
*/
233+
foreach ($calendarExports as ['name' => $name, 'vCalendar' => $vCalendar]) {
234+
// Set filename to sanitized calendar name appended with the date
235+
$filename = preg_replace('/[^a-zA-Z0-9-_ ]/um', '', $name) . '_' . date('Y-m-d') . CalendarMigrator::FILENAME_EXT;
236+
$exportPath = CalendarMigrator::EXPORT_ROOT . $filename;
237+
238+
$exportDestination->addFileContents($exportPath, $vCalendar->serialize());
239239
}
240+
} catch (Throwable $e) {
241+
throw new CalendarMigratorException('Could not export calendars', 0, $e);
240242
}
241243
}
242244

apps/dav/lib/UserMigration/ContactsMigrator.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -205,26 +205,26 @@ public function export(IUser $user, IExportDestination $exportDestination, Outpu
205205
$output->writeln('No contacts to export…');
206206
}
207207

208-
/**
209-
* @var string $name
210-
* @var string $displayName
211-
* @var ?string $description
212-
* @var VCard[] $vCards
213-
*/
214-
foreach ($addressBookExports as ['name' => $name, 'displayName' => $displayName, 'description' => $description, 'vCards' => $vCards]) {
215-
// Set filename to sanitized address book name appended with the date
216-
$basename = preg_replace('/[^a-zA-Z0-9-_ ]/um', '', $name) . '_' . date('Y-m-d');
217-
$exportPath = ContactsMigrator::PATH_ROOT . $basename . '.' . ContactsMigrator::FILENAME_EXT;
218-
$metadataExportPath = ContactsMigrator::PATH_ROOT . $basename . '.' . ContactsMigrator::METADATA_EXT;
219-
220-
if ($exportDestination->addFileContents($exportPath, $this->serializeCards($vCards)) === false) {
221-
throw new ContactsMigratorException('Could not export address book');
222-
}
223-
224-
$metadata = array_filter(['displayName' => $displayName, 'description' => $description]);
225-
if ($exportDestination->addFileContents($metadataExportPath, json_encode($metadata)) === false) {
226-
throw new ContactsMigratorException('Could not export address book metadata');
208+
try {
209+
/**
210+
* @var string $name
211+
* @var string $displayName
212+
* @var ?string $description
213+
* @var VCard[] $vCards
214+
*/
215+
foreach ($addressBookExports as ['name' => $name, 'displayName' => $displayName, 'description' => $description, 'vCards' => $vCards]) {
216+
// Set filename to sanitized address book name appended with the date
217+
$basename = preg_replace('/[^a-zA-Z0-9-_ ]/um', '', $name) . '_' . date('Y-m-d');
218+
$exportPath = ContactsMigrator::PATH_ROOT . $basename . '.' . ContactsMigrator::FILENAME_EXT;
219+
$metadataExportPath = ContactsMigrator::PATH_ROOT . $basename . '.' . ContactsMigrator::METADATA_EXT;
220+
221+
$exportDestination->addFileContents($exportPath, $this->serializeCards($vCards));
222+
223+
$metadata = array_filter(['displayName' => $displayName, 'description' => $description]);
224+
$exportDestination->addFileContents($metadataExportPath, json_encode($metadata));
227225
}
226+
} catch (Throwable $e) {
227+
throw new CalendarMigratorException('Could not export address book', 0, $e);
228228
}
229229
}
230230

apps/files_trashbin/lib/UserMigration/TrashbinMigrator.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,16 @@ public function export(IUser $user, IExportDestination $exportDestination, Outpu
7474
try {
7575
$trashbinFolder = $this->root->get('/'.$uid.'/files_trashbin');
7676
if (!$trashbinFolder instanceof Folder) {
77-
throw new UserMigrationException('Could not export trashbin, /'.$uid.'/files_trashbin is not a folder');
77+
throw new UserMigrationException('/'.$uid.'/files_trashbin is not a folder');
7878
}
7979
$output->writeln("Exporting trashbin files…");
80-
if ($exportDestination->copyFolder($trashbinFolder, static::PATH_FILES_FOLDER) === false) {
81-
throw new UserMigrationException("Could not export trashbin.");
82-
}
80+
$exportDestination->copyFolder($trashbinFolder, static::PATH_FILES_FOLDER);
8381
$originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($uid);
84-
if ($exportDestination->addFileContents(static::PATH_LOCATIONS_FILE, json_encode($originalLocations)) === false) {
85-
throw new UserMigrationException("Could not export trashbin.");
86-
}
82+
$exportDestination->addFileContents(static::PATH_LOCATIONS_FILE, json_encode($originalLocations));
8783
} catch (NotFoundException $e) {
8884
$output->writeln("No trashbin to export…");
85+
} catch (\Throwable $e) {
86+
throw new UserMigrationException("Could not export trashbin: ".$e->getMessage(), 0, $e);
8987
}
9088
}
9189

@@ -112,8 +110,10 @@ public function import(IUser $user, IImportSource $importSource, OutputInterface
112110
$trashbinFolder = $this->root->newFolder('/'.$uid.'/files_trashbin');
113111
}
114112
$output->writeln("Importing trashbin files…");
115-
if ($importSource->copyToFolder($trashbinFolder, static::PATH_FILES_FOLDER) === false) {
116-
throw new UserMigrationException("Could not import trashbin.");
113+
try {
114+
$importSource->copyToFolder($trashbinFolder, static::PATH_FILES_FOLDER);
115+
} catch (\Throwable $e) {
116+
throw new UserMigrationException("Could not import trashbin.", 0, $e);
117117
}
118118
$locations = json_decode($importSource->getFileContents(static::PATH_LOCATIONS_FILE), true, 512, JSON_THROW_ON_ERROR);
119119
$qb = $this->dbc->getQueryBuilder();

apps/settings/lib/UserMigration/AccountMigrator.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,20 @@ public function __construct(
7474
public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void {
7575
$output->writeln('Exporting account information in ' . AccountMigrator::PATH_ACCOUNT_FILE . '');
7676

77-
$account = $this->accountManager->getAccount($user);
78-
if ($exportDestination->addFileContents(AccountMigrator::PATH_ACCOUNT_FILE, json_encode($account)) === false) {
79-
throw new AccountMigratorException('Could not export account information');
80-
}
77+
try {
78+
$account = $this->accountManager->getAccount($user);
79+
$exportDestination->addFileContents(AccountMigrator::PATH_ACCOUNT_FILE, json_encode($account));
8180

82-
$avatar = $this->avatarManager->getAvatar($user->getUID());
83-
if ($avatar->isCustomAvatar()) {
84-
$avatarFile = $avatar->getFile(-1);
85-
$exportPath = AccountMigrator::PATH_ROOT . AccountMigrator::AVATAR_BASENAME . '.' . $avatarFile->getExtension();
81+
$avatar = $this->avatarManager->getAvatar($user->getUID());
82+
if ($avatar->isCustomAvatar()) {
83+
$avatarFile = $avatar->getFile(-1);
84+
$exportPath = AccountMigrator::PATH_ROOT . AccountMigrator::AVATAR_BASENAME . '.' . $avatarFile->getExtension();
8685

87-
$output->writeln('Exporting avatar to ' . $exportPath . '');
88-
if ($exportDestination->addFileAsStream($exportPath, $avatarFile->read()) === false) {
89-
throw new AccountMigratorException('Could not export avatar');
86+
$output->writeln('Exporting avatar to ' . $exportPath . '');
87+
$exportDestination->addFileAsStream($exportPath, $avatarFile->read());
9088
}
89+
} catch (Throwable $e) {
90+
throw new AccountMigratorException('Could not export account information', 0, $e);
9191
}
9292
}
9393

apps/settings/tests/UserMigration/AccountMigratorTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,12 @@ public function testImportExportAccount(string $userId, array $importData, strin
152152
$this->exportDestination
153153
->expects($this->once())
154154
->method('addFileContents')
155-
->with($this->matchesRegularExpression(self::REGEX_ACCOUNT_FILE), json_encode($exportData))
156-
->willReturn(true);
155+
->with($this->matchesRegularExpression(self::REGEX_ACCOUNT_FILE), json_encode($exportData));
157156

158157
$this->exportDestination
159158
->expects($this->once())
160159
->method('addFileAsStream')
161-
->with($this->matchesRegularExpression(self::REGEX_AVATAR_FILE), $this->isType('resource'))
162-
->willReturn(true);
160+
->with($this->matchesRegularExpression(self::REGEX_AVATAR_FILE), $this->isType('resource'));
163161

164162
$this->migrator->export($user, $this->exportDestination, $this->output);
165163
}

lib/public/UserMigration/IExportDestination.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,44 +38,47 @@ interface IExportDestination {
3838
*
3939
* @param string $path Full path to the file in the export archive. Parent directories will be created if needed.
4040
* @param string $content The full content of the file.
41-
* @return bool whether the file contents were successfully added.
41+
* @throws UserMigrationException
4242
*
4343
* @since 24.0.0
4444
*/
45-
public function addFileContents(string $path, string $content): bool;
45+
public function addFileContents(string $path, string $content): void;
4646

4747
/**
4848
* Adds a file to the export as a stream
4949
*
5050
* @param string $path Full path to the file in the export archive. Parent directories will be created if needed.
5151
* @param resource $stream A stream resource to read from to get the file content.
52-
* @return bool whether the file stream was successfully added.
52+
* @throws UserMigrationException
5353
*
5454
* @since 24.0.0
5555
*/
56-
public function addFileAsStream(string $path, $stream): bool;
56+
public function addFileAsStream(string $path, $stream): void;
5757

5858
/**
5959
* Copy a folder to the export
6060
*
6161
* @param Folder $folder folder to copy to the export archive.
6262
* @param string $destinationPath Full path to the folder in the export archive. Parent directories will be created if needed.
63-
* @return bool whether the folder was successfully added.
63+
* @throws UserMigrationException
6464
*
6565
* @since 24.0.0
6666
*/
67-
public function copyFolder(Folder $folder, string $destinationPath): bool;
67+
public function copyFolder(Folder $folder, string $destinationPath): void;
6868

6969
/**
7070
* @param array<string,int> $versions Migrators and their versions.
71+
* @throws UserMigrationException
7172
*
7273
* @since 24.0.0
7374
*/
74-
public function setMigratorVersions(array $versions): bool;
75+
public function setMigratorVersions(array $versions): void;
7576

7677
/**
7778
* Called after export is complete
7879
*
80+
* @throws UserMigrationException
81+
*
7982
* @since 24.0.0
8083
*/
8184
public function close(): void;

lib/public/UserMigration/IImportSource.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ interface IImportSource {
3939
*
4040
* @param string $path Full path to the file in the export archive.
4141
* @return string The full content of the file.
42+
* @throws UserMigrationException
4243
*
4344
* @since 24.0.0
4445
*/
@@ -49,6 +50,7 @@ public function getFileContents(string $path): string;
4950
*
5051
* @param string $path Full path to the file in the export archive.
5152
* @return resource A stream resource to read from to get the file content.
53+
* @throws UserMigrationException
5254
*
5355
* @since 24.0.0
5456
*/
@@ -59,6 +61,7 @@ public function getFileAsStream(string $path);
5961
*
6062
* @param string $path Full path to the folder in the export archive.
6163
* @return array The list of files.
64+
* @throws UserMigrationException
6265
*
6366
* @since 24.0.0
6467
*/
@@ -67,6 +70,8 @@ public function getFolderListing(string $path): array;
6770
/**
6871
* Test if a path exists, which may be a file or a folder
6972
*
73+
* @throws UserMigrationException
74+
*
7075
* @since 24.0.0
7176
*/
7277
public function pathExists(string $path): bool;
@@ -77,20 +82,23 @@ public function pathExists(string $path): bool;
7782
* Folder $destination folder to copy into
7883
* string $sourcePath path in the export archive
7984
*
85+
* @throws UserMigrationException
86+
*
8087
* @since 24.0.0
8188
*/
82-
public function copyToFolder(Folder $destination, string $sourcePath): bool;
89+
public function copyToFolder(Folder $destination, string $sourcePath): void;
8390

8491
/**
8592
* @return array<string,int> Migrators and their versions from the export archive.
93+
* @throws UserMigrationException
8694
*
8795
* @since 24.0.0
8896
*/
8997
public function getMigratorVersions(): array;
9098

9199
/**
92100
* @return ?int Version for this migrator from the export archive. Null means migrator missing.
93-
*
101+
* @throws UserMigrationException
94102
* @param string $migrator Migrator id (as returned by IMigrator::getId)
95103
*
96104
* @since 24.0.0
@@ -100,13 +108,17 @@ public function getMigratorVersion(string $migrator): ?int;
100108
/**
101109
* Get original uid of the imported account
102110
*
111+
* @throws UserMigrationException
112+
*
103113
* @since 24.0.0
104114
*/
105115
public function getOriginalUid(): string;
106116

107117
/**
108118
* Called after import is complete
109119
*
120+
* @throws UserMigrationException
121+
*
110122
* @since 24.0.0
111123
*/
112124
public function close(): void;

0 commit comments

Comments
 (0)