2013-11-26 14:12:48 +01:00
|
|
|
<?php
|
2024-05-24 19:43:47 +02:00
|
|
|
|
2013-11-26 14:12:48 +01:00
|
|
|
/**
|
2024-05-24 19:43:47 +02:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2013-11-26 14:12:48 +01:00
|
|
|
*/
|
|
|
|
namespace OC\Core\Command\Maintenance;
|
|
|
|
|
2016-04-19 15:36:11 +02:00
|
|
|
use Exception;
|
2022-08-22 17:59:26 +02:00
|
|
|
use OC\Repair\Events\RepairAdvanceEvent;
|
|
|
|
use OC\Repair\Events\RepairErrorEvent;
|
|
|
|
use OC\Repair\Events\RepairFinishEvent;
|
|
|
|
use OC\Repair\Events\RepairInfoEvent;
|
|
|
|
use OC\Repair\Events\RepairStartEvent;
|
|
|
|
use OC\Repair\Events\RepairStepEvent;
|
|
|
|
use OC\Repair\Events\RepairWarningEvent;
|
2017-10-24 14:13:45 +02:00
|
|
|
use OCP\App\IAppManager;
|
2022-08-22 17:59:26 +02:00
|
|
|
use OCP\EventDispatcher\Event;
|
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2016-04-26 16:24:50 +02:00
|
|
|
use OCP\IConfig;
|
2013-11-26 14:12:48 +01:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
2016-04-26 16:24:50 +02:00
|
|
|
use Symfony\Component\Console\Helper\ProgressBar;
|
2013-11-26 14:12:48 +01:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2015-10-19 16:41:43 +02:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2013-11-26 14:12:48 +01:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
class Repair extends Command {
|
2022-04-12 17:55:01 +02:00
|
|
|
private ProgressBar $progress;
|
|
|
|
private OutputInterface $output;
|
2023-02-20 14:33:06 +01:00
|
|
|
protected bool $errored = false;
|
2013-11-26 14:12:48 +01:00
|
|
|
|
2023-06-12 18:16:44 +03:30
|
|
|
public function __construct(
|
|
|
|
protected \OC\Repair $repair,
|
|
|
|
protected IConfig $config,
|
|
|
|
private IEventDispatcher $dispatcher,
|
|
|
|
private IAppManager $appManager,
|
|
|
|
) {
|
2013-11-26 14:12:48 +01:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function configure() {
|
|
|
|
$this
|
|
|
|
->setName('maintenance:repair')
|
2015-10-19 16:41:43 +02:00
|
|
|
->setDescription('repair this installation')
|
|
|
|
->addOption(
|
|
|
|
'include-expensive',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
2016-04-19 15:36:11 +02:00
|
|
|
'Use this option when you want to include resource and load expensive tasks');
|
2013-11-26 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
2020-06-26 14:54:51 +02:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
2019-02-02 18:07:48 +01:00
|
|
|
$repairSteps = $this->repair::getRepairSteps();
|
|
|
|
|
|
|
|
if ($input->getOption('include-expensive')) {
|
|
|
|
$repairSteps = array_merge($repairSteps, $this->repair::getExpensiveRepairSteps());
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($repairSteps as $step) {
|
|
|
|
$this->repair->addStep($step);
|
2015-10-19 16:41:43 +02:00
|
|
|
}
|
|
|
|
|
2025-02-10 11:35:38 +01:00
|
|
|
$apps = $this->appManager->getEnabledApps();
|
2016-04-19 15:36:11 +02:00
|
|
|
foreach ($apps as $app) {
|
2017-12-18 20:26:44 +01:00
|
|
|
if (!$this->appManager->isEnabledForUser($app)) {
|
2016-04-19 15:36:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
2022-05-12 17:08:54 +02:00
|
|
|
$info = $this->appManager->getAppInfo($app);
|
2016-04-19 15:36:11 +02:00
|
|
|
if (!is_array($info)) {
|
|
|
|
continue;
|
|
|
|
}
|
2025-02-24 12:44:50 +01:00
|
|
|
$this->appManager->loadApp($app);
|
2016-04-19 15:36:11 +02:00
|
|
|
$steps = $info['repair-steps']['post-migration'];
|
|
|
|
foreach ($steps as $step) {
|
|
|
|
try {
|
|
|
|
$this->repair->addStep($step);
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$output->writeln("<error>Failed to load repair step for $app: {$ex->getMessage()}</error>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 14:33:06 +01:00
|
|
|
|
|
|
|
|
2019-02-06 17:08:41 +01:00
|
|
|
$maintenanceMode = $this->config->getSystemValueBool('maintenance');
|
2014-11-19 00:25:26 +01:00
|
|
|
$this->config->setSystemValue('maintenance', true);
|
2014-03-25 12:51:16 +01:00
|
|
|
|
2016-04-26 16:24:50 +02:00
|
|
|
$this->progress = new ProgressBar($output);
|
|
|
|
$this->output = $output;
|
2022-08-22 17:59:26 +02:00
|
|
|
$this->dispatcher->addListener(RepairStartEvent::class, [$this, 'handleRepairFeedBack']);
|
|
|
|
$this->dispatcher->addListener(RepairAdvanceEvent::class, [$this, 'handleRepairFeedBack']);
|
|
|
|
$this->dispatcher->addListener(RepairFinishEvent::class, [$this, 'handleRepairFeedBack']);
|
|
|
|
$this->dispatcher->addListener(RepairStepEvent::class, [$this, 'handleRepairFeedBack']);
|
|
|
|
$this->dispatcher->addListener(RepairInfoEvent::class, [$this, 'handleRepairFeedBack']);
|
|
|
|
$this->dispatcher->addListener(RepairWarningEvent::class, [$this, 'handleRepairFeedBack']);
|
|
|
|
$this->dispatcher->addListener(RepairErrorEvent::class, [$this, 'handleRepairFeedBack']);
|
2014-03-25 12:51:16 +01:00
|
|
|
|
2013-11-26 14:12:48 +01:00
|
|
|
$this->repair->run();
|
2014-03-25 12:51:16 +01:00
|
|
|
|
2014-11-19 00:25:26 +01:00
|
|
|
$this->config->setSystemValue('maintenance', $maintenanceMode);
|
2023-02-20 14:33:06 +01:00
|
|
|
return $this->errored ? 1 : 0;
|
2013-11-26 14:12:48 +01:00
|
|
|
}
|
2016-04-26 16:24:50 +02:00
|
|
|
|
2022-08-22 17:59:26 +02:00
|
|
|
public function handleRepairFeedBack(Event $event): void {
|
|
|
|
if ($event instanceof RepairStartEvent) {
|
|
|
|
$this->progress->start($event->getMaxStep());
|
|
|
|
} elseif ($event instanceof RepairAdvanceEvent) {
|
2022-08-25 16:26:31 +02:00
|
|
|
$this->progress->advance($event->getIncrement());
|
2022-08-22 17:59:26 +02:00
|
|
|
} elseif ($event instanceof RepairFinishEvent) {
|
|
|
|
$this->progress->finish();
|
|
|
|
$this->output->writeln('');
|
|
|
|
} elseif ($event instanceof RepairStepEvent) {
|
2022-08-25 16:18:53 +02:00
|
|
|
$this->output->writeln('<info> - ' . $event->getStepName() . '</info>');
|
2022-08-22 17:59:26 +02:00
|
|
|
} elseif ($event instanceof RepairInfoEvent) {
|
2022-08-25 16:18:53 +02:00
|
|
|
$this->output->writeln('<info> - ' . $event->getMessage() . '</info>');
|
2022-08-22 17:59:26 +02:00
|
|
|
} elseif ($event instanceof RepairWarningEvent) {
|
2023-01-20 09:58:22 +01:00
|
|
|
$this->output->writeln('<comment> - WARNING: ' . $event->getMessage() . '</comment>');
|
2022-08-22 17:59:26 +02:00
|
|
|
} elseif ($event instanceof RepairErrorEvent) {
|
|
|
|
$this->output->writeln('<error> - ERROR: ' . $event->getMessage() . '</error>');
|
2023-02-20 14:33:06 +01:00
|
|
|
$this->errored = true;
|
2016-04-26 16:24:50 +02:00
|
|
|
}
|
|
|
|
}
|
2013-11-26 14:12:48 +01:00
|
|
|
}
|