| @@ 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. |
|
| @@ 1611-1674 (lines=64) @@ | ||
| 1608 | * @param int $limit |
|
| 1609 | * @return array |
|
| 1610 | */ |
|
| 1611 | function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
| 1612 | // Current synctoken |
|
| 1613 | $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 1614 | $stmt->execute([ $calendarId ]); |
|
| 1615 | $currentToken = $stmt->fetchColumn(0); |
|
| 1616 | ||
| 1617 | if (is_null($currentToken)) { |
|
| 1618 | return null; |
|
| 1619 | } |
|
| 1620 | ||
| 1621 | $result = [ |
|
| 1622 | 'syncToken' => $currentToken, |
|
| 1623 | 'added' => [], |
|
| 1624 | 'modified' => [], |
|
| 1625 | 'deleted' => [], |
|
| 1626 | ]; |
|
| 1627 | ||
| 1628 | if ($syncToken) { |
|
| 1629 | ||
| 1630 | $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`"; |
|
| 1631 | if ($limit>0) { |
|
| 1632 | $query.= " LIMIT " . (int)$limit; |
|
| 1633 | } |
|
| 1634 | ||
| 1635 | // Fetching all changes |
|
| 1636 | $stmt = $this->db->prepare($query); |
|
| 1637 | $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
| 1638 | ||
| 1639 | $changes = []; |
|
| 1640 | ||
| 1641 | // This loop ensures that any duplicates are overwritten, only the |
|
| 1642 | // last change on a node is relevant. |
|
| 1643 | while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1644 | ||
| 1645 | $changes[$row['uri']] = $row['operation']; |
|
| 1646 | ||
| 1647 | } |
|
| 1648 | ||
| 1649 | foreach($changes as $uri => $operation) { |
|
| 1650 | ||
| 1651 | switch($operation) { |
|
| 1652 | case 1 : |
|
| 1653 | $result['added'][] = $uri; |
|
| 1654 | break; |
|
| 1655 | case 2 : |
|
| 1656 | $result['modified'][] = $uri; |
|
| 1657 | break; |
|
| 1658 | case 3 : |
|
| 1659 | $result['deleted'][] = $uri; |
|
| 1660 | break; |
|
| 1661 | } |
|
| 1662 | ||
| 1663 | } |
|
| 1664 | } else { |
|
| 1665 | // No synctoken supplied, this is the initial sync. |
|
| 1666 | $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?"; |
|
| 1667 | $stmt = $this->db->prepare($query); |
|
| 1668 | $stmt->execute([$calendarId]); |
|
| 1669 | ||
| 1670 | $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 1671 | } |
|
| 1672 | return $result; |
|
| 1673 | ||
| 1674 | } |
|
| 1675 | ||
| 1676 | /** |
|
| 1677 | * Returns a list of subscriptions for a principal. |
|