@@ -38,154 +38,154 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class CleanupRemoteStorages extends Command { |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @var IDBConnection |
|
| 43 | - */ |
|
| 44 | - protected $connection; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var ICloudIdManager |
|
| 48 | - */ |
|
| 49 | - private $cloudIdManager; |
|
| 50 | - |
|
| 51 | - public function __construct(IDBConnection $connection, ICloudIdManager $cloudIdManager) { |
|
| 52 | - $this->connection = $connection; |
|
| 53 | - $this->cloudIdManager = $cloudIdManager; |
|
| 54 | - parent::__construct(); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - protected function configure() { |
|
| 58 | - $this |
|
| 59 | - ->setName('sharing:cleanup-remote-storages') |
|
| 60 | - ->setDescription('Cleanup shared storage entries that have no matching entry in the shares_external table') |
|
| 61 | - ->addOption( |
|
| 62 | - 'dry-run', |
|
| 63 | - null, |
|
| 64 | - InputOption::VALUE_NONE, |
|
| 65 | - 'only show which storages would be deleted' |
|
| 66 | - ); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 70 | - $remoteStorages = $this->getRemoteStorages(); |
|
| 71 | - |
|
| 72 | - $output->writeln(count($remoteStorages) . ' remote storage(s) need(s) to be checked'); |
|
| 73 | - |
|
| 74 | - $remoteShareIds = $this->getRemoteShareIds(); |
|
| 75 | - |
|
| 76 | - $output->writeln(count($remoteShareIds) . ' remote share(s) exist'); |
|
| 77 | - |
|
| 78 | - foreach ($remoteShareIds as $id => $remoteShareId) { |
|
| 79 | - if (isset($remoteStorages[$remoteShareId])) { |
|
| 80 | - if ($input->getOption('dry-run') || $output->isVerbose()) { |
|
| 81 | - $output->writeln("<info>$remoteShareId belongs to remote share $id</info>"); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - unset($remoteStorages[$remoteShareId]); |
|
| 85 | - } else { |
|
| 86 | - $output->writeln("<comment>$remoteShareId for share $id has no matching storage, yet</comment>"); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - if (empty($remoteStorages)) { |
|
| 91 | - $output->writeln('<info>no storages deleted</info>'); |
|
| 92 | - } else { |
|
| 93 | - $dryRun = $input->getOption('dry-run'); |
|
| 94 | - foreach ($remoteStorages as $id => $numericId) { |
|
| 95 | - if ($dryRun) { |
|
| 96 | - $output->writeln("<error>$id [$numericId] can be deleted</error>"); |
|
| 97 | - $this->countFiles($numericId, $output); |
|
| 98 | - } else { |
|
| 99 | - $this->deleteStorage($id, $numericId, $output); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - return 0; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - public function countFiles($numericId, OutputInterface $output) { |
|
| 107 | - $queryBuilder = $this->connection->getQueryBuilder(); |
|
| 108 | - $queryBuilder->select($queryBuilder->func()->count('fileid')) |
|
| 109 | - ->from('filecache') |
|
| 110 | - ->where($queryBuilder->expr()->eq( |
|
| 111 | - 'storage', |
|
| 112 | - $queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR), |
|
| 113 | - IQueryBuilder::PARAM_STR) |
|
| 114 | - ); |
|
| 115 | - $result = $queryBuilder->execute(); |
|
| 116 | - $count = $result->fetchOne(); |
|
| 117 | - $output->writeln("$count files can be deleted for storage $numericId"); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - public function deleteStorage($id, $numericId, OutputInterface $output) { |
|
| 121 | - $queryBuilder = $this->connection->getQueryBuilder(); |
|
| 122 | - $queryBuilder->delete('storages') |
|
| 123 | - ->where($queryBuilder->expr()->eq( |
|
| 124 | - 'id', |
|
| 125 | - $queryBuilder->createNamedParameter($id, IQueryBuilder::PARAM_STR), |
|
| 126 | - IQueryBuilder::PARAM_STR) |
|
| 127 | - ); |
|
| 128 | - $output->write("deleting $id [$numericId] ... "); |
|
| 129 | - $count = $queryBuilder->execute(); |
|
| 130 | - $output->writeln("deleted $count storage"); |
|
| 131 | - $this->deleteFiles($numericId, $output); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - public function deleteFiles($numericId, OutputInterface $output) { |
|
| 135 | - $queryBuilder = $this->connection->getQueryBuilder(); |
|
| 136 | - $queryBuilder->delete('filecache') |
|
| 137 | - ->where($queryBuilder->expr()->eq( |
|
| 138 | - 'storage', |
|
| 139 | - $queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR), |
|
| 140 | - IQueryBuilder::PARAM_STR) |
|
| 141 | - ); |
|
| 142 | - $output->write("deleting files for storage $numericId ... "); |
|
| 143 | - $count = $queryBuilder->execute(); |
|
| 144 | - $output->writeln("deleted $count files"); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - public function getRemoteStorages() { |
|
| 148 | - $queryBuilder = $this->connection->getQueryBuilder(); |
|
| 149 | - $queryBuilder->select(['id', 'numeric_id']) |
|
| 150 | - ->from('storages') |
|
| 151 | - ->where($queryBuilder->expr()->like( |
|
| 152 | - 'id', |
|
| 153 | - // match all 'shared::' + 32 characters storages |
|
| 154 | - $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::') . str_repeat('_', 32)), |
|
| 155 | - IQueryBuilder::PARAM_STR) |
|
| 156 | - ) |
|
| 157 | - ->andWhere($queryBuilder->expr()->notLike( |
|
| 158 | - 'id', |
|
| 159 | - // but not the ones starting with a '/', they are for normal shares |
|
| 160 | - $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/') . '%'), |
|
| 161 | - IQueryBuilder::PARAM_STR) |
|
| 162 | - )->orderBy('numeric_id'); |
|
| 163 | - $query = $queryBuilder->execute(); |
|
| 164 | - |
|
| 165 | - $remoteStorages = []; |
|
| 166 | - |
|
| 167 | - while ($row = $query->fetch()) { |
|
| 168 | - $remoteStorages[$row['id']] = $row['numeric_id']; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - return $remoteStorages; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - public function getRemoteShareIds() { |
|
| 175 | - $queryBuilder = $this->connection->getQueryBuilder(); |
|
| 176 | - $queryBuilder->select(['id', 'share_token', 'owner', 'remote']) |
|
| 177 | - ->from('share_external'); |
|
| 178 | - $query = $queryBuilder->execute(); |
|
| 179 | - |
|
| 180 | - $remoteShareIds = []; |
|
| 181 | - |
|
| 182 | - while ($row = $query->fetch()) { |
|
| 183 | - $cloudId = $this->cloudIdManager->getCloudId($row['owner'], $row['remote']); |
|
| 184 | - $remote = $cloudId->getRemote(); |
|
| 185 | - |
|
| 186 | - $remoteShareIds[$row['id']] = 'shared::' . md5($row['share_token'] . '@' . $remote); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - return $remoteShareIds; |
|
| 190 | - } |
|
| 41 | + /** |
|
| 42 | + * @var IDBConnection |
|
| 43 | + */ |
|
| 44 | + protected $connection; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var ICloudIdManager |
|
| 48 | + */ |
|
| 49 | + private $cloudIdManager; |
|
| 50 | + |
|
| 51 | + public function __construct(IDBConnection $connection, ICloudIdManager $cloudIdManager) { |
|
| 52 | + $this->connection = $connection; |
|
| 53 | + $this->cloudIdManager = $cloudIdManager; |
|
| 54 | + parent::__construct(); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + protected function configure() { |
|
| 58 | + $this |
|
| 59 | + ->setName('sharing:cleanup-remote-storages') |
|
| 60 | + ->setDescription('Cleanup shared storage entries that have no matching entry in the shares_external table') |
|
| 61 | + ->addOption( |
|
| 62 | + 'dry-run', |
|
| 63 | + null, |
|
| 64 | + InputOption::VALUE_NONE, |
|
| 65 | + 'only show which storages would be deleted' |
|
| 66 | + ); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 70 | + $remoteStorages = $this->getRemoteStorages(); |
|
| 71 | + |
|
| 72 | + $output->writeln(count($remoteStorages) . ' remote storage(s) need(s) to be checked'); |
|
| 73 | + |
|
| 74 | + $remoteShareIds = $this->getRemoteShareIds(); |
|
| 75 | + |
|
| 76 | + $output->writeln(count($remoteShareIds) . ' remote share(s) exist'); |
|
| 77 | + |
|
| 78 | + foreach ($remoteShareIds as $id => $remoteShareId) { |
|
| 79 | + if (isset($remoteStorages[$remoteShareId])) { |
|
| 80 | + if ($input->getOption('dry-run') || $output->isVerbose()) { |
|
| 81 | + $output->writeln("<info>$remoteShareId belongs to remote share $id</info>"); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + unset($remoteStorages[$remoteShareId]); |
|
| 85 | + } else { |
|
| 86 | + $output->writeln("<comment>$remoteShareId for share $id has no matching storage, yet</comment>"); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + if (empty($remoteStorages)) { |
|
| 91 | + $output->writeln('<info>no storages deleted</info>'); |
|
| 92 | + } else { |
|
| 93 | + $dryRun = $input->getOption('dry-run'); |
|
| 94 | + foreach ($remoteStorages as $id => $numericId) { |
|
| 95 | + if ($dryRun) { |
|
| 96 | + $output->writeln("<error>$id [$numericId] can be deleted</error>"); |
|
| 97 | + $this->countFiles($numericId, $output); |
|
| 98 | + } else { |
|
| 99 | + $this->deleteStorage($id, $numericId, $output); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + return 0; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + public function countFiles($numericId, OutputInterface $output) { |
|
| 107 | + $queryBuilder = $this->connection->getQueryBuilder(); |
|
| 108 | + $queryBuilder->select($queryBuilder->func()->count('fileid')) |
|
| 109 | + ->from('filecache') |
|
| 110 | + ->where($queryBuilder->expr()->eq( |
|
| 111 | + 'storage', |
|
| 112 | + $queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR), |
|
| 113 | + IQueryBuilder::PARAM_STR) |
|
| 114 | + ); |
|
| 115 | + $result = $queryBuilder->execute(); |
|
| 116 | + $count = $result->fetchOne(); |
|
| 117 | + $output->writeln("$count files can be deleted for storage $numericId"); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + public function deleteStorage($id, $numericId, OutputInterface $output) { |
|
| 121 | + $queryBuilder = $this->connection->getQueryBuilder(); |
|
| 122 | + $queryBuilder->delete('storages') |
|
| 123 | + ->where($queryBuilder->expr()->eq( |
|
| 124 | + 'id', |
|
| 125 | + $queryBuilder->createNamedParameter($id, IQueryBuilder::PARAM_STR), |
|
| 126 | + IQueryBuilder::PARAM_STR) |
|
| 127 | + ); |
|
| 128 | + $output->write("deleting $id [$numericId] ... "); |
|
| 129 | + $count = $queryBuilder->execute(); |
|
| 130 | + $output->writeln("deleted $count storage"); |
|
| 131 | + $this->deleteFiles($numericId, $output); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + public function deleteFiles($numericId, OutputInterface $output) { |
|
| 135 | + $queryBuilder = $this->connection->getQueryBuilder(); |
|
| 136 | + $queryBuilder->delete('filecache') |
|
| 137 | + ->where($queryBuilder->expr()->eq( |
|
| 138 | + 'storage', |
|
| 139 | + $queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR), |
|
| 140 | + IQueryBuilder::PARAM_STR) |
|
| 141 | + ); |
|
| 142 | + $output->write("deleting files for storage $numericId ... "); |
|
| 143 | + $count = $queryBuilder->execute(); |
|
| 144 | + $output->writeln("deleted $count files"); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + public function getRemoteStorages() { |
|
| 148 | + $queryBuilder = $this->connection->getQueryBuilder(); |
|
| 149 | + $queryBuilder->select(['id', 'numeric_id']) |
|
| 150 | + ->from('storages') |
|
| 151 | + ->where($queryBuilder->expr()->like( |
|
| 152 | + 'id', |
|
| 153 | + // match all 'shared::' + 32 characters storages |
|
| 154 | + $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::') . str_repeat('_', 32)), |
|
| 155 | + IQueryBuilder::PARAM_STR) |
|
| 156 | + ) |
|
| 157 | + ->andWhere($queryBuilder->expr()->notLike( |
|
| 158 | + 'id', |
|
| 159 | + // but not the ones starting with a '/', they are for normal shares |
|
| 160 | + $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/') . '%'), |
|
| 161 | + IQueryBuilder::PARAM_STR) |
|
| 162 | + )->orderBy('numeric_id'); |
|
| 163 | + $query = $queryBuilder->execute(); |
|
| 164 | + |
|
| 165 | + $remoteStorages = []; |
|
| 166 | + |
|
| 167 | + while ($row = $query->fetch()) { |
|
| 168 | + $remoteStorages[$row['id']] = $row['numeric_id']; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + return $remoteStorages; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + public function getRemoteShareIds() { |
|
| 175 | + $queryBuilder = $this->connection->getQueryBuilder(); |
|
| 176 | + $queryBuilder->select(['id', 'share_token', 'owner', 'remote']) |
|
| 177 | + ->from('share_external'); |
|
| 178 | + $query = $queryBuilder->execute(); |
|
| 179 | + |
|
| 180 | + $remoteShareIds = []; |
|
| 181 | + |
|
| 182 | + while ($row = $query->fetch()) { |
|
| 183 | + $cloudId = $this->cloudIdManager->getCloudId($row['owner'], $row['remote']); |
|
| 184 | + $remote = $cloudId->getRemote(); |
|
| 185 | + |
|
| 186 | + $remoteShareIds[$row['id']] = 'shared::' . md5($row['share_token'] . '@' . $remote); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + return $remoteShareIds; |
|
| 190 | + } |
|
| 191 | 191 | } |
@@ -69,11 +69,11 @@ discard block |
||
| 69 | 69 | public function execute(InputInterface $input, OutputInterface $output): int { |
| 70 | 70 | $remoteStorages = $this->getRemoteStorages(); |
| 71 | 71 | |
| 72 | - $output->writeln(count($remoteStorages) . ' remote storage(s) need(s) to be checked'); |
|
| 72 | + $output->writeln(count($remoteStorages).' remote storage(s) need(s) to be checked'); |
|
| 73 | 73 | |
| 74 | 74 | $remoteShareIds = $this->getRemoteShareIds(); |
| 75 | 75 | |
| 76 | - $output->writeln(count($remoteShareIds) . ' remote share(s) exist'); |
|
| 76 | + $output->writeln(count($remoteShareIds).' remote share(s) exist'); |
|
| 77 | 77 | |
| 78 | 78 | foreach ($remoteShareIds as $id => $remoteShareId) { |
| 79 | 79 | if (isset($remoteStorages[$remoteShareId])) { |
@@ -151,13 +151,13 @@ discard block |
||
| 151 | 151 | ->where($queryBuilder->expr()->like( |
| 152 | 152 | 'id', |
| 153 | 153 | // match all 'shared::' + 32 characters storages |
| 154 | - $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::') . str_repeat('_', 32)), |
|
| 154 | + $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::').str_repeat('_', 32)), |
|
| 155 | 155 | IQueryBuilder::PARAM_STR) |
| 156 | 156 | ) |
| 157 | 157 | ->andWhere($queryBuilder->expr()->notLike( |
| 158 | 158 | 'id', |
| 159 | 159 | // but not the ones starting with a '/', they are for normal shares |
| 160 | - $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/') . '%'), |
|
| 160 | + $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/').'%'), |
|
| 161 | 161 | IQueryBuilder::PARAM_STR) |
| 162 | 162 | )->orderBy('numeric_id'); |
| 163 | 163 | $query = $queryBuilder->execute(); |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | $cloudId = $this->cloudIdManager->getCloudId($row['owner'], $row['remote']); |
| 184 | 184 | $remote = $cloudId->getRemote(); |
| 185 | 185 | |
| 186 | - $remoteShareIds[$row['id']] = 'shared::' . md5($row['share_token'] . '@' . $remote); |
|
| 186 | + $remoteShareIds[$row['id']] = 'shared::'.md5($row['share_token'].'@'.$remote); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | return $remoteShareIds; |