| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Circles - Bring cloud-users closer together. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * This file is licensed under the Affero General Public License version 3 or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * later. See the COPYING file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @author Maxence Lange <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @copyright 2017 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @license GNU AGPL version 3 or any later version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * it under the terms of the GNU Affero General Public License as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * published by the Free Software Foundation, either version 3 of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * License, or (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | namespace OCA\Circles\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | use OC\Core\Command\Base; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | use OCA\Circles\Exceptions\MigrationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | use OCA\Circles\Exceptions\RequestBuilderException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | use OCA\Circles\Service\ConfigService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | use OCA\Circles\Service\SyncService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  * Class CirclesSync | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  * @package OCA\Circles\Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | class CirclesSync extends Base { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	/** @var SyncService */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	private $syncService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	/** @var ConfigService */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	private $configService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * CirclesSync constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * @param SyncService $syncService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * @param ConfigService $configService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	public function __construct(SyncService $syncService, ConfigService $configService) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$this->syncService = $syncService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$this->configService = $configService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	protected function configure() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		parent::configure(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		$this->setName('circles:sync') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			 ->setDescription('Sync Circles and Members') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			 ->addOption('migration', '', InputOption::VALUE_NONE, 'Migrate from Circles 0.21.0') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			 ->addOption('force', '', InputOption::VALUE_NONE, 'Force migration') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			 ->addOption('force-run', '', InputOption::VALUE_NONE, 'Force migration run') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			 ->addOption('apps', '', InputOption::VALUE_NONE, 'Sync Apps') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			 ->addOption('users', '', InputOption::VALUE_NONE, 'Sync Nextcloud Users') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			 ->addOption('groups', '', InputOption::VALUE_NONE, 'Sync Nextcloud Groups') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			 ->addOption('contacts', '', InputOption::VALUE_NONE, 'Sync Contacts') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			 ->addOption('remotes', '', InputOption::VALUE_NONE, 'Sync Remotes') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			 ->addOption('global-scale', '', InputOption::VALUE_NONE, 'Sync GlobalScale'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * @throws MigrationException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * @throws RequestBuilderException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | 	protected function execute(InputInterface $input, OutputInterface $output): int { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 		$this->syncService->setOccOutput($output); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  | 		if ($input->getOption('migration')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  | 			if ($input->getOption('force-run')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  | 				$this->configService->setAppValue(ConfigService::MIGRATION_RUN, '0'); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  | 			$this->syncService->migration($input->getOption('force')); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  | 			$output->writeln(''); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  | 			$output->writeln('Migration done'); | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  | 			return 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  | 		$sync = $this->filterSync($input); | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  | 		$this->syncService->sync($sync); | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  | 		$output->writeln(''); | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  | 		$output->writeln('Sync done'); | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  | 		return 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	private function filterSync(InputInterface $input): int { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		$sync = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		if ($input->getOption('apps')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 			$sync += SyncService::SYNC_APPS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		if ($input->getOption('users')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			$sync += SyncService::SYNC_USERS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		if ($input->getOption('groups')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			$sync += SyncService::SYNC_GROUPS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		if ($input->getOption('global-scale')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 			$sync += SyncService::SYNC_GLOBALSCALE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		if ($input->getOption('remotes')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 			$sync += SyncService::SYNC_REMOTES; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		if ($input->getOption('contacts')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			$sync += SyncService::SYNC_CONTACTS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		if ($sync === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 			$sync = SyncService::SYNC_ALL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		return $sync; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |  |