‪TYPO3CMS  ‪main
FrontendGenerationPageIndexingTrigger.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\EventDispatcher\EventDispatcherInterface;
28 use TYPO3\CMS\IndexedSearch\Indexer;
29 
37 {
38  public function ‪__construct(
39  private ‪ExtensionConfiguration $extensionConfiguration,
40  private ‪TimeTracker $timeTracker,
41  private ‪PageTitleProviderManager $pageTitleProviderManager,
42  private Indexer $indexer,
43  private EventDispatcherInterface $eventDispatcher,
44  private ‪Context $context,
45  ) {}
46 
51  #[AsEventListener('indexed-search')]
53  {
54  if (!$event->‪isCachingEnabled()) {
55  return;
56  }
57  $request = $event->‪getRequest();
58  $typoScriptConfigArray = $request->getAttribute('frontend.typoscript')->getConfigArray();
59  $pageArguments = $request->getAttribute('routing');
60  $pageInformation = $request->getAttribute('frontend.page.information');
61  $pageRecord = $pageInformation->getPageRecord();
62  $tsfe = $request->getAttribute('frontend.controller');
63 
64  // Determine if page should be indexed, and if so, configure and initialize indexer
65  if (!($typoScriptConfigArray['index_enable'] ?? false)) {
66  return;
67  }
68 
69  // Indexer configuration from Extension Manager interface:
70  $disableFrontendIndexing = (bool)$this->extensionConfiguration->get('indexed_search', 'disableFrontendIndexing');
71  $forceIndexing = $this->eventDispatcher->dispatch(new ‪EnableIndexingEvent($event->‪getRequest()))->isIndexingEnabled();
72 
73  $this->timeTracker->push('Index page');
74  if ($disableFrontendIndexing && !$forceIndexing) {
75  $this->timeTracker->setTSlogMessage('Index page? No, Ordinary Frontend indexing during rendering is disabled.');
76  return;
77  }
78 
79  if ($pageRecord['no_search'] ?? false) {
80  $this->timeTracker->setTSlogMessage('Index page? No, The "No Search" flag has been set in the page properties!');
81  return;
82  }
83  $languageAspect = $this->context->getAspect('language');
84  if ($languageAspect->getId() !== $languageAspect->getContentId()) {
85  $this->timeTracker->setTSlogMessage(
86  'Index page? No, languageId was different from contentId which indicates that the page contains'
87  . ' fall-back content and that would be falsely indexed as localized content.'
88  );
89  return;
90  }
91 
92  $this->indexer->forceIndexing = $forceIndexing;
93 
94  $configuration = [
95  // Page id
96  'id' => $pageInformation->getId(),
97  // Page type
98  'type' => $pageArguments->getPageType(),
99  // site language id of the language of the indexing.
100  'sys_language_uid' => $languageAspect->getId(),
101  // MP variable, if any (Mount Points)
102  'MP' => $pageInformation->getMountPoint(),
103  // Group list
104  'gr_list' => implode(',', $this->context->getPropertyFromAspect('frontend.user', 'groupIds', [0, -1])),
105  // page arguments array
106  'staticPageArguments' => $pageArguments->getStaticArguments(),
107  // The creation date of the TYPO3 page
108  'crdate' => $pageRecord['crdate'],
109  'rootline_uids' => [],
110  'content' => $tsfe->content,
111  // Alternative title for indexing
112  'indexedDocTitle' => $this->pageTitleProviderManager->getTitle($request),
113  // Most recent modification time (seconds) of the content on the page. Used to evaluate whether it should be re-indexed.
114  'mtime' => $tsfe->register['SYS_LASTCHANGED'] ?? $pageRecord['SYS_LASTCHANGED'],
115  // Whether to index external documents like PDF, DOC etc.
116  'index_externals' => $typoScriptConfigArray['index_externals'] ?? true,
117  // Length of description text (max 250, default 200)
118  'index_descrLgd' => $typoScriptConfigArray['index_descrLgd'] ?? 0,
119  'index_metatags' => $typoScriptConfigArray['index_metatags'] ?? true,
120  // Set to zero (@todo: why is this needed?)
121  'recordUid' => 0,
122  'freeIndexUid' => 0,
123  'freeIndexSetId' => 0,
124  ];
125  $localRootLine = $pageInformation->getLocalRootLine();
126  foreach ($localRootLine as $rlkey => $rldat) {
127  $configuration['rootline_uids'][$rlkey] = $rldat['uid'];
128  }
129 
130  $this->indexer->init($configuration);
131  $this->indexer->indexTypo3PageContent();
132  $this->timeTracker->pull();
133  }
134 }
‪TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent\getRequest
‪getRequest()
Definition: AfterCacheableContentIsGeneratedEvent.php:37
‪TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent\isCachingEnabled
‪isCachingEnabled()
Definition: AfterCacheableContentIsGeneratedEvent.php:47
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration
Definition: ExtensionConfiguration.php:47
‪TYPO3\CMS\Core\Attribute\AsEventListener
Definition: AsEventListener.php:25
‪TYPO3\CMS\IndexedSearch\EventListener
Definition: FrontendGenerationPageIndexingTrigger.php:18
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent
Definition: AfterCacheableContentIsGeneratedEvent.php:29
‪TYPO3\CMS\IndexedSearch\EventListener\FrontendGenerationPageIndexingTrigger\__construct
‪__construct(private ExtensionConfiguration $extensionConfiguration, private TimeTracker $timeTracker, private PageTitleProviderManager $pageTitleProviderManager, private Indexer $indexer, private EventDispatcherInterface $eventDispatcher, private Context $context,)
Definition: FrontendGenerationPageIndexingTrigger.php:38
‪TYPO3\CMS\IndexedSearch\Event\EnableIndexingEvent
Definition: EnableIndexingEvent.php:23
‪TYPO3\CMS\IndexedSearch\EventListener\FrontendGenerationPageIndexingTrigger
Definition: FrontendGenerationPageIndexingTrigger.php:37
‪TYPO3\CMS\Core\PageTitle\PageTitleProviderManager
Definition: PageTitleProviderManager.php:33
‪TYPO3\CMS\IndexedSearch\EventListener\FrontendGenerationPageIndexingTrigger\indexPageContent
‪indexPageContent(AfterCacheableContentIsGeneratedEvent $event)
Definition: FrontendGenerationPageIndexingTrigger.php:52
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:34