1515use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Service \IndexWorkspaceTrait ;
1616use TYPO3 \Flow \Annotations as Flow ;
1717use TYPO3 \Flow \Cli \CommandController ;
18+ use TYPO3 \TYPO3CR \Domain \Model \NodeInterface ;
1819
1920/**
2021 * Provides CLI features for index handling
@@ -61,6 +62,11 @@ class NodeIndexCommandController extends CommandController
6162 */
6263 protected $ nodeTypeMappingBuilder ;
6364
65+ /**
66+ * @var integer
67+ */
68+ protected $ limit ;
69+
6470 /**
6571 * @Flow\Inject
6672 * @var \Flowpack\ElasticSearch\ContentRepositoryAdaptor\LoggerInterface
@@ -160,6 +166,7 @@ public function buildCommand($limit = null, $update = false, $workspace = null,
160166 $ this ->logger ->log (sprintf ('Indexing %snodes ... ' , ($ limit !== null ? 'the first ' . $ limit . ' ' : '' )), LOG_INFO );
161167
162168 $ count = 0 ;
169+ $ this ->limit = $ limit ;
163170
164171 if ($ workspace === null && $ this ->settings ['indexAllWorkspaces ' ] === false ) {
165172 $ workspace = 'live ' ;
@@ -174,10 +181,10 @@ public function buildCommand($limit = null, $update = false, $workspace = null,
174181 };
175182 if ($ workspace === null ) {
176183 foreach ($ this ->workspaceRepository ->findAll () as $ workspace ) {
177- $ count += $ this ->indexWorkspace ($ workspace ->getName (), $ limit , $ callback );
184+ $ count += $ this ->indexWorkspace ($ workspace ->getName ());
178185 }
179186 } else {
180- $ count + = $ this ->indexWorkspace ($ workspace, $ limit , $ callback );
187+ $ count = $ this ->indexWorkspace ($ workspace );
181188 }
182189
183190 $ this ->nodeIndexingManager ->flushQueues ();
@@ -199,17 +206,81 @@ public function buildCommand($limit = null, $update = false, $workspace = null,
199206 public function cleanupCommand ()
200207 {
201208 try {
202- $ indicesToBeRemoved = $ this ->nodeIndexer ->removeOldIndices ();
203- if (count ($ indicesToBeRemoved ) > 0 ) {
204- foreach ($ indicesToBeRemoved as $ indexToBeRemoved ) {
205- $ this ->logger ->log ('Removing old index ' . $ indexToBeRemoved );
209+ $ removedIndices = $ this ->nodeIndexer ->removeOldIndices ();
210+ if (count ($ removedIndices ) > 0 ) {
211+ if (count ($ removedIndices ) === 1 ) {
212+ $ this ->logger ->log ('Removed old index ' . current ($ removedIndices ) . '. ' );
213+ } else {
214+ $ this ->logger ->log ('Removed old indices ' . implode (', ' , $ removedIndices ) . '. ' );
206215 }
207216 } else {
208217 $ this ->logger ->log ('Nothing to remove. ' );
209218 }
210219 } catch (\Flowpack \ElasticSearch \Transfer \Exception \ApiException $ exception ) {
211220 $ response = json_decode ($ exception ->getResponse ());
212- $ this ->logger ->log (sprintf ('Nothing removed. ElasticSearch responded with status %s, saying "%s" ' , $ response ->status , $ response ->error ));
221+ $ this ->logger ->log (sprintf ('Nothing removed. ElasticSearch responded with status %s, saying "%s: %s" ' , $ response ->status , $ response ->error ->type , $ response ->error ->reason ));
222+ }
223+ }
224+
225+ /**
226+ * @param string $workspaceName
227+ * @return int
228+ */
229+ protected function indexWorkspace ($ workspaceName )
230+ {
231+ $ indexedNodes = 0 ;
232+ $ combinations = $ this ->contentDimensionCombinator ->getAllAllowedCombinations ();
233+ if ($ combinations === []) {
234+ $ indexedNodes += $ this ->indexWorkspaceWithDimensions ($ workspaceName );
235+ } else {
236+ foreach ($ combinations as $ combination ) {
237+ $ indexedNodes += $ this ->indexWorkspaceWithDimensions ($ workspaceName , $ combination );
238+ }
239+ }
240+
241+ return $ indexedNodes ;
242+ }
243+
244+ /**
245+ * @param string $workspaceName
246+ * @param array $dimensions
247+ * @return int
248+ */
249+ protected function indexWorkspaceWithDimensions ($ workspaceName , array $ dimensions = [])
250+ {
251+ $ indexedNodes = 0 ;
252+ $ context = $ this ->contextFactory ->create (['workspaceName ' => $ workspaceName , 'dimensions ' => $ dimensions ]);
253+ $ rootNode = $ context ->getRootNode ();
254+
255+ $ indexedNodes += $ this ->traverseNodes ($ rootNode );
256+
257+ if ($ dimensions === []) {
258+ $ this ->outputLine ('Workspace " ' . $ workspaceName . '" without dimensions done. (Indexed ' . $ indexedNodes . ' nodes) ' );
259+ } else {
260+ $ this ->outputLine ('Workspace " ' . $ workspaceName . '" and dimensions " ' . json_encode ($ dimensions ) . '" done. (Indexed ' . $ indexedNodes . ' nodes) ' );
261+ }
262+
263+ return $ indexedNodes ;
264+ }
265+
266+ /**
267+ * @param NodeInterface $currentNode
268+ * @param integer $traversedUntilNow
269+ * @return integer Indexed nodes in this traversal
270+ */
271+ protected function traverseNodes (NodeInterface $ currentNode , $ traversedUntilNow = 0 )
272+ {
273+ if ($ this ->limit !== null && $ traversedUntilNow > $ this ->limit ) {
274+ return $ traversedUntilNow ;
213275 }
276+
277+ $ this ->nodeIndexingManager ->indexNode ($ currentNode );
278+ $ traversedUntilNow ++;
279+
280+ foreach ($ currentNode ->getChildNodes () as $ childNode ) {
281+ $ traversedUntilNow = $ this ->traverseNodes ($ childNode , $ traversedUntilNow );
282+ }
283+
284+ return $ traversedUntilNow ;
214285 }
215286}
0 commit comments