Code Duplication    Length = 61-64 lines in 2 locations

apps/dav/lib/CardDAV/CardDavBackend.php 1 location

@@ 780-840 (lines=61) @@
777
	 * @param int $limit
778
	 * @return array
779
	 */
780
	function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) {
781
		// Current synctoken
782
		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?');
783
		$stmt->execute([ $addressBookId ]);
784
		$currentToken = $stmt->fetchColumn(0);
785
786
		if (is_null($currentToken)) return null;
787
788
		$result = [
789
			'syncToken' => $currentToken,
790
			'added'     => [],
791
			'modified'  => [],
792
			'deleted'   => [],
793
		];
794
795
		if ($syncToken) {
796
797
			$query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`";
798
			if ($limit>0) {
799
				$query .= " `LIMIT` " . (int)$limit;
800
			}
801
802
			// Fetching all changes
803
			$stmt = $this->db->prepare($query);
804
			$stmt->execute([$syncToken, $currentToken, $addressBookId]);
805
806
			$changes = [];
807
808
			// This loop ensures that any duplicates are overwritten, only the
809
			// last change on a node is relevant.
810
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
811
812
				$changes[$row['uri']] = $row['operation'];
813
814
			}
815
816
			foreach($changes as $uri => $operation) {
817
818
				switch($operation) {
819
					case 1:
820
						$result['added'][] = $uri;
821
						break;
822
					case 2:
823
						$result['modified'][] = $uri;
824
						break;
825
					case 3:
826
						$result['deleted'][] = $uri;
827
						break;
828
				}
829
830
			}
831
		} else {
832
			// No synctoken supplied, this is the initial sync.
833
			$query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?";
834
			$stmt = $this->db->prepare($query);
835
			$stmt->execute([$addressBookId]);
836
837
			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
838
		}
839
		return $result;
840
	}
841
842
	/**
843
	 * Adds a change record to the addressbookchanges table.

apps/dav/lib/CalDAV/CalDavBackend.php 1 location

@@ 1440-1503 (lines=64) @@
1437
	 * @param int $limit
1438
	 * @return array
1439
	 */
1440
	function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) {
1441
		// Current synctoken
1442
		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?');
1443
		$stmt->execute([ $calendarId ]);
1444
		$currentToken = $stmt->fetchColumn(0);
1445
1446
		if (is_null($currentToken)) {
1447
			return null;
1448
		}
1449
1450
		$result = [
1451
			'syncToken' => $currentToken,
1452
			'added'     => [],
1453
			'modified'  => [],
1454
			'deleted'   => [],
1455
		];
1456
1457
		if ($syncToken) {
1458
1459
			$query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`";
1460
			if ($limit>0) {
1461
				$query.= " LIMIT " . (int)$limit;
1462
			}
1463
1464
			// Fetching all changes
1465
			$stmt = $this->db->prepare($query);
1466
			$stmt->execute([$syncToken, $currentToken, $calendarId]);
1467
1468
			$changes = [];
1469
1470
			// This loop ensures that any duplicates are overwritten, only the
1471
			// last change on a node is relevant.
1472
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1473
1474
				$changes[$row['uri']] = $row['operation'];
1475
1476
			}
1477
1478
			foreach($changes as $uri => $operation) {
1479
1480
				switch($operation) {
1481
					case 1 :
1482
						$result['added'][] = $uri;
1483
						break;
1484
					case 2 :
1485
						$result['modified'][] = $uri;
1486
						break;
1487
					case 3 :
1488
						$result['deleted'][] = $uri;
1489
						break;
1490
				}
1491
1492
			}
1493
		} else {
1494
			// No synctoken supplied, this is the initial sync.
1495
			$query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?";
1496
			$stmt = $this->db->prepare($query);
1497
			$stmt->execute([$calendarId]);
1498
1499
			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
1500
		}
1501
		return $result;
1502
1503
	}
1504
1505
	/**
1506
	 * Returns a list of subscriptions for a principal.