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

@@ 1110-1173 (lines=64) @@
1107
	 * @param int $limit
1108
	 * @return array
1109
	 */
1110
	function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) {
1111
		// Current synctoken
1112
		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?');
1113
		$stmt->execute([ $calendarId ]);
1114
		$currentToken = $stmt->fetchColumn(0);
1115
1116
		if (is_null($currentToken)) {
1117
			return null;
1118
		}
1119
1120
		$result = [
1121
			'syncToken' => $currentToken,
1122
			'added'     => [],
1123
			'modified'  => [],
1124
			'deleted'   => [],
1125
		];
1126
1127
		if ($syncToken) {
1128
1129
			$query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`";
1130
			if ($limit>0) {
1131
				$query.= " `LIMIT` " . (int)$limit;
1132
			}
1133
1134
			// Fetching all changes
1135
			$stmt = $this->db->prepare($query);
1136
			$stmt->execute([$syncToken, $currentToken, $calendarId]);
1137
1138
			$changes = [];
1139
1140
			// This loop ensures that any duplicates are overwritten, only the
1141
			// last change on a node is relevant.
1142
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1143
1144
				$changes[$row['uri']] = $row['operation'];
1145
1146
			}
1147
1148
			foreach($changes as $uri => $operation) {
1149
1150
				switch($operation) {
1151
					case 1 :
1152
						$result['added'][] = $uri;
1153
						break;
1154
					case 2 :
1155
						$result['modified'][] = $uri;
1156
						break;
1157
					case 3 :
1158
						$result['deleted'][] = $uri;
1159
						break;
1160
				}
1161
1162
			}
1163
		} else {
1164
			// No synctoken supplied, this is the initial sync.
1165
			$query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?";
1166
			$stmt = $this->db->prepare($query);
1167
			$stmt->execute([$calendarId]);
1168
1169
			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
1170
		}
1171
		return $result;
1172
1173
	}
1174
1175
	/**
1176
	 * Returns a list of subscriptions for a principal.