Repair.php 4.8 KB

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