vendor/ezsystems/ezplatform-kernel/eZ/Bundle/EzPublishCoreBundle/EventListener/BackgroundIndexingTerminateListener.php line 24

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace eZ\Bundle\EzPublishCoreBundle\EventListener;
  7. use eZ\Publish\API\Repository\Exceptions\NotFoundException;
  8. use eZ\Publish\Core\Search\Common\BackgroundIndexer as BackgroundIndexerInterface;
  9. use eZ\Publish\SPI\Persistence\Content\ContentInfo;
  10. use eZ\Publish\SPI\Persistence\Content\Location;
  11. use eZ\Publish\SPI\Persistence\Handler as PersistenceHandler;
  12. use eZ\Publish\SPI\Search\Handler as SearchHandler;
  13. use Psr\Log\LoggerAwareTrait;
  14. use Psr\Log\LoggerInterface;
  15. use Symfony\Component\Console\ConsoleEvents;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. /**
  19.  * Kernel and Console terminate event based background indexer.
  20.  */
  21. class BackgroundIndexingTerminateListener implements BackgroundIndexerInterfaceEventSubscriberInterface
  22. {
  23.     use LoggerAwareTrait;
  24.     /** @var \eZ\Publish\SPI\Persistence\Handler */
  25.     protected $persistenceHandler;
  26.     /** @var \eZ\Publish\SPI\Search\Handler */
  27.     protected $searchHandler;
  28.     /** @var \eZ\Publish\SPI\Persistence\Content\ContentInfo[] */
  29.     protected $contentInfo = [];
  30.     /** @var \eZ\Publish\SPI\Persistence\Content\Location[] */
  31.     protected $locations = [];
  32.     public function __construct(PersistenceHandler $persistenceHandlerSearchHandler $searchHandler)
  33.     {
  34.         $this->persistenceHandler $persistenceHandler;
  35.         $this->searchHandler $searchHandler;
  36.     }
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             KernelEvents::TERMINATE => 'reindex',
  41.             KernelEvents::EXCEPTION => 'reindex',
  42.             ConsoleEvents::TERMINATE => 'reindex',
  43.         ];
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     public function registerContent(ContentInfo $contentInfo)
  49.     {
  50.         $this->contentInfo[] = $contentInfo;
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function registerLocation(Location $location)
  56.     {
  57.         $this->locations[] = $location;
  58.     }
  59.     public function reindex()
  60.     {
  61.         $contentHandler $this->persistenceHandler->contentHandler();
  62.         $contentIndexed = [];
  63.         $contentRemoved = [];
  64.         foreach ($this->contentInfo as $contentInfo) {
  65.             if (isset($contentIndexed[$contentInfo->id]) || isset($contentRemoved[$contentInfo->id])) {
  66.                 continue;
  67.             }
  68.             try {
  69.                 // In case version has changed we make sure to fetch fresh ContentInfo
  70.                 $contentInfo $contentHandler->loadContentInfo($contentInfo->id);
  71.                 if ($contentInfo->isPublished) {
  72.                     $this->searchHandler->indexContent(
  73.                         $contentHandler->load($contentInfo->id$contentInfo->currentVersionNo)
  74.                     );
  75.                     $contentIndexed[$contentInfo->id] = $contentInfo->id;
  76.                     continue;
  77.                 }
  78.             } catch (NotFoundException $e) {
  79.                 // Catch this so we delete the index for this content below
  80.             }
  81.             $this->searchHandler->deleteContent($contentInfo->id);
  82.             if ($contentInfo->mainLocationId) {
  83.                 $this->searchHandler->deleteLocation($contentInfo->mainLocationId$contentInfo->id);
  84.             }
  85.             $contentRemoved[$contentInfo->id] = $contentInfo->id;
  86.         }
  87.         $this->contentInfo = [];
  88.         foreach ($this->locations as $location) {
  89.             if (isset($contentIndexed[$location->contentId]) || isset($contentRemoved[$location->contentId])) {
  90.                 continue;
  91.             }
  92.             try {
  93.                 // In case version has changed we make sure to fetch fresh ContentInfo
  94.                 $contentInfo $contentHandler->loadContentInfo($location->contentId);
  95.                 if ($contentInfo->isPublished) {
  96.                     $this->searchHandler->indexContent(
  97.                         $contentHandler->load($contentInfo->id$contentInfo->currentVersionNo)
  98.                     );
  99.                     $contentIndexed[$contentInfo->id] = $contentInfo->id;
  100.                     continue;
  101.                 }
  102.             } catch (NotFoundException $e) {
  103.                 // Catch this so we delete the index for this content below
  104.             }
  105.             $this->searchHandler->deleteContent($location->contentId);
  106.             $this->searchHandler->deleteLocation($location->id$location->contentId);
  107.             $contentRemoved[$location->contentId] = $location->contentId;
  108.         }
  109.         $this->locations = [];
  110.         if ($this->logger instanceof LoggerInterface && (!empty($contentIndexed) || !empty($contentRemoved))) {
  111.             $this->logger->warning(
  112.                 sprintf(
  113.                     'Exceptions detected on search index. Content %s was re-indexed and %s was removed from index',
  114.                     implode(', '$contentIndexed),
  115.                     implode(', '$contentRemoved)
  116.                 )
  117.             );
  118.         }
  119.     }
  120. }