Code Duplication    Length = 61-64 lines in 2 locations

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

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

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

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