-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathNodeIndexCommandController.php
More file actions
176 lines (157 loc) · 5.55 KB
/
NodeIndexCommandController.php
File metadata and controls
176 lines (157 loc) · 5.55 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
<?php
declare(strict_types=1);
namespace Flowpack\SimpleSearch\ContentRepositoryAdaptor\Command;
use Flowpack\SimpleSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
use Flowpack\SimpleSearch\Domain\Service\IndexInterface;
use Flowpack\SimpleSearch\Exception;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Domain\Repository\NodeDataRepository;
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository;
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
use Neos\ContentRepository\Exception\NodeException;
use Neos\ContentRepository\Search\Exception\IndexingException;
use Neos\Eel\Exception as EelException;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Neos\Domain\Service\ContentDimensionPresetSourceInterface;
/**
* Provides CLI features for index handling
*
* @Flow\Scope("singleton")
*/
class NodeIndexCommandController extends CommandController
{
/**
* @Flow\Inject
* @var NodeIndexer
*/
protected $nodeIndexer;
/**
* @Flow\Inject
* @var IndexInterface
*/
protected $indexClient;
/**
* @Flow\Inject
* @var WorkspaceRepository
*/
protected $workspaceRepository;
/**
* @Flow\Inject
* @var NodeDataRepository
*/
protected $nodeDataRepository;
/**
* @Flow\Inject
* @var ContextFactoryInterface
*/
protected $contextFactory;
/**
* @Flow\Inject
* @var ContentDimensionPresetSourceInterface
*/
protected $contentDimensionPresetSource;
/**
* @var integer
*/
protected $indexedNodes;
/**
* @var array
* @Flow\InjectConfiguration(package="Neos.ContentRepository.Search")
*/
protected $settings;
/**
* Index all nodes.
*
* This command (re-)indexes all nodes contained in the content repository and sets the schema beforehand.
*
* @throws Exception
*/
public function buildCommand(string $workspace = null): void
{
if ($workspace === null && $this->settings['indexAllWorkspaces'] === false) {
$workspace = 'live';
}
$this->indexedNodes = 0;
if ($workspace === null) {
foreach ($this->workspaceRepository->findAll() as $workspaceInstance) {
$this->indexWorkspace($workspaceInstance->getName());
}
} else {
$this->indexWorkspace($workspace);
}
$this->outputLine('Finished indexing.');
}
/**
* @throws Exception
*/
protected function indexWorkspace(string $workspaceName): void
{
$dimensionCombinations = $this->nodeIndexer->calculateDimensionCombinations();
if ($dimensionCombinations !== []) {
foreach ($dimensionCombinations as $combination) {
$context = $this->contextFactory->create(['workspaceName' => $workspaceName, 'dimensions' => $combination]);
$rootNode = $context->getRootNode();
$this->traverseNodes($rootNode);
$rootNode->getContext()->getFirstLevelNodeCache()->flush();
$this->outputLine('Workspace "' . $workspaceName . '" and dimensions "' . json_encode($combination) . '" done. (Indexed ' . $this->indexedNodes . ' nodes)');
$this->indexedNodes = 0;
}
} else {
$context = $this->contextFactory->create(['workspaceName' => $workspaceName]);
$rootNode = $context->getRootNode();
$this->traverseNodes($rootNode);
$this->outputLine('Workspace "' . $workspaceName . '" without dimensions done. (Indexed ' . $this->indexedNodes . ' nodes)');
$this->indexedNodes = 0;
}
}
/**
* @throws Exception
*/
protected function traverseNodes(NodeInterface $currentNode): void
{
try {
$this->nodeIndexer->indexNode($currentNode, null, false);
} catch (NodeException|IndexingException|EelException $exception) {
throw new Exception(sprintf('Error during indexing of node %s (%s)', $currentNode->findNodePath(), (string) $currentNode->getNodeAggregateIdentifier()), 1579170291, $exception);
}
$this->indexedNodes++;
foreach ($currentNode->findChildNodes() as $childNode) {
$this->traverseNodes($childNode);
}
}
/**
* Clears the node index from all data.
*
* @param boolean $confirmation Should be set to true for something to actually happen.
*/
public function flushCommand(bool $confirmation = false): void
{
if ($confirmation) {
$this->indexClient->flush();
$this->outputLine('The node index was flushed.');
} else {
$this->outputLine('The node index was NOT flushed, confirmation option was missing.');
}
}
/**
* Optimize the search index. Depends on the underlaying technology what will happen.
* For sqlite the VACUUM command is sent to rebuild the full database file.
*/
public function optimizeCommand(): void
{
$this->outputLine('Starting optimization, do not interrupt or your index may be corrupted...');
$this->indexClient->optimize();
$this->outputLine('Optimization finished.');
}
/**
* Utility to check the content of the index.
*
* @param string $queryString raw SQL to send to the index
*/
public function findCommand(string $queryString): void
{
$result = $this->indexClient->executeStatement($queryString, []);
$this->output->outputTable($result);
}
}