scan.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Thomas Müller <thomas.mueller@tmit.eu>
  4. * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later.
  7. * See the COPYING-README file.
  8. */
  9. namespace OCA\Files\Command;
  10. use OC\ForbiddenException;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class Scan extends Command {
  17. /**
  18. * @var \OC\User\Manager $userManager
  19. */
  20. private $userManager;
  21. public function __construct(\OC\User\Manager $userManager) {
  22. $this->userManager = $userManager;
  23. parent::__construct();
  24. }
  25. protected function configure() {
  26. $this
  27. ->setName('files:scan')
  28. ->setDescription('rescan filesystem')
  29. ->addArgument(
  30. 'user_id',
  31. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  32. 'will rescan all files of the given user(s)'
  33. )
  34. ->addOption(
  35. 'all',
  36. null,
  37. InputOption::VALUE_NONE,
  38. 'will rescan all files of all known users'
  39. );
  40. }
  41. protected function scanFiles($user, OutputInterface $output) {
  42. $scanner = new \OC\Files\Utils\Scanner($user);
  43. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
  44. $output->writeln("Scanning <info>$path</info>");
  45. });
  46. $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
  47. $output->writeln("Scanning <info>$path</info>");
  48. });
  49. try {
  50. $scanner->scan('');
  51. } catch (ForbiddenException $e) {
  52. $output->writeln("<error>Home storage for user $user not writable</error>");
  53. $output->writeln("Make sure you're running the scan command only as the user the web server runs as");
  54. }
  55. }
  56. protected function execute(InputInterface $input, OutputInterface $output) {
  57. if ($input->getOption('all')) {
  58. $users = $this->userManager->search('');
  59. } else {
  60. $users = $input->getArgument('user_id');
  61. }
  62. if (count($users) === 0) {
  63. $output->writeln("<error>Please specify the user id to scan or \"--all\" to scan for all users</error>");
  64. return;
  65. }
  66. foreach ($users as $user) {
  67. if (is_object($user)) {
  68. $user = $user->getUID();
  69. }
  70. $this->scanFiles($user, $output);
  71. }
  72. }
  73. }