Code Duplication    Length = 61-64 lines in 2 locations

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

@@ 1263-1326 (lines=64) @@
1260
	 * @param int $limit
1261
	 * @return array
1262
	 */
1263
	function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) {
1264
		// Current synctoken
1265
		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?');
1266
		$stmt->execute([ $calendarId ]);
1267
		$currentToken = $stmt->fetchColumn(0);
1268
1269
		if (is_null($currentToken)) {
1270
			return null;
1271
		}
1272
1273
		$result = [
1274
			'syncToken' => $currentToken,
1275
			'added'     => [],
1276
			'modified'  => [],
1277
			'deleted'   => [],
1278
		];
1279
1280
		if ($syncToken) {
1281
1282
			$query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`";
1283
			if ($limit>0) {
1284
				$query.= " `LIMIT` " . (int)$limit;
1285
			}
1286
1287
			// Fetching all changes
1288
			$stmt = $this->db->prepare($query);
1289
			$stmt->execute([$syncToken, $currentToken, $calendarId]);
1290
1291
			$changes = [];
1292
1293
			// This loop ensures that any duplicates are overwritten, only the
1294
			// last change on a node is relevant.
1295
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1296
1297
				$changes[$row['uri']] = $row['operation'];
1298
1299
			}
1300
1301
			foreach($changes as $uri => $operation) {
1302
1303
				switch($operation) {
1304
					case 1 :
1305
						$result['added'][] = $uri;
1306
						break;
1307
					case 2 :
1308
						$result['modified'][] = $uri;
1309
						break;
1310
					case 3 :
1311
						$result['deleted'][] = $uri;
1312
						break;
1313
				}
1314
1315
			}
1316
		} else {
1317
			// No synctoken supplied, this is the initial sync.
1318
			$query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?";
1319
			$stmt = $this->db->prepare($query);
1320
			$stmt->execute([$calendarId]);
1321
1322
			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
1323
		}
1324
		return $result;
1325
1326
	}
1327
1328
	/**
1329
	 * Returns a list of subscriptions for a principal.

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

@@ 772-832 (lines=61) @@
769
	 * @param int $limit
770
	 * @return array
771
	 */
772
	function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) {
773
		// Current synctoken
774
		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?');
775
		$stmt->execute([ $addressBookId ]);
776
		$currentToken = $stmt->fetchColumn(0);
777
778
		if (is_null($currentToken)) return null;
779
780
		$result = [
781
			'syncToken' => $currentToken,
782
			'added'     => [],
783
			'modified'  => [],
784
			'deleted'   => [],
785
		];
786
787
		if ($syncToken) {
788
789
			$query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`";
790
			if ($limit>0) {
791
				$query .= " `LIMIT` " . (int)$limit;
792
			}
793
794
			// Fetching all changes
795
			$stmt = $this->db->prepare($query);
796
			$stmt->execute([$syncToken, $currentToken, $addressBookId]);
797
798
			$changes = [];
799
800
			// This loop ensures that any duplicates are overwritten, only the
801
			// last change on a node is relevant.
802
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
803
804
				$changes[$row['uri']] = $row['operation'];
805
806
			}
807
808
			foreach($changes as $uri => $operation) {
809
810
				switch($operation) {
811
					case 1:
812
						$result['added'][] = $uri;
813
						break;
814
					case 2:
815
						$result['modified'][] = $uri;
816
						break;
817
					case 3:
818
						$result['deleted'][] = $uri;
819
						break;
820
				}
821
822
			}
823
		} else {
824
			// No synctoken supplied, this is the initial sync.
825
			$query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?";
826
			$stmt = $this->db->prepare($query);
827
			$stmt->execute([$addressBookId]);
828
829
			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
830
		}
831
		return $result;
832
	}
833
834
	/**
835
	 * Adds a change record to the addressbookchanges table.