2016-10-12 13:59:13 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2017-11-06 17:56:42 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud GmbH.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-10-12 13:59:13 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-10-12 13:59:13 +03:00
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-10-12 13:59:13 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files_Sharing\Command;
|
|
|
|
|
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2021-01-26 12:26:10 +03:00
|
|
|
use OCP\Federation\ICloudIdManager;
|
2016-10-12 13:59:13 +03:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleanup 'shared::' storage entries that have no matching entries in the
|
|
|
|
* shares_external table.
|
|
|
|
*/
|
|
|
|
class CleanupRemoteStorages extends Command {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IDBConnection
|
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
2021-01-26 12:26:10 +03:00
|
|
|
/**
|
|
|
|
* @var ICloudIdManager
|
|
|
|
*/
|
|
|
|
private $cloudIdManager;
|
|
|
|
|
|
|
|
public function __construct(IDBConnection $connection, ICloudIdManager $cloudIdManager) {
|
2016-10-12 13:59:13 +03:00
|
|
|
$this->connection = $connection;
|
2021-01-26 12:26:10 +03:00
|
|
|
$this->cloudIdManager = $cloudIdManager;
|
2016-10-12 13:59:13 +03:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function configure() {
|
|
|
|
$this
|
|
|
|
->setName('sharing:cleanup-remote-storages')
|
2017-03-17 11:59:25 +03:00
|
|
|
->setDescription('Cleanup shared storage entries that have no matching entry in the shares_external table')
|
2016-10-12 13:59:13 +03:00
|
|
|
->addOption(
|
|
|
|
'dry-run',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'only show which storages would be deleted'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute(InputInterface $input, OutputInterface $output) {
|
|
|
|
$remoteStorages = $this->getRemoteStorages();
|
|
|
|
|
2017-07-24 08:44:09 +03:00
|
|
|
$output->writeln(count($remoteStorages) . ' remote storage(s) need(s) to be checked');
|
2016-10-12 13:59:13 +03:00
|
|
|
|
|
|
|
$remoteShareIds = $this->getRemoteShareIds();
|
|
|
|
|
2017-07-24 08:44:09 +03:00
|
|
|
$output->writeln(count($remoteShareIds) . ' remote share(s) exist');
|
2016-10-12 13:59:13 +03:00
|
|
|
|
|
|
|
foreach ($remoteShareIds as $id => $remoteShareId) {
|
|
|
|
if (isset($remoteStorages[$remoteShareId])) {
|
2017-03-17 11:59:25 +03:00
|
|
|
if ($input->getOption('dry-run') || $output->isVerbose()) {
|
|
|
|
$output->writeln("<info>$remoteShareId belongs to remote share $id</info>");
|
|
|
|
}
|
|
|
|
|
2016-10-12 13:59:13 +03:00
|
|
|
unset($remoteStorages[$remoteShareId]);
|
|
|
|
} else {
|
2017-03-17 11:59:25 +03:00
|
|
|
$output->writeln("<comment>$remoteShareId for share $id has no matching storage, yet</comment>");
|
2016-10-12 13:59:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($remoteStorages)) {
|
2017-07-24 08:44:09 +03:00
|
|
|
$output->writeln('<info>no storages deleted</info>');
|
2016-10-12 13:59:13 +03:00
|
|
|
} else {
|
|
|
|
$dryRun = $input->getOption('dry-run');
|
|
|
|
foreach ($remoteStorages as $id => $numericId) {
|
|
|
|
if ($dryRun) {
|
2017-03-17 11:59:25 +03:00
|
|
|
$output->writeln("<error>$id [$numericId] can be deleted</error>");
|
2016-10-12 13:59:13 +03:00
|
|
|
$this->countFiles($numericId, $output);
|
|
|
|
} else {
|
|
|
|
$this->deleteStorage($id, $numericId, $output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 11:59:25 +03:00
|
|
|
public function countFiles($numericId, OutputInterface $output) {
|
2016-10-12 13:59:13 +03:00
|
|
|
$queryBuilder = $this->connection->getQueryBuilder();
|
2018-10-19 17:44:28 +03:00
|
|
|
$queryBuilder->select($queryBuilder->func()->count('fileid'))
|
2016-10-12 13:59:13 +03:00
|
|
|
->from('filecache')
|
|
|
|
->where($queryBuilder->expr()->eq(
|
|
|
|
'storage',
|
|
|
|
$queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR),
|
|
|
|
IQueryBuilder::PARAM_STR)
|
|
|
|
);
|
|
|
|
$result = $queryBuilder->execute();
|
|
|
|
$count = $result->fetchColumn();
|
|
|
|
$output->writeln("$count files can be deleted for storage $numericId");
|
|
|
|
}
|
|
|
|
|
2017-03-17 11:59:25 +03:00
|
|
|
public function deleteStorage($id, $numericId, OutputInterface $output) {
|
2016-10-12 13:59:13 +03:00
|
|
|
$queryBuilder = $this->connection->getQueryBuilder();
|
|
|
|
$queryBuilder->delete('storages')
|
|
|
|
->where($queryBuilder->expr()->eq(
|
|
|
|
'id',
|
|
|
|
$queryBuilder->createNamedParameter($id, IQueryBuilder::PARAM_STR),
|
|
|
|
IQueryBuilder::PARAM_STR)
|
|
|
|
);
|
|
|
|
$output->write("deleting $id [$numericId] ... ");
|
|
|
|
$count = $queryBuilder->execute();
|
2017-03-17 11:59:25 +03:00
|
|
|
$output->writeln("deleted $count storage");
|
2016-10-12 13:59:13 +03:00
|
|
|
$this->deleteFiles($numericId, $output);
|
|
|
|
}
|
|
|
|
|
2017-03-17 11:59:25 +03:00
|
|
|
public function deleteFiles($numericId, OutputInterface $output) {
|
2016-10-12 13:59:13 +03:00
|
|
|
$queryBuilder = $this->connection->getQueryBuilder();
|
|
|
|
$queryBuilder->delete('filecache')
|
|
|
|
->where($queryBuilder->expr()->eq(
|
|
|
|
'storage',
|
|
|
|
$queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR),
|
|
|
|
IQueryBuilder::PARAM_STR)
|
|
|
|
);
|
|
|
|
$output->write("deleting files for storage $numericId ... ");
|
|
|
|
$count = $queryBuilder->execute();
|
2017-03-17 11:59:25 +03:00
|
|
|
$output->writeln("deleted $count files");
|
2016-10-12 13:59:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getRemoteStorages() {
|
|
|
|
$queryBuilder = $this->connection->getQueryBuilder();
|
|
|
|
$queryBuilder->select(['id', 'numeric_id'])
|
|
|
|
->from('storages')
|
|
|
|
->where($queryBuilder->expr()->like(
|
|
|
|
'id',
|
|
|
|
// match all 'shared::' + 32 characters storages
|
2017-03-17 11:59:56 +03:00
|
|
|
$queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::') . str_repeat('_', 32)),
|
2016-10-12 13:59:13 +03:00
|
|
|
IQueryBuilder::PARAM_STR)
|
|
|
|
)
|
|
|
|
->andWhere($queryBuilder->expr()->notLike(
|
|
|
|
'id',
|
|
|
|
// but not the ones starting with a '/', they are for normal shares
|
2017-03-17 11:59:56 +03:00
|
|
|
$queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/') . '%'),
|
2016-10-12 13:59:13 +03:00
|
|
|
IQueryBuilder::PARAM_STR)
|
|
|
|
)->orderBy('numeric_id');
|
|
|
|
$query = $queryBuilder->execute();
|
|
|
|
|
|
|
|
$remoteStorages = [];
|
|
|
|
|
|
|
|
while ($row = $query->fetch()) {
|
|
|
|
$remoteStorages[$row['id']] = $row['numeric_id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $remoteStorages;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRemoteShareIds() {
|
|
|
|
$queryBuilder = $this->connection->getQueryBuilder();
|
2021-01-26 12:26:10 +03:00
|
|
|
$queryBuilder->select(['id', 'share_token', 'owner', 'remote'])
|
2016-10-12 13:59:13 +03:00
|
|
|
->from('share_external');
|
|
|
|
$query = $queryBuilder->execute();
|
|
|
|
|
|
|
|
$remoteShareIds = [];
|
|
|
|
|
|
|
|
while ($row = $query->fetch()) {
|
2021-01-26 12:26:10 +03:00
|
|
|
$cloudId = $this->cloudIdManager->getCloudId($row['owner'], $row['remote']);
|
|
|
|
$remote = $cloudId->getRemote();
|
|
|
|
|
|
|
|
$remoteShareIds[$row['id']] = 'shared::' . md5($row['share_token'] . '@' . $remote);
|
2016-10-12 13:59:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $remoteShareIds;
|
|
|
|
}
|
|
|
|
}
|