Completed
Push — stable13 ( 2d5d8b...1a016f )
by Roeland
17:00
created
apps/dav/lib/Files/FileSearchBackend.php 1 patch
Indentation   +210 added lines, -210 removed lines patch added patch discarded remove patch
@@ -51,234 +51,234 @@
 block discarded – undo
51 51
 use SearchDAV\XML\Order;
52 52
 
53 53
 class FileSearchBackend implements ISearchBackend {
54
-	/** @var CachingTree */
55
-	private $tree;
54
+    /** @var CachingTree */
55
+    private $tree;
56 56
 
57
-	/** @var IUser */
58
-	private $user;
57
+    /** @var IUser */
58
+    private $user;
59 59
 
60
-	/** @var IRootFolder */
61
-	private $rootFolder;
60
+    /** @var IRootFolder */
61
+    private $rootFolder;
62 62
 
63
-	/** @var IManager */
64
-	private $shareManager;
63
+    /** @var IManager */
64
+    private $shareManager;
65 65
 
66
-	/** @var View */
67
-	private $view;
66
+    /** @var View */
67
+    private $view;
68 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
-	}
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 86
 
87
-	/**
88
-	 * Search endpoint will be remote.php/dav
89
-	 *
90
-	 * @return string
91
-	 */
92
-	public function getArbiterPath() {
93
-		return '';
94
-	}
87
+    /**
88
+     * Search endpoint will be remote.php/dav
89
+     *
90
+     * @return string
91
+     */
92
+    public function getArbiterPath() {
93
+        return '';
94
+    }
95 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
-		}
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 101
 
102
-		try {
103
-			$node = $this->tree->getNodeForPath($path);
104
-			return $node instanceof Directory;
105
-		} catch (NotFound $e) {
106
-			return false;
107
-		}
108
-	}
102
+        try {
103
+            $node = $this->tree->getNodeForPath($path);
104
+            return $node instanceof Directory;
105
+        } catch (NotFound $e) {
106
+            return false;
107
+        }
108
+    }
109 109
 
110
-	public function getPropertyDefinitionsForScope($href, $path) {
111
-		// all valid scopes support the same schema
110
+    public function getPropertyDefinitionsForScope($href, $path) {
111
+        // all valid scopes support the same schema
112 112
 
113
-		//todo dynamically load all propfind properties that are supported
114
-		return [
115
-			// queryable properties
116
-			new SearchPropertyDefinition('{DAV:}displayname', true, false, 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),
113
+        //todo dynamically load all propfind properties that are supported
114
+        return [
115
+            // queryable properties
116
+            new SearchPropertyDefinition('{DAV:}displayname', true, false, 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 122
 
123
-			// select only properties
124
-			new SearchPropertyDefinition('{DAV:}resourcetype', false, true, false),
125
-			new SearchPropertyDefinition('{DAV:}getcontentlength', false, true, false),
126
-			new SearchPropertyDefinition(FilesPlugin::CHECKSUMS_PROPERTYNAME, false, true, false),
127
-			new SearchPropertyDefinition(FilesPlugin::PERMISSIONS_PROPERTYNAME, false, true, false),
128
-			new SearchPropertyDefinition(FilesPlugin::GETETAG_PROPERTYNAME, false, true, false),
129
-			new SearchPropertyDefinition(FilesPlugin::OWNER_ID_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
-	}
123
+            // select only properties
124
+            new SearchPropertyDefinition('{DAV:}resourcetype', false, true, false),
125
+            new SearchPropertyDefinition('{DAV:}getcontentlength', false, true, false),
126
+            new SearchPropertyDefinition(FilesPlugin::CHECKSUMS_PROPERTYNAME, false, true, false),
127
+            new SearchPropertyDefinition(FilesPlugin::PERMISSIONS_PROPERTYNAME, false, true, false),
128
+            new SearchPropertyDefinition(FilesPlugin::GETETAG_PROPERTYNAME, false, true, false),
129
+            new SearchPropertyDefinition(FilesPlugin::OWNER_ID_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 136
 
137
-	/**
138
-	 * @param BasicSearch $search
139
-	 * @return SearchResult[]
140
-	 */
141
-	public function search(BasicSearch $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
-		}
137
+    /**
138
+     * @param BasicSearch $search
139
+     * @return SearchResult[]
140
+     */
141
+    public function search(BasicSearch $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 154
 
155
-		$fileInfo = $node->getFileInfo();
156
-		$folder = $this->rootFolder->get($fileInfo->getPath());
157
-		/** @var Folder $folder $results */
158
-		$results = $folder->search($query);
155
+        $fileInfo = $node->getFileInfo();
156
+        $folder = $this->rootFolder->get($fileInfo->getPath());
157
+        /** @var Folder $folder $results */
158
+        $results = $folder->search($query);
159 159
 
160
-		return array_map(function (Node $node) {
161
-			if ($node instanceof Folder) {
162
-				$davNode = new \OCA\DAV\Connector\Sabre\Directory($this->view, $node, $this->tree, $this->shareManager);
163
-			} else {
164
-				$davNode = new \OCA\DAV\Connector\Sabre\File($this->view, $node, $this->shareManager);
165
-			}
166
-			$path = $this->getHrefForNode($node);
167
-			$this->tree->cacheNode($davNode, $path);
168
-			return new SearchResult($davNode, $path);
169
-		}, $results);
170
-	}
160
+        return array_map(function (Node $node) {
161
+            if ($node instanceof Folder) {
162
+                $davNode = new \OCA\DAV\Connector\Sabre\Directory($this->view, $node, $this->tree, $this->shareManager);
163
+            } else {
164
+                $davNode = new \OCA\DAV\Connector\Sabre\File($this->view, $node, $this->shareManager);
165
+            }
166
+            $path = $this->getHrefForNode($node);
167
+            $this->tree->cacheNode($davNode, $path);
168
+            return new SearchResult($davNode, $path);
169
+        }, $results);
170
+    }
171 171
 
172
-	/**
173
-	 * @param Node $node
174
-	 * @return string
175
-	 */
176
-	private function getHrefForNode(Node $node) {
177
-		$base = '/files/' . $this->user->getUID();
178
-		return $base . $this->view->getRelativePath($node->getPath());
179
-	}
172
+    /**
173
+     * @param Node $node
174
+     * @return string
175
+     */
176
+    private function getHrefForNode(Node $node) {
177
+        $base = '/files/' . $this->user->getUID();
178
+        return $base . $this->view->getRelativePath($node->getPath());
179
+    }
180 180
 
181
-	/**
182
-	 * @param BasicSearch $query
183
-	 * @return ISearchQuery
184
-	 */
185
-	private function transformQuery(BasicSearch $query) {
186
-		// TODO offset, limit
187
-		$orders = array_map([$this, 'mapSearchOrder'], $query->orderBy);
188
-		return new SearchQuery($this->transformSearchOperation($query->where), 0, 0, $orders, $this->user);
189
-	}
181
+    /**
182
+     * @param BasicSearch $query
183
+     * @return ISearchQuery
184
+     */
185
+    private function transformQuery(BasicSearch $query) {
186
+        // TODO offset, limit
187
+        $orders = array_map([$this, 'mapSearchOrder'], $query->orderBy);
188
+        return new SearchQuery($this->transformSearchOperation($query->where), 0, 0, $orders, $this->user);
189
+    }
190 190
 
191
-	/**
192
-	 * @param Order $order
193
-	 * @return ISearchOrder
194
-	 */
195
-	private function mapSearchOrder(Order $order) {
196
-		return new SearchOrder($order->order === Order::ASC ? ISearchOrder::DIRECTION_ASCENDING : ISearchOrder::DIRECTION_DESCENDING, $this->mapPropertyNameToColumn($order->property));
197
-	}
191
+    /**
192
+     * @param Order $order
193
+     * @return ISearchOrder
194
+     */
195
+    private function mapSearchOrder(Order $order) {
196
+        return new SearchOrder($order->order === Order::ASC ? ISearchOrder::DIRECTION_ASCENDING : ISearchOrder::DIRECTION_DESCENDING, $this->mapPropertyNameToColumn($order->property));
197
+    }
198 198
 
199
-	/**
200
-	 * @param Operator $operator
201
-	 * @return ISearchOperator
202
-	 */
203
-	private function transformSearchOperation(Operator $operator) {
204
-		list(, $trimmedType) = explode('}', $operator->type);
205
-		switch ($operator->type) {
206
-			case Operator::OPERATION_AND:
207
-			case Operator::OPERATION_OR:
208
-			case Operator::OPERATION_NOT:
209
-				$arguments = array_map([$this, 'transformSearchOperation'], $operator->arguments);
210
-				return new SearchBinaryOperator($trimmedType, $arguments);
211
-			case Operator::OPERATION_EQUAL:
212
-			case Operator::OPERATION_GREATER_OR_EQUAL_THAN:
213
-			case Operator::OPERATION_GREATER_THAN:
214
-			case Operator::OPERATION_LESS_OR_EQUAL_THAN:
215
-			case Operator::OPERATION_LESS_THAN:
216
-			case Operator::OPERATION_IS_LIKE:
217
-				if (count($operator->arguments) !== 2) {
218
-					throw new \InvalidArgumentException('Invalid number of arguments for ' . $trimmedType . ' operation');
219
-				}
220
-				if (!is_string($operator->arguments[0])) {
221
-					throw new \InvalidArgumentException('Invalid argument 1 for ' . $trimmedType . ' operation, expected property');
222
-				}
223
-				if (!($operator->arguments[1] instanceof Literal)) {
224
-					throw new \InvalidArgumentException('Invalid argument 2 for ' . $trimmedType . ' operation, expected literal');
225
-				}
226
-				return new SearchComparison($trimmedType, $this->mapPropertyNameToColumn($operator->arguments[0]), $this->castValue($operator->arguments[0], $operator->arguments[1]->value));
227
-			case Operator::OPERATION_IS_COLLECTION:
228
-				return new SearchComparison('eq', 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE);
229
-			default:
230
-				throw new \InvalidArgumentException('Unsupported operation ' . $trimmedType . ' (' . $operator->type . ')');
231
-		}
232
-	}
199
+    /**
200
+     * @param Operator $operator
201
+     * @return ISearchOperator
202
+     */
203
+    private function transformSearchOperation(Operator $operator) {
204
+        list(, $trimmedType) = explode('}', $operator->type);
205
+        switch ($operator->type) {
206
+            case Operator::OPERATION_AND:
207
+            case Operator::OPERATION_OR:
208
+            case Operator::OPERATION_NOT:
209
+                $arguments = array_map([$this, 'transformSearchOperation'], $operator->arguments);
210
+                return new SearchBinaryOperator($trimmedType, $arguments);
211
+            case Operator::OPERATION_EQUAL:
212
+            case Operator::OPERATION_GREATER_OR_EQUAL_THAN:
213
+            case Operator::OPERATION_GREATER_THAN:
214
+            case Operator::OPERATION_LESS_OR_EQUAL_THAN:
215
+            case Operator::OPERATION_LESS_THAN:
216
+            case Operator::OPERATION_IS_LIKE:
217
+                if (count($operator->arguments) !== 2) {
218
+                    throw new \InvalidArgumentException('Invalid number of arguments for ' . $trimmedType . ' operation');
219
+                }
220
+                if (!is_string($operator->arguments[0])) {
221
+                    throw new \InvalidArgumentException('Invalid argument 1 for ' . $trimmedType . ' operation, expected property');
222
+                }
223
+                if (!($operator->arguments[1] instanceof Literal)) {
224
+                    throw new \InvalidArgumentException('Invalid argument 2 for ' . $trimmedType . ' operation, expected literal');
225
+                }
226
+                return new SearchComparison($trimmedType, $this->mapPropertyNameToColumn($operator->arguments[0]), $this->castValue($operator->arguments[0], $operator->arguments[1]->value));
227
+            case Operator::OPERATION_IS_COLLECTION:
228
+                return new SearchComparison('eq', 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE);
229
+            default:
230
+                throw new \InvalidArgumentException('Unsupported operation ' . $trimmedType . ' (' . $operator->type . ')');
231
+        }
232
+    }
233 233
 
234
-	/**
235
-	 * @param string $propertyName
236
-	 * @return string
237
-	 */
238
-	private function mapPropertyNameToColumn($propertyName) {
239
-		switch ($propertyName) {
240
-			case '{DAV:}displayname':
241
-				return 'name';
242
-			case '{DAV:}getcontenttype':
243
-				return 'mimetype';
244
-			case '{DAV:}getlastmodified':
245
-				return 'mtime';
246
-			case FilesPlugin::SIZE_PROPERTYNAME:
247
-				return 'size';
248
-			case TagsPlugin::FAVORITE_PROPERTYNAME:
249
-				return 'favorite';
250
-			case TagsPlugin::TAGS_PROPERTYNAME:
251
-				return 'tagname';
252
-			case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME:
253
-				return 'fileid';
254
-			default:
255
-				throw new \InvalidArgumentException('Unsupported property for search or order: ' . $propertyName);
256
-		}
257
-	}
234
+    /**
235
+     * @param string $propertyName
236
+     * @return string
237
+     */
238
+    private function mapPropertyNameToColumn($propertyName) {
239
+        switch ($propertyName) {
240
+            case '{DAV:}displayname':
241
+                return 'name';
242
+            case '{DAV:}getcontenttype':
243
+                return 'mimetype';
244
+            case '{DAV:}getlastmodified':
245
+                return 'mtime';
246
+            case FilesPlugin::SIZE_PROPERTYNAME:
247
+                return 'size';
248
+            case TagsPlugin::FAVORITE_PROPERTYNAME:
249
+                return 'favorite';
250
+            case TagsPlugin::TAGS_PROPERTYNAME:
251
+                return 'tagname';
252
+            case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME:
253
+                return 'fileid';
254
+            default:
255
+                throw new \InvalidArgumentException('Unsupported property for search or order: ' . $propertyName);
256
+        }
257
+    }
258 258
 
259
-	private function castValue($propertyName, $value) {
260
-		$allProps = $this->getPropertyDefinitionsForScope('', '');
261
-		foreach ($allProps as $prop) {
262
-			if ($prop->name === $propertyName) {
263
-				$dataType = $prop->dataType;
264
-				switch ($dataType) {
265
-					case SearchPropertyDefinition::DATATYPE_BOOLEAN:
266
-						return $value === 'yes';
267
-					case SearchPropertyDefinition::DATATYPE_DECIMAL:
268
-					case SearchPropertyDefinition::DATATYPE_INTEGER:
269
-					case SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER:
270
-						return 0 + $value;
271
-					case SearchPropertyDefinition::DATATYPE_DATETIME:
272
-						if (is_numeric($value)) {
273
-							return max(0, 0 + $value);
274
-						}
275
-						$date = \DateTime::createFromFormat(\DateTime::ATOM, $value);
276
-						return ($date instanceof \DateTime) ? $date->getTimestamp() : 0;
277
-					default:
278
-						return $value;
279
-				}
280
-			}
281
-		}
282
-		return $value;
283
-	}
259
+    private function castValue($propertyName, $value) {
260
+        $allProps = $this->getPropertyDefinitionsForScope('', '');
261
+        foreach ($allProps as $prop) {
262
+            if ($prop->name === $propertyName) {
263
+                $dataType = $prop->dataType;
264
+                switch ($dataType) {
265
+                    case SearchPropertyDefinition::DATATYPE_BOOLEAN:
266
+                        return $value === 'yes';
267
+                    case SearchPropertyDefinition::DATATYPE_DECIMAL:
268
+                    case SearchPropertyDefinition::DATATYPE_INTEGER:
269
+                    case SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER:
270
+                        return 0 + $value;
271
+                    case SearchPropertyDefinition::DATATYPE_DATETIME:
272
+                        if (is_numeric($value)) {
273
+                            return max(0, 0 + $value);
274
+                        }
275
+                        $date = \DateTime::createFromFormat(\DateTime::ATOM, $value);
276
+                        return ($date instanceof \DateTime) ? $date->getTimestamp() : 0;
277
+                    default:
278
+                        return $value;
279
+                }
280
+            }
281
+        }
282
+        return $value;
283
+    }
284 284
 }
Please login to merge, or discard this patch.