DeleteOrphanedFiles.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files\Command;
  24. use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
  25. use Doctrine\DBAL\Platforms\SqlitePlatform;
  26. use OCP\IDBConnection;
  27. use Symfony\Component\Console\Command\Command;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. /**
  31. * Delete all file entries that have no matching entries in the storage table.
  32. */
  33. class DeleteOrphanedFiles extends Command {
  34. const CHUNK_SIZE = 200;
  35. /**
  36. * @var IDBConnection
  37. */
  38. protected $connection;
  39. public function __construct(IDBConnection $connection) {
  40. $this->connection = $connection;
  41. parent::__construct();
  42. }
  43. protected function configure() {
  44. $this
  45. ->setName('files:cleanup')
  46. ->setDescription('cleanup filecache');
  47. }
  48. public function execute(InputInterface $input, OutputInterface $output) {
  49. $deletedEntries = 0;
  50. $query = $this->connection->getQueryBuilder();
  51. $query->select('fc.fileid')
  52. ->from('filecache', 'fc')
  53. ->where($query->expr()->isNull('s.numeric_id'))
  54. ->leftJoin('fc', 'storages', 's', $query->expr()->eq('fc.storage', 's.numeric_id'))
  55. ->setMaxResults(self::CHUNK_SIZE);
  56. $deleteQuery = $this->connection->getQueryBuilder();
  57. $deleteQuery->delete('filecache')
  58. ->where($deleteQuery->expr()->eq('fileid', $deleteQuery->createParameter('objectid')));
  59. $deletedInLastChunk = self::CHUNK_SIZE;
  60. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  61. $deletedInLastChunk = 0;
  62. $result = $query->execute();
  63. while ($row = $result->fetch()) {
  64. $deletedInLastChunk++;
  65. $deletedEntries += $deleteQuery->setParameter('objectid', (int) $row['fileid'])
  66. ->execute();
  67. }
  68. $result->closeCursor();
  69. }
  70. $output->writeln("$deletedEntries orphaned file cache entries deleted");
  71. }
  72. }