mirror of
https://codeberg.org/icewind/SearchDAV.git
synced 2026-06-03 17:44:06 +02:00
initial working version
supports grammar discovery, schema discovery and search
This commit is contained in:
parent
a74bfec383
commit
88db2ae6c1
18 changed files with 1040 additions and 0 deletions
60
src/Backend/ISearchBackend.php
Normal file
60
src/Backend/ISearchBackend.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?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 SearchDAV\Backend;
|
||||
|
||||
use Sabre\DAV\INode;
|
||||
use SearchDAV\XML\BasicSearch;
|
||||
|
||||
interface ISearchBackend {
|
||||
/**
|
||||
* Get the path of the search arbiter of this backend
|
||||
*
|
||||
* The search arbiter is the URI that the client will send it's SEARCH requests to
|
||||
* Note that this is not required to be the same as the search scopes which determine what to search in
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getArbiterPath();
|
||||
|
||||
/**
|
||||
* Whether or not the search backend supports search requests on this scope
|
||||
*
|
||||
* @param string $href
|
||||
* @param string|integer $depth 0, 1 or 'inifinite'
|
||||
* @return bool
|
||||
*/
|
||||
public function isValidScope($href, $depth);
|
||||
|
||||
/**
|
||||
* List the available properties that can be used in search
|
||||
*
|
||||
* @return SearchPropertyDefinition[]
|
||||
*/
|
||||
public function getPropertyDefinitionsForScope($href);
|
||||
|
||||
/**
|
||||
* @param INode $searchNode the DAV node that the search request was made to
|
||||
* @param BasicSearch $query
|
||||
* @return SearchResult[]
|
||||
*/
|
||||
public function search(INode $searchNode, BasicSearch $query);
|
||||
}
|
||||
65
src/Backend/SearchPropertyDefinition.php
Normal file
65
src/Backend/SearchPropertyDefinition.php
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
<?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 SearchDAV\Backend;
|
||||
|
||||
class SearchPropertyDefinition {
|
||||
const XS = '{http://www.w3.org/2001/XMLSchema}';
|
||||
const DATATYPE_STRING = self::XS . 'string';
|
||||
const DATATYPE_INTEGER = self::XS . 'integer';
|
||||
const DATATYPE_NONNEGATIVE_INTEGER = self::XS . 'nonNegativeInteger';
|
||||
const DATATYPE_DECIMAL = self::XS . 'decimal';
|
||||
const DATATYPE_DATETIME = self::XS . 'dateTime';
|
||||
const DATATYPE_BOOLEAN = self::XS . 'boolean';
|
||||
|
||||
|
||||
/** @var boolean */
|
||||
public $searchable;
|
||||
/** @var boolean */
|
||||
public $selectable;
|
||||
/** @var boolean */
|
||||
public $sortable;
|
||||
/** @var boolean */
|
||||
public $caseSensitive;
|
||||
/** @var string */
|
||||
public $dataType;
|
||||
/** @var string */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* SearchProperty constructor.
|
||||
*
|
||||
* @param string $name the name and namespace of the property in clark notation
|
||||
* @param bool $searchable whether or not this property can be used as part of a search query
|
||||
* @param bool $selectable whether or not this property can be returned as part of a search result
|
||||
* @param bool $sortable whether or not this property can be used to sort the search result
|
||||
* @param string $dataType the datatype of the property, one of the SearchProperty::DATATYPE_ constants or any XSD datatype in clark notation
|
||||
* @param bool $caseSensitive whether or not comparisons on the property are case sensitive, only applies to string propertries
|
||||
*/
|
||||
public function __construct($name, $searchable, $selectable, $sortable, $dataType = self::DATATYPE_STRING, $caseSensitive = true) {
|
||||
$this->searchable = $searchable;
|
||||
$this->selectable = $selectable;
|
||||
$this->sortable = $sortable;
|
||||
$this->dataType = $dataType;
|
||||
$this->name = $name;
|
||||
$this->caseSensitive = $caseSensitive;
|
||||
}
|
||||
}
|
||||
43
src/Backend/SearchResult.php
Normal file
43
src/Backend/SearchResult.php
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<?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 SearchDAV\Backend;
|
||||
|
||||
|
||||
use Sabre\DAV\Node;
|
||||
|
||||
class SearchResult {
|
||||
/** @var Node */
|
||||
public $node;
|
||||
/** @var string */
|
||||
public $href;
|
||||
|
||||
/**
|
||||
* SearchResult constructor.
|
||||
*
|
||||
* @param Node $node
|
||||
* @param string $href
|
||||
*/
|
||||
public function __construct(Node $node, $href) {
|
||||
$this->node = $node;
|
||||
$this->href = $href;
|
||||
}
|
||||
}
|
||||
75
src/DAV/QueryParser.php
Normal file
75
src/DAV/QueryParser.php
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
<?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 SearchDAV\DAV;
|
||||
|
||||
use Sabre\Xml\Element;
|
||||
use Sabre\Xml\Reader;
|
||||
use Sabre\Xml\Service;
|
||||
use SearchDAV\XML\BasicSearch;
|
||||
use SearchDAV\XML\Literal;
|
||||
use SearchDAV\XML\Operator;
|
||||
use SearchDAV\XML\Order;
|
||||
use SearchDAV\XML\Scope;
|
||||
|
||||
class QueryParser extends Service {
|
||||
public $namespaceMap = [
|
||||
'DAV:' => 'd',
|
||||
'http://sabredav.org/ns' => 's',
|
||||
'http://www.w3.org/2001/XMLSchema' => 'xs'
|
||||
];
|
||||
|
||||
public function __construct() {
|
||||
$this->elementMap = [
|
||||
'{DAV:}literal' => Literal::class,
|
||||
'{DAV:}searchrequest' => Element\KeyValue::class,
|
||||
'{DAV:}query-schema-discovery' => Element\KeyValue::class,
|
||||
'{DAV:}basicsearch' => BasicSearch::class,
|
||||
'{DAV:}select' => function (Reader $reader) {
|
||||
return \Sabre\Xml\Deserializer\keyValue($reader, '{DAV:}scope')['{DAV:}prop'];
|
||||
},
|
||||
'{DAV:}from' => function (Reader $reader) {
|
||||
return \Sabre\Xml\Deserializer\repeatingElements($reader, '{DAV:}scope');
|
||||
},
|
||||
'{DAV:}orderby' => function (Reader $reader) {
|
||||
return \Sabre\Xml\Deserializer\repeatingElements($reader, '{DAV:}order');
|
||||
},
|
||||
'{DAV:}scope' => Scope::class,
|
||||
'{DAV:}where' => function (Reader $reader) {
|
||||
return array_map(function ($element) {
|
||||
return $element['value'];
|
||||
}, $reader->parseGetElements());
|
||||
},
|
||||
'{DAV:}prop' => Element\Elements::class,
|
||||
'{DAV:}order' => Order::class,
|
||||
'{DAV:}eq' => Operator::class,
|
||||
'{DAV:}gt' => Operator::class,
|
||||
'{DAV:}gte' => Operator::class,
|
||||
'{DAV:}lt' => Operator::class,
|
||||
'{DAV:}lte' => Operator::class,
|
||||
'{DAV:}and' => Operator::class,
|
||||
'{DAV:}or' => Operator::class,
|
||||
'{DAV:}like' => Operator::class,
|
||||
'{DAV:}contains' => Operator::class,
|
||||
'{DAV:}not' => Operator::class
|
||||
];
|
||||
}
|
||||
}
|
||||
215
src/DAV/SearchPlugin.php
Normal file
215
src/DAV/SearchPlugin.php
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
<?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 SearchDAV\DAV;
|
||||
|
||||
use Sabre\DAV\Exception\BadRequest;
|
||||
use Sabre\DAV\INode;
|
||||
use Sabre\DAV\Node;
|
||||
use Sabre\DAV\PropFind;
|
||||
use Sabre\DAV\Server;
|
||||
use Sabre\DAV\ServerPlugin;
|
||||
use Sabre\DAV\Xml\Element\Response;
|
||||
use Sabre\DAV\Xml\Response\MultiStatus;
|
||||
use Sabre\HTTP\RequestInterface;
|
||||
use Sabre\HTTP\ResponseInterface;
|
||||
use Sabre\Xml\Writer;
|
||||
use SearchDAV\Backend\ISearchBackend;
|
||||
use SearchDAV\Backend\SearchPropertyDefinition;
|
||||
use SearchDAV\Backend\SearchResult;
|
||||
use SearchDAV\XML\BasicSearch;
|
||||
use SearchDAV\XML\BasicSearchSchema;
|
||||
use SearchDAV\XML\PropDesc;
|
||||
use SearchDAV\XML\QueryDiscoverResponse;
|
||||
use SearchDAV\XML\Scope;
|
||||
use SearchDAV\XML\SupportedQueryGrammar;
|
||||
|
||||
class SearchPlugin extends ServerPlugin {
|
||||
/** @var Server */
|
||||
private $server;
|
||||
|
||||
/** @var ISearchBackend */
|
||||
private $searchBackend;
|
||||
|
||||
/** @var QueryParser */
|
||||
private $queryParser;
|
||||
|
||||
public function __construct(ISearchBackend $searchBackend) {
|
||||
$this->searchBackend = $searchBackend;
|
||||
}
|
||||
|
||||
public function initialize(Server $server) {
|
||||
$this->server = $server;
|
||||
$this->queryParser = new QueryParser($this->server->xml);
|
||||
$server->on('method:SEARCH', [$this, 'searchHandler']);
|
||||
$server->on('afterMethod:OPTIONS', [$this, 'optionHandler']);
|
||||
$server->on('propFind', [$this, 'propFindHandler']);
|
||||
}
|
||||
|
||||
public function propFindHandler(PropFind $propFind, INode $node) {
|
||||
if ($propFind->getPath() === $this->searchBackend->getArbiterPath()) {
|
||||
$propFind->handle('{DAV:}supported-query-grammar-set', new SupportedQueryGrammar());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SEARCH is allowed for users files
|
||||
*
|
||||
* @param string $uri
|
||||
* @return array
|
||||
*/
|
||||
public function getHTTPMethods($uri) {
|
||||
$path = ($uri === '' && $this->server->getBaseUri() === '/') ? '' : $this->server->calculateUri($uri);
|
||||
if ($this->searchBackend->getArbiterPath() === $path) {
|
||||
return ['SEARCH'];
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public function optionHandler(RequestInterface $request, ResponseInterface $response) {
|
||||
if ($request->getPath() === '') {
|
||||
$response->addHeader('DASL', '<DAV:basicsearch>');
|
||||
}
|
||||
}
|
||||
|
||||
public function searchHandler(RequestInterface $request, ResponseInterface $response) {
|
||||
$contentType = $request->getHeader('Content-Type');
|
||||
|
||||
// Currently we only support xml search queries
|
||||
if ((strpos($contentType, 'text/xml') === false) && (strpos($contentType, 'application/xml') === false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$xml = $this->queryParser->parse(
|
||||
$request->getBody(),
|
||||
$request->getUrl(),
|
||||
$documentType
|
||||
);
|
||||
|
||||
switch ($documentType) {
|
||||
case '{DAV:}searchrequest':
|
||||
if (!$xml['{DAV:}basicsearch']) {
|
||||
throw new BadRequest('Unexpected xml content for searchrequest, expected basicsearch');
|
||||
}
|
||||
/** @var BasicSearch $query */
|
||||
$query = $xml['{DAV:}basicsearch'];
|
||||
$node = $this->server->tree->getNodeForPath($request->getPath());
|
||||
$response->setStatus(207);
|
||||
$response->setHeader('Content-Type', 'application/xml; charset="utf-8"');
|
||||
$results = $this->searchBackend->search($node, $query);
|
||||
$data = $this->server->generateMultiStatus($this->getPropertiesIteratorResults($results, $query->select), false);
|
||||
$response->setBody($data);
|
||||
return false;
|
||||
case '{DAV:}query-schema-discovery':
|
||||
if (!$xml['{DAV:}basicsearch']) {
|
||||
throw new BadRequest('Unexpected xml content for query-schema-discovery, expected basicsearch');
|
||||
}
|
||||
/** @var BasicSearch $query */
|
||||
$query = $xml['{DAV:}basicsearch'];
|
||||
$scopes = $query->from;
|
||||
$results = array_map(function(Scope $scope) {
|
||||
if ($this->searchBackend->isValidScope($scope->href, $scope->depth)) {
|
||||
$searchProperties = $this->searchBackend->getPropertyDefinitionsForScope($scope->href);
|
||||
$searchSchema = $this->getBasicSearchForProperties($searchProperties);
|
||||
return new QueryDiscoverResponse($scope->href, $searchSchema , 200);
|
||||
} else {
|
||||
return new QueryDiscoverResponse($scope->href, null, 404); // TODO something other than 404? 403 maybe
|
||||
}
|
||||
}, $scopes);
|
||||
$multiStatus = new MultiStatus($results);
|
||||
$response->setStatus(207);
|
||||
$response->setHeader('Content-Type', 'application/xml; charset="utf-8"');
|
||||
$response->setBody($this->queryParser->write('{DAV:}multistatus', $multiStatus, $request->getUrl()));
|
||||
return false;
|
||||
default:
|
||||
throw new BadRequest('Unexpected document type: ' . $documentType . ' for this Content-Type');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of properties for a given path
|
||||
*
|
||||
* The path that should be supplied should have the baseUrl stripped out
|
||||
* The list of properties should be supplied in Clark notation. If the list is empty
|
||||
* 'allprops' is assumed.
|
||||
*
|
||||
* If a depth of 1 is requested child elements will also be returned.
|
||||
*
|
||||
* @param SearchResult[] $results
|
||||
* @param array $propertyNames
|
||||
* @param int $depth
|
||||
* @return \Iterator
|
||||
*/
|
||||
function getPropertiesIteratorResults(array $results, $propertyNames = [], $depth = 0) {
|
||||
$propFindType = $propertyNames ? PropFind::NORMAL : PropFind::ALLPROPS;
|
||||
|
||||
foreach ($results as $result) {
|
||||
$node = $result->node;
|
||||
$propFind = new PropFind($result->href, (array)$propertyNames, $depth, $propFindType);
|
||||
$r = $this->server->getPropertiesByNode($propFind, $node);
|
||||
if ($r) {
|
||||
$result = $propFind->getResultForMultiStatus();
|
||||
$result['href'] = $propFind->getPath();
|
||||
|
||||
// WebDAV recommends adding a slash to the path, if the path is
|
||||
// a collection.
|
||||
// Furthermore, iCal also demands this to be the case for
|
||||
// principals. This is non-standard, but we support it.
|
||||
$resourceType = $this->server->getResourceTypeForNode($node);
|
||||
if (in_array('{DAV:}collection', $resourceType) || in_array('{DAV:}principal', $resourceType)) {
|
||||
$result['href'] .= '/';
|
||||
}
|
||||
yield $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function hashDefinition(SearchPropertyDefinition $definition) {
|
||||
return $definition->dataType
|
||||
. (($definition->searchable) ? '1' : '0')
|
||||
. (($definition->sortable) ? '1' : '0')
|
||||
. (($definition->selectable) ? '1' : '0');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SearchPropertyDefinition[] $propertyDefinitions
|
||||
* @return BasicSearchSchema
|
||||
*/
|
||||
private function getBasicSearchForProperties(array $propertyDefinitions) {
|
||||
/** @var PropDesc[] $groups */
|
||||
$groups = [];
|
||||
foreach ($propertyDefinitions as $propertyDefinition) {
|
||||
$key = $this->hashDefinition($propertyDefinition);
|
||||
if (!isset($groups[$key])) {
|
||||
$desc = new PropDesc();
|
||||
$desc->dataType = $propertyDefinition->dataType;
|
||||
$desc->sortable = $propertyDefinition->sortable;
|
||||
$desc->selectable = $propertyDefinition->selectable;
|
||||
$desc->searchable = $propertyDefinition->searchable;
|
||||
$groups[$key] = $desc;
|
||||
}
|
||||
$groups[$key]->properties[] = $propertyDefinition->name;
|
||||
}
|
||||
|
||||
return new BasicSearchSchema(array_values($groups));
|
||||
}
|
||||
}
|
||||
60
src/XML/BasicSearch.php
Normal file
60
src/XML/BasicSearch.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?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 SearchDAV\XML;
|
||||
|
||||
use Sabre\Xml\Reader;
|
||||
use Sabre\Xml\XmlDeserializable;
|
||||
|
||||
class BasicSearch implements XmlDeserializable {
|
||||
/**
|
||||
* @var string[]
|
||||
*
|
||||
* The list of properties to be selected in clark notation
|
||||
*/
|
||||
public $select;
|
||||
/**
|
||||
* @var Scope[]
|
||||
*
|
||||
* The collections to perform the search in
|
||||
*/
|
||||
public $from;
|
||||
/**
|
||||
* @var Operator[]
|
||||
*/
|
||||
public $where;
|
||||
/**
|
||||
* @var Order[]
|
||||
*/
|
||||
public $orderBy;
|
||||
|
||||
static function xmlDeserialize(Reader $reader) {
|
||||
$search = new self();
|
||||
|
||||
$elements = \Sabre\Xml\Deserializer\keyValue($reader);
|
||||
$search->select = isset($elements['{DAV:}select']) ? $elements['{DAV:}select'] : null;
|
||||
$search->from = isset($elements['{DAV:}from']) ? $elements['{DAV:}from'] : null;
|
||||
$search->where = isset($elements['{DAV:}where']) ? $elements['{DAV:}where'] : null;
|
||||
$search->orderBy = isset($elements['{DAV:}orderby']) ? $elements['{DAV:}orderby'] : null;
|
||||
|
||||
return $search;
|
||||
}
|
||||
}
|
||||
50
src/XML/BasicSearchSchema.php
Normal file
50
src/XML/BasicSearchSchema.php
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?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 SearchDAV\XML;
|
||||
|
||||
|
||||
use Sabre\Xml\Writer;
|
||||
use Sabre\Xml\XmlSerializable;
|
||||
|
||||
class BasicSearchSchema implements XmlSerializable {
|
||||
/** @var PropDesc[] */
|
||||
public $properties;
|
||||
|
||||
/**
|
||||
* BasicSearchSchema constructor.
|
||||
*
|
||||
* @param PropDesc[] $properties
|
||||
*/
|
||||
public function __construct(array $properties) {
|
||||
$this->properties = $properties;
|
||||
}
|
||||
|
||||
function xmlSerialize(Writer $writer) {
|
||||
$childs = array_map(function(PropDesc $propDesc) {
|
||||
return [
|
||||
'name' => '{DAV:}propdesc',
|
||||
'value' => $propDesc
|
||||
];
|
||||
}, $this->properties);
|
||||
$writer->writeElement('{DAV:}properties', $childs);
|
||||
}
|
||||
}
|
||||
40
src/XML/Literal.php
Normal file
40
src/XML/Literal.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?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 SearchDAV\XML;
|
||||
|
||||
|
||||
use Sabre\Xml\Reader;
|
||||
use Sabre\Xml\XmlDeserializable;
|
||||
|
||||
class Literal implements XmlDeserializable {
|
||||
/** @var string|boolean|\DateTime|integer */
|
||||
public $value;
|
||||
|
||||
static function xmlDeserialize(Reader $reader) {
|
||||
$literal = new self();
|
||||
|
||||
$literal->value = $reader->readText();
|
||||
$reader->read();
|
||||
|
||||
return $literal;
|
||||
}
|
||||
}
|
||||
86
src/XML/Operator.php
Normal file
86
src/XML/Operator.php
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
<?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 SearchDAV\XML;
|
||||
|
||||
|
||||
use Sabre\Xml\Reader;
|
||||
use Sabre\Xml\XmlDeserializable;
|
||||
|
||||
class Operator implements XmlDeserializable {
|
||||
const OPERATION_AND = '{DAV:}and';
|
||||
const OPERATION_OR = '{DAV:}or';
|
||||
const OPERATION_NOT = '{DAV:}not';
|
||||
const OPERATION_EQUAL = '{DAV:}eq';
|
||||
const OPERATION_LESS_THAN = '{DAV:}lt';
|
||||
const OPERATION_LESS_OR_EQUAL_THAN = '{DAV:}lte';
|
||||
const OPERATION_GREATER_THAN = '{DAV:}gt';
|
||||
const OPERATION_GREATER_OR_EQUAL_THAN = '{DAV:}gte';
|
||||
const OPERATION_IS_COLLECTION = '[DAV:}is-collection';
|
||||
const OPERATION_IS_DEFINED = '[DAV:}is-defined';
|
||||
const OPERATION_IS_LIKE = '[DAV:}like';
|
||||
const OPERATION_CONTAINS = '[DAV:}contains';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* The type of operation, one of the Operation::OPERATION_* constants
|
||||
*/
|
||||
public $type;
|
||||
/**
|
||||
* @var (Literal|string|Operation)[]
|
||||
*
|
||||
* The list of arguments for the operation
|
||||
*
|
||||
* - string: property name for comparison
|
||||
* - Literal: literal value for comparison
|
||||
* - Operation: nested operation for and/or/not operations
|
||||
*/
|
||||
public $arguments;
|
||||
|
||||
static function xmlDeserialize(Reader $reader) {
|
||||
$operator = new self();
|
||||
|
||||
// If there's no children, we don't do anything.
|
||||
if ($reader->isEmptyElement) {
|
||||
$reader->next();
|
||||
return null;
|
||||
}
|
||||
$operator->type = $reader->getClark();
|
||||
$reader->read();
|
||||
do {
|
||||
if ($reader->nodeType === Reader::ELEMENT) {
|
||||
$argument = $reader->parseCurrentElement();
|
||||
if ($argument['name'] === '{DAV:}prop') {
|
||||
$operator->arguments[] = $argument['value'][0];
|
||||
} else {
|
||||
$operator->arguments[] = $argument['value'];
|
||||
}
|
||||
} else {
|
||||
$reader->read();
|
||||
}
|
||||
} while ($reader->nodeType !== Reader::END_ELEMENT);
|
||||
|
||||
$reader->read();
|
||||
|
||||
return $operator;
|
||||
}
|
||||
}
|
||||
45
src/XML/Order.php
Normal file
45
src/XML/Order.php
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?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 SearchDAV\XML;
|
||||
|
||||
|
||||
use Sabre\Xml\Reader;
|
||||
use Sabre\Xml\XmlDeserializable;
|
||||
|
||||
class Order implements XmlDeserializable {
|
||||
const ASC = 'ascending';
|
||||
const DESC = 'descending';
|
||||
|
||||
public $properties;
|
||||
public $order;
|
||||
|
||||
static function xmlDeserialize(Reader $reader) {
|
||||
$order = new self();
|
||||
|
||||
$childs = \Sabre\Xml\Deserializer\keyValue($reader);
|
||||
|
||||
$order->order = isset($childs['{DAV:}descending']) ? self::DESC : self::ASC;
|
||||
$order->properties = $childs['{DAV:}prop'];
|
||||
|
||||
return $order;
|
||||
}
|
||||
}
|
||||
59
src/XML/PropDesc.php
Normal file
59
src/XML/PropDesc.php
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?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 SearchDAV\XML;
|
||||
|
||||
|
||||
use Sabre\Xml\Writer;
|
||||
use Sabre\Xml\XmlSerializable;
|
||||
|
||||
class PropDesc implements XmlSerializable {
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
public $properties = [];
|
||||
public $dataType;
|
||||
public $searchable;
|
||||
public $selectable;
|
||||
public $sortable;
|
||||
|
||||
function xmlSerialize(Writer $writer) {
|
||||
$data = [
|
||||
'{DAV:}dataType' => [$this->dataType => null]
|
||||
];
|
||||
if ($this->searchable) {
|
||||
$data['{DAV:}searchable'] = null;
|
||||
}
|
||||
if ($this->sortable) {
|
||||
$data['{DAV:}sortable'] = null;
|
||||
}
|
||||
if ($this->selectable) {
|
||||
$data['{DAV:}selectable'] = null;
|
||||
}
|
||||
$writer->write(array_map(function ($propName) {
|
||||
return [
|
||||
'name' => '{DAV:}prop',
|
||||
'value' => $propName
|
||||
];
|
||||
}, $this->properties));
|
||||
$writer->write($data);
|
||||
}
|
||||
}
|
||||
56
src/XML/QueryDiscoverResponse.php
Normal file
56
src/XML/QueryDiscoverResponse.php
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<?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 SearchDAV\XML;
|
||||
|
||||
|
||||
use Sabre\DAV\Xml\Element\Response;
|
||||
use Sabre\Xml\Writer;
|
||||
|
||||
class QueryDiscoverResponse extends Response {
|
||||
protected $schema;
|
||||
|
||||
/**
|
||||
* QueryDiscoverResponse constructor.
|
||||
*
|
||||
* @param string $href
|
||||
* @param BasicSearchSchema|null $schema
|
||||
* @param null|int|string $httpStatus
|
||||
*/
|
||||
function __construct($href, BasicSearchSchema $schema, $httpStatus = null) {
|
||||
parent::__construct($href, [], $httpStatus);
|
||||
$this->schema = $schema;
|
||||
|
||||
}
|
||||
|
||||
function xmlSerialize(Writer $writer) {
|
||||
if ($status = $this->getHTTPStatus()) {
|
||||
$writer->writeElement('{DAV:}status', 'HTTP/1.1 ' . $status . ' ' . \Sabre\HTTP\Response::$statusCodes[$status]);
|
||||
}
|
||||
$writer->writeElement('{DAV:}href', $writer->contextUri . \Sabre\HTTP\encodePath($this->getHref()));
|
||||
|
||||
if ($this->schema) {
|
||||
$writer->writeElement('{DAV:}query-schema', [
|
||||
'{DAV:basicsearchschema}' => $this->schema
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
41
src/XML/Scope.php
Normal file
41
src/XML/Scope.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?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 SearchDAV\XML;
|
||||
|
||||
|
||||
use Sabre\Xml\Reader;
|
||||
use Sabre\Xml\XmlDeserializable;
|
||||
|
||||
class Scope implements XmlDeserializable {
|
||||
public $href;
|
||||
public $depth;
|
||||
|
||||
static function xmlDeserialize(Reader $reader) {
|
||||
$scope = new self();
|
||||
|
||||
$values = \Sabre\Xml\Deserializer\keyValue($reader);
|
||||
$scope->href = $values['{DAV:}href'];
|
||||
$scope->depth = $values['{DAV:}depth'];
|
||||
|
||||
return $scope;
|
||||
}
|
||||
}
|
||||
41
src/XML/SupportedQueryGrammar.php
Normal file
41
src/XML/SupportedQueryGrammar.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?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 SearchDAV\XML;
|
||||
|
||||
|
||||
use Sabre\Xml\Writer;
|
||||
use Sabre\Xml\XmlSerializable;
|
||||
|
||||
class SupportedQueryGrammar implements XmlSerializable {
|
||||
const GRAMMAR_BASICSEARCH = '{DAV:}basicsearch';
|
||||
|
||||
public $grammar = self::GRAMMAR_BASICSEARCH;
|
||||
|
||||
function xmlSerialize(Writer $writer) {
|
||||
$writer->startElement('{DAV:}supported-query-grammar');
|
||||
$writer->startElement('{DAV:}grammar');
|
||||
$writer->startElement($this->grammar);
|
||||
$writer->endElement();
|
||||
$writer->endElement();
|
||||
$writer->endElement();
|
||||
}
|
||||
}
|
||||
55
tests/DummyBackend.php
Normal file
55
tests/DummyBackend.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?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 SearchDAV\Test;
|
||||
|
||||
|
||||
use Sabre\DAV\INode;
|
||||
use Sabre\DAV\SimpleFile;
|
||||
use SearchDAV\Backend\ISearchBackend;
|
||||
use SearchDAV\Backend\SearchResult;
|
||||
use SearchDAV\XML\BasicSearch;
|
||||
use SearchDAV\Backend\SearchPropertyDefinition;
|
||||
|
||||
class DummyBackend implements ISearchBackend {
|
||||
public function getArbiterPath() {
|
||||
return '';
|
||||
}
|
||||
|
||||
public function isValidScope($href, $depth) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getPropertyDefinitionsForScope($href) {
|
||||
return [
|
||||
new SearchPropertyDefinition('{DAV:}getcontentlength', true, true, true, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
|
||||
new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true),
|
||||
new SearchPropertyDefinition('{DAV:}displayname', true, true, true),
|
||||
new SearchPropertyDefinition('{http://ns.nextcloud.com:}fileid', false, true, true, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
|
||||
];
|
||||
}
|
||||
|
||||
public function search(INode $searchNode, BasicSearch $query) {
|
||||
return [
|
||||
new SearchResult(new SimpleFile('foo.txt', 'foobar', 'text/plain'), '/bar/foo.txt')
|
||||
];
|
||||
}
|
||||
}
|
||||
32
tests/basicquery.xml
Normal file
32
tests/basicquery.xml
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0"?>
|
||||
<d:searchrequest xmlns:d="DAV:">
|
||||
<d:basicsearch>
|
||||
<d:select>
|
||||
<d:prop>
|
||||
<d:getcontentlength/>
|
||||
</d:prop>
|
||||
</d:select>
|
||||
<d:from>
|
||||
<d:scope>
|
||||
<d:href>/container1/</d:href>
|
||||
<d:depth>infinity</d:depth>
|
||||
</d:scope>
|
||||
</d:from>
|
||||
<d:where>
|
||||
<d:gt>
|
||||
<d:prop>
|
||||
<d:getcontentlength/>
|
||||
</d:prop>
|
||||
<d:literal>10000</d:literal>
|
||||
</d:gt>
|
||||
</d:where>
|
||||
<d:orderby>
|
||||
<d:order>
|
||||
<d:prop>
|
||||
<d:getcontentlength/>
|
||||
</d:prop>
|
||||
<d:ascending/>
|
||||
</d:order>
|
||||
</d:orderby>
|
||||
</d:basicsearch>
|
||||
</d:searchrequest>
|
||||
9
tests/test.php
Normal file
9
tests/test.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
require_once '../vendor/autoload.php';
|
||||
|
||||
$parser = new \SearchDAV\DAV\QueryParser();
|
||||
|
||||
$body = file_get_contents('./basicquery.xml');
|
||||
|
||||
var_dump($parser->parse($body)['{DAV:}basicsearch']);
|
||||
8
tests/testserver.php
Normal file
8
tests/testserver.php
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$server = new \Sabre\DAV\Server();
|
||||
$server->addPlugin(new \SearchDAV\DAV\SearchPlugin(new \SearchDAV\Test\DummyBackend()));
|
||||
|
||||
$server->exec();
|
||||
Loading…
Add table
Add a link
Reference in a new issue