Skip to content

Commit 6905f58

Browse files
authored
Merge pull request #119 from dfeyer/refactor-cli-controller
TASK: Extract logic to index workspace from the controller to a trait
2 parents e4fae80 + f512fbc commit 6905f58

2 files changed

Lines changed: 104 additions & 99 deletions

File tree

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Command/NodeIndexCommandController.php

Lines changed: 12 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
* The TYPO3 project - inspiring people to share! *
1212
* */
1313

14+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service\IndexWorkspaceTrait;
1415
use TYPO3\Flow\Annotations as Flow;
1516
use TYPO3\Flow\Cli\CommandController;
1617
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Mapping\NodeTypeMappingBuilder;
17-
use TYPO3\TYPO3CR\Domain\Service\ContentDimensionCombinator;
18-
use TYPO3\TYPO3CR\Search\Indexer\NodeIndexingManager;
1918

2019
/**
2120
* Provides CLI features for index handling
@@ -24,18 +23,14 @@
2423
*/
2524
class NodeIndexCommandController extends CommandController
2625
{
26+
use IndexWorkspaceTrait;
27+
2728
/**
2829
* @Flow\Inject
2930
* @var \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer
3031
*/
3132
protected $nodeIndexer;
3233

33-
/**
34-
* @Flow\Inject
35-
* @var NodeIndexingManager
36-
*/
37-
protected $nodeIndexingManager;
38-
3934
/**
4035
* @Flow\Inject
4136
* @var \TYPO3\TYPO3CR\Domain\Repository\WorkspaceRepository
@@ -54,12 +49,6 @@ class NodeIndexCommandController extends CommandController
5449
*/
5550
protected $nodeFactory;
5651

57-
/**
58-
* @Flow\Inject
59-
* @var \TYPO3\TYPO3CR\Domain\Service\ContextFactory
60-
*/
61-
protected $contextFactory;
62-
6352
/**
6453
* @Flow\Inject
6554
* @var \TYPO3\Neos\Domain\Service\ContentDimensionPresetSourceInterface
@@ -72,21 +61,6 @@ class NodeIndexCommandController extends CommandController
7261
*/
7362
protected $nodeTypeMappingBuilder;
7463

75-
/**
76-
* @var integer
77-
*/
78-
protected $indexedNodes;
79-
80-
/**
81-
* @var integer
82-
*/
83-
protected $countedIndexedNodes;
84-
85-
/**
86-
* @var integer
87-
*/
88-
protected $limit;
89-
9064
/**
9165
* @Flow\Inject
9266
* @var \Flowpack\ElasticSearch\ContentRepositoryAdaptor\LoggerInterface
@@ -99,12 +73,6 @@ class NodeIndexCommandController extends CommandController
9973
*/
10074
protected $configurationManager;
10175

102-
/**
103-
* @Flow\Inject
104-
* @var ContentDimensionCombinator
105-
*/
106-
protected $contentDimensionCombinator;
107-
10876
/**
10977
* @var array
11078
*/
@@ -192,23 +160,24 @@ public function buildCommand($limit = null, $update = false, $workspace = null,
192160
$this->logger->log(sprintf('Indexing %snodes ... ', ($limit !== null ? 'the first ' . $limit . ' ' : '')), LOG_INFO);
193161

194162
$count = 0;
195-
$this->limit = $limit;
196-
$this->indexedNodes = 0;
197-
$this->countedIndexedNodes = 0;
198163

199164
if ($workspace === null && $this->settings['indexAllWorkspaces'] === false) {
200165
$workspace = 'live';
201166
}
202167

168+
$callback = function ($workspaceName, $indexedNodes, $dimensions) {
169+
if ($dimensions === array()) {
170+
$this->outputLine('Workspace "' . $workspaceName . '" without dimensions done. (Indexed ' . $indexedNodes . ' nodes)');
171+
} else {
172+
$this->outputLine('Workspace "' . $workspaceName . '" and dimensions "' . json_encode($dimensions) . '" done. (Indexed ' . $indexedNodes . ' nodes)');
173+
}
174+
};
203175
if ($workspace === null) {
204176
foreach ($this->workspaceRepository->findAll() as $workspace) {
205-
$this->indexWorkspace($workspace->getName());
206-
207-
$count = $count + $this->countedIndexedNodes;
177+
$count += $this->indexWorkspace($workspace->getName(), $limit, $callback);
208178
}
209179
} else {
210-
$this->indexWorkspace($workspace);
211-
$count = $count + $this->countedIndexedNodes;
180+
$count += $this->indexWorkspace($workspace, $limit, $callback);
212181
}
213182

214183
$this->nodeIndexingManager->flushQueues();
@@ -243,60 +212,4 @@ public function cleanupCommand()
243212
$this->logger->log(sprintf('Nothing removed. ElasticSearch responded with status %s, saying "%s"', $response->status, $response->error));
244213
}
245214
}
246-
247-
/**
248-
* @param string $workspaceName
249-
* @return void
250-
*/
251-
protected function indexWorkspace($workspaceName)
252-
{
253-
$combinations = $this->contentDimensionCombinator->getAllAllowedCombinations();
254-
if ($combinations === array()) {
255-
$this->indexWorkspaceWithDimensions($workspaceName);
256-
} else {
257-
foreach ($combinations as $combination) {
258-
$this->indexWorkspaceWithDimensions($workspaceName, $combination);
259-
}
260-
}
261-
}
262-
263-
/**
264-
* @param string $workspaceName
265-
* @param array $dimensions
266-
* @return void
267-
*/
268-
protected function indexWorkspaceWithDimensions($workspaceName, array $dimensions = array())
269-
{
270-
$context = $this->contextFactory->create(array('workspaceName' => $workspaceName, 'dimensions' => $dimensions));
271-
$rootNode = $context->getRootNode();
272-
273-
$this->traverseNodes($rootNode);
274-
275-
if ($dimensions === array()) {
276-
$this->outputLine('Workspace "' . $workspaceName . '" without dimensions done. (Indexed ' . $this->indexedNodes . ' nodes)');
277-
} else {
278-
$this->outputLine('Workspace "' . $workspaceName . '" and dimensions "' . json_encode($dimensions) . '" done. (Indexed ' . $this->indexedNodes . ' nodes)');
279-
}
280-
281-
$this->countedIndexedNodes = $this->countedIndexedNodes + $this->indexedNodes;
282-
$this->indexedNodes = 0;
283-
}
284-
285-
/**
286-
* @param \TYPO3\TYPO3CR\Domain\Model\NodeInterface $currentNode
287-
* @return void
288-
*/
289-
protected function traverseNodes(\TYPO3\TYPO3CR\Domain\Model\NodeInterface $currentNode)
290-
{
291-
if ($this->limit !== null && $this->indexedNodes > $this->limit) {
292-
return;
293-
}
294-
295-
$this->nodeIndexingManager->indexNode($currentNode);
296-
$this->indexedNodes++;
297-
298-
foreach ($currentNode->getChildNodes() as $childNode) {
299-
$this->traverseNodes($childNode);
300-
}
301-
}
302215
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service;
3+
4+
/* *
5+
* This script belongs to the TYPO3 Flow package "Flowpack.ElasticSearch.ContentRepositoryAdaptor". *
6+
* *
7+
* It is free software; you can redistribute it and/or modify it under *
8+
* the terms of the GNU Lesser General Public License, either version 3 *
9+
* of the License, or (at your option) any later version. *
10+
* *
11+
* The TYPO3 project - inspiring people to share! *
12+
* */
13+
14+
use TYPO3\Flow\Annotations as Flow;
15+
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
16+
17+
/**
18+
* Index Workspace Trait
19+
*/
20+
trait IndexWorkspaceTrait
21+
{
22+
/**
23+
* @Flow\Inject
24+
* @var \TYPO3\TYPO3CR\Domain\Service\ContextFactory
25+
*/
26+
protected $contextFactory;
27+
28+
/**
29+
* @Flow\Inject
30+
* @var \TYPO3\TYPO3CR\Domain\Service\ContentDimensionCombinator
31+
*/
32+
protected $contentDimensionCombinator;
33+
34+
/**
35+
* @Flow\Inject
36+
* @var \TYPO3\TYPO3CR\Search\Indexer\NodeIndexingManager
37+
*/
38+
protected $nodeIndexingManager;
39+
40+
/**
41+
* @param string $workspaceName
42+
* @param integer $limit
43+
* @param callable $callback
44+
* @return integer
45+
*/
46+
protected function indexWorkspace($workspaceName, $limit = null, callable $callback = null)
47+
{
48+
$count = 0;
49+
$combinations = $this->contentDimensionCombinator->getAllAllowedCombinations();
50+
if ($combinations === []) {
51+
$count += $this->indexWorkspaceWithDimensions($workspaceName, [], $limit, $callback);
52+
} else {
53+
foreach ($combinations as $combination) {
54+
$count += $this->indexWorkspaceWithDimensions($workspaceName, $combination, $limit, $callback);
55+
}
56+
}
57+
return $count;
58+
}
59+
60+
/**
61+
* @param string $workspaceName
62+
* @param array $dimensions
63+
* @param integer $limit
64+
* @param callable $callback
65+
* @return integer
66+
*/
67+
protected function indexWorkspaceWithDimensions($workspaceName, array $dimensions = [], $limit = null, callable $callback = null)
68+
{
69+
$context = $this->contextFactory->create(['workspaceName' => $workspaceName, 'dimensions' => $dimensions]);
70+
$rootNode = $context->getRootNode();
71+
$indexedNodes = 0;
72+
73+
$traverseNodes = function (NodeInterface $currentNode, &$indexedNodes) use ($limit, &$indexedNodes, &$traverseNodes) {
74+
if ($limit !== null && $indexedNodes > $limit) {
75+
return;
76+
}
77+
$this->nodeIndexingManager->indexNode($currentNode);
78+
$indexedNodes++;
79+
array_map(function(NodeInterface $childNode) use ($traverseNodes, &$indexedNodes) {
80+
$traverseNodes($childNode, $indexedNodes);
81+
}, $currentNode->getChildNodes());
82+
};
83+
84+
$traverseNodes($rootNode, $indexedNodes);
85+
86+
if ($callback !== null) {
87+
$callback($workspaceName, $indexedNodes, $dimensions);
88+
}
89+
90+
return $indexedNodes;
91+
}
92+
}

0 commit comments

Comments
 (0)