Code Duplication    Length = 61-64 lines in 2 locations

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

@@ 660-720 (lines=61) @@
657
	 * @param int $limit
658
	 * @return array
659
	 */
660
	function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) {
661
		// Current synctoken
662
		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?');
663
		$stmt->execute([ $addressBookId ]);
664
		$currentToken = $stmt->fetchColumn(0);
665
666
		if (is_null($currentToken)) return null;
667
668
		$result = [
669
			'syncToken' => $currentToken,
670
			'added'     => [],
671
			'modified'  => [],
672
			'deleted'   => [],
673
		];
674
675
		if ($syncToken) {
676
677
			$query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`";
678
			if ($limit>0) {
679
				$query .= " `LIMIT` " . (int)$limit;
680
			}
681
682
			// Fetching all changes
683
			$stmt = $this->db->prepare($query);
684
			$stmt->execute([$syncToken, $currentToken, $addressBookId]);
685
686
			$changes = [];
687
688
			// This loop ensures that any duplicates are overwritten, only the
689
			// last change on a node is relevant.
690
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
691
692
				$changes[$row['uri']] = $row['operation'];
693
694
			}
695
696
			foreach($changes as $uri => $operation) {
697
698
				switch($operation) {
699
					case 1:
700
						$result['added'][] = $uri;
701
						break;
702
					case 2:
703
						$result['modified'][] = $uri;
704
						break;
705
					case 3:
706
						$result['deleted'][] = $uri;
707
						break;
708
				}
709
710
			}
711
		} else {
712
			// No synctoken supplied, this is the initial sync.
713
			$query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?";
714
			$stmt = $this->db->prepare($query);
715
			$stmt->execute([$addressBookId]);
716
717
			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
718
		}
719
		return $result;
720
	}
721
722
	/**
723
	 * Adds a change record to the addressbookchanges table.

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

@@ 1044-1107 (lines=64) @@
1041
	 * @param int $limit
1042
	 * @return array
1043
	 */
1044
	function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) {
1045
		// Current synctoken
1046
		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?');
1047
		$stmt->execute([ $calendarId ]);
1048
		$currentToken = $stmt->fetchColumn(0);
1049
1050
		if (is_null($currentToken)) {
1051
			return null;
1052
		}
1053
1054
		$result = [
1055
			'syncToken' => $currentToken,
1056
			'added'     => [],
1057
			'modified'  => [],
1058
			'deleted'   => [],
1059
		];
1060
1061
		if ($syncToken) {
1062
1063
			$query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`";
1064
			if ($limit>0) {
1065
				$query.= " `LIMIT` " . (int)$limit;
1066
			}
1067
1068
			// Fetching all changes
1069
			$stmt = $this->db->prepare($query);
1070
			$stmt->execute([$syncToken, $currentToken, $calendarId]);
1071
1072
			$changes = [];
1073
1074
			// This loop ensures that any duplicates are overwritten, only the
1075
			// last change on a node is relevant.
1076
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1077
1078
				$changes[$row['uri']] = $row['operation'];
1079
1080
			}
1081
1082
			foreach($changes as $uri => $operation) {
1083
1084
				switch($operation) {
1085
					case 1 :
1086
						$result['added'][] = $uri;
1087
						break;
1088
					case 2 :
1089
						$result['modified'][] = $uri;
1090
						break;
1091
					case 3 :
1092
						$result['deleted'][] = $uri;
1093
						break;
1094
				}
1095
1096
			}
1097
		} else {
1098
			// No synctoken supplied, this is the initial sync.
1099
			$query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?";
1100
			$stmt = $this->db->prepare($query);
1101
			$stmt->execute([$calendarId]);
1102
1103
			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
1104
		}
1105
		return $result;
1106
1107
	}
1108
1109
	/**
1110
	 * Returns a list of subscriptions for a principal.