From e0d3fec643ab95ab920288873a19ee7ced287f4d Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Fri, 24 Dec 2010 15:25:39 -0500 Subject: [PATCH] Implement remaining getChanges() methods, simplify activesync driver --- .../Core/lib/Horde/Core/ActiveSync/Connector.php | 43 +++++++++-------- .../Core/lib/Horde/Core/ActiveSync/Driver.php | 56 ++++++++++------------ kronolith/lib/Api.php | 1 - nag/lib/Api.php | 17 +++++++ turba/lib/Api.php | 17 +++++++ 5 files changed, 81 insertions(+), 53 deletions(-) diff --git a/framework/Core/lib/Horde/Core/ActiveSync/Connector.php b/framework/Core/lib/Horde/Core/ActiveSync/Connector.php index 74642000e..1c6cb8ad2 100644 --- a/framework/Core/lib/Horde/Core/ActiveSync/Connector.php +++ b/framework/Core/lib/Horde/Core/ActiveSync/Connector.php @@ -75,26 +75,6 @@ class Horde_Core_ActiveSync_Connector } /** - * Obtain all calendar server chages that occured in the specified time - * interval. - * - * @param integer $from_ts Starting timestamp - * @param integer $to_ts Ending timestamp - * - * @return array Hash of add, modify, and delete arrays - */ - public function calendar_getChanges($from_ts, $to_ts) - { - try { - return $this->_registry->calendar->getChanges($from_ts, $to_ts); - } catch (Exception $e) { - return array('add' => array(), - 'modify' => array(), - 'delete' => array()); - } - } - - /** * Export the specified event as an ActiveSync message * * @param string $uid The calendar id @@ -392,4 +372,27 @@ class Horde_Core_ActiveSync_Connector return $this->_registry->hasInterface($api); } + /** + * Get all server changes for the specified collection + * @param string $collection The collection type (calendar, contacts, tasks) + * @param integer $from_ts Starting timestamp + * @param integer $to_ts Ending timestamp + * + * @return array A hash of add, modify, and delete uids + * @throws InvalidArgumentException + */ + public function getChanges($collection, $from_ts, $to_ts) + { + if (!in_array($collection, array('calendar', 'contacts', 'tasks'))) { + throw new InvalidArgumentException('collection must be one of calendar, contacts, or tasks'); + } + try { + return $this->_registry->{$collection}->getChanges($from_ts, $to_ts); + } catch (Exception $e) { + return array('add' => array(), + 'modify' => array(), + 'delete' => array()); + } + } + } \ No newline at end of file diff --git a/framework/Core/lib/Horde/Core/ActiveSync/Driver.php b/framework/Core/lib/Horde/Core/ActiveSync/Driver.php index ddfb3e9a2..77c62c3c4 100644 --- a/framework/Core/lib/Horde/Core/ActiveSync/Driver.php +++ b/framework/Core/lib/Horde/Core/ActiveSync/Driver.php @@ -219,9 +219,9 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base public function getMessageList($folderid, $cutoffdate) { $this->_logger->debug('Horde::getMessageList(' . $folderid . ', ' . $cutoffdate . ')'); + ob_start(); $messages = array(); - switch ($folderid) { case self::APPOINTMENTS_FOLDER: $startstamp = (int)$cutoffdate; @@ -270,8 +270,8 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base $this->_endBuffer(); return array(); } - $this->_endBuffer(); + return $messages; } @@ -279,9 +279,10 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base * Get a list of server changes that occured during the specified time * period. * - * @param string $folderId The server id of the collection to check. - * @param integer $from_ts The starting timestamp - * @param integer $to_ts The ending timestamp + * @param string $folderId The server id of the collection to check. + * @param integer $from_ts The starting timestamp + * @param integer $to_ts The ending timestamp + * @param integer $cutoffdate The earliest date to retrieve back to * * @return array A list of messge uids that have chnaged in the specified * time period. @@ -289,8 +290,8 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base public function getServerChanges($folderId, $from_ts, $to_ts, $cutoffdate) { $this->_logger->debug("Horde_ActiveSync_Driver_Horde::getServerChanges($folderId, $from_ts, $to_ts, $cutoffdate)"); - $adds = array(); + $changes = array(); ob_start(); switch ($folderId) { case self::APPOINTMENTS_FOLDER: @@ -299,20 +300,15 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base $startstamp = (int)$cutoffdate; $endstamp = time() + 32140800; //60 * 60 * 24 * 31 * 12 == one year try { - $adds = $this->_connector->calendar_listUids($startstamp, $endstamp); + $changes['add'] = $this->_connector->calendar_listUids($startstamp, $endstamp); } catch (Horde_Exception $e) { $this->_logger->err($e->getMessage()); $this->_endBuffer(); return array(); } - $edits = $deletes = array(); } else { try { - $changes = $this->_connector->calendar_getChanges($from_ts, $to_ts); - // @TODO: these assignments are just until all collections are refactored. - $adds = $changes['add']; - $edits = $changes['modify']; - $deletes = $changes['delete']; + $changes = $this->_connector->getChanges('calendar', $from_ts, $to_ts); } catch (Horde_Exception $e) { $this->_logger->err($e->getMessage()); $this->_endBuffer(); @@ -320,11 +316,12 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base } } break; + case self::CONTACTS_FOLDER: /* Can't use History for first sync */ if ($from_ts == 0) { try { - $adds = $this->_connector->contacts_listUids(); + $changes['add'] = $this->_connector->contacts_listUids(); } catch (Horde_Exception $e) { $this->_logger->err($e->getMessage()); $this->_endBuffer(); @@ -333,9 +330,7 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base $edits = $deletes = array(); } else { try { - $adds = $this->_connector->contacts_listBy('add', $from_ts, $to_ts); - $edits = $this->_connector->contacts_listBy('modify', $from_ts, $to_ts); - $deletes = $this->_connector->contacts_listBy('delete', $from_ts, $to_ts); + $changes = $this->_connector->getChanges('contacts', $from_ts, $to_ts); } catch (Horde_Exception $e) { $this->_logger->err($e->getMessage()); $this->_endBuffer(); @@ -343,22 +338,20 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base } } break; + case self::TASKS_FOLDER: /* Can't use History for first sync */ if ($from_ts == 0) { try { - $adds = $this->_connector->tasks_listUids(); + $changes['add'] = $this->_connector->tasks_listUids(); } catch (Horde_Exception $e) { $this->_logger->err($e->getMessage()); $this->_endBuffer(); return array(); } - $edits = $deletes = array(); } else { try { - $adds = $this->_connector->tasks_listBy('add', $from_ts, $to_ts); - $edits = $this->_connector->tasks_listBy('modify', $from_ts, $to_ts); - $deletes = $this->_connector->tasks_listBy('delete', $from_ts, $to_ts); + $changes = $this->_connector->getChanges('tasks', $from_ts, $to_ts); } catch (Horde_Exception $e) { $this->_logger->err($e->getMessage()); $this->_endBuffer(); @@ -368,33 +361,32 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base break; } - /* Build the changes array */ - $changes = array(); + $results = array(); /* Server additions */ - foreach ($adds as $add) { - $changes[] = array( + foreach ($changes['add'] as $add) { + $results[] = array( 'id' => $add, 'type' => 'change', 'flags' => Horde_ActiveSync::FLAG_NEWMESSAGE); } /* Server changes */ - foreach ($edits as $change) { - $changes[] = array( + foreach ($changes['modify'] as $change) { + $results[] = array( 'id' => $change, 'type' => 'change'); } /* Server Deletions */ - foreach ($deletes as $deleted) { - $changes[] = array( + foreach ($changes['delete'] as $deleted) { + $results[] = array( 'id' => $deleted, 'type' => 'delete'); } - $this->_endBuffer(); - return $changes; + + return $results; } /** diff --git a/kronolith/lib/Api.php b/kronolith/lib/Api.php index 7afd9434e..39595794e 100644 --- a/kronolith/lib/Api.php +++ b/kronolith/lib/Api.php @@ -537,7 +537,6 @@ class Kronolith_Api extends Horde_Registry_Api throw new Horde_Exception_PermissionDenied(); } - /* Need to get changes, then ensure they are not from an exception */ $changes = array('add' => array(), 'modify' => array(), 'delete' => array()); diff --git a/nag/lib/Api.php b/nag/lib/Api.php index 5c9ee865d..03c112d26 100644 --- a/nag/lib/Api.php +++ b/nag/lib/Api.php @@ -710,6 +710,23 @@ class Nag_Api extends Horde_Registry_Api } /** + * Method for obtaining all server changes between two timestamps. Basically + * a wrapper around listBy(), but returns an array containing all adds, + * edits and deletions. + * + * @param integer $start The starting timestamp + * @param integer $end The ending timestamp. + * + * @return array An hash with 'add', 'modify' and 'delete' arrays. + */ + public function getChanges($start, $end) + { + return array('add' => $this->listBy('add', $start, null, $end), + 'modify' => $this->listBy('modify', $start, null, $end), + 'delete' => $this->listBy('delete', $start, null, $end)); + } + + /** * Returns the timestamp of an operation for a given uid an action. * * @param string $uid The uid to look for. diff --git a/turba/lib/Api.php b/turba/lib/Api.php index 9a669fbaf..de667c382 100644 --- a/turba/lib/Api.php +++ b/turba/lib/Api.php @@ -535,6 +535,23 @@ class Turba_Api extends Horde_Registry_Api } /** + * Method for obtaining all server changes between two timestamps. Basically + * a wrapper around listBy(), but returns an array containing all adds, + * edits and deletions. + * + * @param integer $start The starting timestamp + * @param integer $end The ending timestamp. + * + * @return array An hash with 'add', 'modify' and 'delete' arrays. + */ + public function getChanges($start, $end) + { + return array('add' => $this->listBy('add', $start, null, $end), + 'modify' => $this->listBy('modify', $start, null, $end), + 'delete' => $this->listBy('delete', $start, null, $end)); + } + + /** * Returns the timestamp of an operation for a given uid an action. * * @param string $uid The uid to look for. -- 2.11.0