[Pkg-owncloud-commits] [php-sabredav] 13/42: Optimizing getChild on the caldav calendar home.

David Prévot taffit at moszumanska.debian.org
Wed Oct 29 20:52:05 UTC 2014


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

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

commit 09bc880921b30e961af26ca1938b38488f69cf6f
Author: Evert Pot <me at evertpot.com>
Date:   Mon Oct 6 12:52:24 2014 -0400

    Optimizing getChild on the caldav calendar home.
---
 ChangeLog.md                                       |   2 +
 lib/CalDAV/CalendarHome.php                        |  50 ++++--
 tests/Sabre/CalDAV/Backend/Mock.php                | 161 +------------------
 tests/Sabre/CalDAV/Backend/MockSharing.php         | 170 +++++++++++++++++++++
 .../Sabre/CalDAV/CalendarHomeNotificationsTest.php |  53 +++++++
 .../CalDAV/CalendarHomeSharedCalendarsTest.php     |   2 +-
 .../Sabre/CalDAV/CalendarHomeSubscriptionsTest.php |   4 +-
 .../Sabre/CalDAV/Notifications/CollectionTest.php  |   2 +-
 tests/Sabre/CalDAV/Notifications/NodeTest.php      |   2 +-
 tests/Sabre/CalDAV/Notifications/PluginTest.php    |   2 +-
 tests/Sabre/CalDAV/ShareableCalendarTest.php       |   6 +-
 tests/Sabre/CalDAV/SharedCalendarTest.php          |   2 +-
 tests/Sabre/DAVServerTest.php                      |   3 +
 13 files changed, 280 insertions(+), 179 deletions(-)

diff --git a/ChangeLog.md b/ChangeLog.md
index a1ec032..a8b16e7 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -9,6 +9,8 @@ ChangeLog
 * Changed: Moved all 'notifications' functionality from `Sabre\CalDAV\Plugin`
   to a new plugin: `Sabre\CalDAV\Notifications\Plugin`. If you want to use
   notifications-related functionality, just add this plugin.
+* Changed: Accessing the caldav inbox, outbox or notification collection no
+  longer triggers getCalendarsForUser() on backends.
 
 2.1.0-alpha1 (2014-09-23)
 -------------------------
diff --git a/lib/CalDAV/CalendarHome.php b/lib/CalDAV/CalendarHome.php
index 4f24a11..d6141b1 100644
--- a/lib/CalDAV/CalendarHome.php
+++ b/lib/CalDAV/CalendarHome.php
@@ -4,6 +4,7 @@ namespace Sabre\CalDAV;
 
 use
     Sabre\DAV,
+    Sabre\DAV\Exception\NotFound,
     Sabre\DAVACL,
     Sabre\HTTP\URLUtil;
 
@@ -127,17 +128,46 @@ class CalendarHome implements DAV\IExtendedCollection, DAVACL\IACL {
      * Returns a single calendar, by name
      *
      * @param string $name
-     * @todo needs optimizing
      * @return Calendar
      */
     function getChild($name) {
 
-        foreach($this->getChildren() as $child) {
-            if ($name==$child->getName())
-                return $child;
+        // Special nodes
+        if ($name === 'inbox' && $this->caldavBackend instanceof Backend\SchedulingSupport) {
+            return new Schedule\Inbox($this->caldavBackend, $this->principalInfo['uri']);
+        }
+        if ($name === 'outbox' && $this->caldavBackend instanceof Backend\SchedulingSupport) {
+            return new Schedule\Outbox($this->principalInfo['uri']);
+        }
+        if ($name === 'notifications' && $this->caldavBackend instanceof Backend\NotificationSupport) {
+            return new Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
+        }
 
+        // Calendars
+        foreach($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) {
+            if ($calendar['uri'] === $name) {
+                if ($this->caldavBackend instanceof Backend\SharingSupport) {
+                    if (isset($calendar['{http://calendarserver.org/ns/}shared-url'])) {
+                        return new SharedCalendar($this->caldavBackend, $calendar);
+                    } else {
+                        return new ShareableCalendar($this->caldavBackend, $calendar);
+                    }
+                } else {
+                    return new Calendar($this->caldavBackend, $calendar);
+                }
+            }
         }
-        throw new DAV\Exception\NotFound('Calendar with name \'' . $name . '\' could not be found');
+
+        if ($this->caldavBackend instanceof Backend\SubscriptionSupport) {
+            foreach($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
+                if ($subscription['uri'] === $name) {
+                    return new Subscriptions\Subscription($this->caldavBackend, $subscription);
+                }
+            }
+
+        }
+
+        throw new NotFound('Node with name \'' . $name . '\' could not be found');
 
     }
 
@@ -145,17 +175,15 @@ class CalendarHome implements DAV\IExtendedCollection, DAVACL\IACL {
      * Checks if a calendar exists.
      *
      * @param string $name
-     * @todo needs optimizing
      * @return bool
      */
     function childExists($name) {
 
-        foreach($this->getChildren() as $child) {
-            if ($name==$child->getName())
-                return true;
-
+        try {
+            return !!$this->getChild($name);
+        } catch (NotFound $e) {
+            return false;
         }
-        return false;
 
     }
 
diff --git a/tests/Sabre/CalDAV/Backend/Mock.php b/tests/Sabre/CalDAV/Backend/Mock.php
index 30502d4..8551c39 100644
--- a/tests/Sabre/CalDAV/Backend/Mock.php
+++ b/tests/Sabre/CalDAV/Backend/Mock.php
@@ -4,14 +4,12 @@ namespace Sabre\CalDAV\Backend;
 use Sabre\DAV;
 use Sabre\CalDAV;
 
-class Mock extends AbstractBackend implements NotificationSupport, SharingSupport {
+class Mock extends AbstractBackend {
 
-    private $calendarData;
-    private $calendars;
-    private $notifications;
-    private $shares = array();
+    protected $calendarData;
+    protected $calendars;
 
-    function __construct(array $calendars = [], array $calendarData = [], array $notifications = []) {
+    function __construct(array $calendars = [], array $calendarData = []) {
 
         foreach($calendars as &$calendar) {
             if (!isset($calendar['id'])) {
@@ -21,7 +19,6 @@ class Mock extends AbstractBackend implements NotificationSupport, SharingSuppor
 
         $this->calendars = $calendars;
         $this->calendarData = $calendarData;
-        $this->notifications = $notifications;
 
     }
 
@@ -214,154 +211,4 @@ class Mock extends AbstractBackend implements NotificationSupport, SharingSuppor
 
     }
 
-    /**
-     * Returns a list of notifications for a given principal url.
-     *
-     * The returned array should only consist of implementations of
-     * Sabre\CalDAV\Notifications\INotificationType.
-     *
-     * @param string $principalUri
-     * @return array
-     */
-    public function getNotificationsForPrincipal($principalUri) {
-
-        if (isset($this->notifications[$principalUri])) {
-            return $this->notifications[$principalUri];
-        }
-        return array();
-
-    }
-
-    /**
-     * This deletes a specific notifcation.
-     *
-     * This may be called by a client once it deems a notification handled.
-     *
-     * @param string $principalUri
-     * @param Sabre\CalDAV\Notifications\INotificationType $notification
-     * @return void
-     */
-    public function deleteNotification($principalUri, CalDAV\Notifications\INotificationType $notification) {
-
-        foreach($this->notifications[$principalUri] as $key=>$value) {
-            if ($notification === $value) {
-                unset($this->notifications[$principalUri][$key]);
-            }
-        }
-
-    }
-
-    /**
-     * Updates the list of shares.
-     *
-     * The first array is a list of people that are to be added to the
-     * calendar.
-     *
-     * Every element in the add array has the following properties:
-     *   * href - A url. Usually a mailto: address
-     *   * commonName - Usually a first and last name, or false
-     *   * summary - A description of the share, can also be false
-     *   * readOnly - A boolean value
-     *
-     * Every element in the remove array is just the address string.
-     *
-     * Note that if the calendar is currently marked as 'not shared' by and
-     * this method is called, the calendar should be 'upgraded' to a shared
-     * calendar.
-     *
-     * @param mixed $calendarId
-     * @param array $add
-     * @param array $remove
-     * @return void
-     */
-    public function updateShares($calendarId, array $add, array $remove) {
-
-        if (!isset($this->shares[$calendarId])) {
-            $this->shares[$calendarId] = array();
-        }
-
-        foreach($add as $val) {
-            $val['status'] = CalDAV\SharingPlugin::STATUS_NORESPONSE;
-            $this->shares[$calendarId][] = $val;
-        }
-
-        foreach($this->shares[$calendarId] as $k=>$share) {
-
-            if (in_array($share['href'], $remove)) {
-                unset($this->shares[$calendarId][$k]);
-            }
-
-        }
-
-        // Re-numbering keys
-        $this->shares[$calendarId] = array_values($this->shares[$calendarId]);
-
-    }
-
-    /**
-     * Returns the list of people whom this calendar is shared with.
-     *
-     * Every element in this array should have the following properties:
-     *   * href - Often a mailto: address
-     *   * commonName - Optional, for example a first + last name
-     *   * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
-     *   * readOnly - boolean
-     *   * summary - Optional, a description for the share
-     *
-     * @param mixed $calendarId
-     * @return array
-     */
-    public function getShares($calendarId) {
-
-        if (!isset($this->shares[$calendarId])) {
-            return array();
-        }
-
-        return $this->shares[$calendarId];
-
-    }
-
-    /**
-     * This method is called when a user replied to a request to share.
-     *
-     * @param string href The sharee who is replying (often a mailto: address)
-     * @param int status One of the SharingPlugin::STATUS_* constants
-     * @param string $calendarUri The url to the calendar thats being shared
-     * @param string $inReplyTo The unique id this message is a response to
-     * @param string $summary A description of the reply
-     * @return void
-     */
-    public function shareReply($href, $status, $calendarUri, $inReplyTo, $summary = null) {
-
-        // This operation basically doesn't do anything yet
-        if ($status === CalDAV\SharingPlugin::STATUS_ACCEPTED) {
-            return 'calendars/blabla/calendar';
-        }
-
-    }
-
-    /**
-     * Publishes a calendar
-     *
-     * @param mixed $calendarId
-     * @param bool $value
-     * @return void
-     */
-    public function setPublishStatus($calendarId, $value) {
-
-        foreach($this->calendars as $k=>$cal) {
-            if ($cal['id'] === $calendarId) {
-                if (!$value) {
-                    unset($cal['{http://calendarserver.org/ns/}publish-url']);
-                } else {
-                    $cal['{http://calendarserver.org/ns/}publish-url'] = 'http://example.org/public/ ' . $calendarId . '.ics';
-                }
-                return;
-            }
-        }
-
-        throw new DAV\Exception('Calendar with id "' . $calendarId . '" not found');
-
-    }
-
 }
diff --git a/tests/Sabre/CalDAV/Backend/MockSharing.php b/tests/Sabre/CalDAV/Backend/MockSharing.php
new file mode 100644
index 0000000..f9cd8dc
--- /dev/null
+++ b/tests/Sabre/CalDAV/Backend/MockSharing.php
@@ -0,0 +1,170 @@
+<?php
+
+namespace Sabre\CalDAV\Backend;
+use Sabre\DAV;
+use Sabre\CalDAV;
+
+class MockSharing extends Mock implements NotificationSupport, SharingSupport {
+
+    private $shares = array();
+    private $notifications;
+
+    function __construct(array $calendars = [], array $calendarData = [], array $notifications = []) {
+
+        parent::__construct($calendars, $calendarData);
+        $this->notifications = $notifications;
+
+    }
+
+    /**
+     * Returns a list of notifications for a given principal url.
+     *
+     * The returned array should only consist of implementations of
+     * Sabre\CalDAV\Notifications\INotificationType.
+     *
+     * @param string $principalUri
+     * @return array
+     */
+    public function getNotificationsForPrincipal($principalUri) {
+
+        if (isset($this->notifications[$principalUri])) {
+            return $this->notifications[$principalUri];
+        }
+        return array();
+
+    }
+
+    /**
+     * This deletes a specific notifcation.
+     *
+     * This may be called by a client once it deems a notification handled.
+     *
+     * @param string $principalUri
+     * @param Sabre\CalDAV\Notifications\INotificationType $notification
+     * @return void
+     */
+    public function deleteNotification($principalUri, CalDAV\Notifications\INotificationType $notification) {
+
+        foreach($this->notifications[$principalUri] as $key=>$value) {
+            if ($notification === $value) {
+                unset($this->notifications[$principalUri][$key]);
+            }
+        }
+
+    }
+
+    /**
+     * Updates the list of shares.
+     *
+     * The first array is a list of people that are to be added to the
+     * calendar.
+     *
+     * Every element in the add array has the following properties:
+     *   * href - A url. Usually a mailto: address
+     *   * commonName - Usually a first and last name, or false
+     *   * summary - A description of the share, can also be false
+     *   * readOnly - A boolean value
+     *
+     * Every element in the remove array is just the address string.
+     *
+     * Note that if the calendar is currently marked as 'not shared' by and
+     * this method is called, the calendar should be 'upgraded' to a shared
+     * calendar.
+     *
+     * @param mixed $calendarId
+     * @param array $add
+     * @param array $remove
+     * @return void
+     */
+    public function updateShares($calendarId, array $add, array $remove) {
+
+        if (!isset($this->shares[$calendarId])) {
+            $this->shares[$calendarId] = array();
+        }
+
+        foreach($add as $val) {
+            $val['status'] = CalDAV\SharingPlugin::STATUS_NORESPONSE;
+            $this->shares[$calendarId][] = $val;
+        }
+
+        foreach($this->shares[$calendarId] as $k=>$share) {
+
+            if (in_array($share['href'], $remove)) {
+                unset($this->shares[$calendarId][$k]);
+            }
+
+        }
+
+        // Re-numbering keys
+        $this->shares[$calendarId] = array_values($this->shares[$calendarId]);
+
+    }
+
+    /**
+     * Returns the list of people whom this calendar is shared with.
+     *
+     * Every element in this array should have the following properties:
+     *   * href - Often a mailto: address
+     *   * commonName - Optional, for example a first + last name
+     *   * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
+     *   * readOnly - boolean
+     *   * summary - Optional, a description for the share
+     *
+     * @param mixed $calendarId
+     * @return array
+     */
+    public function getShares($calendarId) {
+
+        if (!isset($this->shares[$calendarId])) {
+            return array();
+        }
+
+        return $this->shares[$calendarId];
+
+    }
+
+    /**
+     * This method is called when a user replied to a request to share.
+     *
+     * @param string href The sharee who is replying (often a mailto: address)
+     * @param int status One of the SharingPlugin::STATUS_* constants
+     * @param string $calendarUri The url to the calendar thats being shared
+     * @param string $inReplyTo The unique id this message is a response to
+     * @param string $summary A description of the reply
+     * @return void
+     */
+    public function shareReply($href, $status, $calendarUri, $inReplyTo, $summary = null) {
+
+        // This operation basically doesn't do anything yet
+        if ($status === CalDAV\SharingPlugin::STATUS_ACCEPTED) {
+            return 'calendars/blabla/calendar';
+        }
+
+    }
+
+    /**
+     * Publishes a calendar
+     *
+     * @param mixed $calendarId
+     * @param bool $value
+     * @return void
+     */
+    public function setPublishStatus($calendarId, $value) {
+
+        foreach($this->calendars as $k=>$cal) {
+            if ($cal['id'] === $calendarId) {
+                if (!$value) {
+                    unset($cal['{http://calendarserver.org/ns/}publish-url']);
+                } else {
+                    $cal['{http://calendarserver.org/ns/}publish-url'] = 'http://example.org/public/ ' . $calendarId . '.ics';
+                }
+                return;
+            }
+        }
+
+        throw new DAV\Exception('Calendar with id "' . $calendarId . '" not found');
+
+    }
+
+}
+
diff --git a/tests/Sabre/CalDAV/CalendarHomeNotificationsTest.php b/tests/Sabre/CalDAV/CalendarHomeNotificationsTest.php
new file mode 100644
index 0000000..a0880d8
--- /dev/null
+++ b/tests/Sabre/CalDAV/CalendarHomeNotificationsTest.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace Sabre\CalDAV;
+
+use Sabre\DAVACL;
+
+class CalendarHomeNotificationsTest extends \PHPUnit_Framework_TestCase {
+
+    protected $backend;
+
+    function testGetChildrenNoSupport() {
+
+        $backend = new Backend\Mock();
+        $calendarHome = new CalendarHome($backend,['uri' => 'principals/user']);
+
+        $this->assertEquals(
+            [],
+            $calendarHome->getChildren()
+        );
+
+    }
+
+    /**
+     * @expectedException \Sabre\DAV\Exception\NotFound
+     */
+    function testGetChildNoSupport() {
+
+        $backend = new Backend\Mock();
+        $calendarHome = new CalendarHome($backend,['uri' => 'principals/user']);
+        $calendarHome->getChild('notifications');
+
+    }
+
+    function testGetChildren() {
+
+        $backend = new Backend\MockSharing();
+        $calendarHome = new CalendarHome($backend,['uri' => 'principals/user']);
+
+        $result = $calendarHome->getChildren();
+        $this->assertEquals('notifications', $result[0]->getName());
+
+    }
+
+    function testGetChild() {
+
+        $backend = new Backend\MockSharing();
+        $calendarHome = new CalendarHome($backend,['uri' => 'principals/user']);
+        $result = $calendarHome->getChild('notifications');
+        $this->assertEquals('notifications', $result->getName());
+
+    }
+
+}
diff --git a/tests/Sabre/CalDAV/CalendarHomeSharedCalendarsTest.php b/tests/Sabre/CalDAV/CalendarHomeSharedCalendarsTest.php
index e2f20d4..bfb0eae 100644
--- a/tests/Sabre/CalDAV/CalendarHomeSharedCalendarsTest.php
+++ b/tests/Sabre/CalDAV/CalendarHomeSharedCalendarsTest.php
@@ -28,7 +28,7 @@ class CalendarHomeSharedCalendarsTest extends \PHPUnit_Framework_TestCase {
             ),
         );
 
-        $this->backend = new Backend\Mock(
+        $this->backend = new Backend\MockSharing(
             $calendars,
             array(),
             array()
diff --git a/tests/Sabre/CalDAV/CalendarHomeSubscriptionsTest.php b/tests/Sabre/CalDAV/CalendarHomeSubscriptionsTest.php
index 4087d6a..7aa450f 100644
--- a/tests/Sabre/CalDAV/CalendarHomeSubscriptionsTest.php
+++ b/tests/Sabre/CalDAV/CalendarHomeSubscriptionsTest.php
@@ -35,7 +35,7 @@ class CalendarHomeSubscriptionsTest extends \PHPUnit_Framework_TestCase {
 
         $instance = $this->getInstance();
         $children = $instance->getChildren();
-        $this->assertEquals(2, count($children));
+        $this->assertEquals(1, count($children));
         foreach($children as $child) {
             if ($child instanceof Subscriptions\Subscription) {
                 return;
@@ -57,7 +57,7 @@ class CalendarHomeSubscriptionsTest extends \PHPUnit_Framework_TestCase {
         $instance->createExtendedCollection('sub2', $rt, $props);
 
         $children = $instance->getChildren();
-        $this->assertEquals(3, count($children));
+        $this->assertEquals(2, count($children));
 
     }
 
diff --git a/tests/Sabre/CalDAV/Notifications/CollectionTest.php b/tests/Sabre/CalDAV/Notifications/CollectionTest.php
index eaed4f5..940ab05 100644
--- a/tests/Sabre/CalDAV/Notifications/CollectionTest.php
+++ b/tests/Sabre/CalDAV/Notifications/CollectionTest.php
@@ -16,7 +16,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase {
 
         $this->notification = new Notification\SystemStatus(1,'"1"');
 
-        $this->caldavBackend = new CalDAV\Backend\Mock(array(),array(), array(
+        $this->caldavBackend = new CalDAV\Backend\MockSharing(array(),array(), array(
             'principals/user1' => array(
                 $this->notification
             )
diff --git a/tests/Sabre/CalDAV/Notifications/NodeTest.php b/tests/Sabre/CalDAV/Notifications/NodeTest.php
index 28e43ce..a0906d3 100644
--- a/tests/Sabre/CalDAV/Notifications/NodeTest.php
+++ b/tests/Sabre/CalDAV/Notifications/NodeTest.php
@@ -15,7 +15,7 @@ class NodeTest extends \PHPUnit_Framework_TestCase {
 
         $this->systemStatus = new Notification\SystemStatus(1,'"1"');
 
-        $this->caldavBackend = new CalDAV\Backend\Mock(array(),array(), array(
+        $this->caldavBackend = new CalDAV\Backend\MockSharing(array(),array(), array(
             'principals/user1' => array(
                 $this->systemStatus
             )
diff --git a/tests/Sabre/CalDAV/Notifications/PluginTest.php b/tests/Sabre/CalDAV/Notifications/PluginTest.php
index 2a2a122..8426373 100644
--- a/tests/Sabre/CalDAV/Notifications/PluginTest.php
+++ b/tests/Sabre/CalDAV/Notifications/PluginTest.php
@@ -24,7 +24,7 @@ class PluginTest extends \PHPUnit_Framework_TestCase {
 
     function setup() {
 
-        $this->caldavBackend = new CalDAV\Backend\Mock();
+        $this->caldavBackend = new CalDAV\Backend\MockSharing();
         $principalBackend = new DAVACL\PrincipalBackend\Mock();
         $calendars = new CalDAV\CalendarRoot($principalBackend,$this->caldavBackend);
         $principals = new CalDAV\Principal\Collection($principalBackend);
diff --git a/tests/Sabre/CalDAV/ShareableCalendarTest.php b/tests/Sabre/CalDAV/ShareableCalendarTest.php
index d41ddc1..15b869d 100644
--- a/tests/Sabre/CalDAV/ShareableCalendarTest.php
+++ b/tests/Sabre/CalDAV/ShareableCalendarTest.php
@@ -15,10 +15,8 @@ class ShareableCalendarTest extends \PHPUnit_Framework_TestCase {
             'id' => 1,
         );
 
-        $this->backend = new Backend\Mock(
-            array($props),
-            array(),
-            array()
+        $this->backend = new Backend\MockSharing(
+            array($props)
         );
         $this->backend->updateShares(1, array(
             array(
diff --git a/tests/Sabre/CalDAV/SharedCalendarTest.php b/tests/Sabre/CalDAV/SharedCalendarTest.php
index a2b07ca..337b658 100644
--- a/tests/Sabre/CalDAV/SharedCalendarTest.php
+++ b/tests/Sabre/CalDAV/SharedCalendarTest.php
@@ -20,7 +20,7 @@ class SharedCalendarTest extends \PHPUnit_Framework_TestCase {
             );
         }
 
-        $this->backend = new Backend\Mock(
+        $this->backend = new Backend\MockSharing(
             array($props),
             array(),
             array()
diff --git a/tests/Sabre/DAVServerTest.php b/tests/Sabre/DAVServerTest.php
index ab6a68d..3a60de7 100644
--- a/tests/Sabre/DAVServerTest.php
+++ b/tests/Sabre/DAVServerTest.php
@@ -192,6 +192,9 @@ abstract class DAVServerTest extends \PHPUnit_Framework_TestCase {
 
     function setUpBackends() {
 
+        if ($this->setupCalDAVSharing && is_null($this->caldavBackend)) {
+            $this->caldavBackend = new CalDAV\Backend\MockSharing($this->caldavCalendars, $this->caldavCalendarObjects);
+        }
         if ($this->setupCalDAVSubscriptions && is_null($this->caldavBackend)) {
             $this->caldavBackend = new CalDAV\Backend\MockSubscriptionSupport($this->caldavCalendars, $this->caldavCalendarObjects);
         }

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



More information about the Pkg-owncloud-commits mailing list