[Pkg-owncloud-commits] [php-sabre-vobject] 17/65: Removed UID index.

David Prévot taffit at moszumanska.debian.org
Tue Feb 24 23:57:14 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository php-sabre-vobject.

commit 5b473b56205323094e487bd5dbcf48523cc74b2d
Author: Evert Pot <evert at rooftopsolutions.nl>
Date:   Fri Jan 23 01:15:47 2015 -0500

    Removed UID index.
---
 lib/Component/VCalendar.php | 176 +++++++++-----------------------------------
 lib/Recur/EventIterator.php |  27 +++++--
 2 files changed, 53 insertions(+), 150 deletions(-)

diff --git a/lib/Component/VCalendar.php b/lib/Component/VCalendar.php
index a4e3e01..156899b 100644
--- a/lib/Component/VCalendar.php
+++ b/lib/Component/VCalendar.php
@@ -253,36 +253,44 @@ class VCalendar extends VObject\Document {
             $timeZone = new DateTimeZone('UTC');
         }
 
+        // An array of events. Events are indexed by UID. Each item in this
+        // array is a list of one or more events that match the UID.
+        $recurringEvents = array();
+
         foreach($this->select('VEVENT') as $key=>$vevent) {
 
-            if (isset($vevent->{'RECURRENCE-ID'})) {
-                unset($this->children[$key]);
-                continue;
+            $uid = (string)$vevent->UID;
+            if (!$uid) {
+                throw new \LogicException('Event did not have a UID!');
             }
 
+            if (isset($vevent->{'RECURRENCE-ID'}) || isset($vevent->RRULE)) {
+                if (isset($recurringEvents[$uid])) {
+                    $recurringEvents[$uid][] = $vevent;
+                } else {
+                    $recurringEvents[$uid] = array($vevent);
+                }
+                continue;
+            }
 
-            if (!$vevent->rrule) {
-                unset($this->children[$key]);
+            if (!isset($vevent->RRULE)) {
                 if ($vevent->isInTimeRange($start, $end)) {
                     $newEvents[] = $vevent;
                 }
                 continue;
             }
 
+        }
 
-            $uid = (string)$vevent->uid;
-            if (!$uid) {
-                throw new \LogicException('Event did not have a UID!');
-            }
+        foreach($recurringEvents as $events) {
 
             try {
-                $it = new EventIterator($this, $vevent->uid, $timeZone);
+                $it = new EventIterator($events, $timeZone);
 
             } catch (NoInstancesException $e) {
                 // This event is recurring, but it doesn't have a single
                 // instance. We are skipping this event from the output
                 // entirely.
-                unset($this->children[$key]);
                 continue;
             }
             $it->fastForward($start);
@@ -298,10 +306,11 @@ class VCalendar extends VObject\Document {
 
             }
 
-            unset($this->children[$key]);
-
         }
 
+        // Wiping out all old VEVENT objects
+        unset($this->VEVENT);
+
         // Setting all properties to UTC time.
         foreach($newEvents as $newEvent) {
 
@@ -316,7 +325,6 @@ class VCalendar extends VObject\Document {
                 }
 
             }
-
             $this->add($newEvent);
 
         }
@@ -487,141 +495,25 @@ class VCalendar extends VObject\Document {
     }
 
     /**
-     * If this is marked true, it means the UID index needs to be regenerated.
+     * Returns all components with a specific UID value.
      *
-     * @var bool
-     */
-    protected $dirtyIndex = false;
-
-    /**
-     * Index with events, todos and journals that have a specific id.
-     *
-     * @var array
-     */
-    protected $uidIndex = [];
-
-    /**
-     * Mark the UID index as 'dirty', which means it needs to be regenerated
-     * on the next use.
-     *
-     * @return void
-     */
-    function dirty() {
-
-        $this->dirtyIndex = true;
-
-    }
-
-    /**
-     * Returns all components that match a UID.
+     * @return array
      */
     function getByUID($uid) {
 
-        if ($this->dirtyIndex) {
-            $this->uidIndex = [];
-            foreach($this->children as $key=>$child) {
-                if (!$child instanceof Component) {
-                    continue;
-                }
-                $cuid = $child->select('UID');
-                if ($cuid) {
-                    $cuid = current($cuid)->getValue();
-                    if (isset($this->uidIndex[$cuid])) {
-                        $this->uidIndex[$cuid][] = $key;
-                    } else {
-                        $this->uidIndex[$cuid] = [$key];
-                    }
-                }
-            }
-            $this->dirtyIndex = false;
-        }
-        if (isset($this->uidIndex[$uid])) {
-            return array_map(
-                function($key) {
-                    return $this->children[$key];
-                },
-                $this->uidIndex[$uid]
-            );
-        } else {
-            return [];
-        }
-
-    }
-
-    /**
-     * Adds a new property or component, and returns the new item.
-     *
-     * This method has 3 possible signatures:
-     *
-     * add(Component $comp) // Adds a new component
-     * add(Property $prop)  // Adds a new property
-     * add($name, $value, array $parameters = array()) // Adds a new property
-     * add($name, array $children = array()) // Adds a new component
-     * by name.
-     *
-     * @return Node
-     */
-    function add($a1, $a2 = null, $a3 = null) {
-        $r = parent::add($a1, $a2, $a3);
-        $this->dirty();
-        return $r;
-    }
-
-
-    /**
-     * This method removes a component or property from this component.
-     *
-     * You can either specify the item by name (like DTSTART), in which case
-     * all properties/components with that name will be removed, or you can
-     * pass an instance of a property or component, in which case only that
-     * exact item will be removed.
-     *
-     * The removed item will be returned. In case there were more than 1 items
-     * removed, only the last one will be returned.
-     *
-     * @param mixed $item
-     * @return void
-     */
-    function remove($item) {
-
-        parent::remove($item);
-        $this->dirty();
+        return array_filter($this->children, function($item) use ($uid) {
 
-    }
-
-    /**
-     * Using the setter method you can add properties or subcomponents
-     *
-     * You can either pass a Component, Property
-     * object, or a string to automatically create a Property.
-     *
-     * If the item already exists, it will be removed. If you want to add
-     * a new item with the same name, always use the add() method.
-     *
-     * @param string $name
-     * @param mixed $value
-     * @return void
-     */
-    function __set($name, $value) {
-
-        parent::__set($name, $value);
-        $this->dirty();
-
-    }
-
-    /**
-     * Removes all properties and components within this component with the
-     * specified name.
-     *
-     * @param string $name
-     * @return void
-     */
-    function __unset($name) {
+            if (!$item instanceof Component) {
+                return false;
+            }
+            if (!$itemUid = $item->select('UID')) {
+                return false;
+            }
+            $itemUid = current($itemUid)->getValue();
+            return $uid === $itemUid;
 
-        parent::__unset($name);
-        $this->dirty();
+        });
 
     }
 
 }
-
diff --git a/lib/Recur/EventIterator.php b/lib/Recur/EventIterator.php
index bf3fe50..e5b5d87 100644
--- a/lib/Recur/EventIterator.php
+++ b/lib/Recur/EventIterator.php
@@ -73,33 +73,44 @@ class EventIterator implements \Iterator {
     /**
      * Creates the iterator
      *
-     * You should pass a VCALENDAR component, as well as the UID of the event
-     * we're going to traverse.
+     * There's three ways to set up the iterator.
      *
-     * @param Component $vcal
+     * 1. You can pass a VCALENDAR component and a UID.
+     * 2. You can pass an array of VEVENTs (all UIDS should match).
+     * 3. You can pass a single VEVENT component.
+     *
+     * Only the second method is recomended. The other 1 and 3 will be removed
+     * at some point in the future.
+     *
+     * The $uid parameter is only required for the first method.
+     *
+     * @param Component|array $input
      * @param string|null $uid
      * @param DateTimeZone $timeZone Reference timezone for floating dates and
      *                               times.
      */
-    public function __construct(Component $vcal, $uid = null, DateTimeZone $timeZone = null) {
+    public function __construct($input, $uid = null, DateTimeZone $timeZone = null) {
 
         if (is_null($this->timeZone)) {
             $timeZone = new DateTimeZone('UTC');
         }
         $this->timeZone = $timeZone;
 
-        if ($vcal instanceof VEvent) {
+        if (is_array($input)) {
+            $events = $input;
+        } elseif ($input instanceof VEvent) {
             // Single instance mode.
-            $events = array($vcal);
+            $events = array($input);
         } else {
+            // Calendar + UID mode.
             $uid = (string)$uid;
             if (!$uid) {
                 throw new InvalidArgumentException('The UID argument is required when a VCALENDAR is passed to this constructor');
             }
-            if (!isset($vcal->VEVENT)) {
+            if (!isset($input->VEVENT)) {
                 throw new InvalidArgumentException('No events found in this calendar');
             }
-            $events = $vcal->getByUID($uid);
+            $events = $input->getByUID($uid);
 
         }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabre-vobject.git



More information about the Pkg-owncloud-commits mailing list