Repair.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. $includeExpensive = $input->getOption('include-expensive');
  75. if ($includeExpensive) {
  76. foreach ($this->repair->getExpensiveRepairSteps() as $step) {
  77. $this->repair->addStep($step);
  78. }
  79. }
  80. $apps = $this->appManager->getInstalledApps();
  81. foreach ($apps as $app) {
  82. if (!$this->appManager->isEnabledForUser($app)) {
  83. continue;
  84. }
  85. $info = \OC_App::getAppInfo($app);
  86. if (!is_array($info)) {
  87. continue;
  88. }
  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->getSystemValue('maintenance', false);
  99. $this->config->setSystemValue('maintenance', true);
  100. $this->progress = new ProgressBar($output);
  101. $this->output = $output;
  102. $this->dispatcher->addListener('\OC\Repair::startProgress', [$this, 'handleRepairFeedBack']);
  103. $this->dispatcher->addListener('\OC\Repair::advance', [$this, 'handleRepairFeedBack']);
  104. $this->dispatcher->addListener('\OC\Repair::finishProgress', [$this, 'handleRepairFeedBack']);
  105. $this->dispatcher->addListener('\OC\Repair::step', [$this, 'handleRepairFeedBack']);
  106. $this->dispatcher->addListener('\OC\Repair::info', [$this, 'handleRepairFeedBack']);
  107. $this->dispatcher->addListener('\OC\Repair::warning', [$this, 'handleRepairFeedBack']);
  108. $this->dispatcher->addListener('\OC\Repair::error', [$this, 'handleRepairFeedBack']);
  109. $this->repair->run();
  110. $this->config->setSystemValue('maintenance', $maintenanceMode);
  111. }
  112. public function handleRepairFeedBack($event) {
  113. if (!$event instanceof GenericEvent) {
  114. return;
  115. }
  116. switch ($event->getSubject()) {
  117. case '\OC\Repair::startProgress':
  118. $this->progress->start($event->getArgument(0));
  119. break;
  120. case '\OC\Repair::advance':
  121. $this->progress->advance($event->getArgument(0));
  122. break;
  123. case '\OC\Repair::finishProgress':
  124. $this->progress->finish();
  125. $this->output->writeln('');
  126. break;
  127. case '\OC\Repair::step':
  128. $this->output->writeln(' - ' . $event->getArgument(0));
  129. break;
  130. case '\OC\Repair::info':
  131. $this->output->writeln(' - ' . $event->getArgument(0));
  132. break;
  133. case '\OC\Repair::warning':
  134. $this->output->writeln(' - WARNING: ' . $event->getArgument(0));
  135. break;
  136. case '\OC\Repair::error':
  137. $this->output->writeln(' - ERROR: ' . $event->getArgument(0));
  138. break;
  139. }
  140. }
  141. }