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
|
|
|
class IndexDocument implements \JsonSerializable { |
30
|
|
|
|
31
|
|
|
const NOT_ENCODED = 0; |
32
|
|
|
const ENCODED_BASE64 = 1; |
33
|
|
|
|
34
|
|
|
/** @var string|int */ |
35
|
|
|
private $id; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
private $providerId; |
39
|
|
|
|
40
|
|
|
/** @var DocumentAccess */ |
41
|
|
|
private $access; |
42
|
|
|
|
43
|
|
|
/** @var Index */ |
44
|
|
|
private $index; |
45
|
|
|
|
46
|
|
|
/** @var int */ |
47
|
|
|
private $modifiedTime = 0; |
48
|
|
|
|
49
|
|
|
/** @var array */ |
50
|
|
|
private $tags = []; |
51
|
|
|
|
52
|
|
|
/** @var string */ |
53
|
|
|
private $source; |
54
|
|
|
|
55
|
|
|
/** @var string */ |
56
|
|
|
private $title; |
57
|
|
|
|
58
|
|
|
/** @var string */ |
59
|
|
|
private $content = null; |
60
|
|
|
|
61
|
|
|
/** @var string */ |
62
|
|
|
private $link = ''; |
63
|
|
|
|
64
|
|
|
/** @var array */ |
65
|
|
|
private $more = []; |
66
|
|
|
|
67
|
|
|
/** @var array */ |
68
|
|
|
private $excerpts = []; |
69
|
|
|
|
70
|
|
|
/** @var string */ |
71
|
|
|
private $score; |
72
|
|
|
|
73
|
|
|
/** @var array */ |
74
|
|
|
private $info = []; |
75
|
|
|
|
76
|
|
|
/** @var int */ |
77
|
|
|
private $contentEncoded; |
78
|
|
|
|
79
|
|
|
public function __construct($providerId, $id) { |
80
|
|
|
$this->providerId = $providerId; |
81
|
|
|
$this->id = $id; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string|integer $id |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setId($id) { |
91
|
|
|
$this->id = $id; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string|integer |
98
|
|
|
*/ |
99
|
|
|
public function getId() { |
100
|
|
|
return $this->id; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $providerId |
106
|
|
|
* |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function setProviderId($providerId) { |
110
|
|
|
$this->providerId = $providerId; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getProviderId() { |
119
|
|
|
return $this->providerId; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param Index $index |
125
|
|
|
*/ |
126
|
|
|
public function setIndex(Index $index) { |
127
|
|
|
$this->index = $index; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return Index |
132
|
|
|
*/ |
133
|
|
|
public function getIndex() { |
134
|
|
|
return $this->index; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param int $modifiedTime |
140
|
|
|
* |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function setModifiedTime($modifiedTime) { |
144
|
|
|
$this->modifiedTime = $modifiedTime; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return int |
151
|
|
|
*/ |
152
|
|
|
public function getModifiedTime() { |
153
|
|
|
return $this->modifiedTime; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param int $time |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public function isOlderThan($time) { |
162
|
|
|
return ($this->modifiedTime < $time); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param DocumentAccess $access |
168
|
|
|
* |
169
|
|
|
* @return $this |
170
|
|
|
*/ |
171
|
|
|
public function setAccess(DocumentAccess $access) { |
172
|
|
|
$this->access = $access; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return DocumentAccess |
179
|
|
|
*/ |
180
|
|
|
public function getAccess() { |
181
|
|
|
return $this->access; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param array $tags |
187
|
|
|
* |
188
|
|
|
* @return $this |
189
|
|
|
*/ |
190
|
|
|
public function setTags($tags) { |
191
|
|
|
$this->tags = $tags; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
|
|
public function getTags() { |
200
|
|
|
return $this->tags; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param $tag |
205
|
|
|
* |
206
|
|
|
* @return $this |
207
|
|
|
*/ |
208
|
|
|
public function addTag($tag) { |
209
|
|
|
$this->tags[] = $tag; |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
public function getSource() { |
218
|
|
|
return $this->source; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param string $source |
223
|
|
|
* |
224
|
|
|
* @return $this |
225
|
|
|
*/ |
226
|
|
|
public function setSource($source) { |
227
|
|
|
$this->source = $source; |
228
|
|
|
|
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param string $title |
235
|
|
|
* |
236
|
|
|
* @return $this |
237
|
|
|
*/ |
238
|
|
|
public function setTitle($title) { |
239
|
|
|
$this->title = $title; |
240
|
|
|
|
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
|
|
public function getTitle() { |
248
|
|
|
return $this->title; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param string $content |
254
|
|
|
* @param int $encoded |
255
|
|
|
* |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
|
|
public function setContent($content, $encoded = 0) { |
259
|
|
|
$this->content = $content; |
260
|
|
|
$this->contentEncoded = $encoded; |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
public function getContent() { |
269
|
|
|
return $this->content; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return int |
275
|
|
|
*/ |
276
|
|
|
public function isContentEncoded() { |
277
|
|
|
return $this->contentEncoded; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param string $link |
283
|
|
|
* |
284
|
|
|
* @return $this |
285
|
|
|
*/ |
286
|
|
|
public function setLink($link) { |
287
|
|
|
$this->link = $link; |
288
|
|
|
|
289
|
|
|
return $this; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return string |
294
|
|
|
*/ |
295
|
|
|
public function getLink() { |
296
|
|
|
return $this->link; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param array $more |
302
|
|
|
* |
303
|
|
|
* @return $this |
304
|
|
|
*/ |
305
|
|
|
public function setMore($more) { |
306
|
|
|
$this->more = $more; |
307
|
|
|
|
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return array |
313
|
|
|
*/ |
314
|
|
|
public function getMore() { |
315
|
|
|
return $this->more; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param array $excerpts |
321
|
|
|
* |
322
|
|
|
* @return $this |
323
|
|
|
*/ |
324
|
|
|
public function setExcerpts($excerpts) { |
325
|
|
|
$excerpts = array_map([$this, 'cleanExcerpt'], $excerpts); |
326
|
|
|
|
327
|
|
|
$this->excerpts = $excerpts; |
328
|
|
|
|
329
|
|
|
return $this; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @return array |
334
|
|
|
*/ |
335
|
|
|
public function getExcerpts() { |
336
|
|
|
return $this->excerpts; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @param string $excerpt |
341
|
|
|
*/ |
342
|
|
|
public function addExcerpt($excerpt) { |
343
|
|
|
$excerpt = $this->cleanExcerpt($excerpt); |
344
|
|
|
|
345
|
|
|
$this->excerpts[] = $excerpt; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @param $excerpt |
350
|
|
|
* |
351
|
|
|
* @return mixed |
352
|
|
|
*/ |
353
|
|
|
public function cleanExcerpt($excerpt) { |
354
|
|
|
$excerpt = str_replace("\\n", ' ', $excerpt); |
355
|
|
|
$excerpt = str_replace("\\r", ' ', $excerpt); |
356
|
|
|
$excerpt = str_replace("\n", ' ', $excerpt); |
357
|
|
|
$excerpt = str_replace("\r", ' ', $excerpt); |
358
|
|
|
|
359
|
|
|
return $excerpt; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @param string $score |
364
|
|
|
* |
365
|
|
|
* @return $this |
366
|
|
|
*/ |
367
|
|
|
public function setScore($score) { |
368
|
|
|
$this->score = $score; |
369
|
|
|
|
370
|
|
|
return $this; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @return string |
375
|
|
|
*/ |
376
|
|
|
public function getScore() { |
377
|
|
|
return $this->score; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @param string $info |
383
|
|
|
* @param mixed $value |
384
|
|
|
* |
385
|
|
|
* @return $this |
386
|
|
|
*/ |
387
|
|
|
public function setInfo($info, $value) { |
388
|
|
|
$this->info[$info] = $value; |
389
|
|
|
|
390
|
|
|
return $this; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @param string $info |
395
|
|
|
* @param mixed $default |
396
|
|
|
* |
397
|
|
|
* @return mixed |
398
|
|
|
*/ |
399
|
|
|
public function getInfo($info, $default = '') { |
400
|
|
|
if (!key_exists($info, $this->info)) { |
401
|
|
|
return $default; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
return $this->info[$info]; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* @return array |
410
|
|
|
*/ |
411
|
|
|
public function getInfoAll() { |
412
|
|
|
|
413
|
|
|
$info = []; |
414
|
|
|
foreach ($this->info as $k => $v) { |
415
|
|
|
if (substr($k, 0, 1) === '_') { |
416
|
|
|
continue; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
$info[$k] = $v; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
return $info; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
|
426
|
|
|
public function __destruct() { |
427
|
|
|
unset($this->id); |
428
|
|
|
unset($this->providerId); |
429
|
|
|
unset($this->access); |
430
|
|
|
unset($this->modifiedTime); |
431
|
|
|
unset($this->title); |
432
|
|
|
unset($this->content); |
433
|
|
|
unset($this->link); |
434
|
|
|
unset($this->more); |
435
|
|
|
unset($this->excerpts); |
436
|
|
|
unset($this->score); |
437
|
|
|
unset($this->info); |
438
|
|
|
unset($this->contentEncoded); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* @return array<string,string|integer|DocumentAccess|array> |
443
|
|
|
*/ |
444
|
|
|
public function jsonSerialize() { |
445
|
|
|
return [ |
446
|
|
|
'id' => $this->getId(), |
447
|
|
|
'providerId' => $this->getProviderId(), |
448
|
|
|
'access' => $this->getAccess(), |
449
|
|
|
'modifiedTime' => $this->getModifiedTime(), |
450
|
|
|
'title' => $this->getTitle(), |
451
|
|
|
'link' => $this->getLink(), |
452
|
|
|
'more' => $this->getMore(), |
453
|
|
|
'excerpts' => $this->getExcerpts(), |
454
|
|
|
'score' => $this->getScore() |
455
|
|
|
]; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
} |