‪TYPO3CMS  11.5
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 {
40  public function ‪indexPageContent(array $parameters, ‪TypoScriptFrontendController $tsfe)
41  {
42  // Determine if page should be indexed, and if so, configure and initialize indexer
43  if (!($tsfe->config['config']['index_enable'] ?? false)) {
44  return;
45  }
46 
47  // Indexer configuration from Extension Manager interface:
48  $disableFrontendIndexing = (bool)GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('indexed_search', 'disableFrontendIndexing');
49  $forceIndexing = $tsfe->applicationData['forceIndexing'] ?? false;
50 
51  $timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
52  $timeTracker->push('Index page');
53  if ($disableFrontendIndexing && !$forceIndexing) {
54  $timeTracker->setTSlogMessage('Index page? No, Ordinary Frontend indexing during rendering is disabled.');
55  return;
56  }
57 
58  if ($tsfe->page['no_search']) {
59  $timeTracker->setTSlogMessage('Index page? No, The "No Search" flag has been set in the page properties!');
60  return;
61  }
63  $languageAspect = GeneralUtility::makeInstance(Context::class)->getAspect('language');
64  if ($languageAspect->getId() !== $languageAspect->getContentId()) {
65  $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.');
66  return;
67  }
68  // Init and start indexing
69  $indexer = GeneralUtility::makeInstance(Indexer::class);
70  $indexer->forceIndexing = $forceIndexing;
71  $indexer->init($this->‪initializeIndexerConfiguration($tsfe, $languageAspect));
72  $indexer->indexTypo3PageContent();
73  $timeTracker->pull();
74  }
75 
85  {
86  $pageArguments = $tsfe->‪getPageArguments();
87  $configuration = [
88  // Page id
89  'id' => $tsfe->id,
90  // Page type
91  'type' => $tsfe->type,
92  // sys_language UID of the language of the indexing.
93  'sys_language_uid' => $languageAspect->‪getId(),
94  // MP variable, if any (Mount Points)
95  'MP' => $tsfe->MP,
96  // Group list
97  'gr_list' => implode(',', GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('frontend.user', 'groupIds', [0, -1])),
98  // page arguments array
99  'staticPageArguments' => $pageArguments->getStaticArguments(),
100  // The creation date of the TYPO3 page
101  'crdate' => $tsfe->page['crdate'],
102  'rootline_uids' => [],
103  ];
104 
105  // Root line uids
106  foreach ($tsfe->config['rootLine'] as $rlkey => $rldat) {
107  $configuration['rootline_uids'][$rlkey] = $rldat['uid'];
108  }
109  // Content of page
110  $configuration['content'] = $this->‪convOutputCharset($tsfe->content, $tsfe->metaCharset);
111  // Content string (HTML of TYPO3 page)
112  $configuration['indexedDocTitle'] = $this->‪convOutputCharset($tsfe->indexedDocTitle, $tsfe->metaCharset);
113  // Alternative title for indexing
114  $configuration['metaCharset'] = $tsfe->metaCharset;
115  // Character set of content (will be converted to utf-8 during indexing)
116  $configuration['mtime'] = $tsfe->register['SYS_LASTCHANGED'] ?? $tsfe->page['SYS_LASTCHANGED'];
117  // Most recent modification time (seconds) of the content on the page. Used to evaluate whether it should be re-indexed.
118  // Configuration of behavior
119  $configuration['index_externals'] = $tsfe->config['config']['index_externals'] ?? true;
120  // Whether to index external documents like PDF, DOC etc. (if possible)
121  $configuration['index_descrLgd'] = $tsfe->config['config']['index_descrLgd'] ?? 0;
122  // Length of description text (max 250, default 200)
123  $configuration['index_metatags'] = $tsfe->config['config']['index_metatags'] ?? true;
124  // Set to zero
125  $configuration['recordUid'] = 0;
126  $configuration['freeIndexUid'] = 0;
127  $configuration['freeIndexSetId'] = 0;
128  return $configuration;
129  }
130 
138  protected function ‪convOutputCharset(string $content, string $metaCharset): string
139  {
140  if ($metaCharset !== 'utf-8') {
141  $charsetConverter = GeneralUtility::makeInstance(CharsetConverter::class);
142  try {
143  $content = $charsetConverter->conv($content, 'utf-8', $metaCharset);
144  } catch (‪UnknownCharsetException $e) {
145  throw new \RuntimeException('Invalid config.metaCharset: ' . $e->getMessage(), 1508916285);
146  }
147  }
148  return $content;
149  }
150 }
‪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:3606
‪TYPO3\CMS\IndexedSearch\Hook\TypoScriptFrontendHook
Definition: TypoScriptFrontendHook.php:33
‪TYPO3\CMS\Core\Charset\UnknownCharsetException
Definition: UnknownCharsetException.php:23
‪TYPO3\CMS\IndexedSearch\Hook\TypoScriptFrontendHook\indexPageContent
‪indexPageContent(array $parameters, TypoScriptFrontendController $tsfe)
Definition: TypoScriptFrontendHook.php:40
‪TYPO3\CMS\Core\Context\LanguageAspect
Definition: LanguageAspect.php:57
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:104
‪TYPO3\CMS\IndexedSearch\Indexer
Definition: Indexer.php:39
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\IndexedSearch\Hook
Definition: AvailableTcaTables.php:18
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:31
‪TYPO3\CMS\IndexedSearch\Hook\TypoScriptFrontendHook\initializeIndexerConfiguration
‪array initializeIndexerConfiguration(TypoScriptFrontendController $tsfe, LanguageAspect $languageAspect)
Definition: TypoScriptFrontendHook.php:84
‪TYPO3\CMS\IndexedSearch\Hook\TypoScriptFrontendHook\convOutputCharset
‪string convOutputCharset(string $content, string $metaCharset)
Definition: TypoScriptFrontendHook.php:138