nextcloud /
server
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
||
| 4 | * |
||
| 5 | * @author Björn Schießle <[email protected]> |
||
| 6 | * @author Joas Schilling <[email protected]> |
||
| 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 | |||
| 24 | namespace OCA\Files_Trashbin\Command; |
||
| 25 | |||
| 26 | use OCP\Files\IRootFolder; |
||
| 27 | use OCP\IDBConnection; |
||
| 28 | use OCP\IUserBackend; |
||
| 29 | use OCP\IUserManager; |
||
| 30 | use Symfony\Component\Console\Command\Command; |
||
| 31 | use Symfony\Component\Console\Input\InputArgument; |
||
| 32 | use Symfony\Component\Console\Input\InputInterface; |
||
| 33 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 34 | |||
| 35 | class CleanUp extends Command { |
||
| 36 | |||
| 37 | /** @var IUserManager */ |
||
| 38 | protected $userManager; |
||
| 39 | |||
| 40 | /** @var IRootFolder */ |
||
| 41 | protected $rootFolder; |
||
| 42 | |||
| 43 | /** @var \OCP\IDBConnection */ |
||
| 44 | protected $dbConnection; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param IRootFolder $rootFolder |
||
| 48 | * @param IUserManager $userManager |
||
| 49 | * @param IDBConnection $dbConnection |
||
| 50 | */ |
||
| 51 | function __construct(IRootFolder $rootFolder, IUserManager $userManager, IDBConnection $dbConnection) { |
||
| 52 | parent::__construct(); |
||
| 53 | $this->userManager = $userManager; |
||
| 54 | $this->rootFolder = $rootFolder; |
||
| 55 | $this->dbConnection = $dbConnection; |
||
| 56 | } |
||
| 57 | |||
| 58 | protected function configure() { |
||
| 59 | $this |
||
| 60 | ->setName('trashbin:cleanup') |
||
| 61 | ->setDescription('Remove deleted files') |
||
| 62 | ->addArgument( |
||
| 63 | 'user_id', |
||
| 64 | InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
||
| 65 | 'remove deleted files of the given user(s), if no user is given all deleted files will be removed' |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | View Code Duplication | protected function execute(InputInterface $input, OutputInterface $output) { |
|
|
0 ignored issues
–
show
|
|||
| 70 | $users = $input->getArgument('user_id'); |
||
| 71 | if (!empty($users)) { |
||
| 72 | foreach ($users as $user) { |
||
| 73 | if ($this->userManager->userExists($user)) { |
||
| 74 | $output->writeln("Remove deleted files of <info>$user</info>"); |
||
| 75 | $this->removeDeletedFiles($user); |
||
| 76 | } else { |
||
| 77 | $output->writeln("<error>Unknown user $user</error>"); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } else { |
||
| 81 | $output->writeln('Remove all deleted files'); |
||
| 82 | foreach ($this->userManager->getBackends() as $backend) { |
||
| 83 | $name = get_class($backend); |
||
| 84 | if ($backend instanceof IUserBackend) { |
||
| 85 | $name = $backend->getBackendName(); |
||
| 86 | } |
||
| 87 | $output->writeln("Remove deleted files for users on backend <info>$name</info>"); |
||
| 88 | $limit = 500; |
||
| 89 | $offset = 0; |
||
| 90 | do { |
||
| 91 | $users = $backend->getUsers('', $limit, $offset); |
||
| 92 | foreach ($users as $user) { |
||
| 93 | $output->writeln(" <info>$user</info>"); |
||
| 94 | $this->removeDeletedFiles($user); |
||
| 95 | } |
||
| 96 | $offset += $limit; |
||
| 97 | } while (count($users) >= $limit); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * remove deleted files for the given user |
||
| 104 | * |
||
| 105 | * @param string $uid |
||
| 106 | */ |
||
| 107 | protected function removeDeletedFiles($uid) { |
||
| 108 | \OC_Util::tearDownFS(); |
||
| 109 | \OC_Util::setupFS($uid); |
||
| 110 | if ($this->rootFolder->nodeExists('/' . $uid . '/files_trashbin')) { |
||
| 111 | $this->rootFolder->get('/' . $uid . '/files_trashbin')->delete(); |
||
| 112 | $query = $this->dbConnection->getQueryBuilder(); |
||
| 113 | $query->delete('files_trash') |
||
| 114 | ->where($query->expr()->eq('user', $query->createParameter('uid'))) |
||
| 115 | ->setParameter('uid', $uid); |
||
| 116 | $query->execute(); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | } |
||
| 121 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.