|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: gordon |
|
6
|
|
|
* Date: 25/3/2561 |
|
7
|
|
|
* Time: 17:01 น. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Suilven\FreeTextSearch\Helper; |
|
11
|
|
|
|
|
12
|
|
|
use SilverStripe\ORM\DataObject; |
|
13
|
|
|
use Suilven\FreeTextSearch\Factory\IndexablePayloadMutatorFactory; |
|
14
|
|
|
use Suilven\FreeTextSearch\Indexes; |
|
15
|
|
|
|
|
16
|
|
|
class IndexingHelper |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Get a list of index names associated with the data object |
|
20
|
|
|
* |
|
21
|
|
|
* @return array<string> names of indexes associated with the DataObject in question |
|
22
|
|
|
*/ |
|
23
|
|
|
public function getIndexes(DataObject $dataObject): array |
|
24
|
|
|
{ |
|
25
|
|
|
$indexes = new Indexes(); |
|
26
|
|
|
$indices = $indexes->getIndexes(); |
|
27
|
|
|
|
|
28
|
|
|
$result = []; |
|
29
|
|
|
|
|
30
|
|
|
/** @var \Suilven\FreeTextSearch\Index $indice */ |
|
31
|
|
|
foreach ($indices as $indice) { |
|
32
|
|
|
$clazz = $indice->getClass(); |
|
33
|
|
|
$classes = $dataObject->getClassAncestry(); |
|
34
|
|
|
|
|
35
|
|
|
foreach ($classes as $indiceClass) { |
|
36
|
|
|
if ($indiceClass !== $clazz) { |
|
37
|
|
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$result[] = $indice->getName(); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $result; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get the indexable fields for a given dataobject as an array. This also includes the stored fields |
|
50
|
|
|
* |
|
51
|
|
|
* @param \SilverStripe\ORM\DataObject $dataObject get the indexable fields for the provided data object |
|
52
|
|
|
* @return array<string, array<string,string|int|float|bool>> |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getFieldsToIndex(DataObject $dataObject): array |
|
55
|
|
|
{ |
|
56
|
|
|
$indexes = new Indexes(); |
|
57
|
|
|
$indices = $indexes->getIndexes(); |
|
58
|
|
|
|
|
59
|
|
|
$payload = []; |
|
60
|
|
|
|
|
61
|
|
|
/** @var \Suilven\FreeTextSearch\Index $indice */ |
|
62
|
|
|
foreach ($indices as $indice) { |
|
63
|
|
|
$indicePayload = []; |
|
64
|
|
|
|
|
65
|
|
|
$clazz = $indice->getClass(); |
|
66
|
|
|
$classes = $dataObject->getClassAncestry(); |
|
67
|
|
|
|
|
68
|
|
|
foreach ($classes as $indiceClass) { |
|
69
|
|
|
if ($indiceClass !== $clazz) { |
|
70
|
|
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$fields = \array_merge($indice->getFields(), $indice->getStoredFields()); |
|
74
|
|
|
foreach ($fields as $field) { |
|
75
|
|
|
// @phpstan-ignore-next-line |
|
76
|
|
|
$value = $dataObject->$field; |
|
77
|
|
|
$indicePayload[$field] = $value; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
$payload[$indice->getName()] = $indicePayload; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$factory = new IndexablePayloadMutatorFactory(); |
|
84
|
|
|
$mutator = $factory->getIndexablePayloadMutator(); |
|
85
|
|
|
$mutator->mutatePayload($dataObject, $payload); |
|
86
|
|
|
|
|
87
|
|
|
return $payload; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** @return array<string> */ |
|
92
|
|
|
public function getFields(string $indexName): array |
|
93
|
|
|
{ |
|
94
|
|
|
$indexes = new Indexes(); |
|
95
|
|
|
$index = $indexes->getIndex($indexName); |
|
96
|
|
|
|
|
97
|
|
|
$fields = []; |
|
98
|
|
|
|
|
99
|
|
|
foreach ($index->getFields() as $field) { |
|
100
|
|
|
$fields[] = $field; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
foreach ($index->getTokens() as $token) { |
|
104
|
|
|
$fields[] = $token; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
foreach ($index->getStoredFields() as $storedField) { |
|
108
|
|
|
$fields[] = $storedField; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if (!\in_array('Link', $fields, true)) { |
|
112
|
|
|
$fields[] = 'Link'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $fields; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|