Code Duplication    Length = 61-64 lines in 2 locations

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

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

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

@@ 1638-1701 (lines=64) @@
1635
	 * @param int $limit
1636
	 * @return array
1637
	 */
1638
	function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) {
1639
		// Current synctoken
1640
		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?');
1641
		$stmt->execute([ $calendarId ]);
1642
		$currentToken = $stmt->fetchColumn(0);
1643
1644
		if (is_null($currentToken)) {
1645
			return null;
1646
		}
1647
1648
		$result = [
1649
			'syncToken' => $currentToken,
1650
			'added'     => [],
1651
			'modified'  => [],
1652
			'deleted'   => [],
1653
		];
1654
1655
		if ($syncToken) {
1656
1657
			$query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`";
1658
			if ($limit>0) {
1659
				$query.= " LIMIT " . (int)$limit;
1660
			}
1661
1662
			// Fetching all changes
1663
			$stmt = $this->db->prepare($query);
1664
			$stmt->execute([$syncToken, $currentToken, $calendarId]);
1665
1666
			$changes = [];
1667
1668
			// This loop ensures that any duplicates are overwritten, only the
1669
			// last change on a node is relevant.
1670
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1671
1672
				$changes[$row['uri']] = $row['operation'];
1673
1674
			}
1675
1676
			foreach($changes as $uri => $operation) {
1677
1678
				switch($operation) {
1679
					case 1 :
1680
						$result['added'][] = $uri;
1681
						break;
1682
					case 2 :
1683
						$result['modified'][] = $uri;
1684
						break;
1685
					case 3 :
1686
						$result['deleted'][] = $uri;
1687
						break;
1688
				}
1689
1690
			}
1691
		} else {
1692
			// No synctoken supplied, this is the initial sync.
1693
			$query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?";
1694
			$stmt = $this->db->prepare($query);
1695
			$stmt->execute([$calendarId]);
1696
1697
			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
1698
		}
1699
		return $result;
1700
1701
	}
1702
1703
	/**
1704
	 * Returns a list of subscriptions for a principal.