-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathNodeIndexer.php
More file actions
157 lines (136 loc) · 5.87 KB
/
NodeIndexer.php
File metadata and controls
157 lines (136 loc) · 5.87 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
<?php
declare(strict_types=1);
namespace Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\Indexer;
/*
* This file is part of the Flowpack.ElasticSearch.ContentRepositoryQueueIndexer package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Flowpack\ElasticSearch\ContentRepositoryAdaptor;
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\Command\NodeIndexQueueCommandController;
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\IndexingJob;
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\RemovalJob;
use Flowpack\JobQueue\Common\Job\JobManager;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\PersistenceManagerInterface;
/**
* NodeIndexer for use in batch jobs
*
* @Flow\Scope("singleton")
*/
class NodeIndexer extends ContentRepositoryAdaptor\Indexer\NodeIndexer
{
/**
* @var JobManager
* @Flow\Inject
*/
protected $jobManager;
/**
* @var PersistenceManagerInterface
* @Flow\Inject
*/
protected $persistenceManager;
/**
* @var bool
* @Flow\InjectConfiguration(path="enableLiveAsyncIndexing")
*/
protected $enableLiveAsyncIndexing;
/**
* @param NodeInterface $node
* @param string|null $targetWorkspaceName In case indexing is triggered during publishing, a target workspace name will be passed in
* @throws ContentRepositoryAdaptor\Exception
*/
public function indexNode(NodeInterface $node, $targetWorkspaceName = null): void
{
if( $node->isRemoved() ){
$this->removeNode($node, $targetWorkspaceName);
return;
}
// During bulk builds (postfix is set), always index directly to avoid async queuing
if ($this->enableLiveAsyncIndexing !== true || $this->indexNamePostfix !== '') {
parent::indexNode($node, $targetWorkspaceName);
return;
}
if ($this->settings['indexAllWorkspaces'] === false) {
if ($targetWorkspaceName !== null && $targetWorkspaceName !== 'live') {
return;
}
if ($targetWorkspaceName === null && $node->getContext()->getWorkspaceName() !== 'live') {
return;
}
}
$indexingJob = new IndexingJob($this->indexNamePostfix, $targetWorkspaceName, $this->nodeAsArray($node));
$this->jobManager->queue(NodeIndexQueueCommandController::LIVE_QUEUE_NAME, $indexingJob);
}
/**
* @param NodeInterface $node
* @param string|null $targetWorkspaceName In case indexing is triggered during publishing, a target workspace name will be passed in
* @throws ContentRepositoryAdaptor\Exception
* @throws \Flowpack\ElasticSearch\Exception
* @throws \Neos\Flow\Persistence\Exception\IllegalObjectTypeException
* @throws \Neos\Utility\Exception\FilesException
*/
public function removeNode(NodeInterface $node, string $targetWorkspaceName = null): void
{
// During bulk builds (postfix is set), always remove directly to avoid async queuing
if ($this->enableLiveAsyncIndexing !== true || $this->indexNamePostfix !== '') {
parent::removeNode($node, $targetWorkspaceName);
return;
}
if ($this->settings['indexAllWorkspaces'] === false) {
if ($targetWorkspaceName !== null && $targetWorkspaceName !== 'live') {
return;
}
if ($targetWorkspaceName === null && $node->getContext()->getWorkspaceName() !== 'live') {
return;
}
}
$dimensionCombinations = $this->dimensionService->getDimensionCombinationsForIndexing($node);
$targetWorkspaceName = $targetWorkspaceName ?? $node->getWorkspace()->getName();
if (array_filter($dimensionCombinations) === []) {
$removalJob = new RemovalJob($this->indexNamePostfix, $targetWorkspaceName, $this->nodeAsArray($node));
$this->jobManager->queue(NodeIndexQueueCommandController::LIVE_QUEUE_NAME, $removalJob);
} else {
foreach ($dimensionCombinations as $combination) {
$nodeFromContext = $this->createContentContext($targetWorkspaceName, $combination)->getNodeByIdentifier($node->getIdentifier());
if ($nodeFromContext instanceof NodeInterface && !$nodeFromContext->isRemoved()) {
continue;
}
$fakeNodeArray = [
'persistenceObjectIdentifier' => 'fake',
'workspace' => $node->getWorkspace()->getName(),
'path' => $node->getPath(),
'identifier' => $node->getIdentifier(),
'nodeType' => $node->getNodeType()->getName(),
'dimensions' => $combination
];
$removalJob = new RemovalJob($this->indexNamePostfix, $targetWorkspaceName, [$fakeNodeArray]);
$this->jobManager->queue(NodeIndexQueueCommandController::LIVE_QUEUE_NAME, $removalJob);
}
}
}
/**
* Returns an array of data from the node for use as job payload.
*
* @param NodeInterface $node
* @return array
*/
protected function nodeAsArray(NodeInterface $node): array
{
return [
[
'persistenceObjectIdentifier' => $this->persistenceManager->getIdentifierByObject($node->getNodeData()),
'identifier' => $node->getIdentifier(),
'dimensions' => $node->getContext()->getDimensions(),
'workspace' => $node->getWorkspace()->getName(),
'nodeType' => $node->getNodeType()->getName(),
'path' => $node->getPath()
]
];
}
}