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 $wildcardFields = []; |
62
|
|
|
|
63
|
|
|
/** @var array */ |
64
|
|
|
private $wildcardQueries = []; |
65
|
|
|
|
66
|
|
|
/** @var array */ |
67
|
|
|
private $wildcardFilters = []; |
68
|
|
|
|
69
|
|
|
/** @var array */ |
70
|
|
|
private $regexFilters = []; |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* SearchRequest constructor. |
75
|
|
|
*/ |
76
|
|
|
public function __construct() { |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function getProviders() { |
84
|
|
|
return $this->providers; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string|array $providers |
89
|
|
|
*/ |
90
|
|
|
public function setProviders($providers) { |
91
|
|
|
if (!is_array($providers)) { |
92
|
|
|
$providers = [$providers]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->providers = $providers; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getAuthor() { |
103
|
|
|
return $this->author; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $author |
108
|
|
|
*/ |
109
|
|
|
public function setAuthor($author) { |
110
|
|
|
$this->author = $author; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getSearch() { |
118
|
|
|
return $this->search; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $search |
123
|
|
|
*/ |
124
|
|
|
public function setSearch($search) { |
125
|
|
|
$this->search = $search; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* |
131
|
|
|
*/ |
132
|
|
|
public function cleanSearch() { |
133
|
|
|
$search = trim(str_replace(' ', ' ', $this->getSearch())); |
134
|
|
|
|
135
|
|
|
preg_match_all('/[^?]"(?:\\\\.|[^\\\\"])*"|\S+/', " $search ", $words); |
136
|
|
|
$searchItems = []; |
137
|
|
|
foreach ($words[0] as $word) { |
138
|
|
|
if ($this->searchQueryOptions($word)) { |
139
|
|
|
continue; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$searchItems[] = $word; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->setSearch(implode(" ", $searchItems)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param $word |
151
|
|
|
* |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
private function searchQueryOptions($word) { |
155
|
|
|
if (($pos = strpos($word, ':')) === false) { |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
list($kw, $value) = explode(':', $word, 2); |
160
|
|
|
$options = ['is', 'show']; |
161
|
|
|
|
162
|
|
|
if (in_array($kw, $options)) { |
163
|
|
|
$this->addOption($kw . '_' . $value, '1'); |
164
|
|
|
|
165
|
|
|
return true; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return int |
174
|
|
|
*/ |
175
|
|
|
public function getPage() { |
176
|
|
|
return $this->page; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param int $page |
181
|
|
|
*/ |
182
|
|
|
public function setPage($page) { |
183
|
|
|
if ($page < 1) { |
184
|
|
|
$page = 1; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$this->page = $page; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return int |
193
|
|
|
*/ |
194
|
|
|
public function getSize() { |
195
|
|
|
return $this->size; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param int $size |
200
|
|
|
*/ |
201
|
|
|
public function setSize($size) { |
202
|
|
|
$this->size = $size; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
public function getOptions() { |
210
|
|
|
return $this->options; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param array $options |
215
|
|
|
*/ |
216
|
|
|
public function setOptions($options) { |
217
|
|
|
$this->options = $options; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param $key |
222
|
|
|
* @param $value |
223
|
|
|
* |
224
|
|
|
* @return $this |
225
|
|
|
*/ |
226
|
|
|
public function addOption($key, $value) { |
227
|
|
|
$this->options[$key] = $value; |
228
|
|
|
|
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $option |
234
|
|
|
* |
235
|
|
|
* @return mixed|string |
236
|
|
|
*/ |
237
|
|
|
public function getOption($option) { |
238
|
|
|
if (array_key_exists($option, $this->options)) { |
239
|
|
|
return $this->options[$option]; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return ''; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param array $parts |
248
|
|
|
* |
249
|
|
|
* @return $this |
250
|
|
|
*/ |
251
|
|
|
public function setParts($parts) { |
252
|
|
|
$this->parts = $parts; |
253
|
|
|
|
254
|
|
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return array |
259
|
|
|
*/ |
260
|
|
|
public function getParts() { |
261
|
|
|
return $this->parts; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return array |
267
|
|
|
*/ |
268
|
|
|
public function getFields() { |
269
|
|
|
return $this->fields; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param array $fields |
274
|
|
|
* |
275
|
|
|
* @return $this |
276
|
|
|
*/ |
277
|
|
|
public function setFields($fields) { |
278
|
|
|
$this->fields = $fields; |
279
|
|
|
|
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param $field |
285
|
|
|
* |
286
|
|
|
* @return $this |
287
|
|
|
*/ |
288
|
|
|
public function addField($field) { |
289
|
|
|
$this->fields[] = $field; |
290
|
|
|
|
291
|
|
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param string $tag |
297
|
|
|
*/ |
298
|
|
|
public function addTag($tag) { |
299
|
|
|
$this->tags[] = $tag; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @return array |
304
|
|
|
*/ |
305
|
|
|
public function getTags() { |
306
|
|
|
return $this->tags; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param array $tags |
311
|
|
|
*/ |
312
|
|
|
public function setTags($tags) { |
313
|
|
|
$this->tags = $tags; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param array $query |
|
|
|
|
319
|
|
|
* |
320
|
|
|
* @return $this |
321
|
|
|
*/ |
322
|
|
|
public function addWildcardField($field) { |
323
|
|
|
$this->wildcardFields[] = $field; |
324
|
|
|
|
325
|
|
|
return $this; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @return array |
330
|
|
|
*/ |
331
|
|
|
public function getWildcardFields() { |
332
|
|
|
return $this->wildcardFields; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @param array $query |
|
|
|
|
338
|
|
|
* |
339
|
|
|
* @return $this |
340
|
|
|
*/ |
341
|
|
|
public function addWildcardQuery($field) { |
|
|
|
|
342
|
|
|
$this->addWildcardQueries([$query]); |
|
|
|
|
343
|
|
|
|
344
|
|
|
return $this; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @param array $query |
349
|
|
|
* |
350
|
|
|
* @return $this |
351
|
|
|
*/ |
352
|
|
|
public function addWildcardQueries($query) { |
353
|
|
|
array_push($this->wildcardQueries, $query); |
354
|
|
|
|
355
|
|
|
return $this; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @return array |
360
|
|
|
*/ |
361
|
|
|
public function getWildcardQueries() { |
362
|
|
|
return $this->wildcardQueries; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @param array $filter |
368
|
|
|
* |
369
|
|
|
* @return $this |
370
|
|
|
*/ |
371
|
|
|
public function addWildcardFilter($filter) { |
372
|
|
|
$this->addWildcardFilters([$filter]); |
373
|
|
|
|
374
|
|
|
return $this; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @param array $filters |
379
|
|
|
* |
380
|
|
|
* @return $this |
381
|
|
|
*/ |
382
|
|
|
public function addWildcardFilters($filters) { |
383
|
|
|
array_push($this->wildcardFilters, $filters); |
384
|
|
|
|
385
|
|
|
return $this; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @return array |
390
|
|
|
*/ |
391
|
|
|
public function getWildcardFilters() { |
392
|
|
|
return $this->wildcardFilters; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* @param array $filter |
398
|
|
|
* |
399
|
|
|
* @return $this |
400
|
|
|
*/ |
401
|
|
|
public function addRegexFilter($filter) { |
402
|
|
|
$this->addRegexFilters([$filter]); |
403
|
|
|
|
404
|
|
|
return $this; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @param array $filters |
409
|
|
|
* |
410
|
|
|
* @return $this |
411
|
|
|
*/ |
412
|
|
|
public function addRegexFilters($filters) { |
413
|
|
|
array_push($this->regexFilters, $filters); |
414
|
|
|
|
415
|
|
|
return $this; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* @return array |
420
|
|
|
*/ |
421
|
|
|
public function getRegexFilters() { |
422
|
|
|
return $this->regexFilters; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* @return array |
|
|
|
|
428
|
|
|
*/ |
429
|
|
|
public function jsonSerialize() { |
430
|
|
|
return [ |
431
|
|
|
'providers' => $this->getProviders(), |
432
|
|
|
'author' => $this->getAuthor(), |
433
|
|
|
'search' => $this->getSearch(), |
434
|
|
|
'page' => $this->getPage(), |
435
|
|
|
'size' => $this->getSize(), |
436
|
|
|
'parts' => $this->getParts(), |
437
|
|
|
'options' => $this->getOptions(), |
438
|
|
|
'tags' => $this->getTags() |
439
|
|
|
]; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* @param string $json |
445
|
|
|
* |
446
|
|
|
* @return SearchRequest |
447
|
|
|
*/ |
448
|
|
|
public static function fromJSON($json) { |
449
|
|
|
return self::fromArray(json_decode($json, true)); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* @param array $arr |
454
|
|
|
* |
455
|
|
|
* @return SearchRequest |
456
|
|
|
*/ |
457
|
|
|
public static function fromArray($arr) { |
458
|
|
|
$request = new SearchRequest(); |
459
|
|
|
$request->setProviders($arr['providers']); |
460
|
|
|
$request->setAuthor(MiscService::get($arr, 'author', '')); |
461
|
|
|
$request->setSearch(MiscService::get($arr, 'search', '')); |
462
|
|
|
$request->setPage(MiscService::get($arr, 'page', 0)); |
463
|
|
|
$request->setParts(MiscService::get($arr, 'parts', [])); |
|
|
|
|
464
|
|
|
$request->setSize(MiscService::get($arr, 'size', 10)); |
465
|
|
|
$request->setOptions(MiscService::get($arr, 'options', [])); |
|
|
|
|
466
|
|
|
$request->setTags(MiscService::get($arr, 'tags', [])); |
|
|
|
|
467
|
|
|
|
468
|
|
|
return $request; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
|
472
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.