-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathNodeIndexer.php
More file actions
313 lines (268 loc) · 10.6 KB
/
NodeIndexer.php
File metadata and controls
313 lines (268 loc) · 10.6 KB
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
<?php
declare(strict_types=1);
namespace Flowpack\SimpleSearch\ContentRepositoryAdaptor\Indexer;
use Flowpack\SimpleSearch\Domain\Service\IndexInterface;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository;
use Neos\ContentRepository\Domain\Service\ContentDimensionPresetSourceInterface;
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
use Neos\ContentRepository\Domain\Service\NodeTypeManager;
use Neos\ContentRepository\Exception\NodeException;
use Neos\ContentRepository\Search\Exception\IndexingException;
use Neos\ContentRepository\Search\Indexer\AbstractNodeIndexer;
use Neos\ContentRepository\Search\Search\QueryBuilderInterface;
use Neos\Eel\Exception;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Configuration\Exception\InvalidConfigurationTypeException;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Flow\Security\Context;
use Symfony\Component\Yaml\Yaml;
/**
* Indexer for Content Repository Nodes.
*
* @Flow\Scope("singleton")
*/
class NodeIndexer extends AbstractNodeIndexer
{
/**
* @Flow\Inject
* @var IndexInterface
*/
protected $indexClient;
/**
* @Flow\Inject
* @var QueryBuilderInterface
*/
protected $queryBuilder;
/**
* @Flow\Inject
* @var PersistenceManagerInterface
*/
protected $persistenceManager;
/**
* @Flow\Inject
* @var NodeTypeManager
*/
protected $nodeTypeManager;
/**
* @Flow\Inject
* @var ContentDimensionPresetSourceInterface
*/
protected $contentDimensionPresetSource;
/**
* @Flow\Inject
* @var WorkspaceRepository
*/
protected $workspaceRepository;
/**
* @Flow\Inject
* @var ContextFactoryInterface
*/
protected $contextFactory;
/**
* @var array
* @Flow\InjectConfiguration(package="Neos.ContentRepository.Search")
*/
protected $settings;
/**
* @Flow\Inject
* @var Context
*/
protected $securityContext;
/**
* @var array
*/
protected $fulltextRootNodeTypes = [];
/**
* @var array
*/
protected $indexedNodeData = [];
/**
* Called by the Flow object framework after creating the object and resolving all dependencies.
*
* @param integer $cause Creation cause
* @throws InvalidConfigurationTypeException
*/
public function initializeObject($cause): void
{
parent::initializeObject($cause);
foreach ($this->nodeTypeManager->getNodeTypes() as $nodeType) {
$searchSettingsForNodeType = $nodeType->getConfiguration('search');
if (is_array($searchSettingsForNodeType) && isset($searchSettingsForNodeType['fulltext']['isRoot']) && $searchSettingsForNodeType['fulltext']['isRoot'] === true) {
$this->fulltextRootNodeTypes[] = $nodeType->getName();
}
}
}
public function getIndexClient(): IndexInterface
{
return $this->indexClient;
}
/**
* index this node, and add it to the current bulk request.
*
* @param string $targetWorkspaceName
* @param boolean $indexVariants
* @throws NodeException|IndexingException|Exception
*/
public function indexNode(NodeInterface $node, $targetWorkspaceName = null, $indexVariants = true): void
{
if ($this->settings['indexAllWorkspaces'] === false) {
// we are only supposed to index the live workspace.
// We need to check the workspace at two occasions; checking the
// $targetWorkspaceName and the workspace name of the node's context as fallback
if ($targetWorkspaceName !== null && $targetWorkspaceName !== 'live') {
return;
}
if ($targetWorkspaceName === null && $node->getContext()->getWorkspaceName() !== 'live') {
return;
}
}
if ($indexVariants === true) {
$this->indexAllNodeVariants($node);
return;
}
$identifier = $this->generateUniqueNodeIdentifier($node);
if ($node->isRemoved()) {
$this->indexClient->removeData($identifier);
return;
}
$fulltextData = [];
if (isset($this->indexedNodeData[$identifier]) && ($properties = $this->indexClient->findOneByIdentifier($identifier)) !== false) {
unset($properties['__identifier__']);
$properties['__workspace'] .= ', #' . ($targetWorkspaceName ?? $node->getContext()->getWorkspaceName()) . '#';
if (array_key_exists('__dimensionshash', $properties)) {
$properties['__dimensionshash'] .= ', #' . md5(json_encode($node->getContext()->getDimensions(), 0, 512)) . '#';
} else {
$properties['__dimensionshash'] = '#' . md5(json_encode($node->getContext()->getDimensions(), 0, 512)) . '#';
}
$this->indexClient->insertOrUpdatePropertiesToIndex($properties, $identifier);
} else {
$nodePropertiesToBeStoredInIndex = $this->extractPropertiesAndFulltext($node, $fulltextData);
if (count($fulltextData) !== 0) {
$this->addFulltextToRoot($node, $fulltextData);
}
$nodePropertiesToBeStoredInIndex = $this->postProcess($nodePropertiesToBeStoredInIndex);
$this->indexClient->indexData($identifier, $nodePropertiesToBeStoredInIndex, $fulltextData);
$this->indexedNodeData[$identifier] = $identifier;
}
}
public function removeNode(NodeInterface $node): void
{
$identifier = $this->generateUniqueNodeIdentifier($node);
$this->indexClient->removeData($identifier);
}
public function flush(): void
{
$this->indexedNodeData = [];
}
/**
* @throws \Exception
*/
protected function indexAllNodeVariants(NodeInterface $node): void
{
$nodeIdentifier = (string) $node->getNodeAggregateIdentifier();
$allIndexedVariants = $this->indexClient->executeStatement(
$this->queryBuilder->getFindIdentifiersByNodeIdentifierQuery('identifier'),
[':identifier' => $nodeIdentifier]
);
foreach ($allIndexedVariants as $nodeVariant) {
$this->indexClient->removeData($nodeVariant['__identifier__']);
}
foreach ($this->workspaceRepository->findAll() as $workspace) {
$this->indexNodeInWorkspace($nodeIdentifier, $workspace->getName());
}
}
/**
* @throws \Exception
*/
protected function indexNodeInWorkspace(string $nodeIdentifier, string $workspaceName): void
{
$indexer = $this;
$this->securityContext->withoutAuthorizationChecks(static function () use ($indexer, $nodeIdentifier, $workspaceName) {
$dimensionCombinations = $indexer->calculateDimensionCombinations();
if ($dimensionCombinations !== []) {
foreach ($dimensionCombinations as $combination) {
$context = $indexer->contextFactory->create(['workspaceName' => $workspaceName, 'dimensions' => $combination]);
$node = $context->getNodeByIdentifier($nodeIdentifier);
if ($node !== null) {
$indexer->indexNode($node, null, false);
}
}
} else {
$context = $indexer->contextFactory->create(['workspaceName' => $workspaceName]);
$node = $context->getNodeByIdentifier($nodeIdentifier);
if ($node !== null) {
$indexer->indexNode($node, null, false);
}
}
});
}
protected function addFulltextToRoot(NodeInterface $node, array $fulltext): void
{
$fulltextRoot = $this->findFulltextRoot($node);
if ($fulltextRoot !== null) {
$identifier = $this->generateUniqueNodeIdentifier($fulltextRoot);
$this->indexClient->addToFulltext($fulltext, $identifier);
}
}
protected function findFulltextRoot(NodeInterface $node): ?NodeInterface
{
if (in_array($node->getNodeType()->getName(), $this->fulltextRootNodeTypes, true)) {
return null;
}
try {
$currentNode = $node->findParentNode();
while ($currentNode !== null) {
if (in_array($currentNode->getNodeType()->getName(), $this->fulltextRootNodeTypes, true)) {
return $currentNode;
}
$currentNode = $currentNode->findParentNode();
}
} catch (NodeException $exception) {
return null;
}
return null;
}
/**
* Generate identifier for index entry based on node identifier and context
*/
protected function generateUniqueNodeIdentifier(NodeInterface $node): string
{
return $this->persistenceManager->getIdentifierByObject($node->getNodeData());
}
protected function postProcess(array $nodePropertiesToBeStoredInIndex): array
{
foreach ($nodePropertiesToBeStoredInIndex as $propertyName => $propertyValue) {
if (is_array($propertyValue)) {
$nodePropertiesToBeStoredInIndex[$propertyName] = Yaml::dump($propertyValue);
}
}
return $nodePropertiesToBeStoredInIndex;
}
public function calculateDimensionCombinations(): array
{
$dimensionPresets = $this->contentDimensionPresetSource->getAllPresets();
$dimensionValueCountByDimension = [];
$possibleCombinationCount = 1;
$combinations = [];
foreach ($dimensionPresets as $dimensionName => $dimensionPreset) {
if (isset($dimensionPreset['presets']) && !empty($dimensionPreset['presets'])) {
$dimensionValueCountByDimension[$dimensionName] = count($dimensionPreset['presets']);
$possibleCombinationCount *= $dimensionValueCountByDimension[$dimensionName];
}
}
foreach ($dimensionPresets as $dimensionName => $dimensionPreset) {
for ($i = 0; $i < $possibleCombinationCount; $i++) {
if (!isset($combinations[$i]) || !is_array($combinations[$i])) {
$combinations[$i] = [];
}
$currentDimensionCurrentPreset = current($dimensionPresets[$dimensionName]['presets']);
$combinations[$i][$dimensionName] = $currentDimensionCurrentPreset['values'];
if (!next($dimensionPresets[$dimensionName]['presets'])) {
reset($dimensionPresets[$dimensionName]['presets']);
}
}
}
return $combinations;
}
}