Code Duplication    Length = 61-64 lines in 2 locations

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

@@ 948-1011 (lines=64) @@
945
	 * @param int $limit
946
	 * @return array
947
	 */
948
	function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) {
949
		// Current synctoken
950
		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?');
951
		$stmt->execute([ $calendarId ]);
952
		$currentToken = $stmt->fetchColumn(0);
953
954
		if (is_null($currentToken)) {
955
			return null;
956
		}
957
958
		$result = [
959
			'syncToken' => $currentToken,
960
			'added'     => [],
961
			'modified'  => [],
962
			'deleted'   => [],
963
		];
964
965
		if ($syncToken) {
966
967
			$query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`";
968
			if ($limit>0) {
969
				$query.= " `LIMIT` " . (int)$limit;
970
			}
971
972
			// Fetching all changes
973
			$stmt = $this->db->prepare($query);
974
			$stmt->execute([$syncToken, $currentToken, $calendarId]);
975
976
			$changes = [];
977
978
			// This loop ensures that any duplicates are overwritten, only the
979
			// last change on a node is relevant.
980
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
981
982
				$changes[$row['uri']] = $row['operation'];
983
984
			}
985
986
			foreach($changes as $uri => $operation) {
987
988
				switch($operation) {
989
					case 1 :
990
						$result['added'][] = $uri;
991
						break;
992
					case 2 :
993
						$result['modified'][] = $uri;
994
						break;
995
					case 3 :
996
						$result['deleted'][] = $uri;
997
						break;
998
				}
999
1000
			}
1001
		} else {
1002
			// No synctoken supplied, this is the initial sync.
1003
			$query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?";
1004
			$stmt = $this->db->prepare($query);
1005
			$stmt->execute([$calendarId]);
1006
1007
			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
1008
		}
1009
		return $result;
1010
1011
	}
1012
1013
	/**
1014
	 * Returns a list of subscriptions for a principal.

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

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