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 | * |
||
| 7 | * @license AGPL-3.0 |
||
| 8 | * |
||
| 9 | * This code is free software: you can redistribute it and/or modify |
||
| 10 | * it under the terms of the GNU Affero General Public License, version 3, |
||
| 11 | * as published by the Free Software Foundation. |
||
| 12 | * |
||
| 13 | * This program is distributed in the hope that it will be useful, |
||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 16 | * GNU Affero General Public License for more details. |
||
| 17 | * |
||
| 18 | * You should have received a copy of the GNU Affero General Public License, version 3, |
||
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/> |
||
| 20 | * |
||
| 21 | */ |
||
| 22 | |||
| 23 | namespace OCA\Files_Versions\Command; |
||
| 24 | |||
| 25 | |||
| 26 | use OCP\Files\IRootFolder; |
||
| 27 | use OCP\IUserBackend; |
||
| 28 | use OCP\IUserManager; |
||
| 29 | use Symfony\Component\Console\Command\Command; |
||
| 30 | use Symfony\Component\Console\Input\InputArgument; |
||
| 31 | use Symfony\Component\Console\Input\InputInterface; |
||
| 32 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 33 | |||
| 34 | class CleanUp extends Command { |
||
| 35 | |||
| 36 | /** @var IUserManager */ |
||
| 37 | protected $userManager; |
||
| 38 | |||
| 39 | /** @var IRootFolder */ |
||
| 40 | protected $rootFolder; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param IRootFolder $rootFolder |
||
| 44 | * @param IUserManager $userManager |
||
| 45 | */ |
||
| 46 | function __construct(IRootFolder $rootFolder, IUserManager $userManager) { |
||
| 47 | parent::__construct(); |
||
| 48 | $this->userManager = $userManager; |
||
| 49 | $this->rootFolder = $rootFolder; |
||
| 50 | } |
||
| 51 | |||
| 52 | protected function configure() { |
||
| 53 | $this |
||
| 54 | ->setName('versions:cleanup') |
||
| 55 | ->setDescription('Delete versions') |
||
| 56 | ->addArgument( |
||
| 57 | 'user_id', |
||
| 58 | InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
||
| 59 | 'delete versions of the given user(s), if no user is given all versions will be deleted' |
||
| 60 | ); |
||
| 61 | } |
||
| 62 | |||
| 63 | |||
| 64 | View Code Duplication | protected function execute(InputInterface $input, OutputInterface $output) { |
|
|
0 ignored issues
–
show
|
|||
| 65 | |||
| 66 | $users = $input->getArgument('user_id'); |
||
| 67 | if (!empty($users)) { |
||
| 68 | foreach ($users as $user) { |
||
| 69 | if ($this->userManager->userExists($user)) { |
||
| 70 | $output->writeln("Delete versions of <info>$user</info>"); |
||
| 71 | $this->deleteVersions($user); |
||
| 72 | } else { |
||
| 73 | $output->writeln("<error>Unknown user $user</error>"); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | } else { |
||
| 77 | $output->writeln('Delete all versions'); |
||
| 78 | foreach ($this->userManager->getBackends() as $backend) { |
||
| 79 | $name = get_class($backend); |
||
| 80 | |||
| 81 | if ($backend instanceof IUserBackend) { |
||
| 82 | $name = $backend->getBackendName(); |
||
| 83 | } |
||
| 84 | |||
| 85 | $output->writeln("Delete versions for users on backend <info>$name</info>"); |
||
| 86 | |||
| 87 | $limit = 500; |
||
| 88 | $offset = 0; |
||
| 89 | do { |
||
| 90 | $users = $backend->getUsers('', $limit, $offset); |
||
| 91 | foreach ($users as $user) { |
||
| 92 | $output->writeln(" <info>$user</info>"); |
||
| 93 | $this->deleteVersions($user); |
||
| 94 | } |
||
| 95 | $offset += $limit; |
||
| 96 | } while (count($users) >= $limit); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * delete versions for the given user |
||
| 104 | * |
||
| 105 | * @param string $user |
||
| 106 | */ |
||
| 107 | protected function deleteVersions($user) { |
||
| 108 | \OC_Util::tearDownFS(); |
||
| 109 | \OC_Util::setupFS($user); |
||
| 110 | if ($this->rootFolder->nodeExists('/' . $user . '/files_versions')) { |
||
| 111 | $this->rootFolder->get('/' . $user . '/files_versions')->delete(); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | } |
||
| 116 |
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.