From 4ba30d40cfc892bee0ea28486e63c479e1aadd20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 22 Aug 2022 15:29:54 +0200 Subject: [PATCH 01/12] Switch to string keys for argument of GenericEvent for OC\DB\Migrator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It seems checkTable is actually never dispatched? Signed-off-by: Côme Chilliet --- core/Command/Upgrade.php | 8 ++++---- core/ajax/update.php | 4 ++-- lib/private/DB/Migrator.php | 31 ++++++++++--------------------- lib/private/Updater.php | 4 ++-- 4 files changed, 18 insertions(+), 29 deletions(-) diff --git a/core/Command/Upgrade.php b/core/Command/Upgrade.php index acf0b503d1973..69a7f1f656ff2 100644 --- a/core/Command/Upgrade.php +++ b/core/Command/Upgrade.php @@ -105,12 +105,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int $message = substr($message, 0, 57) . '...'; } $progress->setMessage($message); - if ($event[0] === 1) { + if ($event['step'] === 1) { $output->writeln(''); - $progress->start($event[1]); + $progress->start($event['max']); } - $progress->setProgress($event[0]); - if ($event[0] === $event[1]) { + $progress->setProgress($event['step']); + if ($event['step'] === $event['max']) { $progress->setMessage('Done'); $progress->finish(); $output->writeln(''); diff --git a/core/ajax/update.php b/core/ajax/update.php index 39a99323cf53e..a8352cb40be2e 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -128,12 +128,12 @@ public function handleRepairFeedback($event) { $dispatcher = \OC::$server->getEventDispatcher(); $dispatcher->addListener('\OC\DB\Migrator::executeSql', function ($event) use ($eventSource, $l) { if ($event instanceof GenericEvent) { - $eventSource->send('success', $l->t('[%d / %d]: %s', [$event[0], $event[1], $event->getSubject()])); + $eventSource->send('success', $l->t('[%d / %d]: %s', [$event['step'], $event['max'], $event->getSubject()])); } }); $dispatcher->addListener('\OC\DB\Migrator::checkTable', function ($event) use ($eventSource, $l) { if ($event instanceof GenericEvent) { - $eventSource->send('success', $l->t('[%d / %d]: Checking table %s', [$event[0], $event[1], $event->getSubject()])); + $eventSource->send('success', $l->t('[%d / %d]: Checking table %s', [$event['step'], $event['max'], $event->getSubject()])); } }); $feedBack = new FeedBackHandler($eventSource, $l); diff --git a/lib/private/DB/Migrator.php b/lib/private/DB/Migrator.php index 9ca37d7180a5e..d2fdefefb39cc 100644 --- a/lib/private/DB/Migrator.php +++ b/lib/private/DB/Migrator.php @@ -27,11 +27,13 @@ */ namespace OC\DB; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\Exception; use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Schema\AbstractAsset; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Schema; +use Doctrine\DBAL\Schema\SchemaDiff; use Doctrine\DBAL\Types\StringType; use Doctrine\DBAL\Types\Type; use OCP\IConfig; @@ -41,34 +43,27 @@ class Migrator { - /** @var \Doctrine\DBAL\Connection */ + /** @var Connection */ protected $connection; /** @var IConfig */ protected $config; - /** @var EventDispatcherInterface */ + /** @var ?EventDispatcherInterface */ private $dispatcher; /** @var bool */ private $noEmit = false; - /** - * @param \Doctrine\DBAL\Connection $connection - * @param IConfig $config - * @param EventDispatcherInterface $dispatcher - */ - public function __construct(\Doctrine\DBAL\Connection $connection, + public function __construct(Connection $connection, IConfig $config, - EventDispatcherInterface $dispatcher = null) { + ?EventDispatcherInterface $dispatcher = null) { $this->connection = $connection; $this->config = $config; $this->dispatcher = $dispatcher; } /** - * @param \Doctrine\DBAL\Schema\Schema $targetSchema - * * @throws Exception */ public function migrate(Schema $targetSchema) { @@ -77,7 +72,6 @@ public function migrate(Schema $targetSchema) { } /** - * @param \Doctrine\DBAL\Schema\Schema $targetSchema * @return string */ public function generateChangeScript(Schema $targetSchema) { @@ -108,11 +102,9 @@ public function createSchema() { } /** - * @param Schema $targetSchema - * @param \Doctrine\DBAL\Connection $connection - * @return \Doctrine\DBAL\Schema\SchemaDiff + * @return SchemaDiff */ - protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { + protected function getDiff(Schema $targetSchema, Connection $connection) { // adjust varchar columns with a length higher then getVarcharMaxLength to clob foreach ($targetSchema->getTables() as $table) { foreach ($table->getColumns() as $column) { @@ -153,12 +145,9 @@ protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $conn } /** - * @param \Doctrine\DBAL\Schema\Schema $targetSchema - * @param \Doctrine\DBAL\Connection $connection - * * @throws Exception */ - protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $connection = null) { + protected function applySchema(Schema $targetSchema, Connection $connection = null) { if (is_null($connection)) { $connection = $this->connection; } @@ -201,6 +190,6 @@ protected function emit($sql, $step, $max) { if (is_null($this->dispatcher)) { return; } - $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step + 1, $max])); + $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, ['step' => $step + 1, 'max' => $max])); } } diff --git a/lib/private/Updater.php b/lib/private/Updater.php index da989c4db91c3..b25bb76e957a6 100644 --- a/lib/private/Updater.php +++ b/lib/private/Updater.php @@ -467,13 +467,13 @@ private function logAllEvents(): void { if (!$event instanceof GenericEvent) { return; } - $log->info('\OC\DB\Migrator::executeSql: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']); + $log->info('\OC\DB\Migrator::executeSql: ' . $event->getSubject() . ' (' . $event->getArgument('step') . ' of ' . $event->getArgument('max') . ')', ['app' => 'updater']); }); $dispatcher->addListener('\OC\DB\Migrator::checkTable', function ($event) use ($log) { if (!$event instanceof GenericEvent) { return; } - $log->info('\OC\DB\Migrator::checkTable: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']); + $log->info('\OC\DB\Migrator::checkTable: ' . $event->getSubject() . ' (' . $event->getArgument('step') . ' of ' . $event->getArgument('max') . ')', ['app' => 'updater']); }); $repairListener = function ($event) use ($log) { From 4f260dce6e43ddef835105a1d4dc13f2b8742526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 22 Aug 2022 15:58:50 +0200 Subject: [PATCH 02/12] Moving to string key for arguments of GenericEvent in Repair MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- build/psalm-baseline.xml | 17 ------------- core/Command/Maintenance/Repair.php | 12 ++++----- core/Command/Upgrade.php | 16 ++++++------ core/ajax/update.php | 39 +++++++++++++---------------- lib/private/Repair.php | 24 ++++++++---------- lib/private/Updater.php | 16 ++++++------ lib/private/legacy/OC_App.php | 2 +- 7 files changed, 51 insertions(+), 75 deletions(-) diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index 39947ac03151b..d70c2822bfac6 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -3946,23 +3946,6 @@ $path - - - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 1 - 1 - 1 - 1 - - microtime(true) diff --git a/core/Command/Maintenance/Repair.php b/core/Command/Maintenance/Repair.php index 2c1fda7c8e4d9..6dac3085ca8bf 100644 --- a/core/Command/Maintenance/Repair.php +++ b/core/Command/Maintenance/Repair.php @@ -122,26 +122,26 @@ public function handleRepairFeedBack($event) { } switch ($event->getSubject()) { case '\OC\Repair::startProgress': - $this->progress->start($event->getArgument(0)); + $this->progress->start($event->getArgument('max')); break; case '\OC\Repair::advance': - $this->progress->advance($event->getArgument(0)); + $this->progress->advance($event->getArgument('step')); break; case '\OC\Repair::finishProgress': $this->progress->finish(); $this->output->writeln(''); break; case '\OC\Repair::step': - $this->output->writeln(' - ' . $event->getArgument(0)); + $this->output->writeln(' - ' . $event->getArgument('step')); break; case '\OC\Repair::info': - $this->output->writeln(' - ' . $event->getArgument(0)); + $this->output->writeln(' - ' . $event->getArgument('message')); break; case '\OC\Repair::warning': - $this->output->writeln(' - WARNING: ' . $event->getArgument(0)); + $this->output->writeln(' - WARNING: ' . $event->getArgument('message')); break; case '\OC\Repair::error': - $this->output->writeln(' - ERROR: ' . $event->getArgument(0) . ''); + $this->output->writeln(' - ERROR: ' . $event->getArgument('message') . ''); break; } } diff --git a/core/Command/Upgrade.php b/core/Command/Upgrade.php index 69a7f1f656ff2..f848ab21ea6a0 100644 --- a/core/Command/Upgrade.php +++ b/core/Command/Upgrade.php @@ -125,16 +125,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int switch ($event->getSubject()) { case '\OC\Repair::startProgress': $progress->setMessage('Starting ...'); - $output->writeln($event->getArgument(1)); + $output->writeln($event->getArgument('step')); $output->writeln(''); - $progress->start($event->getArgument(0)); + $progress->start($event->getArgument('max')); break; case '\OC\Repair::advance': - $desc = $event->getArgument(1); + $desc = $event->getArgument('desc'); if (!empty($desc)) { $progress->setMessage($desc); } - $progress->advance($event->getArgument(0)); + $progress->advance($event->getArgument('step')); break; case '\OC\Repair::finishProgress': @@ -144,19 +144,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int break; case '\OC\Repair::step': if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { - $output->writeln('Repair step: ' . $event->getArgument(0) . ''); + $output->writeln('Repair step: ' . $event->getArgument('step') . ''); } break; case '\OC\Repair::info': if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { - $output->writeln('Repair info: ' . $event->getArgument(0) . ''); + $output->writeln('Repair info: ' . $event->getArgument('message') . ''); } break; case '\OC\Repair::warning': - $output->writeln('Repair warning: ' . $event->getArgument(0) . ''); + $output->writeln('Repair warning: ' . $event->getArgument('message') . ''); break; case '\OC\Repair::error': - $output->writeln('Repair error: ' . $event->getArgument(0) . ''); + $output->writeln('Repair error: ' . $event->getArgument('message') . ''); break; } }; diff --git a/core/ajax/update.php b/core/ajax/update.php index a8352cb40be2e..8682f47353d74 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -30,6 +30,8 @@ * along with this program. If not, see * */ +use OCP\IEventSource; +use OCP\IL10N; use OCP\ILogger; use Symfony\Component\EventDispatcher\GenericEvent; @@ -48,36 +50,31 @@ $eventSource->send('success', $l->t('Preparing update')); class FeedBackHandler { - /** @var integer */ - private $progressStateMax = 100; - /** @var integer */ - private $progressStateStep = 0; - /** @var string */ - private $currentStep; - /** @var \OCP\IEventSource */ - private $eventSource; - /** @var \OCP\IL10N */ - private $l10n; - - public function __construct(\OCP\IEventSource $eventSource, \OCP\IL10N $l10n) { + private int $progressStateMax = 100; + private int $progressStateStep = 0; + private string $currentStep = ''; + private IEventSource $eventSource; + private IL10N $l10n; + + public function __construct(IEventSource $eventSource, IL10N $l10n) { $this->eventSource = $eventSource; $this->l10n = $l10n; } - public function handleRepairFeedback($event) { + public function handleRepairFeedback($event): void { if (!$event instanceof GenericEvent) { return; } switch ($event->getSubject()) { case '\OC\Repair::startProgress': - $this->progressStateMax = $event->getArgument(0); + $this->progressStateMax = $event->getArgument('max'); $this->progressStateStep = 0; - $this->currentStep = $event->getArgument(1); + $this->currentStep = (string)$event->getArgument('step'); break; case '\OC\Repair::advance': - $this->progressStateStep += $event->getArgument(0); - $desc = $event->getArgument(1); + $this->progressStateStep += $event->getArgument('step'); + $desc = $event->getArgument('desc'); if (empty($desc)) { $desc = $this->currentStep; } @@ -88,16 +85,16 @@ public function handleRepairFeedback($event) { $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $this->currentStep])); break; case '\OC\Repair::step': - $this->eventSource->send('success', $this->l10n->t('Repair step:') . ' ' . $event->getArgument(0)); + $this->eventSource->send('success', $this->l10n->t('Repair step:') . ' ' . $event->getArgument('step')); break; case '\OC\Repair::info': - $this->eventSource->send('success', $this->l10n->t('Repair info:') . ' ' . $event->getArgument(0)); + $this->eventSource->send('success', $this->l10n->t('Repair info:') . ' ' . $event->getArgument('message')); break; case '\OC\Repair::warning': - $this->eventSource->send('notice', $this->l10n->t('Repair warning:') . ' ' . $event->getArgument(0)); + $this->eventSource->send('notice', $this->l10n->t('Repair warning:') . ' ' . $event->getArgument('message')); break; case '\OC\Repair::error': - $this->eventSource->send('notice', $this->l10n->t('Repair error:') . ' ' . $event->getArgument(0)); + $this->eventSource->send('notice', $this->l10n->t('Repair error:') . ' ' . $event->getArgument('message')); break; } } diff --git a/lib/private/Repair.php b/lib/private/Repair.php index 98a1f4ce03611..496e9b15e2e7b 100644 --- a/lib/private/Repair.php +++ b/lib/private/Repair.php @@ -112,19 +112,19 @@ public function __construct(array $repairSteps, EventDispatcherInterface $dispat */ public function run() { if (count($this->repairSteps) === 0) { - $this->emit('\OC\Repair', 'info', ['No repair steps available']); + $this->emit('\OC\Repair', 'info', ['message' => 'No repair steps available']); return; } // run each repair step foreach ($this->repairSteps as $step) { $this->currentStep = $step->getName(); - $this->emit('\OC\Repair', 'step', [$this->currentStep]); + $this->emit('\OC\Repair', 'step', ['step' => $this->currentStep]); try { $step->run($this); } catch (\Exception $e) { $this->logger->error("Exception while executing repair step " . $step->getName(), ['exception' => $e]); - $this->emit('\OC\Repair', 'error', [$e->getMessage()]); + $this->emit('\OC\Repair', 'error', ['message' => $e->getMessage()]); } } } @@ -250,20 +250,16 @@ public static function getBeforeUpgradeRepairSteps() { } /** - * @param string $scope - * @param string $method - * @param array $arguments + * @param array $arguments */ - public function emit($scope, $method, array $arguments = []) { - if (!is_null($this->dispatcher)) { - $this->dispatcher->dispatch("$scope::$method", + public function emit(string $scope, string $method, array $arguments = []): void { + $this->dispatcher->dispatch("$scope::$method", new GenericEvent("$scope::$method", $arguments)); - } } public function info($string) { // for now just emit as we did in the past - $this->emit('\OC\Repair', 'info', [$string]); + $this->emit('\OC\Repair', 'info', ['message' => $string]); } /** @@ -271,7 +267,7 @@ public function info($string) { */ public function warning($message) { // for now just emit as we did in the past - $this->emit('\OC\Repair', 'warning', [$message]); + $this->emit('\OC\Repair', 'warning', ['message' => $message]); } /** @@ -279,7 +275,7 @@ public function warning($message) { */ public function startProgress($max = 0) { // for now just emit as we did in the past - $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]); + $this->emit('\OC\Repair', 'startProgress', ['max' => $max, 'step' => $this->currentStep]); } /** @@ -288,7 +284,7 @@ public function startProgress($max = 0) { */ public function advance($step = 1, $description = '') { // for now just emit as we did in the past - $this->emit('\OC\Repair', 'advance', [$step, $description]); + $this->emit('\OC\Repair', 'advance', ['step' => $step, 'desc' => $description]); } /** diff --git a/lib/private/Updater.php b/lib/private/Updater.php index b25bb76e957a6..0889e87383ffa 100644 --- a/lib/private/Updater.php +++ b/lib/private/Updater.php @@ -293,7 +293,7 @@ private function doUpgrade(string $currentVersion, string $installedVersion): vo $repair->run(); //Invalidate update feed - $this->config->setAppValue('core', 'lastupdatedat', 0); + $this->config->setAppValue('core', 'lastupdatedat', '0'); // Check for code integrity if not disabled if (\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) { @@ -482,30 +482,30 @@ private function logAllEvents(): void { } switch ($event->getSubject()) { case '\OC\Repair::startProgress': - $log->info('\OC\Repair::startProgress: Starting ... ' . $event->getArgument(1) . ' (' . $event->getArgument(0) . ')', ['app' => 'updater']); + $log->info('\OC\Repair::startProgress: Starting ... ' . $event->getArgument('max') . ' (' . $event->getArgument('step') . ')', ['app' => 'updater']); break; case '\OC\Repair::advance': - $desc = $event->getArgument(1); + $desc = $event->getArgument('desc'); if (empty($desc)) { $desc = ''; } - $log->info('\OC\Repair::advance: ' . $desc . ' (' . $event->getArgument(0) . ')', ['app' => 'updater']); + $log->info('\OC\Repair::advance: ' . $desc . ' (' . $event->getArgument('step') . ')', ['app' => 'updater']); break; case '\OC\Repair::finishProgress': $log->info('\OC\Repair::finishProgress', ['app' => 'updater']); break; case '\OC\Repair::step': - $log->info('\OC\Repair::step: Repair step: ' . $event->getArgument(0), ['app' => 'updater']); + $log->info('\OC\Repair::step: Repair step: ' . $event->getArgument('step'), ['app' => 'updater']); break; case '\OC\Repair::info': - $log->info('\OC\Repair::info: Repair info: ' . $event->getArgument(0), ['app' => 'updater']); + $log->info('\OC\Repair::info: Repair info: ' . $event->getArgument('message'), ['app' => 'updater']); break; case '\OC\Repair::warning': - $log->warning('\OC\Repair::warning: Repair warning: ' . $event->getArgument(0), ['app' => 'updater']); + $log->warning('\OC\Repair::warning: Repair warning: ' . $event->getArgument('message'), ['app' => 'updater']); break; case '\OC\Repair::error': - $log->error('\OC\Repair::error: Repair error: ' . $event->getArgument(0), ['app' => 'updater']); + $log->error('\OC\Repair::error: Repair error: ' . $event->getArgument('message'), ['app' => 'updater']); break; } }; diff --git a/lib/private/legacy/OC_App.php b/lib/private/legacy/OC_App.php index 482fc4e88e7d6..cd53dc0afc441 100644 --- a/lib/private/legacy/OC_App.php +++ b/lib/private/legacy/OC_App.php @@ -1055,7 +1055,7 @@ public static function executeRepairSteps(string $appId, array $steps) { try { $r->addStep($step); } catch (Exception $ex) { - $r->emit('\OC\Repair', 'error', [$ex->getMessage()]); + $r->emit('\OC\Repair', 'error', ['message' => $ex->getMessage()]); \OC::$server->getLogger()->logException($ex); } } From 5aac997d448da55f1c43648b1e5cf1a9c1360d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 22 Aug 2022 16:24:35 +0200 Subject: [PATCH 03/12] Remove listeners of \OC\DB\Migrator::checkTable which is never emitted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See commit a2f3077ee802b4b291ba4f846bfceb69ec0f225f from PR #24384 Signed-off-by: Côme Chilliet --- core/Command/Upgrade.php | 1 - core/ajax/update.php | 5 ----- lib/private/Updater.php | 6 ------ 3 files changed, 12 deletions(-) diff --git a/core/Command/Upgrade.php b/core/Command/Upgrade.php index f848ab21ea6a0..e4b831835d466 100644 --- a/core/Command/Upgrade.php +++ b/core/Command/Upgrade.php @@ -162,7 +162,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int }; $dispatcher->addListener('\OC\DB\Migrator::executeSql', $listener); - $dispatcher->addListener('\OC\DB\Migrator::checkTable', $listener); $dispatcher->addListener('\OC\Repair::startProgress', $repairListener); $dispatcher->addListener('\OC\Repair::advance', $repairListener); $dispatcher->addListener('\OC\Repair::finishProgress', $repairListener); diff --git a/core/ajax/update.php b/core/ajax/update.php index 8682f47353d74..3d3af92cd93a8 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -128,11 +128,6 @@ public function handleRepairFeedback($event): void { $eventSource->send('success', $l->t('[%d / %d]: %s', [$event['step'], $event['max'], $event->getSubject()])); } }); - $dispatcher->addListener('\OC\DB\Migrator::checkTable', function ($event) use ($eventSource, $l) { - if ($event instanceof GenericEvent) { - $eventSource->send('success', $l->t('[%d / %d]: Checking table %s', [$event['step'], $event['max'], $event->getSubject()])); - } - }); $feedBack = new FeedBackHandler($eventSource, $l); $dispatcher->addListener('\OC\Repair::startProgress', [$feedBack, 'handleRepairFeedback']); $dispatcher->addListener('\OC\Repair::advance', [$feedBack, 'handleRepairFeedback']); diff --git a/lib/private/Updater.php b/lib/private/Updater.php index 0889e87383ffa..1b91441d676f1 100644 --- a/lib/private/Updater.php +++ b/lib/private/Updater.php @@ -469,12 +469,6 @@ private function logAllEvents(): void { } $log->info('\OC\DB\Migrator::executeSql: ' . $event->getSubject() . ' (' . $event->getArgument('step') . ' of ' . $event->getArgument('max') . ')', ['app' => 'updater']); }); - $dispatcher->addListener('\OC\DB\Migrator::checkTable', function ($event) use ($log) { - if (!$event instanceof GenericEvent) { - return; - } - $log->info('\OC\DB\Migrator::checkTable: ' . $event->getSubject() . ' (' . $event->getArgument('step') . ' of ' . $event->getArgument('max') . ')', ['app' => 'updater']); - }); $repairListener = function ($event) use ($log) { if (!$event instanceof GenericEvent) { From a83a8f0dde07bff67e0ceb7008cc26b3fad32516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 22 Aug 2022 16:56:01 +0200 Subject: [PATCH 04/12] Migrate Migrator::executeSql to OCP\EventDispatcher\Event MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- core/Command/Upgrade.php | 48 ++++++++++--------- core/ajax/update.php | 10 ++-- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/DB/Migrator.php | 12 ++--- lib/private/DB/MigratorExecuteSqlEvent.php | 52 +++++++++++++++++++++ lib/private/Updater.php | 12 +++-- tests/lib/DB/MigratorTest.php | 2 +- 8 files changed, 98 insertions(+), 40 deletions(-) create mode 100644 lib/private/DB/MigratorExecuteSqlEvent.php diff --git a/core/Command/Upgrade.php b/core/Command/Upgrade.php index e4b831835d466..3a45c53f6b274 100644 --- a/core/Command/Upgrade.php +++ b/core/Command/Upgrade.php @@ -33,11 +33,13 @@ */ namespace OC\Core\Command; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\IConfig; +use OCP\Util; use OC\Console\TimestampFormatter; +use OC\DB\MigratorExecuteSqlEvent; use OC\Installer; use OC\Updater; -use OCP\IConfig; -use OCP\Util; use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; @@ -93,28 +95,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int ); $dispatcher = \OC::$server->getEventDispatcher(); + /** @var IEventDispatcher $newDispatcher */ + $newDispatcher = \OC::$server->get(IEventDispatcher::class); $progress = new ProgressBar($output); $progress->setFormat(" %message%\n %current%/%max% [%bar%] %percent:3s%%"); - $listener = function ($event) use ($progress, $output) { - if ($event instanceof GenericEvent) { - $message = $event->getSubject(); - if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { - $output->writeln(' Checking table ' . $message); - } else { - if (strlen($message) > 60) { - $message = substr($message, 0, 57) . '...'; - } - $progress->setMessage($message); - if ($event['step'] === 1) { - $output->writeln(''); - $progress->start($event['max']); - } - $progress->setProgress($event['step']); - if ($event['step'] === $event['max']) { - $progress->setMessage('Done'); - $progress->finish(); - $output->writeln(''); - } + $listener = function (MigratorExecuteSqlEvent $event) use ($progress, $output) { + $message = $event->getSql(); + if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { + $output->writeln(' Executing SQL ' . $message); + } else { + if (strlen($message) > 60) { + $message = substr($message, 0, 57) . '...'; + } + $progress->setMessage($message); + if ($event->getCurrentStep() === 1) { + $output->writeln(''); + $progress->start($event->getMaxStep()); + } + $progress->setProgress($event->getCurrentStep()); + if ($event->getCurrentStep() === $event->getMaxStep()) { + $progress->setMessage('Done'); + $progress->finish(); + $output->writeln(''); } } }; @@ -161,7 +163,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } }; - $dispatcher->addListener('\OC\DB\Migrator::executeSql', $listener); + $newDispatcher->addListener(MigratorExecuteSqlEvent::class, $listener); $dispatcher->addListener('\OC\Repair::startProgress', $repairListener); $dispatcher->addListener('\OC\Repair::advance', $repairListener); $dispatcher->addListener('\OC\Repair::finishProgress', $repairListener); diff --git a/core/ajax/update.php b/core/ajax/update.php index 3d3af92cd93a8..653a594f0699d 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -30,9 +30,11 @@ * along with this program. If not, see * */ +use OCP\EventDispatcher\IEventDispatcher; use OCP\IEventSource; use OCP\IL10N; use OCP\ILogger; +use OC\DB\MigratorExecuteSqlEvent; use Symfony\Component\EventDispatcher\GenericEvent; if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { @@ -123,10 +125,10 @@ public function handleRepairFeedback($event): void { $incompatibleApps = []; $dispatcher = \OC::$server->getEventDispatcher(); - $dispatcher->addListener('\OC\DB\Migrator::executeSql', function ($event) use ($eventSource, $l) { - if ($event instanceof GenericEvent) { - $eventSource->send('success', $l->t('[%d / %d]: %s', [$event['step'], $event['max'], $event->getSubject()])); - } + /** @var IEventDispatcher $newDispatcher */ + $newDispatcher = \OC::$server->get(IEventDispatcher::class); + $newDispatcher->addListener(MigratorExecuteSqlEvent::class, function (MigratorExecuteSqlEvent $event) use ($eventSource, $l) { + $eventSource->send('success', $l->t('[%d / %d]: %s', [$event->getCurrentStep(), $event->getMaxStep(), $event->getSql()])); }); $feedBack = new FeedBackHandler($eventSource, $l); $dispatcher->addListener('\OC\Repair::startProgress', [$feedBack, 'handleRepairFeedback']); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index dbdfd3d72fe79..9aaffee3fbacf 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1061,6 +1061,7 @@ 'OC\\DB\\MigrationException' => $baseDir . '/lib/private/DB/MigrationException.php', 'OC\\DB\\MigrationService' => $baseDir . '/lib/private/DB/MigrationService.php', 'OC\\DB\\Migrator' => $baseDir . '/lib/private/DB/Migrator.php', + 'OC\\DB\\MigratorExecuteSqlEvent' => $baseDir . '/lib/private/DB/MigratorExecuteSqlEvent.php', 'OC\\DB\\MissingColumnInformation' => $baseDir . '/lib/private/DB/MissingColumnInformation.php', 'OC\\DB\\MissingIndexInformation' => $baseDir . '/lib/private/DB/MissingIndexInformation.php', 'OC\\DB\\MissingPrimaryKeyInformation' => $baseDir . '/lib/private/DB/MissingPrimaryKeyInformation.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index e807defc38a31..2b73c398e33f7 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1094,6 +1094,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\DB\\MigrationException' => __DIR__ . '/../../..' . '/lib/private/DB/MigrationException.php', 'OC\\DB\\MigrationService' => __DIR__ . '/../../..' . '/lib/private/DB/MigrationService.php', 'OC\\DB\\Migrator' => __DIR__ . '/../../..' . '/lib/private/DB/Migrator.php', + 'OC\\DB\\MigratorExecuteSqlEvent' => __DIR__ . '/../../..' . '/lib/private/DB/MigratorExecuteSqlEvent.php', 'OC\\DB\\MissingColumnInformation' => __DIR__ . '/../../..' . '/lib/private/DB/MissingColumnInformation.php', 'OC\\DB\\MissingIndexInformation' => __DIR__ . '/../../..' . '/lib/private/DB/MissingIndexInformation.php', 'OC\\DB\\MissingPrimaryKeyInformation' => __DIR__ . '/../../..' . '/lib/private/DB/MissingPrimaryKeyInformation.php', diff --git a/lib/private/DB/Migrator.php b/lib/private/DB/Migrator.php index d2fdefefb39cc..5dc07be1d2b26 100644 --- a/lib/private/DB/Migrator.php +++ b/lib/private/DB/Migrator.php @@ -37,9 +37,8 @@ use Doctrine\DBAL\Types\StringType; use Doctrine\DBAL\Types\Type; use OCP\IConfig; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; use function preg_match; +use OCP\EventDispatcher\IEventDispatcher; class Migrator { @@ -49,15 +48,14 @@ class Migrator { /** @var IConfig */ protected $config; - /** @var ?EventDispatcherInterface */ - private $dispatcher; + private ?IEventDispatcher $dispatcher; /** @var bool */ private $noEmit = false; public function __construct(Connection $connection, IConfig $config, - ?EventDispatcherInterface $dispatcher = null) { + ?IEventDispatcher $dispatcher = null) { $this->connection = $connection; $this->config = $config; $this->dispatcher = $dispatcher; @@ -183,13 +181,13 @@ protected function getFilterExpression() { return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; } - protected function emit($sql, $step, $max) { + protected function emit(string $sql, int $step, int $max): void { if ($this->noEmit) { return; } if (is_null($this->dispatcher)) { return; } - $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, ['step' => $step + 1, 'max' => $max])); + $this->dispatcher->dispatchTyped(new MigratorExecuteSqlEvent($sql, $step, $max)); } } diff --git a/lib/private/DB/MigratorExecuteSqlEvent.php b/lib/private/DB/MigratorExecuteSqlEvent.php new file mode 100644 index 0000000000000..d8a0e2ac3add6 --- /dev/null +++ b/lib/private/DB/MigratorExecuteSqlEvent.php @@ -0,0 +1,52 @@ + + * + * @author Côme Chilliet + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OC\DB; + +use OCP\EventDispatcher\Event; + +class MigratorExecuteSqlEvent extends Event { + private string $sql; + private int $current; + private int $max; + + public function __construct( + string $sql, + int $current, + int $max, + ) { + $this->sql = $sql; + $this->current = $current; + $this->max = $max; + } + + public function getSql(): string { + return $this->sql; + } + + public function getCurrentStep(): int { + return $this->current; + } + + public function getMaxStep(): int { + return $this->max; + } +} diff --git a/lib/private/Updater.php b/lib/private/Updater.php index 1b91441d676f1..e63c158f7a2fe 100644 --- a/lib/private/Updater.php +++ b/lib/private/Updater.php @@ -463,12 +463,14 @@ private function logAllEvents(): void { $log = $this->log; $dispatcher = \OC::$server->getEventDispatcher(); - $dispatcher->addListener('\OC\DB\Migrator::executeSql', function ($event) use ($log) { - if (!$event instanceof GenericEvent) { - return; + /** @var IEventDispatcher $newDispatcher */ + $newDispatcher = \OC::$server->get(IEventDispatcher::class); + $newDispatcher->addListener( + MigratorExecuteSqlEvent::class, + function (MigratorExecuteSqlEvent $event) use ($log) { + $log->info(get_class($event).': ' . $event->getSql() . ' (' . $event->getCurrentStep() . ' of ' . $event->getMaxStep() . ')', ['app' => 'updater']); } - $log->info('\OC\DB\Migrator::executeSql: ' . $event->getSubject() . ' (' . $event->getArgument('step') . ' of ' . $event->getArgument('max') . ')', ['app' => 'updater']); - }); + ); $repairListener = function ($event) use ($log) { if (!$event instanceof GenericEvent) { diff --git a/tests/lib/DB/MigratorTest.php b/tests/lib/DB/MigratorTest.php index af44159efa352..af56730f9f63d 100644 --- a/tests/lib/DB/MigratorTest.php +++ b/tests/lib/DB/MigratorTest.php @@ -62,7 +62,7 @@ protected function setUp(): void { private function getMigrator(): Migrator { $platform = $this->connection->getDatabasePlatform(); $random = \OC::$server->getSecureRandom(); - $dispatcher = \OC::$server->getEventDispatcher(); + $dispatcher = \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class); if ($platform instanceof SqlitePlatform) { return new SQLiteMigrator($this->connection, $this->config, $dispatcher); } elseif ($platform instanceof OraclePlatform) { From a2a7150d6d87a499e6745651edb27c9795939d0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 22 Aug 2022 17:59:26 +0200 Subject: [PATCH 05/12] Migrate Repair events to OCP\EventDispatcher\Event MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- core/Command/Maintenance/Repair.php | 72 +++++----- core/Command/Upgrade.php | 98 +++++++------- core/ajax/update.php | 88 ++++++------- core/register_command.php | 4 +- lib/composer/composer/autoload_classmap.php | 7 + lib/composer/composer/autoload_static.php | 7 + lib/private/DB/Connection.php | 2 +- lib/private/DB/MigratorExecuteSqlEvent.php | 2 +- lib/private/Migration/BackgroundRepair.php | 12 +- lib/private/Repair.php | 66 +++++----- .../Repair/Events/RepairAdvanceEvent.php | 46 +++++++ .../Repair/Events/RepairErrorEvent.php | 38 ++++++ .../Repair/Events/RepairFinishEvent.php | 27 ++++ lib/private/Repair/Events/RepairInfoEvent.php | 38 ++++++ .../Repair/Events/RepairStartEvent.php | 45 +++++++ lib/private/Repair/Events/RepairStepEvent.php | 38 ++++++ .../Repair/Events/RepairWarningEvent.php | 38 ++++++ lib/private/Updater.php | 124 +++++++----------- lib/private/legacy/OC_App.php | 17 +-- lib/public/Migration/IOutput.php | 6 +- 20 files changed, 508 insertions(+), 267 deletions(-) create mode 100644 lib/private/Repair/Events/RepairAdvanceEvent.php create mode 100644 lib/private/Repair/Events/RepairErrorEvent.php create mode 100644 lib/private/Repair/Events/RepairFinishEvent.php create mode 100644 lib/private/Repair/Events/RepairInfoEvent.php create mode 100644 lib/private/Repair/Events/RepairStartEvent.php create mode 100644 lib/private/Repair/Events/RepairStepEvent.php create mode 100644 lib/private/Repair/Events/RepairWarningEvent.php diff --git a/core/Command/Maintenance/Repair.php b/core/Command/Maintenance/Repair.php index 6dac3085ca8bf..650aaf9ce3406 100644 --- a/core/Command/Maintenance/Repair.php +++ b/core/Command/Maintenance/Repair.php @@ -30,24 +30,31 @@ use Exception; use OCP\App\IAppManager; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; +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; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; class Repair extends Command { protected \OC\Repair $repair; protected IConfig $config; - private EventDispatcherInterface $dispatcher; + private IEventDispatcher $dispatcher; private ProgressBar $progress; private OutputInterface $output; private IAppManager $appManager; - public function __construct(\OC\Repair $repair, IConfig $config, EventDispatcherInterface $dispatcher, IAppManager $appManager) { + public function __construct(\OC\Repair $repair, IConfig $config, IEventDispatcher $dispatcher, IAppManager $appManager) { $this->repair = $repair; $this->config = $config; $this->dispatcher = $dispatcher; @@ -102,13 +109,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->progress = new ProgressBar($output); $this->output = $output; - $this->dispatcher->addListener('\OC\Repair::startProgress', [$this, 'handleRepairFeedBack']); - $this->dispatcher->addListener('\OC\Repair::advance', [$this, 'handleRepairFeedBack']); - $this->dispatcher->addListener('\OC\Repair::finishProgress', [$this, 'handleRepairFeedBack']); - $this->dispatcher->addListener('\OC\Repair::step', [$this, 'handleRepairFeedBack']); - $this->dispatcher->addListener('\OC\Repair::info', [$this, 'handleRepairFeedBack']); - $this->dispatcher->addListener('\OC\Repair::warning', [$this, 'handleRepairFeedBack']); - $this->dispatcher->addListener('\OC\Repair::error', [$this, 'handleRepairFeedBack']); + $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']); $this->repair->run(); @@ -116,33 +123,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } - public function handleRepairFeedBack($event) { - if (!$event instanceof GenericEvent) { - return; - } - switch ($event->getSubject()) { - case '\OC\Repair::startProgress': - $this->progress->start($event->getArgument('max')); - break; - case '\OC\Repair::advance': - $this->progress->advance($event->getArgument('step')); - break; - case '\OC\Repair::finishProgress': - $this->progress->finish(); - $this->output->writeln(''); - break; - case '\OC\Repair::step': - $this->output->writeln(' - ' . $event->getArgument('step')); - break; - case '\OC\Repair::info': - $this->output->writeln(' - ' . $event->getArgument('message')); - break; - case '\OC\Repair::warning': - $this->output->writeln(' - WARNING: ' . $event->getArgument('message')); - break; - case '\OC\Repair::error': - $this->output->writeln(' - ERROR: ' . $event->getArgument('message') . ''); - break; + public function handleRepairFeedBack(Event $event): void { + if ($event instanceof RepairStartEvent) { + $this->progress->start($event->getMaxStep()); + } elseif ($event instanceof RepairAdvanceEvent) { + $this->progress->advance($event->getCurrentStep()); + } elseif ($event instanceof RepairFinishEvent) { + $this->progress->finish(); + $this->output->writeln(''); + } elseif ($event instanceof RepairStepEvent) { + $this->output->writeln(' - ' . $event->getStepName()); + } elseif ($event instanceof RepairInfoEvent) { + $this->output->writeln(' - ' . $event->getMessage()); + } elseif ($event instanceof RepairWarningEvent) { + $this->output->writeln(' - WARNING: ' . $event->getMessage()); + } elseif ($event instanceof RepairErrorEvent) { + $this->output->writeln(' - ERROR: ' . $event->getMessage() . ''); } } } diff --git a/core/Command/Upgrade.php b/core/Command/Upgrade.php index 3a45c53f6b274..f8a68c5697cff 100644 --- a/core/Command/Upgrade.php +++ b/core/Command/Upgrade.php @@ -33,19 +33,26 @@ */ namespace OC\Core\Command; +use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\Util; use OC\Console\TimestampFormatter; use OC\DB\MigratorExecuteSqlEvent; use OC\Installer; +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; use OC\Updater; use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\EventDispatcher\GenericEvent; class Upgrade extends Command { public const ERROR_SUCCESS = 0; @@ -94,9 +101,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->installer ); - $dispatcher = \OC::$server->getEventDispatcher(); - /** @var IEventDispatcher $newDispatcher */ - $newDispatcher = \OC::$server->get(IEventDispatcher::class); + /** @var IEventDispatcher $dispatcher */ + $dispatcher = \OC::$server->get(IEventDispatcher::class); $progress = new ProgressBar($output); $progress->setFormat(" %message%\n %current%/%max% [%bar%] %percent:3s%%"); $listener = function (MigratorExecuteSqlEvent $event) use ($progress, $output) { @@ -120,57 +126,45 @@ protected function execute(InputInterface $input, OutputInterface $output): int } } }; - $repairListener = function ($event) use ($progress, $output) { - if (!$event instanceof GenericEvent) { - return; - } - switch ($event->getSubject()) { - case '\OC\Repair::startProgress': - $progress->setMessage('Starting ...'); - $output->writeln($event->getArgument('step')); - $output->writeln(''); - $progress->start($event->getArgument('max')); - break; - case '\OC\Repair::advance': - $desc = $event->getArgument('desc'); - if (!empty($desc)) { - $progress->setMessage($desc); - } - $progress->advance($event->getArgument('step')); - - break; - case '\OC\Repair::finishProgress': - $progress->setMessage('Done'); - $progress->finish(); - $output->writeln(''); - break; - case '\OC\Repair::step': - if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { - $output->writeln('Repair step: ' . $event->getArgument('step') . ''); - } - break; - case '\OC\Repair::info': - if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { - $output->writeln('Repair info: ' . $event->getArgument('message') . ''); - } - break; - case '\OC\Repair::warning': - $output->writeln('Repair warning: ' . $event->getArgument('message') . ''); - break; - case '\OC\Repair::error': - $output->writeln('Repair error: ' . $event->getArgument('message') . ''); - break; + $repairListener = function (Event $event) use ($progress, $output) { + if ($event instanceof RepairStartEvent) { + $progress->setMessage('Starting ...'); + $output->writeln($event->getCurrentStepName()); + $output->writeln(''); + $progress->start($event->getMaxStep()); + } elseif ($event instanceof RepairAdvanceEvent) { + $desc = $event->getDescription(); + if (!empty($desc)) { + $progress->setMessage($desc); + } + $progress->advance($event->getCurrentStep()); + } elseif ($event instanceof RepairFinishEvent) { + $progress->setMessage('Done'); + $progress->finish(); + $output->writeln(''); + } elseif ($event instanceof RepairStepEvent) { + if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { + $output->writeln('Repair step: ' . $event->getStepName() . ''); + } + } elseif ($event instanceof RepairInfoEvent) { + if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { + $output->writeln('Repair info: ' . $event->getMessage() . ''); + } + } elseif ($event instanceof RepairWarningEvent) { + $output->writeln('Repair warning: ' . $event->getMessage() . ''); + } elseif ($event instanceof RepairErrorEvent) { + $output->writeln('Repair error: ' . $event->getMessage() . ''); } }; - $newDispatcher->addListener(MigratorExecuteSqlEvent::class, $listener); - $dispatcher->addListener('\OC\Repair::startProgress', $repairListener); - $dispatcher->addListener('\OC\Repair::advance', $repairListener); - $dispatcher->addListener('\OC\Repair::finishProgress', $repairListener); - $dispatcher->addListener('\OC\Repair::step', $repairListener); - $dispatcher->addListener('\OC\Repair::info', $repairListener); - $dispatcher->addListener('\OC\Repair::warning', $repairListener); - $dispatcher->addListener('\OC\Repair::error', $repairListener); + $dispatcher->addListener(MigratorExecuteSqlEvent::class, $listener); + $dispatcher->addListener(RepairStartEvent::class, $repairListener); + $dispatcher->addListener(RepairAdvanceEvent::class, $repairListener); + $dispatcher->addListener(RepairFinishEvent::class, $repairListener); + $dispatcher->addListener(RepairStepEvent::class, $repairListener); + $dispatcher->addListener(RepairInfoEvent::class, $repairListener); + $dispatcher->addListener(RepairWarningEvent::class, $repairListener); + $dispatcher->addListener(RepairErrorEvent::class, $repairListener); $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($output) { diff --git a/core/ajax/update.php b/core/ajax/update.php index 653a594f0699d..105cedd05f213 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -30,12 +30,19 @@ * along with this program. If not, see * */ +use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; use OCP\IEventSource; use OCP\IL10N; use OCP\ILogger; use OC\DB\MigratorExecuteSqlEvent; -use Symfony\Component\EventDispatcher\GenericEvent; +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; if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { @set_time_limit(0); @@ -63,41 +70,29 @@ public function __construct(IEventSource $eventSource, IL10N $l10n) { $this->l10n = $l10n; } - public function handleRepairFeedback($event): void { - if (!$event instanceof GenericEvent) { - return; - } - - switch ($event->getSubject()) { - case '\OC\Repair::startProgress': - $this->progressStateMax = $event->getArgument('max'); - $this->progressStateStep = 0; - $this->currentStep = (string)$event->getArgument('step'); - break; - case '\OC\Repair::advance': - $this->progressStateStep += $event->getArgument('step'); - $desc = $event->getArgument('desc'); - if (empty($desc)) { - $desc = $this->currentStep; - } - $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $desc])); - break; - case '\OC\Repair::finishProgress': - $this->progressStateMax = $this->progressStateStep; - $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $this->currentStep])); - break; - case '\OC\Repair::step': - $this->eventSource->send('success', $this->l10n->t('Repair step:') . ' ' . $event->getArgument('step')); - break; - case '\OC\Repair::info': - $this->eventSource->send('success', $this->l10n->t('Repair info:') . ' ' . $event->getArgument('message')); - break; - case '\OC\Repair::warning': - $this->eventSource->send('notice', $this->l10n->t('Repair warning:') . ' ' . $event->getArgument('message')); - break; - case '\OC\Repair::error': - $this->eventSource->send('notice', $this->l10n->t('Repair error:') . ' ' . $event->getArgument('message')); - break; + public function handleRepairFeedback(Event $event): void { + if ($event instanceof RepairStartEvent) { + $this->progressStateMax = $event->getMaxStep(); + $this->progressStateStep = 0; + $this->currentStep = $event->getCurrentStepName(); + } elseif ($event instanceof RepairAdvanceEvent) { + $this->progressStateStep += $event->getCurrentStep(); + $desc = $event->getDescription(); + if (empty($desc)) { + $desc = $this->currentStep; + } + $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $desc])); + } elseif ($event instanceof RepairFinishEvent) { + $this->progressStateMax = $this->progressStateStep; + $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $this->currentStep])); + } elseif ($event instanceof RepairStepEvent) { + $this->eventSource->send('success', $this->l10n->t('Repair step:') . ' ' . $event->getStepName()); + } elseif ($event instanceof RepairInfoEvent) { + $this->eventSource->send('success', $this->l10n->t('Repair info:') . ' ' . $event->getMessage()); + } elseif ($event instanceof RepairWarningEvent) { + $this->eventSource->send('notice', $this->l10n->t('Repair warning:') . ' ' . $event->getMessage()); + } elseif ($event instanceof RepairErrorEvent) { + $this->eventSource->send('notice', $this->l10n->t('Repair error:') . ' ' . $event->getMessage()); } } } @@ -124,20 +119,19 @@ public function handleRepairFeedback($event): void { ); $incompatibleApps = []; - $dispatcher = \OC::$server->getEventDispatcher(); - /** @var IEventDispatcher $newDispatcher */ - $newDispatcher = \OC::$server->get(IEventDispatcher::class); - $newDispatcher->addListener(MigratorExecuteSqlEvent::class, function (MigratorExecuteSqlEvent $event) use ($eventSource, $l) { + /** @var IEventDispatcher $dispatcher */ + $dispatcher = \OC::$server->get(IEventDispatcher::class); + $dispatcher->addListener(MigratorExecuteSqlEvent::class, function (MigratorExecuteSqlEvent $event) use ($eventSource, $l) { $eventSource->send('success', $l->t('[%d / %d]: %s', [$event->getCurrentStep(), $event->getMaxStep(), $event->getSql()])); }); $feedBack = new FeedBackHandler($eventSource, $l); - $dispatcher->addListener('\OC\Repair::startProgress', [$feedBack, 'handleRepairFeedback']); - $dispatcher->addListener('\OC\Repair::advance', [$feedBack, 'handleRepairFeedback']); - $dispatcher->addListener('\OC\Repair::finishProgress', [$feedBack, 'handleRepairFeedback']); - $dispatcher->addListener('\OC\Repair::step', [$feedBack, 'handleRepairFeedback']); - $dispatcher->addListener('\OC\Repair::info', [$feedBack, 'handleRepairFeedback']); - $dispatcher->addListener('\OC\Repair::warning', [$feedBack, 'handleRepairFeedback']); - $dispatcher->addListener('\OC\Repair::error', [$feedBack, 'handleRepairFeedback']); + $dispatcher->addListener(RepairStartEvent::class, [$feedBack, 'handleRepairFeedback']); + $dispatcher->addListener(RepairAdvanceEvent::class, [$feedBack, 'handleRepairFeedback']); + $dispatcher->addListener(RepairFinishEvent::class, [$feedBack, 'handleRepairFeedback']); + $dispatcher->addListener(RepairStepEvent::class, [$feedBack, 'handleRepairFeedback']); + $dispatcher->addListener(RepairInfoEvent::class, [$feedBack, 'handleRepairFeedback']); + $dispatcher->addListener(RepairWarningEvent::class, [$feedBack, 'handleRepairFeedback']); + $dispatcher->addListener(RepairErrorEvent::class, [$feedBack, 'handleRepairFeedback']); $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($eventSource, $l) { $eventSource->send('success', $l->t('Turned on maintenance mode')); diff --git a/core/register_command.php b/core/register_command.php index 98a653aed7e41..943180da706b1 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -168,9 +168,9 @@ $application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class), \OC::$server->query(\OC\Installer::class))); $application->add(new OC\Core\Command\Maintenance\Repair( - new \OC\Repair([], \OC::$server->getEventDispatcher(), \OC::$server->get(LoggerInterface::class)), + new \OC\Repair([], \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class), \OC::$server->get(LoggerInterface::class)), \OC::$server->getConfig(), - \OC::$server->getEventDispatcher(), + \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class), \OC::$server->getAppManager() )); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 9aaffee3fbacf..4e0d07ab6bc3f 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1416,6 +1416,13 @@ 'OC\\Repair\\ClearFrontendCaches' => $baseDir . '/lib/private/Repair/ClearFrontendCaches.php', 'OC\\Repair\\ClearGeneratedAvatarCache' => $baseDir . '/lib/private/Repair/ClearGeneratedAvatarCache.php', 'OC\\Repair\\Collation' => $baseDir . '/lib/private/Repair/Collation.php', + 'OC\\Repair\\Events\\RepairAdvanceEvent' => $baseDir . '/lib/private/Repair/Events/RepairAdvanceEvent.php', + 'OC\\Repair\\Events\\RepairErrorEvent' => $baseDir . '/lib/private/Repair/Events/RepairErrorEvent.php', + 'OC\\Repair\\Events\\RepairFinishEvent' => $baseDir . '/lib/private/Repair/Events/RepairFinishEvent.php', + 'OC\\Repair\\Events\\RepairInfoEvent' => $baseDir . '/lib/private/Repair/Events/RepairInfoEvent.php', + 'OC\\Repair\\Events\\RepairStartEvent' => $baseDir . '/lib/private/Repair/Events/RepairStartEvent.php', + 'OC\\Repair\\Events\\RepairStepEvent' => $baseDir . '/lib/private/Repair/Events/RepairStepEvent.php', + 'OC\\Repair\\Events\\RepairWarningEvent' => $baseDir . '/lib/private/Repair/Events/RepairWarningEvent.php', 'OC\\Repair\\MoveUpdaterStepFile' => $baseDir . '/lib/private/Repair/MoveUpdaterStepFile.php', 'OC\\Repair\\NC11\\FixMountStorages' => $baseDir . '/lib/private/Repair/NC11/FixMountStorages.php', 'OC\\Repair\\NC13\\AddLogRotateJob' => $baseDir . '/lib/private/Repair/NC13/AddLogRotateJob.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 2b73c398e33f7..68b5838f0d53d 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1449,6 +1449,13 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Repair\\ClearFrontendCaches' => __DIR__ . '/../../..' . '/lib/private/Repair/ClearFrontendCaches.php', 'OC\\Repair\\ClearGeneratedAvatarCache' => __DIR__ . '/../../..' . '/lib/private/Repair/ClearGeneratedAvatarCache.php', 'OC\\Repair\\Collation' => __DIR__ . '/../../..' . '/lib/private/Repair/Collation.php', + 'OC\\Repair\\Events\\RepairAdvanceEvent' => __DIR__ . '/../../..' . '/lib/private/Repair/Events/RepairAdvanceEvent.php', + 'OC\\Repair\\Events\\RepairErrorEvent' => __DIR__ . '/../../..' . '/lib/private/Repair/Events/RepairErrorEvent.php', + 'OC\\Repair\\Events\\RepairFinishEvent' => __DIR__ . '/../../..' . '/lib/private/Repair/Events/RepairFinishEvent.php', + 'OC\\Repair\\Events\\RepairInfoEvent' => __DIR__ . '/../../..' . '/lib/private/Repair/Events/RepairInfoEvent.php', + 'OC\\Repair\\Events\\RepairStartEvent' => __DIR__ . '/../../..' . '/lib/private/Repair/Events/RepairStartEvent.php', + 'OC\\Repair\\Events\\RepairStepEvent' => __DIR__ . '/../../..' . '/lib/private/Repair/Events/RepairStepEvent.php', + 'OC\\Repair\\Events\\RepairWarningEvent' => __DIR__ . '/../../..' . '/lib/private/Repair/Events/RepairWarningEvent.php', 'OC\\Repair\\MoveUpdaterStepFile' => __DIR__ . '/../../..' . '/lib/private/Repair/MoveUpdaterStepFile.php', 'OC\\Repair\\NC11\\FixMountStorages' => __DIR__ . '/../../..' . '/lib/private/Repair/NC11/FixMountStorages.php', 'OC\\Repair\\NC13\\AddLogRotateJob' => __DIR__ . '/../../..' . '/lib/private/Repair/NC13/AddLogRotateJob.php', diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php index 22c2bbbb793d2..73e0f4b4ac251 100644 --- a/lib/private/DB/Connection.php +++ b/lib/private/DB/Connection.php @@ -588,7 +588,7 @@ private function getMigrator() { $random = \OC::$server->getSecureRandom(); $platform = $this->getDatabasePlatform(); $config = \OC::$server->getConfig(); - $dispatcher = \OC::$server->getEventDispatcher(); + $dispatcher = \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class); if ($platform instanceof SqlitePlatform) { return new SQLiteMigrator($this, $config, $dispatcher); } elseif ($platform instanceof OraclePlatform) { diff --git a/lib/private/DB/MigratorExecuteSqlEvent.php b/lib/private/DB/MigratorExecuteSqlEvent.php index d8a0e2ac3add6..9c67587fc1c5d 100644 --- a/lib/private/DB/MigratorExecuteSqlEvent.php +++ b/lib/private/DB/MigratorExecuteSqlEvent.php @@ -31,7 +31,7 @@ class MigratorExecuteSqlEvent extends Event { public function __construct( string $sql, int $current, - int $max, + int $max ) { $this->sql = $sql; $this->current = $current; diff --git a/lib/private/Migration/BackgroundRepair.php b/lib/private/Migration/BackgroundRepair.php index f3ae8f4bdcf3a..579ba494e589c 100644 --- a/lib/private/Migration/BackgroundRepair.php +++ b/lib/private/Migration/BackgroundRepair.php @@ -26,14 +26,14 @@ */ namespace OC\Migration; -use OC\NeedsUpdateException; -use OC\Repair; -use OC_App; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\TimedJob; +use OCP\EventDispatcher\IEventDispatcher; +use OC\NeedsUpdateException; +use OC\Repair; +use OC_App; use Psr\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Class BackgroundRepair @@ -43,9 +43,9 @@ class BackgroundRepair extends TimedJob { private IJobList $jobList; private LoggerInterface $logger; - private EventDispatcherInterface $dispatcher; + private IEventDispatcher $dispatcher; - public function __construct(EventDispatcherInterface $dispatcher, ITimeFactory $time, LoggerInterface $logger, IJobList $jobList) { + public function __construct(IEventDispatcher $dispatcher, ITimeFactory $time, LoggerInterface $logger, IJobList $jobList) { parent::__construct($time); $this->dispatcher = $dispatcher; $this->logger = $logger; diff --git a/lib/private/Repair.php b/lib/private/Repair.php index 496e9b15e2e7b..97a72707d0338 100644 --- a/lib/private/Repair.php +++ b/lib/private/Repair.php @@ -34,6 +34,12 @@ */ namespace OC; +use OCP\AppFramework\QueryException; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Collaboration\Resources\IManager; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Migration\IOutput; +use OCP\Migration\IRepairStep; use OC\App\AppStore\Bundles\BundleFetcher; use OC\Avatar\AvatarManager; use OC\DB\Connection; @@ -44,15 +50,15 @@ use OC\Repair\ClearFrontendCaches; use OC\Repair\ClearGeneratedAvatarCache; use OC\Repair\Collation; +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; use OC\Repair\MoveUpdaterStepFile; -use OC\Repair\NC22\LookupServerSendCheck; -use OC\Repair\NC24\AddTokenCleanupJob; -use OC\Repair\Owncloud\CleanPreviews; -use OC\Repair\Owncloud\MigrateOauthTables; use OC\Repair\NC11\FixMountStorages; -use OC\Repair\Owncloud\MoveAvatars; -use OC\Repair\Owncloud\InstallCoreBundle; -use OC\Repair\Owncloud\UpdateLanguageCodes; use OC\Repair\NC13\AddLogRotateJob; use OC\Repair\NC14\AddPreviewBackgroundCleanupJob; use OC\Repair\NC16\AddClenupLoginFlowV2BackgroundJob; @@ -64,23 +70,23 @@ use OC\Repair\NC20\ShippedDashboardEnable; use OC\Repair\NC21\AddCheckForUserCertificatesJob; use OC\Repair\NC21\ValidatePhoneNumber; +use OC\Repair\NC22\LookupServerSendCheck; +use OC\Repair\NC24\AddTokenCleanupJob; use OC\Repair\OldGroupMembershipShares; +use OC\Repair\Owncloud\CleanPreviews; use OC\Repair\Owncloud\DropAccountTermsTable; +use OC\Repair\Owncloud\InstallCoreBundle; +use OC\Repair\Owncloud\MigrateOauthTables; +use OC\Repair\Owncloud\MoveAvatars; use OC\Repair\Owncloud\SaveAccountsTableData; +use OC\Repair\Owncloud\UpdateLanguageCodes; use OC\Repair\RemoveLinkShares; use OC\Repair\RepairDavShares; use OC\Repair\RepairInvalidShares; use OC\Repair\RepairMimeTypes; use OC\Repair\SqliteAutoincrement; use OC\Template\JSCombiner; -use OCP\AppFramework\QueryException; -use OCP\AppFramework\Utility\ITimeFactory; -use OCP\Collaboration\Resources\IManager; -use OCP\Migration\IOutput; -use OCP\Migration\IRepairStep; use Psr\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; use Throwable; class Repair implements IOutput { @@ -88,8 +94,7 @@ class Repair implements IOutput { /** @var IRepairStep[] */ private $repairSteps; - /** @var EventDispatcherInterface */ - private $dispatcher; + private IEventDispatcher $dispatcher; /** @var string */ private $currentStep; @@ -101,7 +106,7 @@ class Repair implements IOutput { * * @param IRepairStep[] $repairSteps array of RepairStep instances */ - public function __construct(array $repairSteps, EventDispatcherInterface $dispatcher, LoggerInterface $logger) { + public function __construct(array $repairSteps, IEventDispatcher $dispatcher, LoggerInterface $logger) { $this->repairSteps = $repairSteps; $this->dispatcher = $dispatcher; $this->logger = $logger; @@ -112,19 +117,19 @@ public function __construct(array $repairSteps, EventDispatcherInterface $dispat */ public function run() { if (count($this->repairSteps) === 0) { - $this->emit('\OC\Repair', 'info', ['message' => 'No repair steps available']); + $this->dispatcher->dispatchTyped(new RepairInfoEvent('No repair steps available')); return; } // run each repair step foreach ($this->repairSteps as $step) { $this->currentStep = $step->getName(); - $this->emit('\OC\Repair', 'step', ['step' => $this->currentStep]); + $this->dispatcher->dispatchTyped(new RepairStepEvent($this->currentStep)); try { $step->run($this); } catch (\Exception $e) { $this->logger->error("Exception while executing repair step " . $step->getName(), ['exception' => $e]); - $this->emit('\OC\Repair', 'error', ['message' => $e->getMessage()]); + $this->dispatcher->dispatchTyped(new RepairErrorEvent($e->getMessage())); } } } @@ -138,7 +143,7 @@ public function run() { public function addStep($repairStep) { if (is_string($repairStep)) { try { - $s = \OC::$server->query($repairStep); + $s = \OC::$server->get($repairStep); } catch (QueryException $e) { if (class_exists($repairStep)) { try { @@ -250,16 +255,11 @@ public static function getBeforeUpgradeRepairSteps() { } /** - * @param array $arguments + * @param string $message */ - public function emit(string $scope, string $method, array $arguments = []): void { - $this->dispatcher->dispatch("$scope::$method", - new GenericEvent("$scope::$method", $arguments)); - } - - public function info($string) { + public function info($message) { // for now just emit as we did in the past - $this->emit('\OC\Repair', 'info', ['message' => $string]); + $this->dispatcher->dispatchTyped(new RepairInfoEvent($message)); } /** @@ -267,7 +267,7 @@ public function info($string) { */ public function warning($message) { // for now just emit as we did in the past - $this->emit('\OC\Repair', 'warning', ['message' => $message]); + $this->dispatcher->dispatchTyped(new RepairWarningEvent($message)); } /** @@ -275,7 +275,7 @@ public function warning($message) { */ public function startProgress($max = 0) { // for now just emit as we did in the past - $this->emit('\OC\Repair', 'startProgress', ['max' => $max, 'step' => $this->currentStep]); + $this->dispatcher->dispatchTyped(new RepairStartEvent($max, $this->currentStep)); } /** @@ -284,7 +284,7 @@ public function startProgress($max = 0) { */ public function advance($step = 1, $description = '') { // for now just emit as we did in the past - $this->emit('\OC\Repair', 'advance', ['step' => $step, 'desc' => $description]); + $this->dispatcher->dispatchTyped(new RepairAdvanceEvent($step, $description)); } /** @@ -292,6 +292,6 @@ public function advance($step = 1, $description = '') { */ public function finishProgress() { // for now just emit as we did in the past - $this->emit('\OC\Repair', 'finishProgress', []); + $this->dispatcher->dispatchTyped(new RepairFinishEvent()); } } diff --git a/lib/private/Repair/Events/RepairAdvanceEvent.php b/lib/private/Repair/Events/RepairAdvanceEvent.php new file mode 100644 index 0000000000000..c82c61c186c77 --- /dev/null +++ b/lib/private/Repair/Events/RepairAdvanceEvent.php @@ -0,0 +1,46 @@ + + * + * @author Côme Chilliet + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OC\Repair\Events; + +use OCP\EventDispatcher\Event; + +class RepairAdvanceEvent extends Event { + // TODO Is that current step or step increment? + private int $current; + private string $description; + + public function __construct( + int $current, + string $description + ) { + $this->current = $current; + $this->description = $description; + } + + public function getCurrentStep(): int { + return $this->current; + } + + public function getDescription(): string { + return $this->description; + } +} diff --git a/lib/private/Repair/Events/RepairErrorEvent.php b/lib/private/Repair/Events/RepairErrorEvent.php new file mode 100644 index 0000000000000..c138014321611 --- /dev/null +++ b/lib/private/Repair/Events/RepairErrorEvent.php @@ -0,0 +1,38 @@ + + * + * @author Côme Chilliet + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OC\Repair\Events; + +use OCP\EventDispatcher\Event; + +class RepairErrorEvent extends Event { + private string $message; + + public function __construct( + string $message + ) { + $this->message = $message; + } + + public function getMessage(): string { + return $this->message; + } +} diff --git a/lib/private/Repair/Events/RepairFinishEvent.php b/lib/private/Repair/Events/RepairFinishEvent.php new file mode 100644 index 0000000000000..2d3027c23bcdd --- /dev/null +++ b/lib/private/Repair/Events/RepairFinishEvent.php @@ -0,0 +1,27 @@ + + * + * @author Côme Chilliet + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OC\Repair\Events; + +use OCP\EventDispatcher\Event; + +class RepairFinishEvent extends Event { +} diff --git a/lib/private/Repair/Events/RepairInfoEvent.php b/lib/private/Repair/Events/RepairInfoEvent.php new file mode 100644 index 0000000000000..7acf71b2d9001 --- /dev/null +++ b/lib/private/Repair/Events/RepairInfoEvent.php @@ -0,0 +1,38 @@ + + * + * @author Côme Chilliet + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OC\Repair\Events; + +use OCP\EventDispatcher\Event; + +class RepairInfoEvent extends Event { + private string $message; + + public function __construct( + string $message + ) { + $this->message = $message; + } + + public function getMessage(): string { + return $this->message; + } +} diff --git a/lib/private/Repair/Events/RepairStartEvent.php b/lib/private/Repair/Events/RepairStartEvent.php new file mode 100644 index 0000000000000..115e145f69fe4 --- /dev/null +++ b/lib/private/Repair/Events/RepairStartEvent.php @@ -0,0 +1,45 @@ + + * + * @author Côme Chilliet + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OC\Repair\Events; + +use OCP\EventDispatcher\Event; + +class RepairStartEvent extends Event { + private int $max; + private string $current; + + public function __construct( + int $max, + string $current + ) { + $this->max = $max; + $this->current = $current; + } + + public function getMaxStep(): int { + return $this->max; + } + + public function getCurrentStepName(): string { + return $this->current; + } +} diff --git a/lib/private/Repair/Events/RepairStepEvent.php b/lib/private/Repair/Events/RepairStepEvent.php new file mode 100644 index 0000000000000..03f0c4929200d --- /dev/null +++ b/lib/private/Repair/Events/RepairStepEvent.php @@ -0,0 +1,38 @@ + + * + * @author Côme Chilliet + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OC\Repair\Events; + +use OCP\EventDispatcher\Event; + +class RepairStepEvent extends Event { + private string $stepName; + + public function __construct( + string $stepName + ) { + $this->stepName = $stepName; + } + + public function getStepName(): string { + return $this->stepName; + } +} diff --git a/lib/private/Repair/Events/RepairWarningEvent.php b/lib/private/Repair/Events/RepairWarningEvent.php new file mode 100644 index 0000000000000..f42478873d258 --- /dev/null +++ b/lib/private/Repair/Events/RepairWarningEvent.php @@ -0,0 +1,38 @@ + + * + * @author Côme Chilliet + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OC\Repair\Events; + +use OCP\EventDispatcher\Event; + +class RepairWarningEvent extends Event { + private string $message; + + public function __construct( + string $message + ) { + $this->message = $message; + } + + public function getMessage(): string { + return $this->message; + } +} diff --git a/lib/private/Updater.php b/lib/private/Updater.php index e63c158f7a2fe..889b54eec51c8 100644 --- a/lib/private/Updater.php +++ b/lib/private/Updater.php @@ -40,19 +40,28 @@ */ namespace OC; +use OCP\App\IAppManager; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\HintException; +use OCP\IConfig; +use OCP\ILogger; +use OCP\Util; use OC\App\AppManager; use OC\DB\Connection; use OC\DB\MigrationService; +use OC\DB\MigratorExecuteSqlEvent; use OC\Hooks\BasicEmitter; use OC\IntegrityCheck\Checker; +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; use OC_App; -use OCP\App\IAppManager; -use OCP\HintException; -use OCP\IConfig; -use OCP\ILogger; -use OCP\Util; use Psr\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\GenericEvent; /** * Class that handles autoupdating of ownCloud @@ -102,7 +111,6 @@ public function __construct(IConfig $config, * @return bool true if the operation succeeded, false otherwise */ public function upgrade(): bool { - $this->emitRepairEvents(); $this->logAllEvents(); $logLevel = $this->config->getSystemValue('loglevel', ILogger::WARN); @@ -248,7 +256,7 @@ private function doUpgrade(string $currentVersion, string $installedVersion): vo file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', ''); // pre-upgrade repairs - $repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->getEventDispatcher(), \OC::$server->get(LoggerInterface::class)); + $repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class), \OC::$server->get(LoggerInterface::class)); $repair->run(); $this->doCoreUpgrade(); @@ -289,7 +297,7 @@ private function doUpgrade(string $currentVersion, string $installedVersion): vo } // post-upgrade repairs - $repair = new Repair(Repair::getRepairSteps(), \OC::$server->getEventDispatcher(), \OC::$server->get(LoggerInterface::class)); + $repair = new Repair(Repair::getRepairSteps(), \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class), \OC::$server->get(LoggerInterface::class)); $repair->run(); //Invalidate update feed @@ -432,87 +440,47 @@ private function upgradeAppStoreApps(array $apps, array $previousEnableStates = } } - /** - * Forward messages emitted by the repair routine - */ - private function emitRepairEvents(): void { - $dispatcher = \OC::$server->getEventDispatcher(); - $dispatcher->addListener('\OC\Repair::warning', function ($event) { - if ($event instanceof GenericEvent) { - $this->emit('\OC\Updater', 'repairWarning', $event->getArguments()); - } - }); - $dispatcher->addListener('\OC\Repair::error', function ($event) { - if ($event instanceof GenericEvent) { - $this->emit('\OC\Updater', 'repairError', $event->getArguments()); - } - }); - $dispatcher->addListener('\OC\Repair::info', function ($event) { - if ($event instanceof GenericEvent) { - $this->emit('\OC\Updater', 'repairInfo', $event->getArguments()); - } - }); - $dispatcher->addListener('\OC\Repair::step', function ($event) { - if ($event instanceof GenericEvent) { - $this->emit('\OC\Updater', 'repairStep', $event->getArguments()); - } - }); - } - private function logAllEvents(): void { $log = $this->log; - $dispatcher = \OC::$server->getEventDispatcher(); - /** @var IEventDispatcher $newDispatcher */ - $newDispatcher = \OC::$server->get(IEventDispatcher::class); - $newDispatcher->addListener( + /** @var IEventDispatcher $dispatcher */ + $dispatcher = \OC::$server->get(IEventDispatcher::class); + $dispatcher->addListener( MigratorExecuteSqlEvent::class, function (MigratorExecuteSqlEvent $event) use ($log) { $log->info(get_class($event).': ' . $event->getSql() . ' (' . $event->getCurrentStep() . ' of ' . $event->getMaxStep() . ')', ['app' => 'updater']); } ); - $repairListener = function ($event) use ($log) { - if (!$event instanceof GenericEvent) { - return; - } - switch ($event->getSubject()) { - case '\OC\Repair::startProgress': - $log->info('\OC\Repair::startProgress: Starting ... ' . $event->getArgument('max') . ' (' . $event->getArgument('step') . ')', ['app' => 'updater']); - break; - case '\OC\Repair::advance': - $desc = $event->getArgument('desc'); - if (empty($desc)) { - $desc = ''; - } - $log->info('\OC\Repair::advance: ' . $desc . ' (' . $event->getArgument('step') . ')', ['app' => 'updater']); - - break; - case '\OC\Repair::finishProgress': - $log->info('\OC\Repair::finishProgress', ['app' => 'updater']); - break; - case '\OC\Repair::step': - $log->info('\OC\Repair::step: Repair step: ' . $event->getArgument('step'), ['app' => 'updater']); - break; - case '\OC\Repair::info': - $log->info('\OC\Repair::info: Repair info: ' . $event->getArgument('message'), ['app' => 'updater']); - break; - case '\OC\Repair::warning': - $log->warning('\OC\Repair::warning: Repair warning: ' . $event->getArgument('message'), ['app' => 'updater']); - break; - case '\OC\Repair::error': - $log->error('\OC\Repair::error: Repair error: ' . $event->getArgument('message'), ['app' => 'updater']); - break; + $repairListener = function (Event $event) use ($log) { + if ($event instanceof RepairStartEvent) { + $log->info(get_class($event).': Starting ... ' . $event->getMaxStep() . ' (' . $event->getCurrentStepName() . ')', ['app' => 'updater']); + } elseif ($event instanceof RepairAdvanceEvent) { + $desc = $event->getDescription(); + if (empty($desc)) { + $desc = ''; + } + $log->info(get_class($event).': ' . $desc . ' (' . $event->getCurrentStep() . ')', ['app' => 'updater']); + } elseif ($event instanceof RepairFinishEvent) { + $log->info(get_class($event), ['app' => 'updater']); + } elseif ($event instanceof RepairStepEvent) { + $log->info(get_class($event).': Repair step: ' . $event->getStepName(), ['app' => 'updater']); + } elseif ($event instanceof RepairInfoEvent) { + $log->info(get_class($event).': Repair info: ' . $event->getMessage(), ['app' => 'updater']); + } elseif ($event instanceof RepairWarningEvent) { + $log->warning(get_class($event).': Repair warning: ' . $event->getMessage(), ['app' => 'updater']); + } elseif ($event instanceof RepairErrorEvent) { + $log->error(get_class($event).': Repair error: ' . $event->getMessage(), ['app' => 'updater']); } }; - $dispatcher->addListener('\OC\Repair::startProgress', $repairListener); - $dispatcher->addListener('\OC\Repair::advance', $repairListener); - $dispatcher->addListener('\OC\Repair::finishProgress', $repairListener); - $dispatcher->addListener('\OC\Repair::step', $repairListener); - $dispatcher->addListener('\OC\Repair::info', $repairListener); - $dispatcher->addListener('\OC\Repair::warning', $repairListener); - $dispatcher->addListener('\OC\Repair::error', $repairListener); + $dispatcher->addListener(RepairStartEvent::class, $repairListener); + $dispatcher->addListener(RepairAdvanceEvent::class, $repairListener); + $dispatcher->addListener(RepairFinishEvent::class, $repairListener); + $dispatcher->addListener(RepairStepEvent::class, $repairListener); + $dispatcher->addListener(RepairInfoEvent::class, $repairListener); + $dispatcher->addListener(RepairWarningEvent::class, $repairListener); + $dispatcher->addListener(RepairErrorEvent::class, $repairListener); $this->listen('\OC\Updater', 'maintenanceEnabled', function () use ($log) { diff --git a/lib/private/legacy/OC_App.php b/lib/private/legacy/OC_App.php index cd53dc0afc441..a5eb26d0d4ba2 100644 --- a/lib/private/legacy/OC_App.php +++ b/lib/private/legacy/OC_App.php @@ -50,18 +50,19 @@ * along with this program. If not, see * */ +use OCP\AppFramework\QueryException; +use OCP\App\ManagerEvent; +use OCP\Authentication\IAlternativeLogin; +use OCP\ILogger; +use OCP\Settings\IManager as ISettingsManager; +use OC\AppFramework\Bootstrap\Coordinator; use OC\App\DependencyAnalyzer; use OC\App\Platform; -use OC\AppFramework\Bootstrap\Coordinator; use OC\DB\MigrationService; use OC\Installer; use OC\Repair; +use OC\Repair\Events\RepairErrorEvent; use OC\ServerNotAvailableException; -use OCP\App\ManagerEvent; -use OCP\AppFramework\QueryException; -use OCP\Authentication\IAlternativeLogin; -use OCP\ILogger; -use OCP\Settings\IManager as ISettingsManager; use Psr\Log\LoggerInterface; /** @@ -1047,7 +1048,7 @@ public static function executeRepairSteps(string $appId, array $steps) { // load the app self::loadApp($appId); - $dispatcher = OC::$server->getEventDispatcher(); + $dispatcher = \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class); // load the steps $r = new Repair([], $dispatcher, \OC::$server->get(LoggerInterface::class)); @@ -1055,7 +1056,7 @@ public static function executeRepairSteps(string $appId, array $steps) { try { $r->addStep($step); } catch (Exception $ex) { - $r->emit('\OC\Repair', 'error', ['message' => $ex->getMessage()]); + $dispatcher->dispatchTyped(new RepairErrorEvent($ex->getMessage())); \OC::$server->getLogger()->logException($ex); } } diff --git a/lib/public/Migration/IOutput.php b/lib/public/Migration/IOutput.php index 2dba50ab2c653..212b5d927f88d 100644 --- a/lib/public/Migration/IOutput.php +++ b/lib/public/Migration/IOutput.php @@ -32,18 +32,21 @@ interface IOutput { /** * @param string $message + * @return void * @since 9.1.0 */ public function info($message); /** * @param string $message + * @return void * @since 9.1.0 */ public function warning($message); /** * @param int $max + * @return void * @since 9.1.0 */ public function startProgress($max = 0); @@ -51,12 +54,13 @@ public function startProgress($max = 0); /** * @param int $step * @param string $description + * @return void * @since 9.1.0 */ public function advance($step = 1, $description = ''); /** - * @param int $max + * @return void * @since 9.1.0 */ public function finishProgress(); From 5d313da7098d82cdf6cc4a5235bf1455b6833435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 23 Aug 2022 11:01:01 +0200 Subject: [PATCH 06/12] Fix RepairTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../{RepairStepTest.php => RepairTest.php} | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) rename tests/lib/{RepairStepTest.php => RepairTest.php} (63%) diff --git a/tests/lib/RepairStepTest.php b/tests/lib/RepairTest.php similarity index 63% rename from tests/lib/RepairStepTest.php rename to tests/lib/RepairTest.php index b1d16fdd3fdb3..1a2fd620e49fe 100644 --- a/tests/lib/RepairStepTest.php +++ b/tests/lib/RepairTest.php @@ -8,14 +8,19 @@ namespace Test; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Migration\IRepairStep; +use OC\Repair; +use OC\Repair\Events\RepairInfoEvent; +use OC\Repair\Events\RepairStepEvent; +use OC\Repair\Events\RepairWarningEvent; +use OC\Repair\Events\RepairErrorEvent; use Psr\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcher; -class RepairStepTest implements IRepairStep { - private $warning; +class TestRepairStep implements IRepairStep { + private bool $warning; - public function __construct($warning = false) { + public function __construct(bool $warning = false) { $this->warning = $warning; } @@ -33,28 +38,27 @@ public function run(\OCP\Migration\IOutput $out) { } class RepairTest extends TestCase { - /** @var \OC\Repair */ - private $repair; + private Repair $repair; /** @var string[] */ - private $outputArray; + private array $outputArray = []; protected function setUp(): void { parent::setUp(); - $dispatcher = new EventDispatcher(); + $dispatcher = \OC::$server->get(IEventDispatcher::class); $this->repair = new \OC\Repair([], $dispatcher, $this->createMock(LoggerInterface::class)); - $dispatcher->addListener('\OC\Repair::warning', function ($event) { - /** @var \Symfony\Component\EventDispatcher\GenericEvent $event */ - $this->outputArray[] = 'warning: ' . $event->getArgument(0); + $dispatcher->addListener(RepairWarningEvent::class, function (RepairWarningEvent $event) { + $this->outputArray[] = 'warning: ' . $event->getMessage(); }); - $dispatcher->addListener('\OC\Repair::info', function ($event) { - /** @var \Symfony\Component\EventDispatcher\GenericEvent $event */ - $this->outputArray[] = 'info: ' . $event->getArgument(0); + $dispatcher->addListener(RepairInfoEvent::class, function (RepairInfoEvent $event) { + $this->outputArray[] = 'info: ' . $event->getMessage(); }); - $dispatcher->addListener('\OC\Repair::step', function ($event) { - /** @var \Symfony\Component\EventDispatcher\GenericEvent $event */ - $this->outputArray[] = 'step: ' . $event->getArgument(0); + $dispatcher->addListener(RepairStepEvent::class, function (RepairStepEvent $event) { + $this->outputArray[] = 'step: ' . $event->getStepName(); + }); + $dispatcher->addListener(RepairErrorEvent::class, function (RepairErrorEvent $event) { + $this->outputArray[] = 'error: ' . $event->getMessage(); }); } @@ -88,7 +92,7 @@ public function testRunRepairStepsWithException() { $mock = $this->createMock(TestRepairStep::class); $mock->expects($this->any()) ->method('run') - ->will($this->throwException(new \Exception())); + ->will($this->throwException(new \Exception('Exception text'))); $mock->expects($this->any()) ->method('getName') ->willReturn('Exception Test'); @@ -103,11 +107,14 @@ public function testRunRepairStepsWithException() { $thrown = true; } - $this->assertTrue($thrown); + $this->assertFalse($thrown); // jump out after exception $this->assertEquals( [ 'step: Exception Test', + 'error: Exception text', + 'step: Test Name', + 'info: Simulated info', ], $this->outputArray ); From e5731b3c30f5490f40328809582a738c68ed8686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 23 Aug 2022 11:23:02 +0200 Subject: [PATCH 07/12] Fix BackgroundRepairTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- tests/lib/Migration/BackgroundRepairTest.php | 30 +++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/tests/lib/Migration/BackgroundRepairTest.php b/tests/lib/Migration/BackgroundRepairTest.php index 7da9b18de3070..e14b3179b2784 100644 --- a/tests/lib/Migration/BackgroundRepairTest.php +++ b/tests/lib/Migration/BackgroundRepairTest.php @@ -21,15 +21,17 @@ namespace Test\Migration; -use OC\Migration\BackgroundRepair; -use OC\NeedsUpdateException; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Migration\IOutput; use OCP\Migration\IRepairStep; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; -use Test\TestCase; +use OC\BackgroundJob\JobList; +use OC\Migration\BackgroundRepair; +use OC\NeedsUpdateException; +use OC\Repair\Events\RepairStepEvent; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; +use Test\TestCase; class TestRepairStep implements IRepairStep { @@ -57,16 +59,16 @@ public function run(IOutput $output) { class BackgroundRepairTest extends TestCase { - /** @var \OC\BackgroundJob\JobList|\PHPUnit\Framework\MockObject\MockObject */ + /** @var JobList|MockObject */ private $jobList; - /** @var BackgroundRepair|\PHPUnit\Framework\MockObject\MockObject */ + /** @var BackgroundRepair|MockObject */ private $job; - /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ + /** @var LoggerInterface|MockObject */ private $logger; - /** @var EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject $dispatcher */ + /** @var IEventDispatcher|MockObject $dispatcher */ private $dispatcher; /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject $dispatcher */ @@ -75,13 +77,13 @@ class BackgroundRepairTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->jobList = $this->getMockBuilder('OC\BackgroundJob\JobList') + $this->jobList = $this->getMockBuilder(JobList::class) ->disableOriginalConstructor() ->getMock(); $this->logger = $this->getMockBuilder(LoggerInterface::class) ->disableOriginalConstructor() ->getMock(); - $this->dispatcher = $this->createMock(EventDispatcherInterface::class); + $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->time = $this->createMock(ITimeFactory::class); $this->time->method('getTime') ->willReturn(999999); @@ -107,7 +109,7 @@ public function testAppUpgrading() { } public function testUnknownStep() { - $this->dispatcher->expects($this->never())->method('dispatch'); + $this->dispatcher->expects($this->never())->method('dispatchTyped'); $this->jobList->expects($this->once())->method('remove'); $this->logger->expects($this->once())->method('error'); @@ -120,8 +122,8 @@ public function testUnknownStep() { } public function testWorkingStep() { - $this->dispatcher->expects($this->once())->method('dispatch') - ->with('\OC\Repair::step', new GenericEvent('\OC\Repair::step', ['A test repair step'])); + $this->dispatcher->expects($this->once())->method('dispatchTyped') + ->with($this->equalTo(new RepairStepEvent('A test repair step'))); $this->jobList->expects($this->once())->method('remove'); From 5cfdf9b9eefe458cf3ef663eb20da796ec13325e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 23 Aug 2022 14:57:00 +0200 Subject: [PATCH 08/12] Add void return types to callables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- core/Command/Upgrade.php | 4 ++-- core/ajax/update.php | 9 ++++++--- lib/private/Updater.php | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/core/Command/Upgrade.php b/core/Command/Upgrade.php index f8a68c5697cff..05f0615d33b6a 100644 --- a/core/Command/Upgrade.php +++ b/core/Command/Upgrade.php @@ -105,7 +105,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $dispatcher = \OC::$server->get(IEventDispatcher::class); $progress = new ProgressBar($output); $progress->setFormat(" %message%\n %current%/%max% [%bar%] %percent:3s%%"); - $listener = function (MigratorExecuteSqlEvent $event) use ($progress, $output) { + $listener = function (MigratorExecuteSqlEvent $event) use ($progress, $output): void { $message = $event->getSql(); if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { $output->writeln(' Executing SQL ' . $message); @@ -126,7 +126,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } } }; - $repairListener = function (Event $event) use ($progress, $output) { + $repairListener = function (Event $event) use ($progress, $output): void { if ($event instanceof RepairStartEvent) { $progress->setMessage('Starting ...'); $output->writeln($event->getCurrentStepName()); diff --git a/core/ajax/update.php b/core/ajax/update.php index 105cedd05f213..acd8c3b22c090 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -121,9 +121,12 @@ public function handleRepairFeedback(Event $event): void { /** @var IEventDispatcher $dispatcher */ $dispatcher = \OC::$server->get(IEventDispatcher::class); - $dispatcher->addListener(MigratorExecuteSqlEvent::class, function (MigratorExecuteSqlEvent $event) use ($eventSource, $l) { - $eventSource->send('success', $l->t('[%d / %d]: %s', [$event->getCurrentStep(), $event->getMaxStep(), $event->getSql()])); - }); + $dispatcher->addListener( + MigratorExecuteSqlEvent::class, + function (MigratorExecuteSqlEvent $event) use ($eventSource, $l): void { + $eventSource->send('success', $l->t('[%d / %d]: %s', [$event->getCurrentStep(), $event->getMaxStep(), $event->getSql()])); + } + ); $feedBack = new FeedBackHandler($eventSource, $l); $dispatcher->addListener(RepairStartEvent::class, [$feedBack, 'handleRepairFeedback']); $dispatcher->addListener(RepairAdvanceEvent::class, [$feedBack, 'handleRepairFeedback']); diff --git a/lib/private/Updater.php b/lib/private/Updater.php index 889b54eec51c8..49909fc456237 100644 --- a/lib/private/Updater.php +++ b/lib/private/Updater.php @@ -447,12 +447,12 @@ private function logAllEvents(): void { $dispatcher = \OC::$server->get(IEventDispatcher::class); $dispatcher->addListener( MigratorExecuteSqlEvent::class, - function (MigratorExecuteSqlEvent $event) use ($log) { + function (MigratorExecuteSqlEvent $event) use ($log): void { $log->info(get_class($event).': ' . $event->getSql() . ' (' . $event->getCurrentStep() . ' of ' . $event->getMaxStep() . ')', ['app' => 'updater']); } ); - $repairListener = function (Event $event) use ($log) { + $repairListener = function (Event $event) use ($log): void { if ($event instanceof RepairStartEvent) { $log->info(get_class($event).': Starting ... ' . $event->getMaxStep() . ' (' . $event->getCurrentStepName() . ')', ['app' => 'updater']); } elseif ($event instanceof RepairAdvanceEvent) { From 4ac81733bd69044fdfc048cc26a9be39630a3426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 25 Aug 2022 16:13:04 +0200 Subject: [PATCH 09/12] Declare strict types in all new Event classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/private/DB/MigratorExecuteSqlEvent.php | 3 +++ lib/private/Repair/Events/RepairAdvanceEvent.php | 3 +++ lib/private/Repair/Events/RepairErrorEvent.php | 3 +++ lib/private/Repair/Events/RepairFinishEvent.php | 3 +++ lib/private/Repair/Events/RepairInfoEvent.php | 3 +++ lib/private/Repair/Events/RepairStartEvent.php | 3 +++ lib/private/Repair/Events/RepairStepEvent.php | 3 +++ lib/private/Repair/Events/RepairWarningEvent.php | 3 +++ 8 files changed, 24 insertions(+) diff --git a/lib/private/DB/MigratorExecuteSqlEvent.php b/lib/private/DB/MigratorExecuteSqlEvent.php index 9c67587fc1c5d..997a4eee53a0a 100644 --- a/lib/private/DB/MigratorExecuteSqlEvent.php +++ b/lib/private/DB/MigratorExecuteSqlEvent.php @@ -1,4 +1,7 @@ * diff --git a/lib/private/Repair/Events/RepairAdvanceEvent.php b/lib/private/Repair/Events/RepairAdvanceEvent.php index c82c61c186c77..7602127cf41a8 100644 --- a/lib/private/Repair/Events/RepairAdvanceEvent.php +++ b/lib/private/Repair/Events/RepairAdvanceEvent.php @@ -1,4 +1,7 @@ * diff --git a/lib/private/Repair/Events/RepairErrorEvent.php b/lib/private/Repair/Events/RepairErrorEvent.php index c138014321611..7f8a87cfda30b 100644 --- a/lib/private/Repair/Events/RepairErrorEvent.php +++ b/lib/private/Repair/Events/RepairErrorEvent.php @@ -1,4 +1,7 @@ * diff --git a/lib/private/Repair/Events/RepairFinishEvent.php b/lib/private/Repair/Events/RepairFinishEvent.php index 2d3027c23bcdd..abdc2acd1ce49 100644 --- a/lib/private/Repair/Events/RepairFinishEvent.php +++ b/lib/private/Repair/Events/RepairFinishEvent.php @@ -1,4 +1,7 @@ * diff --git a/lib/private/Repair/Events/RepairInfoEvent.php b/lib/private/Repair/Events/RepairInfoEvent.php index 7acf71b2d9001..5c2a53fce4f4f 100644 --- a/lib/private/Repair/Events/RepairInfoEvent.php +++ b/lib/private/Repair/Events/RepairInfoEvent.php @@ -1,4 +1,7 @@ * diff --git a/lib/private/Repair/Events/RepairStartEvent.php b/lib/private/Repair/Events/RepairStartEvent.php index 115e145f69fe4..7bb270595c590 100644 --- a/lib/private/Repair/Events/RepairStartEvent.php +++ b/lib/private/Repair/Events/RepairStartEvent.php @@ -1,4 +1,7 @@ * diff --git a/lib/private/Repair/Events/RepairStepEvent.php b/lib/private/Repair/Events/RepairStepEvent.php index 03f0c4929200d..82af670b950f8 100644 --- a/lib/private/Repair/Events/RepairStepEvent.php +++ b/lib/private/Repair/Events/RepairStepEvent.php @@ -1,4 +1,7 @@ * diff --git a/lib/private/Repair/Events/RepairWarningEvent.php b/lib/private/Repair/Events/RepairWarningEvent.php index f42478873d258..2aaa44d47195b 100644 --- a/lib/private/Repair/Events/RepairWarningEvent.php +++ b/lib/private/Repair/Events/RepairWarningEvent.php @@ -1,4 +1,7 @@ * From 652ab6da3f5309b0ea6e1e19ce4d915ecb6cc9b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 25 Aug 2022 16:18:53 +0200 Subject: [PATCH 10/12] Surround cli output with appropriate xml tags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- core/Command/Maintenance/Repair.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/Command/Maintenance/Repair.php b/core/Command/Maintenance/Repair.php index 650aaf9ce3406..7f8d98d758dcf 100644 --- a/core/Command/Maintenance/Repair.php +++ b/core/Command/Maintenance/Repair.php @@ -132,11 +132,11 @@ public function handleRepairFeedBack(Event $event): void { $this->progress->finish(); $this->output->writeln(''); } elseif ($event instanceof RepairStepEvent) { - $this->output->writeln(' - ' . $event->getStepName()); + $this->output->writeln(' - ' . $event->getStepName() . ''); } elseif ($event instanceof RepairInfoEvent) { - $this->output->writeln(' - ' . $event->getMessage()); + $this->output->writeln(' - ' . $event->getMessage() . ''); } elseif ($event instanceof RepairWarningEvent) { - $this->output->writeln(' - WARNING: ' . $event->getMessage()); + $this->output->writeln(' - WARNING: ' . $event->getMessage()) . ''; } elseif ($event instanceof RepairErrorEvent) { $this->output->writeln(' - ERROR: ' . $event->getMessage() . ''); } From 9a2902553495baa6fb1515176a8bbaab5633fb58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 25 Aug 2022 16:26:31 +0200 Subject: [PATCH 11/12] Parameter of RepairAdvanceEvent is actually an increment, not a step id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- core/Command/Maintenance/Repair.php | 2 +- core/Command/Upgrade.php | 2 +- core/ajax/update.php | 2 +- lib/private/Repair.php | 2 +- lib/private/Repair/Events/RepairAdvanceEvent.php | 11 +++++------ lib/private/Updater.php | 2 +- 6 files changed, 10 insertions(+), 11 deletions(-) diff --git a/core/Command/Maintenance/Repair.php b/core/Command/Maintenance/Repair.php index 7f8d98d758dcf..e1d1862f3f4be 100644 --- a/core/Command/Maintenance/Repair.php +++ b/core/Command/Maintenance/Repair.php @@ -127,7 +127,7 @@ public function handleRepairFeedBack(Event $event): void { if ($event instanceof RepairStartEvent) { $this->progress->start($event->getMaxStep()); } elseif ($event instanceof RepairAdvanceEvent) { - $this->progress->advance($event->getCurrentStep()); + $this->progress->advance($event->getIncrement()); } elseif ($event instanceof RepairFinishEvent) { $this->progress->finish(); $this->output->writeln(''); diff --git a/core/Command/Upgrade.php b/core/Command/Upgrade.php index 05f0615d33b6a..d476b91d6bfab 100644 --- a/core/Command/Upgrade.php +++ b/core/Command/Upgrade.php @@ -137,7 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int if (!empty($desc)) { $progress->setMessage($desc); } - $progress->advance($event->getCurrentStep()); + $progress->advance($event->getIncrement()); } elseif ($event instanceof RepairFinishEvent) { $progress->setMessage('Done'); $progress->finish(); diff --git a/core/ajax/update.php b/core/ajax/update.php index acd8c3b22c090..0fe398df7bf03 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -76,7 +76,7 @@ public function handleRepairFeedback(Event $event): void { $this->progressStateStep = 0; $this->currentStep = $event->getCurrentStepName(); } elseif ($event instanceof RepairAdvanceEvent) { - $this->progressStateStep += $event->getCurrentStep(); + $this->progressStateStep += $event->getIncrement(); $desc = $event->getDescription(); if (empty($desc)) { $desc = $this->currentStep; diff --git a/lib/private/Repair.php b/lib/private/Repair.php index 97a72707d0338..e2e5da79216b5 100644 --- a/lib/private/Repair.php +++ b/lib/private/Repair.php @@ -279,7 +279,7 @@ public function startProgress($max = 0) { } /** - * @param int $step + * @param int $step number of step to advance * @param string $description */ public function advance($step = 1, $description = '') { diff --git a/lib/private/Repair/Events/RepairAdvanceEvent.php b/lib/private/Repair/Events/RepairAdvanceEvent.php index 7602127cf41a8..174d4ec48cdea 100644 --- a/lib/private/Repair/Events/RepairAdvanceEvent.php +++ b/lib/private/Repair/Events/RepairAdvanceEvent.php @@ -27,20 +27,19 @@ use OCP\EventDispatcher\Event; class RepairAdvanceEvent extends Event { - // TODO Is that current step or step increment? - private int $current; + private int $increment; private string $description; public function __construct( - int $current, + int $increment, string $description ) { - $this->current = $current; + $this->increment = $increment; $this->description = $description; } - public function getCurrentStep(): int { - return $this->current; + public function getIncrement(): int { + return $this->increment; } public function getDescription(): string { diff --git a/lib/private/Updater.php b/lib/private/Updater.php index 49909fc456237..78613ddbb0c28 100644 --- a/lib/private/Updater.php +++ b/lib/private/Updater.php @@ -460,7 +460,7 @@ function (MigratorExecuteSqlEvent $event) use ($log): void { if (empty($desc)) { $desc = ''; } - $log->info(get_class($event).': ' . $desc . ' (' . $event->getCurrentStep() . ')', ['app' => 'updater']); + $log->info(get_class($event).': ' . $desc . ' (' . $event->getIncrement() . ')', ['app' => 'updater']); } elseif ($event instanceof RepairFinishEvent) { $log->info(get_class($event), ['app' => 'updater']); } elseif ($event instanceof RepairStepEvent) { From 0b9a878250d79101756d881d15d22acda2dbeddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 25 Aug 2022 18:32:20 +0200 Subject: [PATCH 12/12] Flag repair errors as error level in eventSource->send MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- core/ajax/update.php | 2 +- lib/public/IEventSource.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/ajax/update.php b/core/ajax/update.php index 0fe398df7bf03..56dffef409c1c 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -92,7 +92,7 @@ public function handleRepairFeedback(Event $event): void { } elseif ($event instanceof RepairWarningEvent) { $this->eventSource->send('notice', $this->l10n->t('Repair warning:') . ' ' . $event->getMessage()); } elseif ($event instanceof RepairErrorEvent) { - $this->eventSource->send('notice', $this->l10n->t('Repair error:') . ' ' . $event->getMessage()); + $this->eventSource->send('error', $this->l10n->t('Repair error:') . ' ' . $event->getMessage()); } } } diff --git a/lib/public/IEventSource.php b/lib/public/IEventSource.php index 85f0983712543..879b365cc774a 100644 --- a/lib/public/IEventSource.php +++ b/lib/public/IEventSource.php @@ -35,7 +35,7 @@ interface IEventSource { /** * send a message to the client * - * @param string $type + * @param string $type One of success, notice, error, failure and done. Used in core/js/update.js * @param mixed $data * * if only one parameter is given, a typeless message will be send with that parameter as data