1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FullTextSearch - Full text search framework for Nextcloud |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Maxence Lange <[email protected]> |
9
|
|
|
* @copyright 2018 |
10
|
|
|
* @license GNU AGPL version 3 or any later version |
11
|
|
|
* |
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
15
|
|
|
* License, or (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU Affero General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace OCA\FullTextSearch\Model; |
28
|
|
|
|
29
|
|
|
use OCA\FullTextSearch\Service\MiscService; |
30
|
|
|
|
31
|
|
|
class SearchRequest implements \JsonSerializable { |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
private $providers; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
private $search; |
38
|
|
|
|
39
|
|
|
/** @var int */ |
40
|
|
|
private $page = 1; |
41
|
|
|
|
42
|
|
|
/** @var int */ |
43
|
|
|
private $size = 10; |
44
|
|
|
|
45
|
|
|
/** @var string */ |
46
|
|
|
private $author; |
47
|
|
|
|
48
|
|
|
/** @var array */ |
49
|
|
|
private $tags; |
50
|
|
|
|
51
|
|
|
/** @var array */ |
52
|
|
|
private $options; |
53
|
|
|
|
54
|
|
|
/** @var array */ |
55
|
|
|
private $parts = []; |
56
|
|
|
|
57
|
|
|
/** @var array */ |
58
|
|
|
private $fields = []; |
59
|
|
|
|
60
|
|
|
/** @var array */ |
61
|
|
|
private $wildcardQueries = []; |
62
|
|
|
|
63
|
|
|
/** @var array */ |
64
|
|
|
private $wildcardFilters = []; |
65
|
|
|
|
66
|
|
|
/** @var array */ |
67
|
|
|
private $regexFilters = []; |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* SearchRequest constructor. |
72
|
|
|
*/ |
73
|
|
|
public function __construct() { |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getProviders() { |
81
|
|
|
return $this->providers; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string|array $providers |
86
|
|
|
*/ |
87
|
|
|
public function setProviders($providers) { |
88
|
|
|
if (!is_array($providers)) { |
89
|
|
|
$providers = [$providers]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->providers = $providers; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getAuthor() { |
100
|
|
|
return $this->author; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $author |
105
|
|
|
*/ |
106
|
|
|
public function setAuthor($author) { |
107
|
|
|
$this->author = $author; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getSearch() { |
115
|
|
|
return $this->search; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $search |
120
|
|
|
*/ |
121
|
|
|
public function setSearch($search) { |
122
|
|
|
$this->search = $search; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
public function cleanSearch() { |
127
|
|
|
$search = trim(str_replace(' ', ' ', $this->getSearch())); |
128
|
|
|
|
129
|
|
|
preg_match_all('/[^?]"(?:\\\\.|[^\\\\"])*"|\S+/', " $search ", $words); |
130
|
|
|
$searchItems = []; |
131
|
|
|
foreach ($words[0] as $word) { |
132
|
|
|
if ($this->searchQueryOptions($word)) { |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$searchItems[] = $word; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->setSearch(implode(" ", $searchItems)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param $word |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
private function searchQueryOptions($word) { |
149
|
|
|
if (($pos = strpos($word, ':')) === false) { |
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
list($kw, $value) = explode(':', $word, 2); |
154
|
|
|
$options = ['is', 'show']; |
155
|
|
|
|
156
|
|
|
if (in_array($kw, $options)) { |
157
|
|
|
$this->addOption($kw . '_' . $value, '1'); |
158
|
|
|
|
159
|
|
|
return true; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return int |
168
|
|
|
*/ |
169
|
|
|
public function getPage() { |
170
|
|
|
return $this->page; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param int $page |
175
|
|
|
*/ |
176
|
|
|
public function setPage($page) { |
177
|
|
|
if ($page < 1) { |
178
|
|
|
$page = 1; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$this->page = $page; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return int |
187
|
|
|
*/ |
188
|
|
|
public function getSize() { |
189
|
|
|
return $this->size; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param int $size |
194
|
|
|
*/ |
195
|
|
|
public function setSize($size) { |
196
|
|
|
$this->size = $size; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
|
|
public function getOptions() { |
204
|
|
|
return $this->options; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param array $options |
209
|
|
|
*/ |
210
|
|
|
public function setOptions($options) { |
211
|
|
|
$this->options = $options; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param $key |
216
|
|
|
* @param $value |
217
|
|
|
* |
218
|
|
|
* @return $this |
219
|
|
|
*/ |
220
|
|
|
public function addOption($key, $value) { |
221
|
|
|
$this->options[$key] = $value; |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param string $option |
228
|
|
|
* |
229
|
|
|
* @return mixed|string |
230
|
|
|
*/ |
231
|
|
|
public function getOption($option) { |
232
|
|
|
if (array_key_exists($option, $this->options)) { |
233
|
|
|
return $this->options[$option]; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return ''; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param array $parts |
242
|
|
|
* |
243
|
|
|
* @return $this |
244
|
|
|
*/ |
245
|
|
|
public function setParts($parts) { |
246
|
|
|
$this->parts = $parts; |
247
|
|
|
|
248
|
|
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return array |
253
|
|
|
*/ |
254
|
|
|
public function getParts() { |
255
|
|
|
return $this->parts; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return array |
261
|
|
|
*/ |
262
|
|
|
public function getFields() { |
263
|
|
|
return $this->fields; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param array $fields |
268
|
|
|
* |
269
|
|
|
* @return $this |
270
|
|
|
*/ |
271
|
|
|
public function setFields($fields) { |
272
|
|
|
$this->fields = $fields; |
273
|
|
|
|
274
|
|
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param $field |
279
|
|
|
* |
280
|
|
|
* @return $this |
281
|
|
|
*/ |
282
|
|
|
public function addField($field) { |
283
|
|
|
$this->fields[] = $field; |
284
|
|
|
|
285
|
|
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @param string $tag |
291
|
|
|
*/ |
292
|
|
|
public function addTag($tag) { |
293
|
|
|
$this->tags[] = $tag; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return array |
298
|
|
|
*/ |
299
|
|
|
public function getTags() { |
300
|
|
|
return $this->tags; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param array $tags |
305
|
|
|
*/ |
306
|
|
|
public function setTags($tags) { |
307
|
|
|
$this->tags = $tags; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param array $query |
313
|
|
|
* |
314
|
|
|
* @return $this |
315
|
|
|
*/ |
316
|
|
|
public function addWildcardQuery($query) { |
317
|
|
|
$this->addWildcardQueries([$query]); |
318
|
|
|
|
319
|
|
|
return $this; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param array $query |
324
|
|
|
* |
325
|
|
|
* @return $this |
326
|
|
|
*/ |
327
|
|
|
public function addWildcardQueries($query) { |
328
|
|
|
array_push($this->wildcardQueries, $query); |
329
|
|
|
|
330
|
|
|
return $this; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @return array |
335
|
|
|
*/ |
336
|
|
|
public function getWildcardQueries() { |
337
|
|
|
return $this->wildcardQueries; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @param array $filter |
343
|
|
|
* |
344
|
|
|
* @return $this |
345
|
|
|
*/ |
346
|
|
|
public function addWildcardFilter($filter) { |
347
|
|
|
$this->addWildcardFilters([$filter]); |
348
|
|
|
|
349
|
|
|
return $this; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @param array $filters |
354
|
|
|
* |
355
|
|
|
* @return $this |
356
|
|
|
*/ |
357
|
|
|
public function addWildcardFilters($filters) { |
358
|
|
|
array_push($this->wildcardFilters, $filters); |
359
|
|
|
|
360
|
|
|
return $this; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @return array |
365
|
|
|
*/ |
366
|
|
|
public function getWildcardFilters() { |
367
|
|
|
return $this->wildcardFilters; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @param array $filter |
373
|
|
|
* |
374
|
|
|
* @return $this |
375
|
|
|
*/ |
376
|
|
|
public function addRegexFilter($filter) { |
377
|
|
|
$this->addRegexFilters([$filter]); |
378
|
|
|
|
379
|
|
|
return $this; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* @param array $filters |
384
|
|
|
* |
385
|
|
|
* @return $this |
386
|
|
|
*/ |
387
|
|
|
public function addRegexFilters($filters) { |
388
|
|
|
array_push($this->regexFilters, $filters); |
389
|
|
|
|
390
|
|
|
return $this; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @return array |
395
|
|
|
*/ |
396
|
|
|
public function getRegexFilters() { |
397
|
|
|
return $this->regexFilters; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @return array |
|
|
|
|
403
|
|
|
*/ |
404
|
|
|
public function jsonSerialize() { |
405
|
|
|
return [ |
406
|
|
|
'providers' => $this->getProviders(), |
407
|
|
|
'author' => $this->getAuthor(), |
408
|
|
|
'search' => $this->getSearch(), |
409
|
|
|
'page' => $this->getPage(), |
410
|
|
|
'size' => $this->getSize(), |
411
|
|
|
'parts' => $this->getParts(), |
412
|
|
|
'options' => $this->getOptions(), |
413
|
|
|
'tags' => $this->getTags() |
414
|
|
|
]; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* @param string $json |
420
|
|
|
* |
421
|
|
|
* @return SearchRequest |
422
|
|
|
*/ |
423
|
|
|
public static function fromJSON($json) { |
424
|
|
|
return self::fromArray(json_decode($json, true)); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* @param array $arr |
429
|
|
|
* |
430
|
|
|
* @return SearchRequest |
431
|
|
|
*/ |
432
|
|
|
public static function fromArray($arr) { |
433
|
|
|
$request = new SearchRequest(); |
434
|
|
|
$request->setProviders($arr['providers']); |
435
|
|
|
$request->setAuthor(MiscService::get($arr, 'author', '')); |
436
|
|
|
$request->setSearch(MiscService::get($arr, 'search', '')); |
437
|
|
|
$request->setPage(MiscService::get($arr, 'page', 0)); |
438
|
|
|
$request->setParts(MiscService::get($arr, 'parts', [])); |
|
|
|
|
439
|
|
|
$request->setSize(MiscService::get($arr, 'size', 10)); |
440
|
|
|
$request->setOptions(MiscService::get($arr, 'options', [])); |
|
|
|
|
441
|
|
|
$request->setTags(MiscService::get($arr, 'tags', [])); |
|
|
|
|
442
|
|
|
|
443
|
|
|
return $request; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
|
447
|
|
|
} |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.