| @@ 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. |
|
| @@ 1410-1473 (lines=64) @@ | ||
| 1407 | * @param int $limit |
|
| 1408 | * @return array |
|
| 1409 | */ |
|
| 1410 | function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
| 1411 | // Current synctoken |
|
| 1412 | $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 1413 | $stmt->execute([ $calendarId ]); |
|
| 1414 | $currentToken = $stmt->fetchColumn(0); |
|
| 1415 | ||
| 1416 | if (is_null($currentToken)) { |
|
| 1417 | return null; |
|
| 1418 | } |
|
| 1419 | ||
| 1420 | $result = [ |
|
| 1421 | 'syncToken' => $currentToken, |
|
| 1422 | 'added' => [], |
|
| 1423 | 'modified' => [], |
|
| 1424 | 'deleted' => [], |
|
| 1425 | ]; |
|
| 1426 | ||
| 1427 | if ($syncToken) { |
|
| 1428 | ||
| 1429 | $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`"; |
|
| 1430 | if ($limit>0) { |
|
| 1431 | $query.= " `LIMIT` " . (int)$limit; |
|
| 1432 | } |
|
| 1433 | ||
| 1434 | // Fetching all changes |
|
| 1435 | $stmt = $this->db->prepare($query); |
|
| 1436 | $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
| 1437 | ||
| 1438 | $changes = []; |
|
| 1439 | ||
| 1440 | // This loop ensures that any duplicates are overwritten, only the |
|
| 1441 | // last change on a node is relevant. |
|
| 1442 | while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1443 | ||
| 1444 | $changes[$row['uri']] = $row['operation']; |
|
| 1445 | ||
| 1446 | } |
|
| 1447 | ||
| 1448 | foreach($changes as $uri => $operation) { |
|
| 1449 | ||
| 1450 | switch($operation) { |
|
| 1451 | case 1 : |
|
| 1452 | $result['added'][] = $uri; |
|
| 1453 | break; |
|
| 1454 | case 2 : |
|
| 1455 | $result['modified'][] = $uri; |
|
| 1456 | break; |
|
| 1457 | case 3 : |
|
| 1458 | $result['deleted'][] = $uri; |
|
| 1459 | break; |
|
| 1460 | } |
|
| 1461 | ||
| 1462 | } |
|
| 1463 | } else { |
|
| 1464 | // No synctoken supplied, this is the initial sync. |
|
| 1465 | $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?"; |
|
| 1466 | $stmt = $this->db->prepare($query); |
|
| 1467 | $stmt->execute([$calendarId]); |
|
| 1468 | ||
| 1469 | $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 1470 | } |
|
| 1471 | return $result; |
|
| 1472 | ||
| 1473 | } |
|
| 1474 | ||
| 1475 | /** |
|
| 1476 | * Returns a list of subscriptions for a principal. |
|