@@ -38,111 +38,111 @@ |
||
38 | 38 | |
39 | 39 | class CleanUp extends Command { |
40 | 40 | |
41 | - /** @var IUserManager */ |
|
42 | - protected $userManager; |
|
41 | + /** @var IUserManager */ |
|
42 | + protected $userManager; |
|
43 | 43 | |
44 | - /** @var IRootFolder */ |
|
45 | - protected $rootFolder; |
|
44 | + /** @var IRootFolder */ |
|
45 | + protected $rootFolder; |
|
46 | 46 | |
47 | - /** @var \OCP\IDBConnection */ |
|
48 | - protected $dbConnection; |
|
47 | + /** @var \OCP\IDBConnection */ |
|
48 | + protected $dbConnection; |
|
49 | 49 | |
50 | - /** |
|
51 | - * @param IRootFolder $rootFolder |
|
52 | - * @param IUserManager $userManager |
|
53 | - * @param IDBConnection $dbConnection |
|
54 | - */ |
|
55 | - public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IDBConnection $dbConnection) { |
|
56 | - parent::__construct(); |
|
57 | - $this->userManager = $userManager; |
|
58 | - $this->rootFolder = $rootFolder; |
|
59 | - $this->dbConnection = $dbConnection; |
|
60 | - } |
|
50 | + /** |
|
51 | + * @param IRootFolder $rootFolder |
|
52 | + * @param IUserManager $userManager |
|
53 | + * @param IDBConnection $dbConnection |
|
54 | + */ |
|
55 | + public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IDBConnection $dbConnection) { |
|
56 | + parent::__construct(); |
|
57 | + $this->userManager = $userManager; |
|
58 | + $this->rootFolder = $rootFolder; |
|
59 | + $this->dbConnection = $dbConnection; |
|
60 | + } |
|
61 | 61 | |
62 | - protected function configure() { |
|
63 | - $this |
|
64 | - ->setName('trashbin:cleanup') |
|
65 | - ->setDescription('Remove deleted files') |
|
66 | - ->addArgument( |
|
67 | - 'user_id', |
|
68 | - InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
|
69 | - 'remove deleted files of the given user(s)' |
|
70 | - ) |
|
71 | - ->addOption( |
|
72 | - 'all-users', |
|
73 | - null, |
|
74 | - InputOption::VALUE_NONE, |
|
75 | - 'run action on all users' |
|
76 | - ); |
|
77 | - } |
|
62 | + protected function configure() { |
|
63 | + $this |
|
64 | + ->setName('trashbin:cleanup') |
|
65 | + ->setDescription('Remove deleted files') |
|
66 | + ->addArgument( |
|
67 | + 'user_id', |
|
68 | + InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
|
69 | + 'remove deleted files of the given user(s)' |
|
70 | + ) |
|
71 | + ->addOption( |
|
72 | + 'all-users', |
|
73 | + null, |
|
74 | + InputOption::VALUE_NONE, |
|
75 | + 'run action on all users' |
|
76 | + ); |
|
77 | + } |
|
78 | 78 | |
79 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
80 | - $users = $input->getArgument('user_id'); |
|
81 | - $verbose = $input->getOption('verbose'); |
|
82 | - if ((!empty($users)) and ($input->getOption('all-users'))) { |
|
83 | - throw new InvalidOptionException('Either specify a user_id or --all-users'); |
|
84 | - } elseif (!empty($users)) { |
|
85 | - foreach ($users as $user) { |
|
86 | - if ($this->userManager->userExists($user)) { |
|
87 | - $output->writeln("Remove deleted files of <info>$user</info>"); |
|
88 | - $this->removeDeletedFiles($user, $output, $verbose); |
|
89 | - } else { |
|
90 | - $output->writeln("<error>Unknown user $user</error>"); |
|
91 | - return 1; |
|
92 | - } |
|
93 | - } |
|
94 | - } elseif ($input->getOption('all-users')) { |
|
95 | - $output->writeln('Remove deleted files for all users'); |
|
96 | - foreach ($this->userManager->getBackends() as $backend) { |
|
97 | - $name = get_class($backend); |
|
98 | - if ($backend instanceof IUserBackend) { |
|
99 | - $name = $backend->getBackendName(); |
|
100 | - } |
|
101 | - $output->writeln("Remove deleted files for users on backend <info>$name</info>"); |
|
102 | - $limit = 500; |
|
103 | - $offset = 0; |
|
104 | - do { |
|
105 | - $users = $backend->getUsers('', $limit, $offset); |
|
106 | - foreach ($users as $user) { |
|
107 | - $output->writeln(" <info>$user</info>"); |
|
108 | - $this->removeDeletedFiles($user, $output, $verbose); |
|
109 | - } |
|
110 | - $offset += $limit; |
|
111 | - } while (count($users) >= $limit); |
|
112 | - } |
|
113 | - } else { |
|
114 | - throw new InvalidOptionException('Either specify a user_id or --all-users'); |
|
115 | - } |
|
116 | - return 0; |
|
117 | - } |
|
79 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
80 | + $users = $input->getArgument('user_id'); |
|
81 | + $verbose = $input->getOption('verbose'); |
|
82 | + if ((!empty($users)) and ($input->getOption('all-users'))) { |
|
83 | + throw new InvalidOptionException('Either specify a user_id or --all-users'); |
|
84 | + } elseif (!empty($users)) { |
|
85 | + foreach ($users as $user) { |
|
86 | + if ($this->userManager->userExists($user)) { |
|
87 | + $output->writeln("Remove deleted files of <info>$user</info>"); |
|
88 | + $this->removeDeletedFiles($user, $output, $verbose); |
|
89 | + } else { |
|
90 | + $output->writeln("<error>Unknown user $user</error>"); |
|
91 | + return 1; |
|
92 | + } |
|
93 | + } |
|
94 | + } elseif ($input->getOption('all-users')) { |
|
95 | + $output->writeln('Remove deleted files for all users'); |
|
96 | + foreach ($this->userManager->getBackends() as $backend) { |
|
97 | + $name = get_class($backend); |
|
98 | + if ($backend instanceof IUserBackend) { |
|
99 | + $name = $backend->getBackendName(); |
|
100 | + } |
|
101 | + $output->writeln("Remove deleted files for users on backend <info>$name</info>"); |
|
102 | + $limit = 500; |
|
103 | + $offset = 0; |
|
104 | + do { |
|
105 | + $users = $backend->getUsers('', $limit, $offset); |
|
106 | + foreach ($users as $user) { |
|
107 | + $output->writeln(" <info>$user</info>"); |
|
108 | + $this->removeDeletedFiles($user, $output, $verbose); |
|
109 | + } |
|
110 | + $offset += $limit; |
|
111 | + } while (count($users) >= $limit); |
|
112 | + } |
|
113 | + } else { |
|
114 | + throw new InvalidOptionException('Either specify a user_id or --all-users'); |
|
115 | + } |
|
116 | + return 0; |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * remove deleted files for the given user |
|
121 | - */ |
|
122 | - protected function removeDeletedFiles(string $uid, OutputInterface $output, bool $verbose): void { |
|
123 | - \OC_Util::tearDownFS(); |
|
124 | - \OC_Util::setupFS($uid); |
|
125 | - $path = '/' . $uid . '/files_trashbin'; |
|
126 | - if ($this->rootFolder->nodeExists($path)) { |
|
127 | - $node = $this->rootFolder->get($path); |
|
119 | + /** |
|
120 | + * remove deleted files for the given user |
|
121 | + */ |
|
122 | + protected function removeDeletedFiles(string $uid, OutputInterface $output, bool $verbose): void { |
|
123 | + \OC_Util::tearDownFS(); |
|
124 | + \OC_Util::setupFS($uid); |
|
125 | + $path = '/' . $uid . '/files_trashbin'; |
|
126 | + if ($this->rootFolder->nodeExists($path)) { |
|
127 | + $node = $this->rootFolder->get($path); |
|
128 | 128 | |
129 | - if ($verbose) { |
|
130 | - $output->writeln("Deleting <info>" . \OC_Helper::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>."); |
|
131 | - } |
|
132 | - $node->delete(); |
|
133 | - if ($this->rootFolder->nodeExists($path)) { |
|
134 | - $output->writeln("<error>Trash folder sill exists after attempting to delete it</error>"); |
|
135 | - return; |
|
136 | - } |
|
137 | - $query = $this->dbConnection->getQueryBuilder(); |
|
138 | - $query->delete('files_trash') |
|
139 | - ->where($query->expr()->eq('user', $query->createParameter('uid'))) |
|
140 | - ->setParameter('uid', $uid); |
|
141 | - $query->execute(); |
|
142 | - } else { |
|
143 | - if ($verbose) { |
|
144 | - $output->writeln("No trash found for <info>$uid</info>"); |
|
145 | - } |
|
146 | - } |
|
147 | - } |
|
129 | + if ($verbose) { |
|
130 | + $output->writeln("Deleting <info>" . \OC_Helper::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>."); |
|
131 | + } |
|
132 | + $node->delete(); |
|
133 | + if ($this->rootFolder->nodeExists($path)) { |
|
134 | + $output->writeln("<error>Trash folder sill exists after attempting to delete it</error>"); |
|
135 | + return; |
|
136 | + } |
|
137 | + $query = $this->dbConnection->getQueryBuilder(); |
|
138 | + $query->delete('files_trash') |
|
139 | + ->where($query->expr()->eq('user', $query->createParameter('uid'))) |
|
140 | + ->setParameter('uid', $uid); |
|
141 | + $query->execute(); |
|
142 | + } else { |
|
143 | + if ($verbose) { |
|
144 | + $output->writeln("No trash found for <info>$uid</info>"); |
|
145 | + } |
|
146 | + } |
|
147 | + } |
|
148 | 148 | } |
@@ -122,12 +122,12 @@ |
||
122 | 122 | protected function removeDeletedFiles(string $uid, OutputInterface $output, bool $verbose): void { |
123 | 123 | \OC_Util::tearDownFS(); |
124 | 124 | \OC_Util::setupFS($uid); |
125 | - $path = '/' . $uid . '/files_trashbin'; |
|
125 | + $path = '/'.$uid.'/files_trashbin'; |
|
126 | 126 | if ($this->rootFolder->nodeExists($path)) { |
127 | 127 | $node = $this->rootFolder->get($path); |
128 | 128 | |
129 | 129 | if ($verbose) { |
130 | - $output->writeln("Deleting <info>" . \OC_Helper::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>."); |
|
130 | + $output->writeln("Deleting <info>".\OC_Helper::humanFileSize($node->getSize())."</info> in trash for <info>$uid</info>."); |
|
131 | 131 | } |
132 | 132 | $node->delete(); |
133 | 133 | if ($this->rootFolder->nodeExists($path)) { |