1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
|
<?php
declare(strict_types=1);
/**
* @copyright 2018
*
* @author Maxence Lange <maxence@artificial-owl.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\FullTextSearch\Model;
use JsonSerializable;
use OCP\FullTextSearch\Model\IDocumentAccess;
use OCP\FullTextSearch\Model\IIndex;
use OCP\FullTextSearch\Model\IIndexDocument;
/**
* Class IndexDocument
*
* This is one of the main class of the FullTextSearch, used as a data transfer
* object. An IndexDocument is created to manage documents around FullTextSearch,
* during an index and during a search.
* The uniqueness of an IndexDocument is made by the Id of the Content Provider
* and the Id of the original document within the Content Provider.
*
* We will call original document the source from which the IndexDocument is
* generated. As an example, an original document can be a file, a mail, ...
*
* @since 15.0.0
*
* @package OC\FullTextSearch\Model
*/
class IndexDocument implements IIndexDocument, JsonSerializable {
/** @var string */
protected $id = '';
/** @var string */
protected $providerId = '';
/** @var DocumentAccess */
protected $access;
/** @var IIndex */
protected $index;
/** @var int */
protected $modifiedTime = 0;
/** @var string */
protected $source = '';
/** @var array */
protected $tags = [];
/** @var array */
protected $metaTags = [];
/** @var array */
protected $subTags = [];
/** @var string */
protected $title = '';
/** @var string */
protected $content = '';
/** @var string */
protected $hash = '';
/** @var array */
protected $parts = [];
/** @var string */
protected $link = '';
/** @var array */
protected $more = [];
/** @var array */
protected $excerpts = [];
/** @var string */
protected $score = '';
/** @var array */
protected $info = [];
/** @var int */
protected $contentEncoded = 0;
/**
* IIndexDocument constructor.
*
* On creation, we assure the uniqueness of the object using the providerId
* and the Id of the original document.
*
* @since 15.0.0
*
* @param string $providerId
* @param string $documentId
*/
public function __construct(string $providerId, string $documentId) {
$this->providerId = $providerId;
$this->id = $documentId;
}
/**
* Returns the Id of the original document.
*
* @since 15.0.0
*
* @return string
*/
final public function getId(): string {
return $this->id;
}
/**
* Returns the Id of the provider.
*
* @since 15.0.0
*
* @return string
*/
final public function getProviderId(): string {
return $this->providerId;
}
/**
* Set the Index related to the IIndexDocument.
*
* @see IIndex
*
* @since 15.0.0
*
* @param IIndex $index
*
* @return IIndexDocument
*/
final public function setIndex(IIndex $index): IIndexDocument {
$this->index = $index;
return $this;
}
/**
* Get the Index.
*
* @since 15.0.0
*
* @return IIndex
*/
final public function getIndex(): IIndex {
return $this->index;
}
/**
* return if Index is defined.
*
* @since 16.0.0
*
* @return bool
*/
final public function hasIndex(): bool {
return ($this->index !== null);
}
/**
* Set the modified time of the original document.
*
* @since 15.0.0
*
* @param int $modifiedTime
*
* @return IIndexDocument
*/
final public function setModifiedTime(int $modifiedTime): IIndexDocument {
$this->modifiedTime = $modifiedTime;
return $this;
}
/**
* Get the modified time of the original document.
*
* @since 15.0.0
*
* @return int
*/
final public function getModifiedTime(): int {
return $this->modifiedTime;
}
/**
* Check if the original document of the IIndexDocument is older than $time.
*
* @since 15.0.0
*
* @param int $time
*
* @return bool
*/
final public function isOlderThan(int $time): bool {
return ($this->modifiedTime < $time);
}
/**
* Set the read rights of the original document using a IDocumentAccess.
*
* @see IDocumentAccess
*
* @since 15.0.0
*
* @param IDocumentAccess $access
*
* @return $this
*/
final public function setAccess(IDocumentAccess $access): IIndexDocument {
$this->access = $access;
return $this;
}
/**
* Get the IDocumentAccess related to the original document.
*
* @since 15.0.0
*
* @return IDocumentAccess
*/
final public function getAccess(): IDocumentAccess {
return $this->access;
}
/**
* Add a tag to the list.
*
* @since 15.0.0
*
* @param string $tag
*
* @return IIndexDocument
*/
final public function addTag(string $tag): IIndexDocument {
$this->tags[] = $tag;
return $this;
}
/**
* Set the list of tags assigned to the original document.
*
* @since 15.0.0
*
* @param array $tags
*
* @return IIndexDocument
*/
final public function setTags(array $tags): IIndexDocument {
$this->tags = $tags;
return $this;
}
/**
* Get the list of tags assigned to the original document.
*
* @since 15.0.0
*
* @return array
*/
final public function getTags(): array {
return $this->tags;
}
/**
* Add a meta tag to the list.
*
* @since 15.0.0
*
* @param string $tag
*
* @return IIndexDocument
*/
final public function addMetaTag(string $tag): IIndexDocument {
$this->metaTags[] = $tag;
return $this;
}
/**
* Set the list of meta tags assigned to the original document.
*
* @since 15.0.0
*
* @param array $tags
*
* @return IIndexDocument
*/
final public function setMetaTags(array $tags): IIndexDocument {
$this->metaTags = $tags;
return $this;
}
/**
* Get the list of meta tags assigned to the original document.
*
* @since 15.0.0
*
* @return array
*/
final public function getMetaTags(): array {
return $this->metaTags;
}
/**
* Add a sub tag to the list.
*
* @since 15.0.0
*
* @param string $sub
* @param string $tag
*
* @return IIndexDocument
*/
final public function addSubTag(string $sub, string $tag): IIndexDocument {
if (!array_key_exists($sub, $this->subTags)) {
$this->subTags[$sub] = [];
}
$this->subTags[$sub][] = $tag;
return $this;
}
/**
* Set the list of sub tags assigned to the original document.
*
* @since 15.0.0
*
* @param array $tags
*
* @return IIndexDocument
*/
final public function setSubTags(array $tags): IIndexDocument {
$this->subTags = $tags;
return $this;
}
/**
* Get the list of sub tags assigned to the original document.
* If $formatted is true, the result will be formatted in a one
* dimensional array.
*
* @since 15.0.0
*
* @param bool $formatted
*
* @return array
*/
final public function getSubTags(bool $formatted = false): array {
if ($formatted === false) {
return $this->subTags;
}
$subTags = [];
$ak = array_keys($this->subTags);
foreach ($ak as $source) {
$tags = $this->subTags[$source];
foreach ($tags as $tag) {
$subTags[] = $source . '_' . $tag;
}
}
return $subTags;
}
/**
* Set the source of the original document.
*
* @since 15.0.0
*
* @param string $source
*
* @return IIndexDocument
*/
final public function setSource(string $source): IIndexDocument {
$this->source = $source;
return $this;
}
/**
* Get the source of the original document.
*
* @since 15.0.0
*
* @return string
*/
final public function getSource(): string {
return $this->source;
}
/**
* Set the title of the original document.
*
* @since 15.0.0
*
* @param string $title
*
* @return IIndexDocument
*/
final public function setTitle(string $title): IIndexDocument {
$this->title = $title;
return $this;
}
/**
* Get the title of the original document.
*
* @since 15.0.0
*
* @return string
*/
final public function getTitle(): string {
return $this->title;
}
/**
* Set the content of the document.
* $encoded can be NOT_ENCODED or ENCODED_BASE64 if the content is raw or
* encoded in base64.
*
* @since 15.0.0
*
* @param string $content
* @param int $encoded
*
* @return IIndexDocument
*/
final public function setContent(string $content, int $encoded = 0): IIndexDocument {
$this->content = $content;
$this->contentEncoded = $encoded;
return $this;
}
/**
* Get the content of the original document.
*
* @since 15.0.0
*
* @return string
*/
final public function getContent(): string {
return $this->content;
}
/**
* Returns the type of the encoding on the content.
*
* @since 15.0.0
*
* @return int
*/
final public function isContentEncoded(): int {
return $this->contentEncoded;
}
/**
* Return the size of the content.
*
* @since 15.0.0
*
* @return int
*/
final public function getContentSize(): int {
return strlen($this->getContent());
}
/**
* Generate an hash, based on the content of the original document.
*
* @since 15.0.0
*
* @return IIndexDocument
*/
final public function initHash(): IIndexDocument {
if ($this->getContent() === '' || is_null($this->getContent())) {
return $this;
}
$this->hash = hash("md5", $this->getContent());
return $this;
}
/**
* Set the hash of the original document.
*
* @since 15.0.0
*
* @param string $hash
*
* @return IIndexDocument
*/
final public function setHash(string $hash): IIndexDocument {
$this->hash = $hash;
return $this;
}
/**
* Get the hash of the original document.
*
* @since 15.0.0
*
* @return string
*/
final public function getHash(): string {
return $this->hash;
}
/**
* Add a part, identified by a string, and its content.
*
* It is strongly advised to use alphanumerical chars with no space in the
* $part string.
*
* @since 15.0.0
*
* @param string $part
* @param string $content
*
* @return IIndexDocument
*/
final public function addPart(string $part, string $content): IIndexDocument {
$this->parts[$part] = $content;
return $this;
}
/**
* Set all parts and their content.
*
* @since 15.0.0
*
* @param array $parts
*
* @return IIndexDocument
*/
final public function setParts(array $parts): IIndexDocument {
$this->parts = $parts;
return $this;
}
/**
* Get all parts of the IIndexDocument.
*
* @since 15.0.0
*
* @return array
*/
final public function getParts(): array {
return $this->parts;
}
/**
* Add a link, usable by the frontend.
*
* @since 15.0.0
*
* @param string $link
*
* @return IIndexDocument
*/
final public function setLink(string $link): IIndexDocument {
$this->link = $link;
return $this;
}
/**
* Get the link.
*
* @since 15.0.0
*
* @return string
*/
final public function getLink(): string {
return $this->link;
}
/**
* Set more information that couldn't be set using other method.
*
* @since 15.0.0
*
* @param array $more
*
* @return IIndexDocument
*/
final public function setMore(array $more): IIndexDocument {
$this->more = $more;
return $this;
}
/**
* Get more information.
*
* @since 15.0.0
*
* @return array
*/
final public function getMore(): array {
return $this->more;
}
/**
* Add some excerpt of the content of the original document, usually based
* on the search request.
*
* @since 16.0.0
*
* @param string $source
* @param string $excerpt
*
* @return IIndexDocument
*/
final public function addExcerpt(string $source, string $excerpt): IIndexDocument {
$this->excerpts[] =
[
'source' => $source,
'excerpt' => $this->cleanExcerpt($excerpt)
];
return $this;
}
/**
* Set all excerpts of the content of the original document.
*
* @since 16.0.0
*
* @param array $excerpts
*
* @return IIndexDocument
*/
final public function setExcerpts(array $excerpts): IIndexDocument {
$new = [];
foreach ($excerpts as $entry) {
$new[] = [
'source' => $entry['source'],
'excerpt' => $this->cleanExcerpt($entry['excerpt'])
];
}
$this->excerpts = $new;
return $this;
}
/**
* Get all excerpts of the content of the original document.
*
* @since 15.0.0
*
* @return array
*/
final public function getExcerpts(): array {
return $this->excerpts;
}
/**
* Clean excerpt.
*
* @since 16.0.0
*
* @param string $excerpt
* @return string
*/
final private function cleanExcerpt(string $excerpt): string {
$excerpt = str_replace("\\n", ' ', $excerpt);
$excerpt = str_replace("\\r", ' ', $excerpt);
$excerpt = str_replace("\\t", ' ', $excerpt);
$excerpt = str_replace("\n", ' ', $excerpt);
$excerpt = str_replace("\r", ' ', $excerpt);
$excerpt = str_replace("\t", ' ', $excerpt);
return $excerpt;
}
/**
* Set the score to the result assigned to this document during a search
* request.
*
* @since 15.0.0
*
* @param string $score
*
* @return IIndexDocument
*/
final public function setScore(string $score): IIndexDocument {
$this->score = $score;
return $this;
}
/**
* Get the score.
*
* @since 15.0.0
*
* @return string
*/
final public function getScore(): string {
return $this->score;
}
/**
* Set some information about the original document that will be available
* to the front-end when displaying search result. (as string)
* Because this information will not be indexed, this method can also be
* used to manage some data while filling the IIndexDocument before its
* indexing.
*
* @since 15.0.0
*
* @param string $info
* @param string $value
*
* @return IIndexDocument
*/
final public function setInfo(string $info, string $value): IIndexDocument {
$this->info[$info] = $value;
return $this;
}
/**
* Get an information about a document. (string)
*
* @since 15.0.0
*
* @param string $info
* @param string $default
*
* @return string
*/
final public function getInfo(string $info, string $default = ''): string {
if (!key_exists($info, $this->info)) {
return $default;
}
return $this->info[$info];
}
/**
* Set some information about the original document that will be available
* to the front-end when displaying search result. (as array)
* Because this information will not be indexed, this method can also be
* used to manage some data while filling the IIndexDocument before its
* indexing.
*
* @since 15.0.0
*
* @param string $info
* @param array $value
*
* @return IIndexDocument
*/
final public function setInfoArray(string $info, array $value): IIndexDocument {
$this->info[$info] = $value;
return $this;
}
/**
* Get an information about a document. (array)
*
* @since 15.0.0
*
* @param string $info
* @param array $default
*
* @return array
*/
final public function getInfoArray(string $info, array $default = []): array {
if (!key_exists($info, $this->info)) {
return $default;
}
return $this->info[$info];
}
/**
* Set some information about the original document that will be available
* to the front-end when displaying search result. (as int)
* Because this information will not be indexed, this method can also be
* used to manage some data while filling the IIndexDocument before its
* indexing.
*
* @since 15.0.0
*
* @param string $info
* @param int $value
*
* @return IIndexDocument
*/
final public function setInfoInt(string $info, int $value): IIndexDocument {
$this->info[$info] = $value;
return $this;
}
/**
* Get an information about a document. (int)
*
* @since 15.0.0
*
* @param string $info
* @param int $default
*
* @return int
*/
final public function getInfoInt(string $info, int $default = 0): int {
if (!key_exists($info, $this->info)) {
return $default;
}
return $this->info[$info];
}
/**
* Set some information about the original document that will be available
* to the front-end when displaying search result. (as bool)
* Because this information will not be indexed, this method can also be
* used to manage some data while filling the IIndexDocument before its
* indexing.
*
* @since 15.0.0
*
* @param string $info
* @param bool $value
*
* @return IIndexDocument
*/
final public function setInfoBool(string $info, bool $value): IIndexDocument {
$this->info[$info] = $value;
return $this;
}
/**
* Get an information about a document. (bool)
*
* @since 15.0.0
*
* @param string $info
* @param bool $default
*
* @return bool
*/
final public function getInfoBool(string $info, bool $default = false): bool {
if (!key_exists($info, $this->info)) {
return $default;
}
return $this->info[$info];
}
/**
* Get all info.
*
* @since 15.0.0
*
* @return array
*/
final public function getInfoAll(): array {
$info = [];
foreach ($this->info as $k => $v) {
if (substr($k, 0, 1) === '_') {
continue;
}
$info[$k] = $v;
}
return $info;
}
/**
* @since 15.0.0
*
* On some version of PHP, it is better to force destruct the object.
* And during the index, the number of generated IIndexDocument can be
* _huge_.
*/
public function __destruct() {
unset($this->id);
unset($this->providerId);
unset($this->access);
unset($this->modifiedTime);
unset($this->title);
unset($this->content);
unset($this->hash);
unset($this->link);
unset($this->source);
unset($this->tags);
unset($this->metaTags);
unset($this->subTags);
unset($this->more);
unset($this->excerpts);
unset($this->score);
unset($this->info);
unset($this->contentEncoded);
}
/**
* @since 15.0.0
*
* @return array
*/
public function jsonSerialize() {
return [
'id' => $this->getId(),
'providerId' => $this->getProviderId(),
'access' => $this->access,
'modifiedTime' => $this->getModifiedTime(),
'title' => $this->getTitle(),
'link' => $this->getLink(),
'index' => $this->index,
'source' => $this->getSource(),
'info' => $this->getInfoAll(),
'hash' => $this->getHash(),
'contentSize' => $this->getContentSize(),
'tags' => $this->getTags(),
'metatags' => $this->getMetaTags(),
'subtags' => $this->getSubTags(),
'more' => $this->getMore(),
'excerpts' => $this->getExcerpts(),
'score' => $this->getScore()
];
}
}
|