You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeleteOrphanedFiles.php 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 OCP\IDBConnection;
  25. use Symfony\Component\Console\Command\Command;
  26. use Symfony\Component\Console\Input\InputInterface;
  27. use Symfony\Component\Console\Output\OutputInterface;
  28. /**
  29. * Delete all file entries that have no matching entries in the storage table.
  30. */
  31. class DeleteOrphanedFiles extends Command {
  32. const CHUNK_SIZE = 200;
  33. /**
  34. * @var IDBConnection
  35. */
  36. protected $connection;
  37. public function __construct(IDBConnection $connection) {
  38. $this->connection = $connection;
  39. parent::__construct();
  40. }
  41. protected function configure() {
  42. $this
  43. ->setName('files:cleanup')
  44. ->setDescription('cleanup filecache');
  45. }
  46. public function execute(InputInterface $input, OutputInterface $output) {
  47. $deletedEntries = 0;
  48. $query = $this->connection->getQueryBuilder();
  49. $query->select('fc.fileid')
  50. ->from('filecache', 'fc')
  51. ->where($query->expr()->isNull('s.numeric_id'))
  52. ->leftJoin('fc', 'storages', 's', $query->expr()->eq('fc.storage', 's.numeric_id'))
  53. ->setMaxResults(self::CHUNK_SIZE);
  54. $deleteQuery = $this->connection->getQueryBuilder();
  55. $deleteQuery->delete('filecache')
  56. ->where($deleteQuery->expr()->eq('fileid', $deleteQuery->createParameter('objectid')));
  57. $deletedInLastChunk = self::CHUNK_SIZE;
  58. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  59. $deletedInLastChunk = 0;
  60. $result = $query->execute();
  61. while ($row = $result->fetch()) {
  62. $deletedInLastChunk++;
  63. $deletedEntries += $deleteQuery->setParameter('objectid', (int) $row['fileid'])
  64. ->execute();
  65. }
  66. $result->closeCursor();
  67. }
  68. $output->writeln("$deletedEntries orphaned file cache entries deleted");
  69. }
  70. }