Code Duplication    Length = 61-64 lines in 2 locations

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

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

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

@@ 701-761 (lines=61) @@
698
	 * @param int $limit
699
	 * @return array
700
	 */
701
	function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) {
702
		// Current synctoken
703
		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?');
704
		$stmt->execute([ $addressBookId ]);
705
		$currentToken = $stmt->fetchColumn(0);
706
707
		if (is_null($currentToken)) return null;
708
709
		$result = [
710
			'syncToken' => $currentToken,
711
			'added'     => [],
712
			'modified'  => [],
713
			'deleted'   => [],
714
		];
715
716
		if ($syncToken) {
717
718
			$query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`";
719
			if ($limit>0) {
720
				$query .= " `LIMIT` " . (int)$limit;
721
			}
722
723
			// Fetching all changes
724
			$stmt = $this->db->prepare($query);
725
			$stmt->execute([$syncToken, $currentToken, $addressBookId]);
726
727
			$changes = [];
728
729
			// This loop ensures that any duplicates are overwritten, only the
730
			// last change on a node is relevant.
731
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
732
733
				$changes[$row['uri']] = $row['operation'];
734
735
			}
736
737
			foreach($changes as $uri => $operation) {
738
739
				switch($operation) {
740
					case 1:
741
						$result['added'][] = $uri;
742
						break;
743
					case 2:
744
						$result['modified'][] = $uri;
745
						break;
746
					case 3:
747
						$result['deleted'][] = $uri;
748
						break;
749
				}
750
751
			}
752
		} else {
753
			// No synctoken supplied, this is the initial sync.
754
			$query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?";
755
			$stmt = $this->db->prepare($query);
756
			$stmt->execute([$addressBookId]);
757
758
			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
759
		}
760
		return $result;
761
	}
762
763
	/**
764
	 * Adds a change record to the addressbookchanges table.