Completed
Push — master ( 2ba6ee...8a1088 )
by Maxence
02:21
created

IndexDocument::getHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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