merge the two almost identical custom property backends
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
ca54813cbb
commit
ce398cf7bd
|
@ -1,351 +0,0 @@
|
||||||
<?php
|
|
||||||
/**
|
|
||||||
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
||||||
*
|
|
||||||
* @author Aaron Wood <aaronjwood@gmail.com>
|
|
||||||
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
||||||
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
||||||
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
||||||
* @author Vincent Petry <pvince81@owncloud.com>
|
|
||||||
*
|
|
||||||
* @license AGPL-3.0
|
|
||||||
*
|
|
||||||
* This code is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero General Public License, version 3,
|
|
||||||
* as published by the Free Software Foundation.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace OCA\DAV\Connector\Sabre;
|
|
||||||
|
|
||||||
use OCP\IDBConnection;
|
|
||||||
use OCP\IUser;
|
|
||||||
use Sabre\DAV\Exception\NotFound;
|
|
||||||
use Sabre\DAV\Exception\ServiceUnavailable;
|
|
||||||
use Sabre\DAV\PropertyStorage\Backend\BackendInterface;
|
|
||||||
use Sabre\DAV\PropFind;
|
|
||||||
use Sabre\DAV\PropPatch;
|
|
||||||
use Sabre\DAV\Tree;
|
|
||||||
|
|
||||||
class CustomPropertiesBackend implements BackendInterface {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ignored properties
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $ignoredProperties = array(
|
|
||||||
'{DAV:}getcontentlength',
|
|
||||||
'{DAV:}getcontenttype',
|
|
||||||
'{DAV:}getetag',
|
|
||||||
'{DAV:}quota-used-bytes',
|
|
||||||
'{DAV:}quota-available-bytes',
|
|
||||||
'{http://owncloud.org/ns}permissions',
|
|
||||||
'{http://owncloud.org/ns}downloadURL',
|
|
||||||
'{http://owncloud.org/ns}dDC',
|
|
||||||
'{http://owncloud.org/ns}size',
|
|
||||||
'{http://nextcloud.org/ns}is-encrypted',
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Tree
|
|
||||||
*/
|
|
||||||
private $tree;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IDBConnection
|
|
||||||
*/
|
|
||||||
private $connection;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IUser
|
|
||||||
*/
|
|
||||||
private $user;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Properties cache
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $cache = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Tree $tree node tree
|
|
||||||
* @param IDBConnection $connection database connection
|
|
||||||
* @param IUser $user owner of the tree and properties
|
|
||||||
*/
|
|
||||||
public function __construct(
|
|
||||||
Tree $tree,
|
|
||||||
IDBConnection $connection,
|
|
||||||
IUser $user) {
|
|
||||||
$this->tree = $tree;
|
|
||||||
$this->connection = $connection;
|
|
||||||
$this->user = $user->getUID();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches properties for a path.
|
|
||||||
*
|
|
||||||
* @param string $path
|
|
||||||
* @param PropFind $propFind
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function propFind($path, PropFind $propFind) {
|
|
||||||
try {
|
|
||||||
$node = $this->tree->getNodeForPath($path);
|
|
||||||
if (!($node instanceof Node)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch (ServiceUnavailable $e) {
|
|
||||||
// might happen for unavailable mount points, skip
|
|
||||||
return;
|
|
||||||
} catch (NotFound $e) {
|
|
||||||
// in some rare (buggy) cases the node might not be found,
|
|
||||||
// we catch the exception to prevent breaking the whole list with a 404
|
|
||||||
// (soft fail)
|
|
||||||
\OC::$server->getLogger()->warning(
|
|
||||||
'Could not get node for path: \"' . $path . '\" : ' . $e->getMessage(),
|
|
||||||
array('app' => 'files')
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$requestedProps = $propFind->get404Properties();
|
|
||||||
|
|
||||||
// these might appear
|
|
||||||
$requestedProps = array_diff(
|
|
||||||
$requestedProps,
|
|
||||||
$this->ignoredProperties
|
|
||||||
);
|
|
||||||
|
|
||||||
if (empty($requestedProps)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$props = $this->getProperties($node, $requestedProps);
|
|
||||||
foreach ($props as $propName => $propValue) {
|
|
||||||
$propFind->set($propName, $propValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates properties for a path
|
|
||||||
*
|
|
||||||
* @param string $path
|
|
||||||
* @param PropPatch $propPatch
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function propPatch($path, PropPatch $propPatch) {
|
|
||||||
$node = $this->tree->getNodeForPath($path);
|
|
||||||
if (!($node instanceof Node)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$propPatch->handleRemaining(function($changedProps) use ($node) {
|
|
||||||
return $this->updateProperties($node, $changedProps);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called after a node is deleted.
|
|
||||||
*
|
|
||||||
* @param string $path path of node for which to delete properties
|
|
||||||
*/
|
|
||||||
public function delete($path) {
|
|
||||||
$statement = $this->connection->prepare(
|
|
||||||
'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'
|
|
||||||
);
|
|
||||||
$statement->execute(array($this->user, '/' . $path));
|
|
||||||
$statement->closeCursor();
|
|
||||||
|
|
||||||
unset($this->cache[$path]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called after a successful MOVE
|
|
||||||
*
|
|
||||||
* @param string $source
|
|
||||||
* @param string $destination
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function move($source, $destination) {
|
|
||||||
$statement = $this->connection->prepare(
|
|
||||||
'UPDATE `*PREFIX*properties` SET `propertypath` = ?' .
|
|
||||||
' WHERE `userid` = ? AND `propertypath` = ?'
|
|
||||||
);
|
|
||||||
$statement->execute(array('/' . $destination, $this->user, '/' . $source));
|
|
||||||
$statement->closeCursor();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a list of properties for this nodes.;
|
|
||||||
* @param Node $node
|
|
||||||
* @param array $requestedProperties requested properties or empty array for "all"
|
|
||||||
* @return array
|
|
||||||
* @note The properties list is a list of propertynames the client
|
|
||||||
* requested, encoded as xmlnamespace#tagName, for example:
|
|
||||||
* http://www.example.org/namespace#author If the array is empty, all
|
|
||||||
* properties should be returned
|
|
||||||
*/
|
|
||||||
private function getProperties(Node $node, array $requestedProperties) {
|
|
||||||
$path = $node->getPath();
|
|
||||||
if (isset($this->cache[$path])) {
|
|
||||||
return $this->cache[$path];
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: chunking if more than 1000 properties
|
|
||||||
$sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?';
|
|
||||||
|
|
||||||
$whereValues = array($this->user, $path);
|
|
||||||
$whereTypes = array(null, null);
|
|
||||||
|
|
||||||
if (!empty($requestedProperties)) {
|
|
||||||
// request only a subset
|
|
||||||
$sql .= ' AND `propertyname` in (?)';
|
|
||||||
$whereValues[] = $requestedProperties;
|
|
||||||
$whereTypes[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY;
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = $this->connection->executeQuery(
|
|
||||||
$sql,
|
|
||||||
$whereValues,
|
|
||||||
$whereTypes
|
|
||||||
);
|
|
||||||
|
|
||||||
$props = [];
|
|
||||||
while ($row = $result->fetch()) {
|
|
||||||
$props[$row['propertyname']] = $row['propertyvalue'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$result->closeCursor();
|
|
||||||
|
|
||||||
$this->cache[$path] = $props;
|
|
||||||
return $props;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update properties
|
|
||||||
*
|
|
||||||
* @param Node $node node for which to update properties
|
|
||||||
* @param array $properties array of properties to update
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function updateProperties($node, $properties) {
|
|
||||||
$path = $node->getPath();
|
|
||||||
|
|
||||||
$deleteStatement = 'DELETE FROM `*PREFIX*properties`' .
|
|
||||||
' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
|
|
||||||
|
|
||||||
$insertStatement = 'INSERT INTO `*PREFIX*properties`' .
|
|
||||||
' (`userid`,`propertypath`,`propertyname`,`propertyvalue`) VALUES(?,?,?,?)';
|
|
||||||
|
|
||||||
$updateStatement = 'UPDATE `*PREFIX*properties` SET `propertyvalue` = ?' .
|
|
||||||
' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
|
|
||||||
|
|
||||||
// TODO: use "insert or update" strategy ?
|
|
||||||
$existing = $this->getProperties($node, array());
|
|
||||||
$this->connection->beginTransaction();
|
|
||||||
foreach ($properties as $propertyName => $propertyValue) {
|
|
||||||
// If it was null, we need to delete the property
|
|
||||||
if (is_null($propertyValue)) {
|
|
||||||
if (array_key_exists($propertyName, $existing)) {
|
|
||||||
$this->connection->executeUpdate($deleteStatement,
|
|
||||||
array(
|
|
||||||
$this->user,
|
|
||||||
$path,
|
|
||||||
$propertyName
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!array_key_exists($propertyName, $existing)) {
|
|
||||||
$this->connection->executeUpdate($insertStatement,
|
|
||||||
array(
|
|
||||||
$this->user,
|
|
||||||
$path,
|
|
||||||
$propertyName,
|
|
||||||
$propertyValue
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
$this->connection->executeUpdate($updateStatement,
|
|
||||||
array(
|
|
||||||
$propertyValue,
|
|
||||||
$this->user,
|
|
||||||
$path,
|
|
||||||
$propertyName
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->connection->commit();
|
|
||||||
unset($this->cache[$path]);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bulk load properties for directory children
|
|
||||||
*
|
|
||||||
* @param Directory $node
|
|
||||||
* @param array $requestedProperties requested properties
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function loadChildrenProperties(Directory $node, $requestedProperties) {
|
|
||||||
$path = $node->getPath();
|
|
||||||
if (isset($this->cache[$path])) {
|
|
||||||
// we already loaded them at some point
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$childNodes = $node->getChildren();
|
|
||||||
// pre-fill cache
|
|
||||||
foreach ($childNodes as $childNode) {
|
|
||||||
$this->cache[$childNode->getPath()] = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` LIKE ?';
|
|
||||||
$sql .= ' AND `propertyname` in (?) ORDER BY `propertypath`, `propertyname`';
|
|
||||||
|
|
||||||
$result = $this->connection->executeQuery(
|
|
||||||
$sql,
|
|
||||||
array($this->user, $this->connection->escapeLikeParameter(rtrim($path, '/')) . '/%', $requestedProperties),
|
|
||||||
array(null, null, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY)
|
|
||||||
);
|
|
||||||
|
|
||||||
$oldPath = null;
|
|
||||||
$props = [];
|
|
||||||
while ($row = $result->fetch()) {
|
|
||||||
$path = $row['propertypath'];
|
|
||||||
if ($oldPath !== $path) {
|
|
||||||
// save previously gathered props
|
|
||||||
$this->cache[$oldPath] = $props;
|
|
||||||
$oldPath = $path;
|
|
||||||
// prepare props for next path
|
|
||||||
$props = [];
|
|
||||||
}
|
|
||||||
$props[$row['propertyname']] = $row['propertyvalue'];
|
|
||||||
}
|
|
||||||
if (!is_null($oldPath)) {
|
|
||||||
// save props from last run
|
|
||||||
$this->cache[$oldPath] = $props;
|
|
||||||
}
|
|
||||||
|
|
||||||
$result->closeCursor();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -195,7 +195,7 @@ class ServerFactory {
|
||||||
// custom properties plugin must be the last one
|
// custom properties plugin must be the last one
|
||||||
$server->addPlugin(
|
$server->addPlugin(
|
||||||
new \Sabre\DAV\PropertyStorage\Plugin(
|
new \Sabre\DAV\PropertyStorage\Plugin(
|
||||||
new \OCA\DAV\Connector\Sabre\CustomPropertiesBackend(
|
new \OCA\DAV\DAV\CustomPropertiesBackend(
|
||||||
$objectTree,
|
$objectTree,
|
||||||
$this->databaseConnection,
|
$this->databaseConnection,
|
||||||
$this->userSession->getUser()
|
$this->userSession->getUser()
|
||||||
|
|
|
@ -3,9 +3,11 @@
|
||||||
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
||||||
* @copyright Copyright (c) 2017, Georg Ehrke <oc.list@georgehrke.com>
|
* @copyright Copyright (c) 2017, Georg Ehrke <oc.list@georgehrke.com>
|
||||||
*
|
*
|
||||||
* @author Georg Ehrke <oc.list@georgehrke.com>
|
* @author Aaron Wood <aaronjwood@gmail.com>
|
||||||
* @author Robin Appelman <robin@icewind.nl>
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
||||||
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
||||||
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
||||||
|
* @author Vincent Petry <pvince81@owncloud.com>
|
||||||
*
|
*
|
||||||
* @license AGPL-3.0
|
* @license AGPL-3.0
|
||||||
*
|
*
|
||||||
|
@ -25,8 +27,12 @@
|
||||||
|
|
||||||
namespace OCA\DAV\DAV;
|
namespace OCA\DAV\DAV;
|
||||||
|
|
||||||
|
use OCA\DAV\Connector\Sabre\Directory;
|
||||||
|
use OCA\DAV\Connector\Sabre\Node;
|
||||||
use OCP\IDBConnection;
|
use OCP\IDBConnection;
|
||||||
use OCP\IUser;
|
use OCP\IUser;
|
||||||
|
use Sabre\DAV\Exception\NotFound;
|
||||||
|
use Sabre\DAV\Exception\ServiceUnavailable;
|
||||||
use Sabre\DAV\PropertyStorage\Backend\BackendInterface;
|
use Sabre\DAV\PropertyStorage\Backend\BackendInterface;
|
||||||
use Sabre\DAV\PropFind;
|
use Sabre\DAV\PropFind;
|
||||||
use Sabre\DAV\PropPatch;
|
use Sabre\DAV\PropPatch;
|
||||||
|
@ -63,7 +69,7 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
private $connection;
|
private $connection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var IUser
|
||||||
*/
|
*/
|
||||||
private $user;
|
private $user;
|
||||||
|
|
||||||
|
@ -85,7 +91,7 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
IUser $user) {
|
IUser $user) {
|
||||||
$this->tree = $tree;
|
$this->tree = $tree;
|
||||||
$this->connection = $connection;
|
$this->connection = $connection;
|
||||||
$this->user = $user->getUID();
|
$this->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -96,6 +102,24 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function propFind($path, PropFind $propFind) {
|
public function propFind($path, PropFind $propFind) {
|
||||||
|
try {
|
||||||
|
$node = $this->tree->getNodeForPath($path);
|
||||||
|
if (!($node instanceof Node)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (ServiceUnavailable $e) {
|
||||||
|
// might happen for unavailable mount points, skip
|
||||||
|
return;
|
||||||
|
} catch (NotFound $e) {
|
||||||
|
// in some rare (buggy) cases the node might not be found,
|
||||||
|
// we catch the exception to prevent breaking the whole list with a 404
|
||||||
|
// (soft fail)
|
||||||
|
\OC::$server->getLogger()->warning(
|
||||||
|
'Could not get node for path: \"' . $path . '\" : ' . $e->getMessage(),
|
||||||
|
array('app' => 'files')
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$requestedProps = $propFind->get404Properties();
|
$requestedProps = $propFind->get404Properties();
|
||||||
|
|
||||||
|
@ -129,7 +153,7 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$props = $this->getProperties($path, $requestedProps);
|
$props = $this->getProperties($node, $requestedProps);
|
||||||
foreach ($props as $propName => $propValue) {
|
foreach ($props as $propName => $propValue) {
|
||||||
$propFind->set($propName, $propValue);
|
$propFind->set($propName, $propValue);
|
||||||
}
|
}
|
||||||
|
@ -144,8 +168,13 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function propPatch($path, PropPatch $propPatch) {
|
public function propPatch($path, PropPatch $propPatch) {
|
||||||
$propPatch->handleRemaining(function($changedProps) use ($path) {
|
$node = $this->tree->getNodeForPath($path);
|
||||||
return $this->updateProperties($path, $changedProps);
|
if (!($node instanceof Node)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$propPatch->handleRemaining(function($changedProps) use ($node) {
|
||||||
|
return $this->updateProperties($node, $changedProps);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,7 +187,7 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
$statement = $this->connection->prepare(
|
$statement = $this->connection->prepare(
|
||||||
'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'
|
'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'
|
||||||
);
|
);
|
||||||
$statement->execute(array($this->user, $path));
|
$statement->execute(array($this->user->getUID(), $path));
|
||||||
$statement->closeCursor();
|
$statement->closeCursor();
|
||||||
|
|
||||||
unset($this->cache[$path]);
|
unset($this->cache[$path]);
|
||||||
|
@ -177,13 +206,13 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
'UPDATE `*PREFIX*properties` SET `propertypath` = ?' .
|
'UPDATE `*PREFIX*properties` SET `propertypath` = ?' .
|
||||||
' WHERE `userid` = ? AND `propertypath` = ?'
|
' WHERE `userid` = ? AND `propertypath` = ?'
|
||||||
);
|
);
|
||||||
$statement->execute(array($destination, $this->user, $source));
|
$statement->execute(array($destination, $this->user->getUID(), $source));
|
||||||
$statement->closeCursor();
|
$statement->closeCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of properties for this nodes.;
|
* Returns a list of properties for this nodes.;
|
||||||
* @param string $path
|
* @param Node $node
|
||||||
* @param array $requestedProperties requested properties or empty array for "all"
|
* @param array $requestedProperties requested properties or empty array for "all"
|
||||||
* @return array
|
* @return array
|
||||||
* @note The properties list is a list of propertynames the client
|
* @note The properties list is a list of propertynames the client
|
||||||
|
@ -191,7 +220,8 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
* http://www.example.org/namespace#author If the array is empty, all
|
* http://www.example.org/namespace#author If the array is empty, all
|
||||||
* properties should be returned
|
* properties should be returned
|
||||||
*/
|
*/
|
||||||
private function getProperties($path, array $requestedProperties) {
|
private function getProperties(Node $node, array $requestedProperties) {
|
||||||
|
$path = $node->getPath();
|
||||||
if (isset($this->cache[$path])) {
|
if (isset($this->cache[$path])) {
|
||||||
return $this->cache[$path];
|
return $this->cache[$path];
|
||||||
}
|
}
|
||||||
|
@ -199,7 +229,7 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
// TODO: chunking if more than 1000 properties
|
// TODO: chunking if more than 1000 properties
|
||||||
$sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?';
|
$sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?';
|
||||||
|
|
||||||
$whereValues = array($this->user, $path);
|
$whereValues = array($this->user->getUID(), $path);
|
||||||
$whereTypes = array(null, null);
|
$whereTypes = array(null, null);
|
||||||
|
|
||||||
if (!empty($requestedProperties)) {
|
if (!empty($requestedProperties)) {
|
||||||
|
@ -229,12 +259,13 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
/**
|
/**
|
||||||
* Update properties
|
* Update properties
|
||||||
*
|
*
|
||||||
* @param string $path node for which to update properties
|
* @param Node $node node for which to update properties
|
||||||
* @param array $properties array of properties to update
|
* @param array $properties array of properties to update
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function updateProperties($path, $properties) {
|
private function updateProperties($node, $properties) {
|
||||||
|
$path = $node->getPath();
|
||||||
|
|
||||||
$deleteStatement = 'DELETE FROM `*PREFIX*properties`' .
|
$deleteStatement = 'DELETE FROM `*PREFIX*properties`' .
|
||||||
' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
|
' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
|
||||||
|
@ -246,7 +277,7 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
|
' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
|
||||||
|
|
||||||
// TODO: use "insert or update" strategy ?
|
// TODO: use "insert or update" strategy ?
|
||||||
$existing = $this->getProperties($path, array());
|
$existing = $this->getProperties($node, array());
|
||||||
$this->connection->beginTransaction();
|
$this->connection->beginTransaction();
|
||||||
foreach ($properties as $propertyName => $propertyValue) {
|
foreach ($properties as $propertyName => $propertyValue) {
|
||||||
// If it was null, we need to delete the property
|
// If it was null, we need to delete the property
|
||||||
|
@ -254,7 +285,7 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
if (array_key_exists($propertyName, $existing)) {
|
if (array_key_exists($propertyName, $existing)) {
|
||||||
$this->connection->executeUpdate($deleteStatement,
|
$this->connection->executeUpdate($deleteStatement,
|
||||||
array(
|
array(
|
||||||
$this->user,
|
$this->user->getUID(),
|
||||||
$path,
|
$path,
|
||||||
$propertyName
|
$propertyName
|
||||||
)
|
)
|
||||||
|
@ -264,7 +295,7 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
if (!array_key_exists($propertyName, $existing)) {
|
if (!array_key_exists($propertyName, $existing)) {
|
||||||
$this->connection->executeUpdate($insertStatement,
|
$this->connection->executeUpdate($insertStatement,
|
||||||
array(
|
array(
|
||||||
$this->user,
|
$this->user->getUID(),
|
||||||
$path,
|
$path,
|
||||||
$propertyName,
|
$propertyName,
|
||||||
$propertyValue
|
$propertyValue
|
||||||
|
@ -274,7 +305,7 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
$this->connection->executeUpdate($updateStatement,
|
$this->connection->executeUpdate($updateStatement,
|
||||||
array(
|
array(
|
||||||
$propertyValue,
|
$propertyValue,
|
||||||
$this->user,
|
$this->user->getUID(),
|
||||||
$path,
|
$path,
|
||||||
$propertyName
|
$propertyName
|
||||||
)
|
)
|
||||||
|
@ -289,4 +320,55 @@ class CustomPropertiesBackend implements BackendInterface {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bulk load properties for directory children
|
||||||
|
*
|
||||||
|
* @param Directory $node
|
||||||
|
* @param array $requestedProperties requested properties
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function loadChildrenProperties(Directory $node, $requestedProperties) {
|
||||||
|
$path = $node->getPath();
|
||||||
|
if (isset($this->cache[$path])) {
|
||||||
|
// we already loaded them at some point
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$childNodes = $node->getChildren();
|
||||||
|
// pre-fill cache
|
||||||
|
foreach ($childNodes as $childNode) {
|
||||||
|
$this->cache[$childNode->getPath()] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` LIKE ?';
|
||||||
|
$sql .= ' AND `propertyname` in (?) ORDER BY `propertypath`, `propertyname`';
|
||||||
|
|
||||||
|
$result = $this->connection->executeQuery(
|
||||||
|
$sql,
|
||||||
|
array($this->user->getUID(), $this->connection->escapeLikeParameter(rtrim($path, '/')) . '/%', $requestedProperties),
|
||||||
|
array(null, null, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY)
|
||||||
|
);
|
||||||
|
|
||||||
|
$oldPath = null;
|
||||||
|
$props = [];
|
||||||
|
while ($row = $result->fetch()) {
|
||||||
|
$path = $row['propertypath'];
|
||||||
|
if ($oldPath !== $path) {
|
||||||
|
// save previously gathered props
|
||||||
|
$this->cache[$oldPath] = $props;
|
||||||
|
$oldPath = $path;
|
||||||
|
// prepare props for next path
|
||||||
|
$props = [];
|
||||||
|
}
|
||||||
|
$props[$row['propertyname']] = $row['propertyvalue'];
|
||||||
|
}
|
||||||
|
if (!is_null($oldPath)) {
|
||||||
|
// save props from last run
|
||||||
|
$this->cache[$oldPath] = $props;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result->closeCursor();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,7 @@ class CustomPropertiesBackendTest extends \Test\TestCase {
|
||||||
private $tree;
|
private $tree;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \OCA\DAV\Connector\Sabre\CustomPropertiesBackend
|
* @var \OCA\DAV\DAV\CustomPropertiesBackend
|
||||||
*/
|
*/
|
||||||
private $plugin;
|
private $plugin;
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ class CustomPropertiesBackendTest extends \Test\TestCase {
|
||||||
->method('getUID')
|
->method('getUID')
|
||||||
->will($this->returnValue($userId));
|
->will($this->returnValue($userId));
|
||||||
|
|
||||||
$this->plugin = new \OCA\DAV\Connector\Sabre\CustomPropertiesBackend(
|
$this->plugin = new \OCA\DAV\DAV\CustomPropertiesBackend(
|
||||||
$this->tree,
|
$this->tree,
|
||||||
\OC::$server->getDatabaseConnection(),
|
\OC::$server->getDatabaseConnection(),
|
||||||
$this->user
|
$this->user
|
||||||
|
|
|
@ -24,9 +24,11 @@
|
||||||
|
|
||||||
namespace OCA\DAV\Tests\DAV;
|
namespace OCA\DAV\Tests\DAV;
|
||||||
|
|
||||||
|
use OCA\DAV\Connector\Sabre\Node;
|
||||||
use OCA\DAV\DAV\CustomPropertiesBackend;
|
use OCA\DAV\DAV\CustomPropertiesBackend;
|
||||||
use OCP\IDBConnection;
|
use OCP\IDBConnection;
|
||||||
use OCP\IUser;
|
use OCP\IUser;
|
||||||
|
use Sabre\DAV\Exception\NotFound;
|
||||||
use Sabre\DAV\PropFind;
|
use Sabre\DAV\PropFind;
|
||||||
use Sabre\DAV\PropPatch;
|
use Sabre\DAV\PropPatch;
|
||||||
use Sabre\DAV\Tree;
|
use Sabre\DAV\Tree;
|
||||||
|
@ -46,19 +48,42 @@ class CustomPropertiesBackendTest extends TestCase {
|
||||||
/** @var CustomPropertiesBackend | \PHPUnit_Framework_MockObject_MockObject */
|
/** @var CustomPropertiesBackend | \PHPUnit_Framework_MockObject_MockObject */
|
||||||
private $backend;
|
private $backend;
|
||||||
|
|
||||||
|
/** @var (Node | \PHPUnit_Framework_MockObject_MockObject)[] */
|
||||||
|
private $nodes = [];
|
||||||
|
|
||||||
protected function setUp(): void {
|
protected function setUp(): void {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->tree = $this->createMock(Tree::class);
|
$this->tree = $this->createMock(Tree::class);
|
||||||
$this->dbConnection = $this->createMock(IDBConnection::class);
|
$this->dbConnection = $this->createMock(IDBConnection::class);
|
||||||
$this->user = $this->createMock(IUser::class);
|
$this->user = $this->createMock(IUser::class);
|
||||||
$this->user->expects($this->once())
|
$this->user->method('getUID')
|
||||||
->method('getUID')
|
|
||||||
->with()
|
->with()
|
||||||
->will($this->returnValue('dummy_user_42'));
|
->will($this->returnValue('dummy_user_42'));
|
||||||
|
|
||||||
$this->backend = new CustomPropertiesBackend($this->tree,
|
$this->backend = new CustomPropertiesBackend($this->tree,
|
||||||
$this->dbConnection, $this->user);
|
$this->dbConnection, $this->user);
|
||||||
|
|
||||||
|
$this->tree->method('getNodeForPath')
|
||||||
|
->willReturnCallback(function ($path) {
|
||||||
|
if (isset($this->nodes[$path])) {
|
||||||
|
return $this->nodes[$path];
|
||||||
|
} else {
|
||||||
|
throw new NotFound();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @return Node|\PHPUnit\Framework\MockObject\MockObject
|
||||||
|
*/
|
||||||
|
private function addNode($path) {
|
||||||
|
$node = $this->createMock(Node::class);
|
||||||
|
$node->method('getPath')
|
||||||
|
->willReturn($path);
|
||||||
|
$this->nodes[$path] = $node;
|
||||||
|
return $node;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPropFindNoDbCalls() {
|
public function testPropFindNoDbCalls() {
|
||||||
|
@ -76,6 +101,7 @@ class CustomPropertiesBackendTest extends TestCase {
|
||||||
$this->dbConnection->expects($this->never())
|
$this->dbConnection->expects($this->never())
|
||||||
->method($this->anything());
|
->method($this->anything());
|
||||||
|
|
||||||
|
$this->addNode('foo_bar_path_1337_0');
|
||||||
$this->backend->propFind('foo_bar_path_1337_0', $propFind);
|
$this->backend->propFind('foo_bar_path_1337_0', $propFind);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +114,7 @@ class CustomPropertiesBackendTest extends TestCase {
|
||||||
'{DAV:}getcontentlength',
|
'{DAV:}getcontentlength',
|
||||||
'{DAV:}getcontenttype',
|
'{DAV:}getcontenttype',
|
||||||
'{DAV:}getetag',
|
'{DAV:}getetag',
|
||||||
'{abc}def'
|
'{abc}def',
|
||||||
]));
|
]));
|
||||||
|
|
||||||
$propFind->expects($this->at(1))
|
$propFind->expects($this->at(1))
|
||||||
|
@ -101,7 +127,7 @@ class CustomPropertiesBackendTest extends TestCase {
|
||||||
'{DAV:}displayname',
|
'{DAV:}displayname',
|
||||||
'{urn:ietf:params:xml:ns:caldav}calendar-description',
|
'{urn:ietf:params:xml:ns:caldav}calendar-description',
|
||||||
'{urn:ietf:params:xml:ns:caldav}calendar-timezone',
|
'{urn:ietf:params:xml:ns:caldav}calendar-timezone',
|
||||||
'{abc}def'
|
'{abc}def',
|
||||||
]));
|
]));
|
||||||
|
|
||||||
$statement = $this->createMock('\Doctrine\DBAL\Driver\Statement');
|
$statement = $this->createMock('\Doctrine\DBAL\Driver\Statement');
|
||||||
|
@ -116,6 +142,7 @@ class CustomPropertiesBackendTest extends TestCase {
|
||||||
[null, null, 102])
|
[null, null, 102])
|
||||||
->will($this->returnValue($statement));
|
->will($this->returnValue($statement));
|
||||||
|
|
||||||
|
$this->addNode('calendars/foo/bar_path_1337_0');
|
||||||
$this->backend->propFind('calendars/foo/bar_path_1337_0', $propFind);
|
$this->backend->propFind('calendars/foo/bar_path_1337_0', $propFind);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,6 +150,7 @@ class CustomPropertiesBackendTest extends TestCase {
|
||||||
* @dataProvider propPatchProvider
|
* @dataProvider propPatchProvider
|
||||||
*/
|
*/
|
||||||
public function testPropPatch($path, $propPatch) {
|
public function testPropPatch($path, $propPatch) {
|
||||||
|
$this->addNode($path);
|
||||||
$propPatch->expects($this->once())
|
$propPatch->expects($this->once())
|
||||||
->method('handleRemaining');
|
->method('handleRemaining');
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue