Repair.php 5.0 KB

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