Repair.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\IConfig;
  33. use Symfony\Component\Console\Command\Command;
  34. use Symfony\Component\Console\Helper\ProgressBar;
  35. use Symfony\Component\Console\Input\InputInterface;
  36. use Symfony\Component\Console\Input\InputOption;
  37. use Symfony\Component\Console\Output\OutputInterface;
  38. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  39. use Symfony\Component\EventDispatcher\GenericEvent;
  40. class Repair extends Command {
  41. /** @var \OC\Repair $repair */
  42. protected $repair;
  43. /** @var IConfig */
  44. protected $config;
  45. /** @var EventDispatcherInterface */
  46. private $dispatcher;
  47. /** @var ProgressBar */
  48. private $progress;
  49. /** @var OutputInterface */
  50. private $output;
  51. /** @var IAppManager */
  52. private $appManager;
  53. /**
  54. * @param \OC\Repair $repair
  55. * @param IConfig $config
  56. * @param EventDispatcherInterface $dispatcher
  57. * @param IAppManager $appManager
  58. */
  59. public function __construct(\OC\Repair $repair, IConfig $config, EventDispatcherInterface $dispatcher, IAppManager $appManager) {
  60. $this->repair = $repair;
  61. $this->config = $config;
  62. $this->dispatcher = $dispatcher;
  63. $this->appManager = $appManager;
  64. parent::__construct();
  65. }
  66. protected function configure() {
  67. $this
  68. ->setName('maintenance:repair')
  69. ->setDescription('repair this installation')
  70. ->addOption(
  71. 'include-expensive',
  72. null,
  73. InputOption::VALUE_NONE,
  74. 'Use this option when you want to include resource and load expensive tasks');
  75. }
  76. protected function execute(InputInterface $input, OutputInterface $output): int {
  77. $repairSteps = $this->repair::getRepairSteps();
  78. if ($input->getOption('include-expensive')) {
  79. $repairSteps = array_merge($repairSteps, $this->repair::getExpensiveRepairSteps());
  80. }
  81. foreach ($repairSteps as $step) {
  82. $this->repair->addStep($step);
  83. }
  84. $apps = $this->appManager->getInstalledApps();
  85. foreach ($apps as $app) {
  86. if (!$this->appManager->isEnabledForUser($app)) {
  87. continue;
  88. }
  89. $info = \OC_App::getAppInfo($app);
  90. if (!is_array($info)) {
  91. continue;
  92. }
  93. \OC_App::loadApp($app);
  94. $steps = $info['repair-steps']['post-migration'];
  95. foreach ($steps as $step) {
  96. try {
  97. $this->repair->addStep($step);
  98. } catch (Exception $ex) {
  99. $output->writeln("<error>Failed to load repair step for $app: {$ex->getMessage()}</error>");
  100. }
  101. }
  102. }
  103. $maintenanceMode = $this->config->getSystemValueBool('maintenance');
  104. $this->config->setSystemValue('maintenance', true);
  105. $this->progress = new ProgressBar($output);
  106. $this->output = $output;
  107. $this->dispatcher->addListener('\OC\Repair::startProgress', [$this, 'handleRepairFeedBack']);
  108. $this->dispatcher->addListener('\OC\Repair::advance', [$this, 'handleRepairFeedBack']);
  109. $this->dispatcher->addListener('\OC\Repair::finishProgress', [$this, 'handleRepairFeedBack']);
  110. $this->dispatcher->addListener('\OC\Repair::step', [$this, 'handleRepairFeedBack']);
  111. $this->dispatcher->addListener('\OC\Repair::info', [$this, 'handleRepairFeedBack']);
  112. $this->dispatcher->addListener('\OC\Repair::warning', [$this, 'handleRepairFeedBack']);
  113. $this->dispatcher->addListener('\OC\Repair::error', [$this, 'handleRepairFeedBack']);
  114. $this->repair->run();
  115. $this->config->setSystemValue('maintenance', $maintenanceMode);
  116. return 0;
  117. }
  118. public function handleRepairFeedBack($event) {
  119. if (!$event instanceof GenericEvent) {
  120. return;
  121. }
  122. switch ($event->getSubject()) {
  123. case '\OC\Repair::startProgress':
  124. $this->progress->start($event->getArgument(0));
  125. break;
  126. case '\OC\Repair::advance':
  127. $this->progress->advance($event->getArgument(0));
  128. break;
  129. case '\OC\Repair::finishProgress':
  130. $this->progress->finish();
  131. $this->output->writeln('');
  132. break;
  133. case '\OC\Repair::step':
  134. $this->output->writeln(' - ' . $event->getArgument(0));
  135. break;
  136. case '\OC\Repair::info':
  137. $this->output->writeln(' - ' . $event->getArgument(0));
  138. break;
  139. case '\OC\Repair::warning':
  140. $this->output->writeln(' - WARNING: ' . $event->getArgument(0));
  141. break;
  142. case '\OC\Repair::error':
  143. $this->output->writeln(' - ERROR: ' . $event->getArgument(0));
  144. break;
  145. }
  146. }
  147. }