diff options
author | Robin Appelman <robin@icewind.nl> | 2017-03-08 13:09:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-08 13:09:19 +0100 |
commit | 2a8e922d67a1246e101f926f1b0ab287db71929e (patch) | |
tree | c3f67150e3e03386eedc6a23b1c2dcf657e4459b /apps | |
parent | 74ac5dffbd07f9a7ac9a248eeafaa0f2852b5f79 (diff) | |
parent | a3e638709b4702156a3ddc0791de1d6ce9fb902e (diff) | |
download | nextcloud-server-2a8e922d67a1246e101f926f1b0ab287db71929e.tar.gz nextcloud-server-2a8e922d67a1246e101f926f1b0ab287db71929e.zip |
Merge pull request #3360 from nextcloud/dav-search
Implement webdav SEARCH
Diffstat (limited to 'apps')
-rw-r--r-- | apps/dav/lib/Files/FileSearchBackend.php | 265 | ||||
-rw-r--r-- | apps/dav/lib/Server.php | 8 | ||||
-rw-r--r-- | apps/dav/tests/unit/Files/FileSearchBackendTest.php | 299 |
3 files changed, 572 insertions, 0 deletions
diff --git a/apps/dav/lib/Files/FileSearchBackend.php b/apps/dav/lib/Files/FileSearchBackend.php new file mode 100644 index 00000000000..c429a1727f8 --- /dev/null +++ b/apps/dav/lib/Files/FileSearchBackend.php @@ -0,0 +1,265 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\DAV\Files; + +use OC\Files\Search\SearchBinaryOperator; +use OC\Files\Search\SearchComparison; +use OC\Files\Search\SearchOrder; +use OC\Files\Search\SearchQuery; +use OC\Files\View; +use OCA\DAV\Connector\Sabre\Directory; +use OCA\DAV\Connector\Sabre\FilesPlugin; +use OCP\Files\Cache\ICacheEntry; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\Files\Node; +use OCP\Files\Search\ISearchOperator; +use OCP\Files\Search\ISearchOrder; +use OCP\Files\Search\ISearchQuery; +use OCP\IUser; +use OCP\Share\IManager; +use Sabre\DAV\Exception\NotFound; +use Sabre\DAV\Tree; +use SearchDAV\Backend\ISearchBackend; +use SearchDAV\Backend\SearchPropertyDefinition; +use SearchDAV\Backend\SearchResult; +use SearchDAV\XML\BasicSearch; +use SearchDAV\XML\Literal; +use SearchDAV\XML\Operator; +use SearchDAV\XML\Order; + +class FileSearchBackend implements ISearchBackend { + /** @var Tree */ + private $tree; + + /** @var IUser */ + private $user; + + /** @var IRootFolder */ + private $rootFolder; + + /** @var IManager */ + private $shareManager; + + /** @var View */ + private $view; + + /** + * FileSearchBackend constructor. + * + * @param Tree $tree + * @param IUser $user + * @param IRootFolder $rootFolder + * @param IManager $shareManager + * @param View $view + * @internal param IRootFolder $rootFolder + */ + public function __construct(Tree $tree, IUser $user, IRootFolder $rootFolder, IManager $shareManager, View $view) { + $this->tree = $tree; + $this->user = $user; + $this->rootFolder = $rootFolder; + $this->shareManager = $shareManager; + $this->view = $view; + } + + /** + * Search endpoint will be remote.php/dav + * + * @return string + */ + public function getArbiterPath() { + return ''; + } + + public function isValidScope($href, $depth, $path) { + // only allow scopes inside the dav server + if (is_null($path)) { + return false; + } + + try { + $node = $this->tree->getNodeForPath($path); + return $node instanceof Directory; + } catch (NotFound $e) { + return false; + } + } + + public function getPropertyDefinitionsForScope($href, $path) { + // all valid scopes support the same schema + + //todo dynamically load all propfind properties that are supported + return [ + // queryable properties + new SearchPropertyDefinition('{DAV:}displayname', true, false, true), + new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true), + new SearchPropertyDefinition('{DAV:}getlastmodifed', true, true, true, SearchPropertyDefinition::DATATYPE_DATETIME), + new SearchPropertyDefinition(FilesPlugin::SIZE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER), + + // select only properties + new SearchPropertyDefinition('{DAV:}resourcetype', false, true, false), + new SearchPropertyDefinition('{DAV:}getcontentlength', false, true, false), + new SearchPropertyDefinition(FilesPlugin::CHECKSUMS_PROPERTYNAME, false, true, false), + new SearchPropertyDefinition(FilesPlugin::PERMISSIONS_PROPERTYNAME, false, true, false), + new SearchPropertyDefinition(FilesPlugin::GETETAG_PROPERTYNAME, false, true, false), + new SearchPropertyDefinition(FilesPlugin::OWNER_ID_PROPERTYNAME, false, true, false), + new SearchPropertyDefinition(FilesPlugin::OWNER_DISPLAY_NAME_PROPERTYNAME, false, true, false), + new SearchPropertyDefinition(FilesPlugin::DATA_FINGERPRINT_PROPERTYNAME, false, true, false), + new SearchPropertyDefinition(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, false, true, false, SearchPropertyDefinition::DATATYPE_BOOLEAN), + new SearchPropertyDefinition(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, false, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER), + new SearchPropertyDefinition(FilesPlugin::FILEID_PROPERTYNAME, false, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER), + ]; + } + + /** + * @param BasicSearch $search + * @return SearchResult[] + */ + public function search(BasicSearch $search) { + if (count($search->from) !== 1) { + throw new \InvalidArgumentException('Searching more than one folder is not supported'); + } + $query = $this->transformQuery($search); + $scope = $search->from[0]; + if ($scope->path === null) { + throw new \InvalidArgumentException('Using uri\'s as scope is not supported, please use a path relative to the search arbiter instead'); + } + $node = $this->tree->getNodeForPath($scope->path); + if (!$node instanceof Directory) { + throw new \InvalidArgumentException('Search is only supported on directories'); + } + + $fileInfo = $node->getFileInfo(); + $folder = $this->rootFolder->get($fileInfo->getPath()); + /** @var Folder $folder $results */ + $results = $folder->search($query); + + return array_map(function (Node $node) { + if ($node instanceof Folder) { + return new SearchResult(new \OCA\DAV\Connector\Sabre\Directory($this->view, $node, $this->tree, $this->shareManager), $this->getHrefForNode($node)); + } else { + return new SearchResult(new \OCA\DAV\Connector\Sabre\File($this->view, $node, $this->shareManager), $this->getHrefForNode($node)); + } + }, $results); + } + + /** + * @param Node $node + * @return string + */ + private function getHrefForNode(Node $node) { + $base = '/files/' . $this->user->getUID(); + return $base . $this->view->getRelativePath($node->getPath()); + } + + /** + * @param BasicSearch $query + * @return ISearchQuery + */ + private function transformQuery(BasicSearch $query) { + // TODO offset, limit + $orders = array_map([$this, 'mapSearchOrder'], $query->orderBy); + return new SearchQuery($this->transformSearchOperation($query->where), 0, 0, $orders); + } + + /** + * @param Order $order + * @return ISearchOrder + */ + private function mapSearchOrder(Order $order) { + return new SearchOrder($order->order === Order::ASC ? ISearchOrder::DIRECTION_ASCENDING : ISearchOrder::DIRECTION_DESCENDING, $this->mapPropertyNameToCollumn($order->property)); + } + + /** + * @param Operator $operator + * @return ISearchOperator + */ + private function transformSearchOperation(Operator $operator) { + list(, $trimmedType) = explode('}', $operator->type); + switch ($operator->type) { + case Operator::OPERATION_AND: + case Operator::OPERATION_OR: + case Operator::OPERATION_NOT: + $arguments = array_map([$this, 'transformSearchOperation'], $operator->arguments); + return new SearchBinaryOperator($trimmedType, $arguments); + case Operator::OPERATION_EQUAL: + case Operator::OPERATION_GREATER_OR_EQUAL_THAN: + case Operator::OPERATION_GREATER_THAN: + case Operator::OPERATION_LESS_OR_EQUAL_THAN: + case Operator::OPERATION_LESS_THAN: + case Operator::OPERATION_IS_LIKE: + if (count($operator->arguments) !== 2) { + throw new \InvalidArgumentException('Invalid number of arguments for ' . $trimmedType . ' operation'); + } + if (gettype($operator->arguments[0]) !== 'string') { + throw new \InvalidArgumentException('Invalid argument 1 for ' . $trimmedType . ' operation, expected property'); + } + if (!($operator->arguments[1] instanceof Literal)) { + throw new \InvalidArgumentException('Invalid argument 2 for ' . $trimmedType . ' operation, expected literal'); + } + return new SearchComparison($trimmedType, $this->mapPropertyNameToCollumn($operator->arguments[0]), $this->castValue($operator->arguments[0], $operator->arguments[1]->value)); + case Operator::OPERATION_IS_COLLECTION: + return new SearchComparison('eq', 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE); + default: + throw new \InvalidArgumentException('Unsupported operation ' . $trimmedType. ' (' . $operator->type . ')'); + } + } + + /** + * @param string $propertyName + * @return string + */ + private function mapPropertyNameToCollumn($propertyName) { + switch ($propertyName) { + case '{DAV:}displayname': + return 'name'; + case '{DAV:}getcontenttype': + return 'mimetype'; + case '{DAV:}getlastmodifed': + return 'mtime'; + case FilesPlugin::SIZE_PROPERTYNAME: + return 'size'; + default: + throw new \InvalidArgumentException('Unsupported property for search or order: ' . $propertyName); + } + } + + private function castValue($propertyName, $value) { + $allProps = $this->getPropertyDefinitionsForScope('', ''); + foreach ($allProps as $prop) { + if ($prop->name === $propertyName) { + $dataType = $prop->dataType; + switch ($dataType) { + case SearchPropertyDefinition::DATATYPE_BOOLEAN: + return $value === 'yes'; + case SearchPropertyDefinition::DATATYPE_DECIMAL: + case SearchPropertyDefinition::DATATYPE_INTEGER: + case SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER: + return 0 + $value; + default: + return $value; + } + } + } + return $value; + } +} diff --git a/apps/dav/lib/Server.php b/apps/dav/lib/Server.php index 5359bfc6991..031bc1d3d81 100644 --- a/apps/dav/lib/Server.php +++ b/apps/dav/lib/Server.php @@ -51,6 +51,7 @@ use OCP\SabrePluginEvent; use Sabre\CardDAV\VCFExportPlugin; use Sabre\DAV\Auth\Plugin; use OCA\DAV\Connector\Sabre\TagsPlugin; +use SearchDAV\DAV\SearchPlugin; class Server { @@ -223,6 +224,13 @@ class Server { \OC::$server->getGroupManager(), $userFolder )); + $this->server->addPlugin(new SearchPlugin(new \OCA\DAV\Files\FileSearchBackend( + $this->server->tree, + $user, + \OC::$server->getRootFolder(), + \OC::$server->getShareManager(), + $view + ))); } } }); diff --git a/apps/dav/tests/unit/Files/FileSearchBackendTest.php b/apps/dav/tests/unit/Files/FileSearchBackendTest.php new file mode 100644 index 00000000000..24b9a9c51e6 --- /dev/null +++ b/apps/dav/tests/unit/Files/FileSearchBackendTest.php @@ -0,0 +1,299 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\DAV\Tests\Files; + +use OC\Files\Search\SearchComparison; +use OC\Files\Search\SearchQuery; +use OC\Files\View; +use OCA\DAV\Connector\Sabre\Directory; +use OCA\DAV\Connector\Sabre\File; +use OCA\DAV\Connector\Sabre\FilesPlugin; +use OCA\DAV\Files\FileSearchBackend; +use OCP\Files\FileInfo; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\Files\Search\ISearchComparison; +use OCP\IUser; +use OCP\Share\IManager; +use Sabre\DAV\Tree; +use SearchDAV\XML\BasicSearch; +use SearchDAV\XML\Literal; +use SearchDAV\XML\Operator; +use SearchDAV\XML\Scope; +use Test\TestCase; + +class FileSearchBackendTest extends TestCase { + /** @var Tree|\PHPUnit_Framework_MockObject_MockObject */ + private $tree; + + /** @var IUser */ + private $user; + + /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ + private $rootFolder; + + /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + private $shareManager; + + /** @var View|\PHPUnit_Framework_MockObject_MockObject */ + private $view; + + /** @var Folder|\PHPUnit_Framework_MockObject_MockObject */ + private $searchFolder; + + /** @var FileSearchBackend */ + private $search; + + /** @var Directory|\PHPUnit_Framework_MockObject_MockObject */ + private $davFolder; + + protected function setUp() { + parent::setUp(); + + $this->user = $this->createMock(IUser::class); + $this->user->expects($this->any()) + ->method('getUID') + ->willReturn('test'); + + $this->tree = $this->getMockBuilder(Tree::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->view = $this->getMockBuilder(View::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->view->expects($this->any()) + ->method('getRelativePath') + ->willReturnArgument(0); + + $this->rootFolder = $this->createMock(IRootFolder::class); + + $this->shareManager = $this->createMock(IManager::class); + + $this->searchFolder = $this->createMock(Folder::class); + + $fileInfo = $this->createMock(FileInfo::class); + + $this->davFolder = $this->createMock(Directory::class); + + $this->davFolder->expects($this->any()) + ->method('getFileInfo') + ->willReturn($fileInfo); + + $this->rootFolder->expects($this->any()) + ->method('get') + ->willReturn($this->searchFolder); + + $this->search = new FileSearchBackend($this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view); + } + + public function testSearchFilename() { + $this->tree->expects($this->any()) + ->method('getNodeForPath') + ->willReturn($this->davFolder); + + $this->searchFolder->expects($this->once()) + ->method('search') + ->with(new SearchQuery( + new SearchComparison( + ISearchComparison::COMPARE_EQUAL, + 'name', + 'foo' + ), + 0, + 0, + [] + )) + ->will($this->returnValue([ + new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path') + ])); + + $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo'); + $result = $this->search->search($query); + + $this->assertCount(1, $result); + $this->assertEquals('/files/test/test/path', $result[0]->href); + } + + public function testSearchMimetype() { + $this->tree->expects($this->any()) + ->method('getNodeForPath') + ->willReturn($this->davFolder); + + $this->searchFolder->expects($this->once()) + ->method('search') + ->with(new SearchQuery( + new SearchComparison( + ISearchComparison::COMPARE_EQUAL, + 'mimetype', + 'foo' + ), + 0, + 0, + [] + )) + ->will($this->returnValue([ + new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path') + ])); + + $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}getcontenttype', 'foo'); + $result = $this->search->search($query); + + $this->assertCount(1, $result); + $this->assertEquals('/files/test/test/path', $result[0]->href); + } + + public function testSearchSize() { + $this->tree->expects($this->any()) + ->method('getNodeForPath') + ->willReturn($this->davFolder); + + $this->searchFolder->expects($this->once()) + ->method('search') + ->with(new SearchQuery( + new SearchComparison( + ISearchComparison::COMPARE_GREATER_THAN, + 'size', + 10 + ), + 0, + 0, + [] + )) + ->will($this->returnValue([ + new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path') + ])); + + $query = $this->getBasicQuery(Operator::OPERATION_GREATER_THAN, FilesPlugin::SIZE_PROPERTYNAME, 10); + $result = $this->search->search($query); + + $this->assertCount(1, $result); + $this->assertEquals('/files/test/test/path', $result[0]->href); + } + + public function testSearchMtime() { + $this->tree->expects($this->any()) + ->method('getNodeForPath') + ->willReturn($this->davFolder); + + $this->searchFolder->expects($this->once()) + ->method('search') + ->with(new SearchQuery( + new SearchComparison( + ISearchComparison::COMPARE_GREATER_THAN, + 'mtime', + 10 + ), + 0, + 0, + [] + )) + ->will($this->returnValue([ + new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path') + ])); + + $query = $this->getBasicQuery(Operator::OPERATION_GREATER_THAN, '{DAV:}getlastmodifed', 10); + $result = $this->search->search($query); + + $this->assertCount(1, $result); + $this->assertEquals('/files/test/test/path', $result[0]->href); + } + + public function testSearchIsCollection() { + $this->tree->expects($this->any()) + ->method('getNodeForPath') + ->willReturn($this->davFolder); + + $this->searchFolder->expects($this->once()) + ->method('search') + ->with(new SearchQuery( + new SearchComparison( + ISearchComparison::COMPARE_EQUAL, + 'mimetype', + FileInfo::MIMETYPE_FOLDER + ), + 0, + 0, + [] + )) + ->will($this->returnValue([ + new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path') + ])); + + $query = $this->getBasicQuery(Operator::OPERATION_IS_COLLECTION, 'yes'); + $result = $this->search->search($query); + + $this->assertCount(1, $result); + $this->assertEquals('/files/test/test/path', $result[0]->href); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testSearchInvalidProp() { + $this->tree->expects($this->any()) + ->method('getNodeForPath') + ->willReturn($this->davFolder); + + $this->searchFolder->expects($this->never()) + ->method('search'); + + $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}getetag', 'foo'); + $this->search->search($query); + } + + private function getBasicQuery($type, $property, $value = null) { + $query = new BasicSearch(); + $scope = new Scope('/', 'infinite'); + $scope->path = '/'; + $query->from = [$scope]; + $query->orderBy = []; + $query->select = []; + if (is_null($value)) { + $query->where = new Operator( + $type, + [new Literal($property)] + ); + } else { + $query->where = new Operator( + $type, + [$property, new Literal($value)] + ); + } + return $query; + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testSearchNonFolder() { + $davNode = $this->createMock(File::class); + + $this->tree->expects($this->any()) + ->method('getNodeForPath') + ->willReturn($davNode); + + $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo'); + $this->search->search($query); + } +} |