Code Duplication    Length = 61-64 lines in 2 locations

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

@@ 1232-1295 (lines=64) @@
1229
	 * @param int $limit
1230
	 * @return array
1231
	 */
1232
	function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) {
1233
		// Current synctoken
1234
		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?');
1235
		$stmt->execute([ $calendarId ]);
1236
		$currentToken = $stmt->fetchColumn(0);
1237
1238
		if (is_null($currentToken)) {
1239
			return null;
1240
		}
1241
1242
		$result = [
1243
			'syncToken' => $currentToken,
1244
			'added'     => [],
1245
			'modified'  => [],
1246
			'deleted'   => [],
1247
		];
1248
1249
		if ($syncToken) {
1250
1251
			$query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`";
1252
			if ($limit>0) {
1253
				$query.= " `LIMIT` " . (int)$limit;
1254
			}
1255
1256
			// Fetching all changes
1257
			$stmt = $this->db->prepare($query);
1258
			$stmt->execute([$syncToken, $currentToken, $calendarId]);
1259
1260
			$changes = [];
1261
1262
			// This loop ensures that any duplicates are overwritten, only the
1263
			// last change on a node is relevant.
1264
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1265
1266
				$changes[$row['uri']] = $row['operation'];
1267
1268
			}
1269
1270
			foreach($changes as $uri => $operation) {
1271
1272
				switch($operation) {
1273
					case 1 :
1274
						$result['added'][] = $uri;
1275
						break;
1276
					case 2 :
1277
						$result['modified'][] = $uri;
1278
						break;
1279
					case 3 :
1280
						$result['deleted'][] = $uri;
1281
						break;
1282
				}
1283
1284
			}
1285
		} else {
1286
			// No synctoken supplied, this is the initial sync.
1287
			$query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?";
1288
			$stmt = $this->db->prepare($query);
1289
			$stmt->execute([$calendarId]);
1290
1291
			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
1292
		}
1293
		return $result;
1294
1295
	}
1296
1297
	/**
1298
	 * Returns a list of subscriptions for a principal.

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

@@ 742-802 (lines=61) @@
739
	 * @param int $limit
740
	 * @return array
741
	 */
742
	function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) {
743
		// Current synctoken
744
		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?');
745
		$stmt->execute([ $addressBookId ]);
746
		$currentToken = $stmt->fetchColumn(0);
747
748
		if (is_null($currentToken)) return null;
749
750
		$result = [
751
			'syncToken' => $currentToken,
752
			'added'     => [],
753
			'modified'  => [],
754
			'deleted'   => [],
755
		];
756
757
		if ($syncToken) {
758
759
			$query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`";
760
			if ($limit>0) {
761
				$query .= " `LIMIT` " . (int)$limit;
762
			}
763
764
			// Fetching all changes
765
			$stmt = $this->db->prepare($query);
766
			$stmt->execute([$syncToken, $currentToken, $addressBookId]);
767
768
			$changes = [];
769
770
			// This loop ensures that any duplicates are overwritten, only the
771
			// last change on a node is relevant.
772
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
773
774
				$changes[$row['uri']] = $row['operation'];
775
776
			}
777
778
			foreach($changes as $uri => $operation) {
779
780
				switch($operation) {
781
					case 1:
782
						$result['added'][] = $uri;
783
						break;
784
					case 2:
785
						$result['modified'][] = $uri;
786
						break;
787
					case 3:
788
						$result['deleted'][] = $uri;
789
						break;
790
				}
791
792
			}
793
		} else {
794
			// No synctoken supplied, this is the initial sync.
795
			$query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?";
796
			$stmt = $this->db->prepare($query);
797
			$stmt->execute([$addressBookId]);
798
799
			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
800
		}
801
		return $result;
802
	}
803
804
	/**
805
	 * Adds a change record to the addressbookchanges table.