‪TYPO3CMS  10.4
TypoScriptFrontendHook.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
27 
33 {
41  public function ‪headerNoCache(array &$params, ‪TypoScriptFrontendController $tsfe)
42  {
43  // Requirements are that the crawler is loaded, a crawler session is running and re-indexing requested as processing instruction:
44  if (in_array('tx_indexedsearch_reindex', $tsfe->applicationData['tx_crawler']['parameters']['procInstructions'] ?? [], true)) {
45  // Disables a look-up for cached page data - thus resulting in re-generation of the page even if cached.
46  $params['disableAcquireCacheData'] = true;
47  // Enable indexing
48  $tsfe->applicationData['forceIndexing'] = true;
49  }
50  }
51 
58  public function ‪indexPageContent(array $parameters, ‪TypoScriptFrontendController $tsfe)
59  {
60  // Determine if page should be indexed, and if so, configure and initialize indexer
61  if (!$tsfe->config['config']['index_enable']) {
62  return;
63  }
64 
65  // Indexer configuration from Extension Manager interface:
66  $disableFrontendIndexing = (bool)GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('indexed_search', 'disableFrontendIndexing');
67  $forceIndexing = $tsfe->applicationData['forceIndexing'] ?? false;
68 
69  $timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
70  $timeTracker->push('Index page');
71  if ($disableFrontendIndexing && !$forceIndexing) {
72  $timeTracker->setTSlogMessage('Index page? No, Ordinary Frontend indexing during rendering is disabled.');
73  return;
74  }
75 
76  if ($tsfe->page['no_search']) {
77  $timeTracker->setTSlogMessage('Index page? No, The "No Search" flag has been set in the page properties!');
78  return;
79  }
81  $languageAspect = GeneralUtility::makeInstance(Context::class)->getAspect('language');
82  if ($languageAspect->getId() !== $languageAspect->getContentId()) {
83  $timeTracker->setTSlogMessage('Index page? No, languageId was different from contentId which indicates that the page contains fall-back content and that would be falsely indexed as localized content.');
84  return;
85  }
86  // Init and start indexing
87  $indexer = GeneralUtility::makeInstance(Indexer::class);
88  $indexer->forceIndexing = $forceIndexing;
89  $indexer->init($this->‪initializeIndexerConfiguration($tsfe, $languageAspect));
90  $indexer->indexTypo3PageContent();
91  $timeTracker->pull();
92  }
93 
103  {
104  $pageArguments = $tsfe->‪getPageArguments();
105  $configuration = [
106  // Page id
107  'id' => $tsfe->id,
108  // Page type
109  'type'=> $tsfe->type,
110  // sys_language UID of the language of the indexing.
111  'sys_language_uid' => $languageAspect->‪getId(),
112  // MP variable, if any (Mount Points)
113  'MP' => $tsfe->MP,
114  // Group list
115  'gr_list' => implode(',', GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('frontend.user', 'groupIds', [0, -1])),
116  // page arguments array
117  'staticPageArguments' => $pageArguments->getStaticArguments(),
118  // The creation date of the TYPO3 page
119  'crdate' => $tsfe->page['crdate'],
120  'rootline_uids' => [],
121  ];
122 
123  // Root line uids
124  foreach ($tsfe->config['rootLine'] as $rlkey => $rldat) {
125  $configuration['rootline_uids'][$rlkey] = $rldat['uid'];
126  }
127  // Content of page
128  $configuration['content'] = $this->‪convOutputCharset($tsfe->content, $tsfe->metaCharset);
129  // Content string (HTML of TYPO3 page)
130  $configuration['indexedDocTitle'] = $this->‪convOutputCharset($tsfe->indexedDocTitle, $tsfe->metaCharset);
131  // Alternative title for indexing
132  $configuration['metaCharset'] = $tsfe->metaCharset;
133  // Character set of content (will be converted to utf-8 during indexing)
134  $configuration['mtime'] = $tsfe->register['SYS_LASTCHANGED'] ?? $tsfe->page['SYS_LASTCHANGED'];
135  // Most recent modification time (seconds) of the content on the page. Used to evaluate whether it should be re-indexed.
136  // Configuration of behavior
137  $configuration['index_externals'] = $tsfe->config['config']['index_externals'];
138  // Whether to index external documents like PDF, DOC etc. (if possible)
139  $configuration['index_descrLgd'] = $tsfe->config['config']['index_descrLgd'];
140  // Length of description text (max 250, default 200)
141  $configuration['index_metatags'] = $tsfe->config['config']['index_metatags'] ?? true;
142  // Set to zero
143  $configuration['recordUid'] = 0;
144  $configuration['freeIndexUid'] = 0;
145  $configuration['freeIndexSetId'] = 0;
146  return $configuration;
147  }
148 
156  protected function ‪convOutputCharset(string $content, string $metaCharset): string
157  {
158  if ($metaCharset !== 'utf-8') {
159  $charsetConverter = GeneralUtility::makeInstance(CharsetConverter::class);
160  try {
161  $content = $charsetConverter->conv($content, 'utf-8', $metaCharset);
162  } catch (‪UnknownCharsetException $e) {
163  throw new \RuntimeException('Invalid config.metaCharset: ' . $e->getMessage(), 1508916285);
164  }
165  }
166  return $content;
167  }
168 }
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration
Definition: ExtensionConfiguration.php:45
‪TYPO3\CMS\Core\Context\LanguageAspect\getId
‪int getId()
Definition: LanguageAspect.php:111
‪TYPO3\CMS\Core\Charset\CharsetConverter
Definition: CharsetConverter.php:54
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController\getPageArguments
‪getPageArguments()
Definition: TypoScriptFrontendController.php:3981
‪TYPO3\CMS\IndexedSearch\Hook\TypoScriptFrontendHook
Definition: TypoScriptFrontendHook.php:33
‪TYPO3\CMS\Core\Charset\UnknownCharsetException
Definition: UnknownCharsetException.php:24
‪TYPO3\CMS\IndexedSearch\Hook\TypoScriptFrontendHook\headerNoCache
‪headerNoCache(array &$params, TypoScriptFrontendController $tsfe)
Definition: TypoScriptFrontendHook.php:41
‪TYPO3\CMS\IndexedSearch\Hook\TypoScriptFrontendHook\indexPageContent
‪indexPageContent(array $parameters, TypoScriptFrontendController $tsfe)
Definition: TypoScriptFrontendHook.php:58
‪TYPO3\CMS\Core\Context\LanguageAspect
Definition: LanguageAspect.php:57
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪TYPO3\CMS\IndexedSearch\Indexer
Definition: Indexer.php:37
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\IndexedSearch\Hook
Definition: CrawlerFilesHook.php:16
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:30
‪TYPO3\CMS\IndexedSearch\Hook\TypoScriptFrontendHook\initializeIndexerConfiguration
‪array initializeIndexerConfiguration(TypoScriptFrontendController $tsfe, LanguageAspect $languageAspect)
Definition: TypoScriptFrontendHook.php:102
‪TYPO3\CMS\IndexedSearch\Hook\TypoScriptFrontendHook\convOutputCharset
‪string convOutputCharset(string $content, string $metaCharset)
Definition: TypoScriptFrontendHook.php:156