|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Files_Sharing\Command; |
| 10 | + |
| 11 | +use OC\Core\Command\Base; |
| 12 | +use OCA\Files_Sharing\OrphanHelper; |
| 13 | +use Symfony\Component\Console\Input\InputInterface; |
| 14 | +use Symfony\Component\Console\Input\InputOption; |
| 15 | +use Symfony\Component\Console\Output\OutputInterface; |
| 16 | + |
| 17 | +class FixBrokenShares extends Base { |
| 18 | + public function __construct( |
| 19 | + private readonly OrphanHelper $orphanHelper |
| 20 | + ) { |
| 21 | + parent::__construct(); |
| 22 | + } |
| 23 | + |
| 24 | + protected function configure(): void { |
| 25 | + $this |
| 26 | + ->setName('sharing:fix-broken-shares') |
| 27 | + ->setDescription('Fix broken shares after transfer ownership on old versions') |
| 28 | + ->addOption( |
| 29 | + 'dry-run', |
| 30 | + null, |
| 31 | + InputOption::VALUE_NONE, |
| 32 | + 'only show which shares would be updated' |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public function execute(InputInterface $input, OutputInterface $output): int { |
| 37 | + $shares = $this->orphanHelper->getAllShares(); |
| 38 | + $dryRun = $input->getOption('dry-run'); |
| 39 | + $count = 0; |
| 40 | + |
| 41 | + foreach ($shares as $share) { |
| 42 | + if ($this->orphanHelper->isShareValid($share['owner'], $share['fileid']) || !$this->orphanHelper->fileExists($share['fileid'])) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + $owner = $this->orphanHelper->findOwner($share['fileid']); |
| 47 | + |
| 48 | + if ($owner !== null) { |
| 49 | + if ($dryRun) { |
| 50 | + $output->writeln("Share with id <info>{$share['id']}</info> (target: <info>{$share['target']}</info>) can be updated to owner <info>$owner</info>"); |
| 51 | + } else { |
| 52 | + $this->orphanHelper->updateShareOwner($share['id'], $owner); |
| 53 | + $output->writeln("Share with id <info>{$share['id']}</info> (target: <info>{$share['target']}</info>) updated to owner <info>$owner</info>"); |
| 54 | + } |
| 55 | + $count++; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if ($count === 0) { |
| 60 | + $output->writeln('No broken shares detected'); |
| 61 | + } |
| 62 | + |
| 63 | + return static::SUCCESS; |
| 64 | + } |
| 65 | +} |
0 commit comments