Passed
Push — master ( 90401e...63cb31 )
by Roeland
20:41 queued 20s
created

FileSearchBackend::transformQuery()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 22
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Robin Appelman <[email protected]>
4
 *
5
 * @author Robin Appelman <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\DAV\Files;
25
26
use OC\Files\Search\SearchBinaryOperator;
27
use OC\Files\Search\SearchComparison;
28
use OC\Files\Search\SearchOrder;
29
use OC\Files\Search\SearchQuery;
30
use OC\Files\View;
31
use OCA\DAV\Connector\Sabre\CachingTree;
32
use OCA\DAV\Connector\Sabre\Directory;
33
use OCA\DAV\Connector\Sabre\FilesPlugin;
34
use OCA\DAV\Connector\Sabre\TagsPlugin;
35
use OCP\Files\Cache\ICacheEntry;
36
use OCP\Files\Folder;
37
use OCP\Files\IRootFolder;
38
use OCP\Files\Node;
39
use OCP\Files\Search\ISearchOperator;
40
use OCP\Files\Search\ISearchOrder;
41
use OCP\Files\Search\ISearchQuery;
42
use OCP\IUser;
43
use OCP\Share\IManager;
44
use Sabre\DAV\Exception\NotFound;
45
use SearchDAV\Backend\ISearchBackend;
46
use SearchDAV\Backend\SearchPropertyDefinition;
47
use SearchDAV\Backend\SearchResult;
48
use SearchDAV\Query\Literal;
49
use SearchDAV\Query\Operator;
50
use SearchDAV\Query\Order;
51
use SearchDAV\Query\Query;
52
53
class FileSearchBackend implements ISearchBackend {
54
	/** @var CachingTree */
55
	private $tree;
56
57
	/** @var IUser */
58
	private $user;
59
60
	/** @var IRootFolder */
61
	private $rootFolder;
62
63
	/** @var IManager */
64
	private $shareManager;
65
66
	/** @var View */
67
	private $view;
68
69
	/**
70
	 * FileSearchBackend constructor.
71
	 *
72
	 * @param CachingTree $tree
73
	 * @param IUser $user
74
	 * @param IRootFolder $rootFolder
75
	 * @param IManager $shareManager
76
	 * @param View $view
77
	 * @internal param IRootFolder $rootFolder
78
	 */
79
	public function __construct(CachingTree $tree, IUser $user, IRootFolder $rootFolder, IManager $shareManager, View $view) {
80
		$this->tree = $tree;
81
		$this->user = $user;
82
		$this->rootFolder = $rootFolder;
83
		$this->shareManager = $shareManager;
84
		$this->view = $view;
85
	}
86
87
	/**
88
	 * Search endpoint will be remote.php/dav
89
	 *
90
	 * @return string
91
	 */
92
	public function getArbiterPath() {
93
		return '';
94
	}
95
96
	public function isValidScope($href, $depth, $path) {
97
		// only allow scopes inside the dav server
98
		if (is_null($path)) {
99
			return false;
100
		}
101
102
		try {
103
			$node = $this->tree->getNodeForPath($path);
104
			return $node instanceof Directory;
105
		} catch (NotFound $e) {
106
			return false;
107
		}
108
	}
109
110
	public function getPropertyDefinitionsForScope($href, $path) {
111
		// all valid scopes support the same schema
112
113
		//todo dynamically load all propfind properties that are supported
114
		return [
115
			// queryable properties
116
			new SearchPropertyDefinition('{DAV:}displayname', true, true, true),
117
			new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true),
118
			new SearchPropertyDefinition('{DAV:}getlastmodified', true, true, true, SearchPropertyDefinition::DATATYPE_DATETIME),
119
			new SearchPropertyDefinition(FilesPlugin::SIZE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
120
			new SearchPropertyDefinition(TagsPlugin::FAVORITE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_BOOLEAN),
121
			new SearchPropertyDefinition(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, true, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
122
			new SearchPropertyDefinition(FilesPlugin::OWNER_ID_PROPERTYNAME, true, true, false),
123
124
			// select only properties
125
			new SearchPropertyDefinition('{DAV:}resourcetype', false, true, false),
126
			new SearchPropertyDefinition('{DAV:}getcontentlength', false, true, false),
127
			new SearchPropertyDefinition(FilesPlugin::CHECKSUMS_PROPERTYNAME, false, true, false),
128
			new SearchPropertyDefinition(FilesPlugin::PERMISSIONS_PROPERTYNAME, false, true, false),
129
			new SearchPropertyDefinition(FilesPlugin::GETETAG_PROPERTYNAME, false, true, false),
130
			new SearchPropertyDefinition(FilesPlugin::OWNER_DISPLAY_NAME_PROPERTYNAME, false, true, false),
131
			new SearchPropertyDefinition(FilesPlugin::DATA_FINGERPRINT_PROPERTYNAME, false, true, false),
132
			new SearchPropertyDefinition(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, false, true, false, SearchPropertyDefinition::DATATYPE_BOOLEAN),
133
			new SearchPropertyDefinition(FilesPlugin::FILEID_PROPERTYNAME, false, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
134
		];
135
	}
136
137
	/**
138
	 * @param Query $search
139
	 * @return SearchResult[]
140
	 */
141
	public function search(Query $search) {
142
		if (count($search->from) !== 1) {
143
			throw new \InvalidArgumentException('Searching more than one folder is not supported');
144
		}
145
		$query = $this->transformQuery($search);
146
		$scope = $search->from[0];
147
		if ($scope->path === null) {
148
			throw new \InvalidArgumentException('Using uri\'s as scope is not supported, please use a path relative to the search arbiter instead');
149
		}
150
		$node = $this->tree->getNodeForPath($scope->path);
151
		if (!$node instanceof Directory) {
152
			throw new \InvalidArgumentException('Search is only supported on directories');
153
		}
154
155
		$fileInfo = $node->getFileInfo();
156
		$folder = $this->rootFolder->get($fileInfo->getPath());
157
		/** @var Folder $folder $results */
158
		$results = $folder->search($query);
159
160
		/** @var SearchResult[] $nodes */
161
		$nodes = array_map(function (Node $node) {
162
			if ($node instanceof Folder) {
163
				$davNode = new \OCA\DAV\Connector\Sabre\Directory($this->view, $node, $this->tree, $this->shareManager);
164
			} else {
165
				$davNode = new \OCA\DAV\Connector\Sabre\File($this->view, $node, $this->shareManager);
166
			}
167
			$path = $this->getHrefForNode($node);
168
			$this->tree->cacheNode($davNode, $path);
169
			return new SearchResult($davNode, $path);
170
		}, $results);
171
172
		if (!$query->limitToHome()) {
173
			// Sort again, since the result from multiple storages is appended and not sorted
174
			usort($nodes, function (SearchResult $a, SearchResult $b) use ($search) {
175
				return $this->sort($a, $b, $search->orderBy);
176
			});
177
		}
178
179
		// If a limit is provided use only return that number of files
180
		if ($search->limit->maxResults !== 0) {
181
			$nodes = \array_slice($nodes, 0, $search->limit->maxResults);
182
		}
183
184
		return $nodes;
185
	}
186
187
	private function sort(SearchResult $a, SearchResult $b, array $orders) {
188
		/** @var Order $order */
189
		foreach ($orders as $order) {
190
			$v1 = $this->getSearchResultProperty($a, $order->property);
191
			$v2 = $this->getSearchResultProperty($b, $order->property);
192
193
194
			if ($v1 === null && $v2 === null) {
195
				continue;
196
			}
197
			if ($v1 === null) {
198
				return $order->order === Order::ASC ? 1 : -1;
199
			}
200
			if ($v2 === null) {
201
				return $order->order === Order::ASC ? -1 : 1;
202
			}
203
204
			$s = $this->compareProperties($v1, $v2, $order);
205
			if ($s === 0) {
206
				continue;
207
			}
208
209
			if ($order->order === Order::DESC) {
210
				$s = -$s;
211
			}
212
			return $s;
213
		}
214
215
		return 0;
216
	}
217
218
	private function compareProperties($a, $b, Order $order) {
219
		switch ($order->property->dataType) {
220
			case SearchPropertyDefinition::DATATYPE_STRING:
221
				return strcmp($a, $b);
222
			case SearchPropertyDefinition::DATATYPE_BOOLEAN:
223
				if ($a === $b) {
224
					return 0;
225
				}
226
				if ($a === false) {
227
					return -1;
228
				}
229
				return 1;
230
			default:
231
				if ($a === $b) {
232
					return 0;
233
				}
234
				if ($a < $b) {
235
					return -1;
236
				}
237
				return 1;
238
		}
239
	}
240
241
	private function getSearchResultProperty(SearchResult $result, SearchPropertyDefinition $property) {
242
		/** @var \OCA\DAV\Connector\Sabre\Node $node */
243
		$node = $result->node;
244
245
		switch ($property->name) {
246
			case '{DAV:}displayname':
247
				return $node->getName();
248
			case '{DAV:}getlastmodified':
249
				return $node->getLastModified();
250
			case FilesPlugin::SIZE_PROPERTYNAME:
251
				return $node->getSize();
252
			case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME:
253
				return $node->getInternalFileId();
254
			default:
255
				return null;
256
		}
257
	}
258
259
	/**
260
	 * @param Node $node
261
	 * @return string
262
	 */
263
	private function getHrefForNode(Node $node) {
264
		$base = '/files/' . $this->user->getUID();
265
		return $base . $this->view->getRelativePath($node->getPath());
266
	}
267
268
	/**
269
	 * @param Query $query
270
	 * @return ISearchQuery
271
	 */
272
	private function transformQuery(Query $query): ISearchQuery {
273
		// TODO offset
274
		$limit = $query->limit;
275
		$orders = array_map([$this, 'mapSearchOrder'], $query->orderBy);
276
277
		$limitHome = false;
278
		$ownerProp = $this->extractWhereValue($query->where, FilesPlugin::OWNER_ID_PROPERTYNAME, Operator::OPERATION_EQUAL);
279
		if ($ownerProp !== null) {
280
			if ($ownerProp === $this->user->getUID()) {
281
				$limitHome = true;
282
			} else {
283
				throw new \InvalidArgumentException("Invalid search value for '{http://owncloud.org/ns}owner-id', only the current user id is allowed");
284
			}
285
		}
286
287
		return new SearchQuery(
288
			$this->transformSearchOperation($query->where),
289
			(int)$limit->maxResults,
290
			0,
291
			$orders,
292
			$this->user,
293
			$limitHome
294
		);
295
	}
296
297
	/**
298
	 * @param Order $order
299
	 * @return ISearchOrder
300
	 */
301
	private function mapSearchOrder(Order $order) {
302
		return new SearchOrder($order->order === Order::ASC ? ISearchOrder::DIRECTION_ASCENDING : ISearchOrder::DIRECTION_DESCENDING, $this->mapPropertyNameToColumn($order->property));
303
	}
304
305
	/**
306
	 * @param Operator $operator
307
	 * @return ISearchOperator
308
	 */
309
	private function transformSearchOperation(Operator $operator) {
310
		list(, $trimmedType) = explode('}', $operator->type);
311
		switch ($operator->type) {
312
			case Operator::OPERATION_AND:
313
			case Operator::OPERATION_OR:
314
			case Operator::OPERATION_NOT:
315
				$arguments = array_map([$this, 'transformSearchOperation'], $operator->arguments);
316
				return new SearchBinaryOperator($trimmedType, $arguments);
317
			case Operator::OPERATION_EQUAL:
318
			case Operator::OPERATION_GREATER_OR_EQUAL_THAN:
319
			case Operator::OPERATION_GREATER_THAN:
320
			case Operator::OPERATION_LESS_OR_EQUAL_THAN:
321
			case Operator::OPERATION_LESS_THAN:
322
			case Operator::OPERATION_IS_LIKE:
323
				if (count($operator->arguments) !== 2) {
324
					throw new \InvalidArgumentException('Invalid number of arguments for ' . $trimmedType . ' operation');
325
				}
326
				if (!($operator->arguments[0] instanceof SearchPropertyDefinition)) {
327
					throw new \InvalidArgumentException('Invalid argument 1 for ' . $trimmedType . ' operation, expected property');
328
				}
329
				if (!($operator->arguments[1] instanceof Literal)) {
330
					throw new \InvalidArgumentException('Invalid argument 2 for ' . $trimmedType . ' operation, expected literal');
331
				}
332
				return new SearchComparison($trimmedType, $this->mapPropertyNameToColumn($operator->arguments[0]), $this->castValue($operator->arguments[0], $operator->arguments[1]->value));
333
			case Operator::OPERATION_IS_COLLECTION:
334
				return new SearchComparison('eq', 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE);
335
			default:
336
				throw new \InvalidArgumentException('Unsupported operation ' . $trimmedType . ' (' . $operator->type . ')');
337
		}
338
	}
339
340
	/**
341
	 * @param SearchPropertyDefinition $property
342
	 * @return string
343
	 */
344
	private function mapPropertyNameToColumn(SearchPropertyDefinition $property) {
345
		switch ($property->name) {
346
			case '{DAV:}displayname':
347
				return 'name';
348
			case '{DAV:}getcontenttype':
349
				return 'mimetype';
350
			case '{DAV:}getlastmodified':
351
				return 'mtime';
352
			case FilesPlugin::SIZE_PROPERTYNAME:
353
				return 'size';
354
			case TagsPlugin::FAVORITE_PROPERTYNAME:
355
				return 'favorite';
356
			case TagsPlugin::TAGS_PROPERTYNAME:
357
				return 'tagname';
358
			case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME:
359
				return 'fileid';
360
			default:
361
				throw new \InvalidArgumentException('Unsupported property for search or order: ' . $property->name);
362
		}
363
	}
364
365
	private function castValue(SearchPropertyDefinition $property, $value) {
366
		switch ($property->dataType) {
367
			case SearchPropertyDefinition::DATATYPE_BOOLEAN:
368
				return $value === 'yes';
369
			case SearchPropertyDefinition::DATATYPE_DECIMAL:
370
			case SearchPropertyDefinition::DATATYPE_INTEGER:
371
			case SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER:
372
				return 0 + $value;
373
			case SearchPropertyDefinition::DATATYPE_DATETIME:
374
				if (is_numeric($value)) {
375
					return max(0, 0 + $value);
376
				}
377
				$date = \DateTime::createFromFormat(\DateTime::ATOM, $value);
378
				return ($date instanceof \DateTime && $date->getTimestamp() !== false) ? $date->getTimestamp() : 0;
379
			default:
380
				return $value;
381
		}
382
	}
383
384
	/**
385
	 * Get a specific property from the were clause
386
	 */
387
	private function extractWhereValue(Operator &$operator, string $propertyName, string $comparison, bool $acceptableLocation = true): ?string {
388
		switch ($operator->type) {
389
			case Operator::OPERATION_AND:
390
			case Operator::OPERATION_OR:
391
			case Operator::OPERATION_NOT:
392
				foreach ($operator->arguments as &$argument) {
393
					$value = $this->extractWhereValue($argument, $propertyName, $comparison, $acceptableLocation && $operator->type === Operator::OPERATION_AND);
394
					if ($value !== null) {
395
						return $value;
396
					}
397
				}
398
				return null;
399
			case Operator::OPERATION_EQUAL:
400
			case Operator::OPERATION_GREATER_OR_EQUAL_THAN:
401
			case Operator::OPERATION_GREATER_THAN:
402
			case Operator::OPERATION_LESS_OR_EQUAL_THAN:
403
			case Operator::OPERATION_LESS_THAN:
404
			case Operator::OPERATION_IS_LIKE:
405
				if ($operator->arguments[0]->name === $propertyName) {
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on SearchDAV\Query\Literal.
Loading history...
406
					if ($operator->type === $comparison) {
407
						if ($acceptableLocation) {
408
							if ($operator->arguments[1] instanceof Literal) {
409
								$value = $operator->arguments[1]->value;
410
411
								// to remove the comparison from the query, we replace it with an empty AND
412
								$operator = new Operator(Operator::OPERATION_AND);
413
414
								return $value;
415
							} else {
416
								throw new \InvalidArgumentException("searching by '$propertyName' is only allowed with a literal value");
417
							}
418
						} else{
419
							throw new \InvalidArgumentException("searching by '$propertyName' is not allowed inside a '{DAV:}or' or '{DAV:}not'");
420
						}
421
					} else {
422
						throw new \InvalidArgumentException("searching by '$propertyName' is only allowed inside a '$comparison'");
423
					}
424
				} else {
425
					return null;
426
				}
427
			default:
428
				return null;
429
		}
430
	}
431
}
432