Skip to content

Commit

Permalink
Differentiating between modifications and additions.
Browse files Browse the repository at this point in the history
  • Loading branch information
evert committed Feb 11, 2013
1 parent ad39d7c commit 495e7bc
Show file tree
Hide file tree
Showing 19 changed files with 124 additions and 76 deletions.
4 changes: 2 additions & 2 deletions bin/migrateto19.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@
uri VARCHAR(200) NOT NULL,
synctoken INT(11) UNSIGNED NOT NULL,
{$itemType}id INT(11) UNSIGNED NOT NULL,
isdelete TINYINT(1) NOT NULL,
operation TINYINT(1) NOT NULL,
INDEX {$itemType}id_synctoken ({$itemType}id, synctoken)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Expand All @@ -233,7 +233,7 @@
uri text,
synctoken integer,
{$itemType}id integer,
isdelete bool
operation bool
);
");
Expand Down
2 changes: 1 addition & 1 deletion examples/sql/mysql.addressbook.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ CREATE TABLE addressbookchanges (
uri VARCHAR(200) NOT NULL,
synctoken INT(11) UNSIGNED NOT NULL,
addressbookid INT(11) UNSIGNED NOT NULL,
isdelete TINYINT(1) NOT NULL,
operation TINYINT(1) NOT NULL,
INDEX addressbookid_synctoken (addressbookid, synctoken)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
2 changes: 1 addition & 1 deletion examples/sql/mysql.calendars.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ CREATE TABLE calendarchanges (
uri VARCHAR(200) NOT NULL,
synctoken INT(11) UNSIGNED NOT NULL,
calendarid INT(11) UNSIGNED NOT NULL,
isdelete TINYINT(1) NOT NULL,
operation TINYINT(1) NOT NULL,
INDEX calendarid_synctoken (calendarid, synctoken)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
2 changes: 1 addition & 1 deletion examples/sql/sqlite.addressbooks.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CREATE TABLE addressbookchanges (
uri text,
synctoken integer,
addressbookid integer,
isdelete bool
operation integer
);

CREATE INDEX addressbookid_synctoken ON addressbookchanges (addressbookid, synctoken);
2 changes: 1 addition & 1 deletion examples/sql/sqlite.calendars.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CREATE TABLE calendarchanges (
uri text,
synctoken integer,
calendarid integer,
isdelete bool
operation integer
);

CREATE INDEX calendarid_synctoken ON calendarchanges (calendarid, synctoken);
44 changes: 27 additions & 17 deletions lib/Sabre/CalDAV/Backend/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function updateCalendar($calendarId, array $mutations) {
$newValues['id'] = $calendarId;
$stmt->execute(array_values($newValues));

$this->addChange($calendarId, "");
$this->addChange($calendarId, "", 2);

return true;

Expand Down Expand Up @@ -449,7 +449,7 @@ public function createCalendarObject($calendarId,$objectUri,$calendarData) {
$extraData['firstOccurence'],
$extraData['lastOccurence'],
]);
$this->addChange($calendarId, $objectUri);
$this->addChange($calendarId, $objectUri, 1);

return '"' . $extraData['etag'] . '"';

Expand Down Expand Up @@ -478,7 +478,7 @@ public function updateCalendarObject($calendarId,$objectUri,$calendarData) {
$stmt = $this->pdo->prepare('UPDATE '.$this->calendarObjectTableName.' SET calendardata = ?, lastmodified = ?, etag = ?, size = ?, componenttype = ?, firstoccurence = ?, lastoccurence = ? WHERE calendarid = ? AND uri = ?');
$stmt->execute([$calendarData, time(), $extraData['etag'], $extraData['size'], $extraData['componentType'], $extraData['firstOccurence'], $extraData['lastOccurence'], $calendarId, $objectUri]);

$this->addChange($calendarId, $objectUri);
$this->addChange($calendarId, $objectUri, 2);

return '"' . $extraData['etag'] . '"';

Expand Down Expand Up @@ -571,7 +571,7 @@ public function deleteCalendarObject($calendarId,$objectUri) {
$stmt = $this->pdo->prepare('DELETE FROM '.$this->calendarObjectTableName.' WHERE calendarid = ? AND uri = ?');
$stmt->execute([$calendarId, $objectUri]);

$this->addChange($calendarId, $objectUri, true);
$this->addChange($calendarId, $objectUri, 3);

}

Expand Down Expand Up @@ -712,9 +712,12 @@ public function calendarQuery($calendarId, array $filters) {
*
* [
* 'syncToken' => 'The current synctoken',
* 'modified' => [
* 'added' => [
* 'new.txt',
* ],
* 'modified' => [
* 'modified.txt',
* ],
* 'deleted' => [
* 'foo.php.bak',
* 'old.txt'
Expand Down Expand Up @@ -768,13 +771,14 @@ public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limi

$result = [
'syncToken' => $currentToken,
'added' => [],
'modified' => [],
'deleted' => [],
];

if ($syncToken) {

$query = "SELECT uri, isdelete FROM " . $this->calendarChangesTableName . " WHERE synctoken >= ? AND synctoken < ? AND calendarid = ? ORDER BY synctoken";
$query = "SELECT uri, operation FROM " . $this->calendarChangesTableName . " WHERE synctoken >= ? AND synctoken < ? AND calendarid = ? ORDER BY synctoken";
if ($limit>0) $query.= " LIMIT " . (int)$limit;

// Fetching all changes
Expand All @@ -785,16 +789,22 @@ public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limi

while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {

$changes[$row['uri']] = $row['isdelete'];
$changes[$row['uri']] = $row['operation'];

}

foreach($changes as $uri => $isDelete) {

if ($isDelete) {
$result['deleted'][] = $uri;
} else {
$result['modified'][] = $uri;
foreach($changes as $uri => $operation) {

switch($operation) {
case 1 :
$result['added'][] = $uri;
break;
case 2 :
$result['modified'][] = $uri;
break;
case 3 :
$result['deleted'][] = $uri;
break;
}

}
Expand All @@ -815,16 +825,16 @@ public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limi
*
* @param mixed $calendarId
* @param string $objectUri
* @param bool $isDelete
* @param int $operation 1 = add, 2 = modify, 3 = delete.
* @return void
*/
protected function addChange($calendarId, $objectUri, $isDelete = false) {
protected function addChange($calendarId, $objectUri, $operation) {

$stmt = $this->pdo->prepare('INSERT INTO ' . $this->calendarChangesTableName .' (uri, synctoken, calendarid, isdelete) SELECT ?, synctoken, ?, ? FROM calendars WHERE id = ?');
$stmt = $this->pdo->prepare('INSERT INTO ' . $this->calendarChangesTableName .' (uri, synctoken, calendarid, operation) SELECT ?, synctoken, ?, ? FROM calendars WHERE id = ?');
$stmt->execute([
$objectUri,
$calendarId,
$isDelete,
$operation,
$calendarId
]);
$stmt = $this->pdo->prepare('UPDATE ' . $this->calendarTableName . ' SET synctoken = synctoken + 1 WHERE id = ?');
Expand Down
5 changes: 4 additions & 1 deletion lib/Sabre/CalDAV/Backend/SyncSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ interface SyncSupport extends BackendInterface {
*
* [
* 'syncToken' => 'The current synctoken',
* 'modified' => [
* 'added' => [
* 'new.txt',
* ],
* 'modified' => [
* 'modified.txt',
* ],
* 'deleted' => [
* 'foo.php.bak',
* 'old.txt'
Expand Down
7 changes: 5 additions & 2 deletions lib/Sabre/CalDAV/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,12 @@ public function getSyncToken() {
*
* array(
* 'syncToken' => 'The current synctoken',
* 'modified' => array(
* 'added' => [
* 'new.txt',
* ),
* ],
* 'modified' => [
* 'modified.txt',
* ],
* 'deleted' => array(
* 'foo.php.bak',
* 'old.txt'
Expand Down
5 changes: 4 additions & 1 deletion lib/Sabre/CardDAV/AddressBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,12 @@ public function getSyncToken() {
*
* [
* 'syncToken' => 'The current synctoken',
* 'modified' => [
* 'added' => [
* 'new.txt',
* ],
* 'modified' => [
* 'modified.txt',
* ],
* 'deleted' => [
* 'foo.php.bak',
* 'old.txt'
Expand Down
44 changes: 27 additions & 17 deletions lib/Sabre/CardDAV/Backend/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function updateAddressBook($addressBookId, array $mutations) {

$stmt->execute($updates);

$this->addChange($addressBookId, "");
$this->addChange($addressBookId, "", 2);

return true;

Expand Down Expand Up @@ -289,7 +289,7 @@ public function createCard($addressBookId, $cardUri, $cardData) {

$result = $stmt->execute(array($cardData, $cardUri, time(), $addressBookId));

$this->addChange($addressBookId, $cardUri);
$this->addChange($addressBookId, $cardUri, 1);

return '"' . md5($cardData) . '"';

Expand Down Expand Up @@ -325,7 +325,7 @@ public function updateCard($addressBookId, $cardUri, $cardData) {
$stmt = $this->pdo->prepare('UPDATE ' . $this->cardsTableName . ' SET carddata = ?, lastmodified = ? WHERE uri = ? AND addressbookid =?');
$stmt->execute(array($cardData, time(), $cardUri, $addressBookId));

$this->addChange($addressBookId, $cardUri);
$this->addChange($addressBookId, $cardUri, 2);

return '"' . md5($cardData) . '"';

Expand All @@ -343,7 +343,7 @@ public function deleteCard($addressBookId, $cardUri) {
$stmt = $this->pdo->prepare('DELETE FROM ' . $this->cardsTableName . ' WHERE addressbookid = ? AND uri = ?');
$stmt->execute(array($addressBookId, $cardUri));

$this->addChange($addressBookId, $cardUri, true);
$this->addChange($addressBookId, $cardUri, 3);

return $stmt->rowCount()===1;

Expand All @@ -357,9 +357,12 @@ public function deleteCard($addressBookId, $cardUri) {
*
* [
* 'syncToken' => 'The current synctoken',
* 'modified' => [
* 'added' => [
* 'new.txt',
* ],
* 'modified' => [
* 'updated.txt',
* ],
* 'deleted' => [
* 'foo.php.bak',
* 'old.txt'
Expand Down Expand Up @@ -413,13 +416,14 @@ public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel,

$result = [
'syncToken' => $currentToken,
'added' => [],
'modified' => [],
'deleted' => [],
];

if ($syncToken) {

$query = "SELECT uri, isdelete FROM " . $this->addressBookChangesTableName . " WHERE synctoken >= ? AND synctoken < ? AND addressbookid = ? ORDER BY synctoken";
$query = "SELECT uri, operation FROM " . $this->addressBookChangesTableName . " WHERE synctoken >= ? AND synctoken < ? AND addressbookid = ? ORDER BY synctoken";
if ($limit>0) $query.= " LIMIT " . (int)$limit;

// Fetching all changes
Expand All @@ -430,16 +434,22 @@ public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel,

while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {

$changes[$row['uri']] = $row['isdelete'];
$changes[$row['uri']] = $row['operation'];

}

foreach($changes as $uri => $isDelete) {

if ($isDelete) {
$result['deleted'][] = $uri;
} else {
$result['modified'][] = $uri;
foreach($changes as $uri => $operation) {

switch($operation) {
case 1:
$result['added'][] = $uri;
break;
case 2:
$result['modified'][] = $uri;
break;
case 3:
$result['deleted'][] = $uri;
break;
}

}
Expand All @@ -460,16 +470,16 @@ public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel,
*
* @param mixed $addressBookId
* @param string $objectUri
* @param bool $isDelete
* @param int $operation 1 = add, 2 = modify, 3 = delete/
* @return void
*/
protected function addChange($addressBookId, $objectUri, $isDelete = false) {
protected function addChange($addressBookId, $objectUri, $operation) {

$stmt = $this->pdo->prepare('INSERT INTO ' . $this->addressBookChangesTableName .' (uri, synctoken, addressbookid, isdelete) SELECT ?, synctoken, ?, ? FROM addressbooks WHERE id = ?');
$stmt = $this->pdo->prepare('INSERT INTO ' . $this->addressBookChangesTableName .' (uri, synctoken, addressbookid, operation) SELECT ?, synctoken, ?, ? FROM addressbooks WHERE id = ?');
$stmt->execute([
$objectUri,
$addressBookId,
$isDelete,
$operation,
$addressBookId
]);
$stmt = $this->pdo->prepare('UPDATE ' . $this->addressBooksTableName . ' SET synctoken = synctoken + 1 WHERE id = ?');
Expand Down
5 changes: 4 additions & 1 deletion lib/Sabre/CardDAV/Backend/SyncSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ interface SyncSupport extends BackendInterface {
*
* [
* 'syncToken' => 'The current synctoken',
* 'modified' => [
* 'added' => [
* 'new.txt',
* ],
* 'modified' => [
* 'modified.txt',
* ],
* 'deleted' => [
* 'foo.php.bak',
* 'old.txt'
Expand Down
7 changes: 5 additions & 2 deletions lib/Sabre/DAV/Sync/ISyncCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ function getSyncToken();
*
* array(
* 'syncToken' => 'The current synctoken',
* 'modified' => array(
* 'added' => [
* 'new.txt',
* ),
* ],
* 'modified' => [
* 'modified.txt',
* ],
* 'deleted' => array(
* 'foo.php.bak',
* 'old.txt'
Expand Down
6 changes: 4 additions & 2 deletions lib/Sabre/DAV/Sync/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public function syncCollection($uri, \DOMDocument $dom) {
$this->sendSyncCollectionResponse(
$changeInfo['syncToken'],
$uri,
$changeInfo['added'],
$changeInfo['modified'],
$changeInfo['deleted'],
$properties
Expand Down Expand Up @@ -219,12 +220,13 @@ protected function parseSyncCollectionRequest(\DOMDocument $dom, $depth) {
*
* @param string $syncToken
* @param string $collectionUrl
* @param array $added
* @param array $modified
* @param array $deleted
* @param array $properties
* @return void
*/
protected function sendSyncCollectionResponse($syncToken, $collectionUrl, array $modified, array $deleted, array $properties) {
protected function sendSyncCollectionResponse($syncToken, $collectionUrl, array $added, array $modified, array $deleted, array $properties) {

$dom = new \DOMDocument('1.0','utf-8');
$dom->formatOutput = true;
Expand All @@ -238,7 +240,7 @@ protected function sendSyncCollectionResponse($syncToken, $collectionUrl, array

}

foreach($modified as $item) {
foreach(array_merge($added, $modified) as $item) {
$fullPath = $collectionUrl . '/' . $item;

// We must still fetch the requested properties from the server
Expand Down
Loading

0 comments on commit 495e7bc

Please sign in to comment.