Repair.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Temtaime <temtaime@gmail.com>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Core\Command\Maintenance;
  30. use Exception;
  31. use OCP\App\IAppManager;
  32. use OCP\EventDispatcher\Event;
  33. use OCP\EventDispatcher\IEventDispatcher;
  34. use OCP\IConfig;
  35. use OC\Repair\Events\RepairAdvanceEvent;
  36. use OC\Repair\Events\RepairErrorEvent;
  37. use OC\Repair\Events\RepairFinishEvent;
  38. use OC\Repair\Events\RepairInfoEvent;
  39. use OC\Repair\Events\RepairStartEvent;
  40. use OC\Repair\Events\RepairStepEvent;
  41. use OC\Repair\Events\RepairWarningEvent;
  42. use Symfony\Component\Console\Command\Command;
  43. use Symfony\Component\Console\Helper\ProgressBar;
  44. use Symfony\Component\Console\Input\InputInterface;
  45. use Symfony\Component\Console\Input\InputOption;
  46. use Symfony\Component\Console\Output\OutputInterface;
  47. class Repair extends Command {
  48. protected \OC\Repair $repair;
  49. protected IConfig $config;
  50. private IEventDispatcher $dispatcher;
  51. private ProgressBar $progress;
  52. private OutputInterface $output;
  53. private IAppManager $appManager;
  54. public function __construct(\OC\Repair $repair, IConfig $config, IEventDispatcher $dispatcher, IAppManager $appManager) {
  55. $this->repair = $repair;
  56. $this->config = $config;
  57. $this->dispatcher = $dispatcher;
  58. $this->appManager = $appManager;
  59. parent::__construct();
  60. }
  61. protected function configure() {
  62. $this
  63. ->setName('maintenance:repair')
  64. ->setDescription('repair this installation')
  65. ->addOption(
  66. 'include-expensive',
  67. null,
  68. InputOption::VALUE_NONE,
  69. 'Use this option when you want to include resource and load expensive tasks');
  70. }
  71. protected function execute(InputInterface $input, OutputInterface $output): int {
  72. $repairSteps = $this->repair::getRepairSteps();
  73. if ($input->getOption('include-expensive')) {
  74. $repairSteps = array_merge($repairSteps, $this->repair::getExpensiveRepairSteps());
  75. }
  76. foreach ($repairSteps as $step) {
  77. $this->repair->addStep($step);
  78. }
  79. $apps = $this->appManager->getInstalledApps();
  80. foreach ($apps as $app) {
  81. if (!$this->appManager->isEnabledForUser($app)) {
  82. continue;
  83. }
  84. $info = $this->appManager->getAppInfo($app);
  85. if (!is_array($info)) {
  86. continue;
  87. }
  88. \OC_App::loadApp($app);
  89. $steps = $info['repair-steps']['post-migration'];
  90. foreach ($steps as $step) {
  91. try {
  92. $this->repair->addStep($step);
  93. } catch (Exception $ex) {
  94. $output->writeln("<error>Failed to load repair step for $app: {$ex->getMessage()}</error>");
  95. }
  96. }
  97. }
  98. $maintenanceMode = $this->config->getSystemValueBool('maintenance');
  99. $this->config->setSystemValue('maintenance', true);
  100. $this->progress = new ProgressBar($output);
  101. $this->output = $output;
  102. $this->dispatcher->addListener(RepairStartEvent::class, [$this, 'handleRepairFeedBack']);
  103. $this->dispatcher->addListener(RepairAdvanceEvent::class, [$this, 'handleRepairFeedBack']);
  104. $this->dispatcher->addListener(RepairFinishEvent::class, [$this, 'handleRepairFeedBack']);
  105. $this->dispatcher->addListener(RepairStepEvent::class, [$this, 'handleRepairFeedBack']);
  106. $this->dispatcher->addListener(RepairInfoEvent::class, [$this, 'handleRepairFeedBack']);
  107. $this->dispatcher->addListener(RepairWarningEvent::class, [$this, 'handleRepairFeedBack']);
  108. $this->dispatcher->addListener(RepairErrorEvent::class, [$this, 'handleRepairFeedBack']);
  109. $this->repair->run();
  110. $this->config->setSystemValue('maintenance', $maintenanceMode);
  111. return 0;
  112. }
  113. public function handleRepairFeedBack(Event $event): void {
  114. if ($event instanceof RepairStartEvent) {
  115. $this->progress->start($event->getMaxStep());
  116. } elseif ($event instanceof RepairAdvanceEvent) {
  117. $this->progress->advance($event->getIncrement());
  118. } elseif ($event instanceof RepairFinishEvent) {
  119. $this->progress->finish();
  120. $this->output->writeln('');
  121. } elseif ($event instanceof RepairStepEvent) {
  122. $this->output->writeln('<info> - ' . $event->getStepName() . '</info>');
  123. } elseif ($event instanceof RepairInfoEvent) {
  124. $this->output->writeln('<info> - ' . $event->getMessage() . '</info>');
  125. } elseif ($event instanceof RepairWarningEvent) {
  126. $this->output->writeln('<comment> - WARNING: ' . $event->getMessage()) . '</comment>';
  127. } elseif ($event instanceof RepairErrorEvent) {
  128. $this->output->writeln('<error> - ERROR: ' . $event->getMessage() . '</error>');
  129. }
  130. }
  131. }