| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Copyright (c) 2019 Joas Schilling <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license GNU AGPL version 3 or any later version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * it under the terms of the GNU Affero General Public License as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * published by the Free Software Foundation, either version 3 of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * License, or (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | namespace OCA\DAV\Migration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use OCA\DAV\CalDAV\CalDavBackend; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use OCP\DB\QueryBuilder\IQueryBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use OCP\IDBConnection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use OCP\Migration\IOutput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use OCP\Migration\IRepairStep; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | class RemoveOrphanEventsAndContacts implements IRepairStep { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	/** @var IDBConnection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	private $connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	public function __construct(IDBConnection $connection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		$this->connection = $connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	public function getName(): string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		return 'Clean up orphan event and contact data'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	public function run(IOutput $output) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$orphanItems = $this->removeOrphanChildren('calendarobjects', 'calendars',  'calendarid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$output->info(sprintf('%d events without a calendar have been cleaned up', $orphanItems)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		$orphanItems = $this->removeOrphanChildren('calendarobjects_props', 'calendarobjects',  'objectid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$output->info(sprintf('%d properties without an events have been cleaned up', $orphanItems)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		$orphanItems = $this->removeOrphanChildren('calendarchanges', 'calendars',  'calendarid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$output->info(sprintf('%d changes without a calendar have been cleaned up', $orphanItems)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$orphanItems = $this->removeOrphanChildren('cards', 'addressbooks',  'addressbookid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$output->info(sprintf('%d contacts without an addressbook have been cleaned up', $orphanItems)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$orphanItems = $this->removeOrphanChildren('cards_properties', 'cards',  'cardid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$output->info(sprintf('%d properties without a contact have been cleaned up', $orphanItems)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$orphanItems = $this->removeOrphanChildren('addressbookchanges', 'addressbooks',  'addressbookid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$output->info(sprintf('%d changes without an addressbook have been cleaned up', $orphanItems)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 	protected function removeOrphanChildren($childTable, $parentTable, $parentId): int { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 		$qb = $this->connection->getQueryBuilder(); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 		$qb->select('c.id') | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 			->from($childTable, 'c') | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 			->leftJoin('c', $parentTable, 'p', $qb->expr()->eq('c.' . $parentId, 'p.id')) | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 			->where($qb->expr()->isNull('p.id')); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 		$result = $qb->execute(); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 		$orphanItems = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 		while ($row = $result->fetch()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 			$orphanItems[] = (int) $row['id']; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 		$result->closeCursor(); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 		if (!empty($orphanItems)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 			$qb->delete($childTable) | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 				->where($qb->expr()->in('id', $qb->createParameter('ids'))); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 			$orphanItemsBatch = array_chunk($orphanItems, 200); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 			foreach ($orphanItemsBatch as $items) { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 				$qb->setParameter('ids', $items, IQueryBuilder::PARAM_INT_ARRAY); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 				$qb->execute(); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 		return count($orphanItems); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  |