Adjust to updated searchdav library

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2018-01-25 15:55:08 +01:00 committed by Roeland Jago Douma
parent ec144281ef
commit 6b5419ddf0
No known key found for this signature in database
GPG Key ID: F941078878347C0C
2 changed files with 52 additions and 68 deletions

@ -1 +1 @@
Subproject commit 546b952d0ec6c53e2e42ecd99ca0d94e35b7912d Subproject commit e42a129bf8c10cf9e519cc9e4a0e6978bbcd7e59

View File

@ -45,10 +45,10 @@ use Sabre\DAV\Exception\NotFound;
use SearchDAV\Backend\ISearchBackend; use SearchDAV\Backend\ISearchBackend;
use SearchDAV\Backend\SearchPropertyDefinition; use SearchDAV\Backend\SearchPropertyDefinition;
use SearchDAV\Backend\SearchResult; use SearchDAV\Backend\SearchResult;
use SearchDAV\XML\BasicSearch; use SearchDAV\Query\Query;
use SearchDAV\XML\Literal; use SearchDAV\Query\Literal;
use SearchDAV\XML\Operator; use SearchDAV\Query\Operator;
use SearchDAV\XML\Order; use SearchDAV\Query\Order;
class FileSearchBackend implements ISearchBackend { class FileSearchBackend implements ISearchBackend {
/** @var CachingTree */ /** @var CachingTree */
@ -135,10 +135,10 @@ class FileSearchBackend implements ISearchBackend {
} }
/** /**
* @param BasicSearch $search * @param Query $search
* @return SearchResult[] * @return SearchResult[]
*/ */
public function search(BasicSearch $search) { public function search(Query $search) {
if (count($search->from) !== 1) { if (count($search->from) !== 1) {
throw new \InvalidArgumentException('Searching more than one folder is not supported'); throw new \InvalidArgumentException('Searching more than one folder is not supported');
} }
@ -170,7 +170,7 @@ class FileSearchBackend implements ISearchBackend {
}, $results); }, $results);
// Sort again, since the result from multiple storages is appended and not sorted // Sort again, since the result from multiple storages is appended and not sorted
usort($nodes, function(SearchResult $a, SearchResult $b) use ($search) { usort($nodes, function (SearchResult $a, SearchResult $b) use ($search) {
return $this->sort($a, $b, $search->orderBy); return $this->sort($a, $b, $search->orderBy);
}); });
@ -183,7 +183,7 @@ class FileSearchBackend implements ISearchBackend {
} }
private function sort(SearchResult $a, SearchResult $b, array $orders) { private function sort(SearchResult $a, SearchResult $b, array $orders) {
/** @var Order $oder */ /** @var Order $order */
foreach ($orders as $order) { foreach ($orders as $order) {
$v1 = $this->getSearchResultProperty($a, $order->property); $v1 = $this->getSearchResultProperty($a, $order->property);
$v2 = $this->getSearchResultProperty($b, $order->property); $v2 = $this->getSearchResultProperty($b, $order->property);
@ -213,43 +213,34 @@ class FileSearchBackend implements ISearchBackend {
return 0; return 0;
} }
private function compareProperties($a, $b, $order) { private function compareProperties($a, $b, Order $order) {
$allProps = $this->getPropertyDefinitionsForScope('', ''); switch ($order->property->dataType) {
case SearchPropertyDefinition::DATATYPE_STRING:
foreach ($allProps as $prop) { return strcmp($a, $b);
if ($prop->name === $order->property) { case SearchPropertyDefinition::DATATYPE_BOOLEAN:
$dataType = $prop->dataType; if ($a === $b) {
switch ($dataType) { return 0;
case SearchPropertyDefinition::DATATYPE_STRING:
return strcmp($a, $b);
case SearchPropertyDefinition::DATATYPE_BOOLEAN:
if ($a === $b) {
return 0;
}
if ($a === false) {
return -1;
}
return 1;
default:
if ($a === $b) {
return 0;
}
if ($a < $b) {
return -1;
}
if ($a > $b) {
return 1;
}
} }
} if ($a === false) {
return -1;
}
return 1;
default:
if ($a === $b) {
return 0;
}
if ($a < $b) {
return -1;
}
return 1;
} }
} }
private function getSearchResultProperty(SearchResult $result, $propertyName) { private function getSearchResultProperty(SearchResult $result, SearchPropertyDefinition $property) {
/** @var \OCA\DAV\Connector\Sabre\Node $node */ /** @var \OCA\DAV\Connector\Sabre\Node $node */
$node = $result->node; $node = $result->node;
switch ($propertyName) { switch ($property->name) {
case '{DAV:}displayname': case '{DAV:}displayname':
return $node->getName(); return $node->getName();
case '{DAV:}getlastmodified': case '{DAV:}getlastmodified':
@ -273,10 +264,10 @@ class FileSearchBackend implements ISearchBackend {
} }
/** /**
* @param BasicSearch $query * @param Query $query
* @return ISearchQuery * @return ISearchQuery
*/ */
private function transformQuery(BasicSearch $query) { private function transformQuery(Query $query) {
// TODO offset // TODO offset
$limit = $query->limit; $limit = $query->limit;
$orders = array_map([$this, 'mapSearchOrder'], $query->orderBy); $orders = array_map([$this, 'mapSearchOrder'], $query->orderBy);
@ -312,7 +303,7 @@ class FileSearchBackend implements ISearchBackend {
if (count($operator->arguments) !== 2) { if (count($operator->arguments) !== 2) {
throw new \InvalidArgumentException('Invalid number of arguments for ' . $trimmedType . ' operation'); throw new \InvalidArgumentException('Invalid number of arguments for ' . $trimmedType . ' operation');
} }
if (!is_string($operator->arguments[0])) { if (!($operator->arguments[0] instanceof SearchPropertyDefinition)) {
throw new \InvalidArgumentException('Invalid argument 1 for ' . $trimmedType . ' operation, expected property'); throw new \InvalidArgumentException('Invalid argument 1 for ' . $trimmedType . ' operation, expected property');
} }
if (!($operator->arguments[1] instanceof Literal)) { if (!($operator->arguments[1] instanceof Literal)) {
@ -327,11 +318,11 @@ class FileSearchBackend implements ISearchBackend {
} }
/** /**
* @param string $propertyName * @param SearchPropertyDefinition $property
* @return string * @return string
*/ */
private function mapPropertyNameToColumn($propertyName) { private function mapPropertyNameToColumn(SearchPropertyDefinition $property) {
switch ($propertyName) { switch ($property->name) {
case '{DAV:}displayname': case '{DAV:}displayname':
return 'name'; return 'name';
case '{DAV:}getcontenttype': case '{DAV:}getcontenttype':
@ -347,33 +338,26 @@ class FileSearchBackend implements ISearchBackend {
case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME: case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME:
return 'fileid'; return 'fileid';
default: default:
throw new \InvalidArgumentException('Unsupported property for search or order: ' . $propertyName); throw new \InvalidArgumentException('Unsupported property for search or order: ' . $property->name);
} }
} }
private function castValue($propertyName, $value) { private function castValue(SearchPropertyDefinition $property, $value) {
$allProps = $this->getPropertyDefinitionsForScope('', ''); switch ($property->dataType) {
foreach ($allProps as $prop) { case SearchPropertyDefinition::DATATYPE_BOOLEAN:
if ($prop->name === $propertyName) { return $value === 'yes';
$dataType = $prop->dataType; case SearchPropertyDefinition::DATATYPE_DECIMAL:
switch ($dataType) { case SearchPropertyDefinition::DATATYPE_INTEGER:
case SearchPropertyDefinition::DATATYPE_BOOLEAN: case SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER:
return $value === 'yes'; return 0 + $value;
case SearchPropertyDefinition::DATATYPE_DECIMAL: case SearchPropertyDefinition::DATATYPE_DATETIME:
case SearchPropertyDefinition::DATATYPE_INTEGER: if (is_numeric($value)) {
case SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER: return 0 + $value;
return 0 + $value;
case SearchPropertyDefinition::DATATYPE_DATETIME:
if (is_numeric($value)) {
return 0 + $value;
}
$date = \DateTime::createFromFormat(\DateTime::ATOM, $value);
return ($date instanceof \DateTime) ? $date->getTimestamp() : 0;
default:
return $value;
} }
} $date = \DateTime::createFromFormat(\DateTime::ATOM, $value);
return ($date instanceof \DateTime) ? $date->getTimestamp() : 0;
default:
return $value;
} }
return $value;
} }
} }