============== User migration ============== The `User migration app `_ may be installed to allow migration of user data. App developers can integrate into User migration and provide ways to export and import the app data of a user. Register a migrator ------------------- A migrator is represented by a class implementing the ``OCP\\UserMigration\\IMigrator`` interface. This class is instantiated whenever a user export or import begins. .. code-block:: php myAppManager = $myAppManager; $this->l10n = $l10n; } /** * Returns an estimate of the exported data size in KiB. * Should be fast, favor performance over accuracy. * * @since 25.0.0 * @since 27.0.0 return value may overflow from int to float */ public function getEstimatedExportSize(IUser $user): int|float { $size = 100; // 100KiB for user data JSON return $size; } /** * Export user data * * @throws UserMigrationException * @since 24.0.0 */ public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void { $output->writeln('Exporting myapp information in ' . MyAppMigrator::PATH_MYAPP_FILE . '…'); try { $data = $this->myAppManager->getUserData($user); $exportDestination->addFileContents(MyAppMigrator::PATH_MYAPP_FILE, json_encode($data)); } catch (Throwable $e) { throw new UserMigrationException('Could not export myapp information', 0, $e); } } /** * Import user data * * @throws UserMigrationException * @since 24.0.0 */ public function import(IUser $user, IImportSource $importSource, OutputInterface $output): void { if ($importSource->getMigratorVersion($this->getId()) === null) { $output->writeln('No version for ' . static::class . ', skipping import…'); return; } $output->writeln('Importing myapp information from ' . MyAppMigrator::PATH_MYAPP_FILE . '…'); $data = json_decode($importSource->getFileContents(MyAppMigrator::PATH_MYAPP_FILE), true, 512, JSON_THROW_ON_ERROR); try { $this->myAppManager->setUserData($user, $data); } catch (Throwable $e) { throw new UserMigrationException('Could not import myapp information', 0, $e); } } /** * Returns the unique ID * * @since 24.0.0 */ public function getId(): string { return 'myapp'; } /** * Returns the display name * * @since 24.0.0 */ public function getDisplayName(): string { return $this->l10n->t('My App'); } /** * Returns the description * * @since 24.0.0 */ public function getDescription(): string { return $this->l10n->t('My App information'); } } The ``MyAppMigrator`` class needs to be registered during the :ref:`app bootstrap`. .. code-block:: php registerUserMigrator(MyAppMigrator::class); } public function boot(IBootContext $context): void { } }