[Pkg-owncloud-commits] [php-sabredav] 21/42: Added a new calendarObjectChange event.
David Prévot
taffit at moszumanska.debian.org
Wed Oct 29 20:52:06 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 e98e91f7e13dc88c77bf40116b1a8a90b701d524
Author: Evert Pot <evert at rooftopsolutions.nl>
Date: Thu Oct 16 23:42:41 2014 -0400
Added a new calendarObjectChange event.
This event allows people to specifically hook into CalDAV operations.
It allows you to get direct access to the VObject. Before this event,
you had to use beforeWriteContent and beforeCreateFile, and you were
forced to parse the iCalendar object.
This also makes scheduling a bit faster, because we only have to parse
the iCalendar/jCal object once.
---
lib/CalDAV/Plugin.php | 52 +++++++++-
lib/CalDAV/Schedule/ISchedulingObject.php | 2 +-
lib/CalDAV/Schedule/Plugin.php | 114 +++++----------------
.../Sabre/CalDAV/Schedule/ScheduleDeliverTest.php | 18 ++--
4 files changed, 83 insertions(+), 103 deletions(-)
diff --git a/lib/CalDAV/Plugin.php b/lib/CalDAV/Plugin.php
index 1f153e0..22da299 100644
--- a/lib/CalDAV/Plugin.php
+++ b/lib/CalDAV/Plugin.php
@@ -708,7 +708,14 @@ class Plugin extends DAV\ServerPlugin {
if (!$node instanceof ICalendarObject)
return;
- $this->validateICalendar($data, $path, $modified);
+ $this->validateICalendar(
+ $data,
+ $path,
+ $modified,
+ $this->server->httpRequest,
+ $this->server->httpResponse,
+ false
+ );
}
@@ -727,10 +734,17 @@ class Plugin extends DAV\ServerPlugin {
*/
function beforeCreateFile($path, &$data, DAV\ICollection $parentNode, &$modified) {
- if (!$parentNode instanceof Calendar)
+ if (!$parentNode instanceof ICalendar)
return;
- $this->validateICalendar($data, $path, $modified);
+ $this->validateICalendar(
+ $data,
+ $path,
+ $modified,
+ $this->server->httpRequest,
+ $this->server->httpResponse,
+ true
+ );
}
@@ -743,9 +757,10 @@ class Plugin extends DAV\ServerPlugin {
* @param string $path
* @param bool $modified Should be set to true, if this event handler
* changed &$data.
+ * @param bool $isNew Is the item a new one, or an update.
* @return void
*/
- protected function validateICalendar(&$data, $path, &$modified) {
+ protected function validateICalendar(&$data, $path, &$modified, RequestInterface $request, ResponseInterface $response, $isNew) {
// If it's a stream, we convert it to a string first.
if (is_resource($data)) {
@@ -830,9 +845,36 @@ class Plugin extends DAV\ServerPlugin {
if (!$foundType)
throw new DAV\Exception\BadRequest('iCalendar object must contain at least 1 of VEVENT, VTODO or VJOURNAL');
- }
+ // We use an extra variable to allow event handles to tell us wether
+ // the object was modified or not.
+ //
+ // This helps us determine if we need to re-serialize the object.
+ $subModified = false;
+
+ $this->server->emit(
+ 'calendarObjectChange',
+ [
+ $request,
+ $response,
+ $vobj,
+ $parentPath,
+ &$subModified,
+ $isNew
+ ]
+ );
+ if ($subModified) {
+ // An event handler told us that it modified the object.
+ $data = $vobj->serialize();
+ // Using md5 to figure out if there was an *actual* change.
+ if (!$modified && $before !== md5($data)) {
+ $modified = true;
+ }
+
+ }
+
+ }
/**
diff --git a/lib/CalDAV/Schedule/ISchedulingObject.php b/lib/CalDAV/Schedule/ISchedulingObject.php
index f8b8f21..3c6e26a 100644
--- a/lib/CalDAV/Schedule/ISchedulingObject.php
+++ b/lib/CalDAV/Schedule/ISchedulingObject.php
@@ -8,6 +8,6 @@ namespace Sabre\CalDAV\Schedule;
* @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
* @copyright Copyright (C) 2007-2014 fruux GmbH. All rights reserved.
*/
-interface ISchedulingObject extends \Sabre\CalDAV\ICalendarObject {
+interface ISchedulingObject extends \Sabre\DAV\IFile {
}
diff --git a/lib/CalDAV/Schedule/Plugin.php b/lib/CalDAV/Schedule/Plugin.php
index cda147c..51e6ff1 100644
--- a/lib/CalDAV/Schedule/Plugin.php
+++ b/lib/CalDAV/Schedule/Plugin.php
@@ -100,12 +100,11 @@ class Plugin extends ServerPlugin {
function initialize(Server $server) {
$this->server = $server;
- $server->on('method:POST', [$this, 'httpPost']);
- $server->on('propFind', [$this, 'propFind']);
- $server->on('beforeCreateFile', [$this, 'beforeCreateFile'], 110);
- $server->on('beforeWriteContent', [$this, 'beforeWriteContent'], 110);
- $server->on('beforeUnbind', [$this, 'beforeUnbind']);
- $server->on('schedule', [$this, 'scheduleLocalDelivery']);
+ $server->on('method:POST', [$this, 'httpPost']);
+ $server->on('propFind', [$this, 'propFind']);
+ $server->on('calendarObjectChange', [$this, 'calendarObjectChange']);
+ $server->on('beforeUnbind', [$this, 'beforeUnbind']);
+ $server->on('schedule', [$this, 'scheduleLocalDelivery']);
$ns = '{' . self::NS_CALDAV . '}';
@@ -262,104 +261,41 @@ class Plugin extends ServerPlugin {
}
/**
- * This method is called before a new node is created.
+ * This method is triggered whenever there was a calendar object gets
+ * created or updated.
*
- * @param string $path path to new object.
- * @param string|resource $data Contents of new object.
- * @param \Sabre\DAV\INode $parentNode Parent object
- * @param bool $modified Wether or not the item's data was modified/
- * @return bool|null
- */
- function beforeCreateFile($path, &$data, $parentNode, &$modified) {
-
- if (!$parentNode instanceof ICalendar) {
- return;
- }
-
- if (!$this->scheduleReply($this->server->httpRequest)) {
- return;
- }
-
- // It's a calendar, so the contents are most likely an iCalendar
- // object. It's time to start processing this.
- //
- // This step also ensures that $data is re-propagated with a string
- // version of the object.
- if (is_resource($data)) {
- $data = stream_get_contents($data);
- }
-
- $vObj = Reader::read($data);
-
- $addresses = $this->getAddressesForPrincipal(
- $parentNode->getOwner()
- );
-
- $this->processICalendarChange(null, $vObj, $addresses);
-
- // After all this exciting action we set $data to the updated event
- // that contains all the new status information (if any).
- $newData = $vObj->serialize();
- if ($newData !== $data) {
- $data = $newData;
-
- // Setting $modified tells sabredav that the object has changed,
- // and that no ETag must be sent back.
- $modified = true;
- }
-
- }
-
- /**
- * This method is triggered before a file gets updated with new content.
- *
- * We use this event to process any changes to scheduling objects.
- *
- * @param string $path
- * @param IFile $node
- * @param resource|string $data
- * @param bool $modified
+ * @param RequestInterface $request HTTP request
+ * @param ResponseInterface $response HTTP Response
+ * @param VCalendar $vCal Parsed iCalendar object
+ * @param mixed $calendarPath Path to calendar collection
+ * @param mixed $modified The iCalendar object has been touched.
+ * @param mixed $isNew Whether this was a new item or we're updating one
* @return void
*/
- function beforeWriteContent($path, IFile $node, &$data, &$modified) {
-
- if (!$node instanceof ICalendarObject || $node instanceof ISchedulingObject) {
- return;
- }
+ function calendarObjectChange(RequestInterface $request, ResponseInterface $response, VCalendar $vCal, $calendarPath, &$modified, $isNew) {
if (!$this->scheduleReply($this->server->httpRequest)) {
return;
}
- // It's a calendar, so the contents are most likely an iCalendar
- // object. It's time to start processing this.
- //
- // This step also ensures that $data is re-propagated with a string
- // version of the object.
- if (is_resource($data)) {
- $data = stream_get_contents($data);
- }
-
- $vObj = Reader::read($data);
+ $calendarNode = $this->server->tree->getNodeForPath($calendarPath);
$addresses = $this->getAddressesForPrincipal(
- $node->getOwner()
+ $calendarNode->getOwner()
);
- $oldObj = Reader::read($node->get());
+ $broker = new ITip\Broker();
- $this->processICalendarChange($oldObj, $vObj, $addresses);
+ if (!$isNew) {
+ $node = $this->server->tree->getNodeForPath($request->getPath());
+ $oldObj = Reader::read($node->get());
+ } else {
+ $oldObj = null;
+ }
- // After all this exciting action we set $data to the updated event
- // that contains all the new status information (if any).
- $newData = $vObj->serialize();
- if ($newData !== $data) {
- $data = $newData;
+ $this->processICalendarChange($oldObj, $vCal, $addresses);
+ $modified = true;
- // Setting $modified tells sabredav that the object has changed,
- // and that no ETag must be sent back.
- $modified = true;
- }
}
/**
diff --git a/tests/Sabre/CalDAV/Schedule/ScheduleDeliverTest.php b/tests/Sabre/CalDAV/Schedule/ScheduleDeliverTest.php
index 5535c1f..292694e 100644
--- a/tests/Sabre/CalDAV/Schedule/ScheduleDeliverTest.php
+++ b/tests/Sabre/CalDAV/Schedule/ScheduleDeliverTest.php
@@ -555,6 +555,7 @@ ICS;
function deliver($oldObject, &$newObject, $disableScheduling = false) {
+ $this->server->httpRequest->setUrl($this->calendarObjectUri);
if ($disableScheduling) {
$this->server->httpRequest->setHeader('Schedule-Reply','F');
}
@@ -568,12 +569,12 @@ ICS;
rewind($stream);
$modified = false;
- $this->caldavSchedulePlugin->beforeWriteContent(
+ $this->server->emit('beforeWriteContent', [
$this->calendarObjectUri,
$this->server->tree->getNodeForPath($this->calendarObjectUri),
- $stream,
- $modified
- );
+ &$stream,
+ &$modified
+ ]);
if ($modified) {
$newObject = $stream;
}
@@ -586,17 +587,18 @@ ICS;
$this->calendarObjectUri
);
} else {
+
// create
$stream = fopen('php://memory','r+');
fwrite($stream, $newObject);
rewind($stream);
$modified = false;
- $this->caldavSchedulePlugin->beforeCreateFile(
+ $this->server->emit('beforeCreateFile', [
$this->calendarObjectUri,
- $stream,
+ &$stream,
$this->server->tree->getNodeForPath(dirname($this->calendarObjectUri)),
- $modified
- );
+ &$modified
+ ]);
if ($modified) {
$newObject = $stream;
--
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