@@ -252,29 +252,37 @@ public function indexNode(NodeInterface $node, $targetWorkspaceName = null)
252252 ];
253253 }
254254
255- $ this ->updateFulltext ($ node , $ fulltextIndexOfNode );
255+ $ this ->updateFulltext ($ node , $ fulltextIndexOfNode, $ targetWorkspaceName );
256256 }
257257
258258 $ this ->logger ->log (sprintf ('NodeIndexer (%s): Added / updated node %s. ' , $ documentIdentifier , $ contextPath ), LOG_DEBUG , null , 'ElasticSearch (CR) ' );
259259 };
260260
261+ $ handleNode = function (NodeInterface $ node , \TYPO3 \TYPO3CR \Domain \Service \Context $ context ) use ($ targetWorkspaceName , $ indexer ) {
262+ $ nodeFromContext = $ context ->getNodeByIdentifier ($ node ->getIdentifier ());
263+ if ($ nodeFromContext instanceof NodeInterface) {
264+ $ indexer ($ nodeFromContext , $ targetWorkspaceName );
265+ } else {
266+ $ documentIdentifier = $ this ->calculateDocumentIdentifier ($ node , $ targetWorkspaceName );
267+ if ($ node ->isRemoved ()) {
268+ $ this ->logger ->log (sprintf ('NodeIndexer (%s): Removing node with identifier %s, no longer in workspace %s ' , $ documentIdentifier , $ node ->getIdentifier (), $ context ->getWorkspaceName ()), LOG_DEBUG , null , 'ElasticSearch (CR) ' );
269+ $ this ->removeNode ($ node , $ context ->getWorkspaceName ());
270+ } else {
271+ $ this ->logger ->log (sprintf ('NodeIndexer (%s): Could not add / update node with identifier %s, not found in workspace %s ' , $ documentIdentifier , $ node ->getIdentifier (), $ context ->getWorkspaceName ()), LOG_DEBUG , null , 'ElasticSearch (CR) ' );
272+ }
273+ }
274+ };
275+
276+ $ workspaceName = $ targetWorkspaceName ?: $ node ->getContext ()->getWorkspaceName ();
261277 $ dimensionCombinations = $ this ->contentDimensionCombinator ->getAllAllowedCombinations ();
262- $ workspaceName = $ targetWorkspaceName ?: 'live ' ;
263- $ nodeIdentifier = $ node ->getIdentifier ();
264278 if ($ dimensionCombinations !== []) {
265279 foreach ($ dimensionCombinations as $ combination ) {
266- $ context = $ this ->contextFactory ->create (['workspaceName ' => $ workspaceName , 'dimensions ' => $ combination ]);
267- $ node = $ context ->getNodeByIdentifier ($ nodeIdentifier );
268- if ($ node !== null ) {
269- $ indexer ($ node , $ targetWorkspaceName );
270- }
280+ $ context = $ this ->contextFactory ->create (['workspaceName ' => $ workspaceName , 'dimensions ' => $ combination , 'invisibleContentShown ' => true ]);
281+ $ handleNode ($ node , $ context );
271282 }
272283 } else {
273- $ context = $ this ->contextFactory ->create (['workspaceName ' => $ workspaceName ]);
274- $ node = $ context ->getNodeByIdentifier ($ nodeIdentifier );
275- if ($ node !== null ) {
276- $ indexer ($ node , $ targetWorkspaceName );
277- }
284+ $ context = $ this ->contextFactory ->create (['workspaceName ' => $ workspaceName , 'invisibleContentShown ' => true ]);
285+ $ handleNode ($ node , $ context );
278286 }
279287 }
280288
@@ -300,9 +308,10 @@ protected function calculateDocumentIdentifier(NodeInterface $node, $targetWorks
300308 *
301309 * @param NodeInterface $node
302310 * @param array $fulltextIndexOfNode
311+ * @param $targetWorkspaceName
303312 * @return void
304313 */
305- protected function updateFulltext (NodeInterface $ node , array $ fulltextIndexOfNode )
314+ protected function updateFulltext (NodeInterface $ node , array $ fulltextIndexOfNode, $ targetWorkspaceName )
306315 {
307316 $ closestFulltextNode = $ node ;
308317 while (!$ this ->isFulltextRoot ($ closestFulltextNode )) {
@@ -315,9 +324,18 @@ protected function updateFulltext(NodeInterface $node, array $fulltextIndexOfNod
315324 }
316325 }
317326
318- $ closestFulltextNodeContextPath = str_replace ($ closestFulltextNode ->getContext ()->getWorkspace ()->getName (), 'live ' , $ closestFulltextNode ->getContextPath ());
327+ $ closestFulltextNodeContextPath = $ closestFulltextNode ->getContextPath ();
328+ if ($ targetWorkspaceName !== null ) {
329+ $ closestFulltextNodeContextPath = str_replace ($ node ->getContext ()->getWorkspace ()->getName (), $ targetWorkspaceName , $ closestFulltextNodeContextPath );
330+ }
319331 $ closestFulltextNodeDocumentIdentifier = sha1 ($ closestFulltextNodeContextPath );
320332
333+ if ($ closestFulltextNode ->isRemoved ()) {
334+ // fulltext root is removed, abort silently...
335+ $ this ->logger ->log (sprintf ('NodeIndexer (%s): Fulltext root found for %s (%s) not updated, it is removed ' , $ closestFulltextNodeDocumentIdentifier , $ node ->getPath (), $ node ->getIdentifier ()), LOG_DEBUG );
336+ return ;
337+ }
338+
321339 $ this ->currentBulkRequest [] = [
322340 [
323341 'update ' => [
@@ -400,18 +418,25 @@ protected function isFulltextRoot(NodeInterface $node)
400418 * Schedule node removal into the current bulk request.
401419 *
402420 * @param NodeInterface $node
421+ * @param string $targetWorkspaceName
403422 * @return string
404423 */
405- public function removeNode (NodeInterface $ node )
424+ public function removeNode (NodeInterface $ node, $ targetWorkspaceName = null )
406425 {
407426 if ($ this ->settings ['indexAllWorkspaces ' ] === false ) {
408- if ($ node ->getContext ()->getWorkspaceName () !== 'live ' ) {
427+ // we are only supposed to index the live workspace.
428+ // We need to check the workspace at two occasions; checking the
429+ // $targetWorkspaceName and the workspace name of the node's context as fallback
430+ if ($ targetWorkspaceName !== null && $ targetWorkspaceName !== 'live ' ) {
431+ return ;
432+ }
433+
434+ if ($ targetWorkspaceName === null && $ node ->getContext ()->getWorkspaceName () !== 'live ' ) {
409435 return ;
410436 }
411437 }
412438
413- // TODO: handle deletion from the fulltext index as well
414- $ documentIdentifier = $ this ->calculateDocumentIdentifier ($ node );
439+ $ documentIdentifier = $ this ->calculateDocumentIdentifier ($ node , $ targetWorkspaceName );
415440
416441 $ this ->currentBulkRequest [] = [
417442 [
@@ -422,6 +447,8 @@ public function removeNode(NodeInterface $node)
422447 ]
423448 ];
424449
450+ $ this ->updateFulltext ($ node , [], $ targetWorkspaceName );
451+
425452 $ this ->logger ->log (sprintf ('NodeIndexer (%s): Removed node %s (%s) from index. ' , $ documentIdentifier , $ node ->getContextPath (), $ node ->getIdentifier ()), LOG_DEBUG , null , 'ElasticSearch (CR) ' );
426453 }
427454
0 commit comments