migratecalendars.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace OCA\Dav\Command;
  3. use OCP\IUser;
  4. use OCP\IUserManager;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Helper\ProgressBar;
  7. use Symfony\Component\Console\Input\InputArgument;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. class MigrateCalendars extends Command {
  11. /** @var IUserManager */
  12. protected $userManager;
  13. /** @var \OCA\Dav\Migration\MigrateCalendars */
  14. private $service;
  15. /**
  16. * @param IUserManager $userManager
  17. * @param \OCA\Dav\Migration\MigrateCalendars $service
  18. */
  19. function __construct(IUserManager $userManager,
  20. \OCA\Dav\Migration\MigrateCalendars $service
  21. ) {
  22. parent::__construct();
  23. $this->userManager = $userManager;
  24. $this->service = $service;
  25. }
  26. protected function configure() {
  27. $this
  28. ->setName('dav:migrate-calendars')
  29. ->setDescription('Migrate calendars from the calendar app to core')
  30. ->addArgument('user',
  31. InputArgument::OPTIONAL,
  32. 'User for whom all calendars will be migrated');
  33. }
  34. protected function execute(InputInterface $input, OutputInterface $output) {
  35. $this->service->setup();
  36. if ($input->hasArgument('user')) {
  37. $user = $input->getArgument('user');
  38. if (!$this->userManager->userExists($user)) {
  39. throw new \InvalidArgumentException("User <$user> in unknown.");
  40. }
  41. $output->writeln("Start migration for $user");
  42. $this->service->migrateForUser($user);
  43. return;
  44. }
  45. $output->writeln("Start migration of all known users ...");
  46. $p = new ProgressBar($output);
  47. $p->start();
  48. $this->userManager->callForAllUsers(function($user) use ($p) {
  49. $p->advance();
  50. /** @var IUser $user */
  51. $this->service->migrateForUser($user->getUID());
  52. });
  53. $p->finish();
  54. $output->writeln('');
  55. }
  56. }