167980cacb09a5bc6fceed71a22efacf5f2e4d99
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / Classes / Controller / SearchController.php
1 <?php
2 namespace TYPO3\CMS\IndexedSearch\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Charset\CharsetConverter;
18 use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
19 use TYPO3\CMS\Core\Database\Connection;
20 use TYPO3\CMS\Core\Database\ConnectionPool;
21 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
22 use TYPO3\CMS\Core\Html\HtmlParser;
23 use TYPO3\CMS\Core\TypoScript\TypoScriptService;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Core\Utility\MathUtility;
26 use TYPO3\CMS\Core\Utility\PathUtility;
27 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
28
29 /**
30 * Index search frontend
31 *
32 * Creates a search form for indexed search. Indexing must be enabled
33 * for this to make sense.
34 */
35 class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
36 {
37 /**
38 * previously known as $this->piVars['sword']
39 *
40 * @var string
41 */
42 protected $sword = '';
43
44 /**
45 * @var array
46 */
47 protected $searchWords = [];
48
49 /**
50 * @var array
51 */
52 protected $searchData;
53
54 /**
55 * This is the id of the site root.
56 * This value may be a comma separated list of integer (prepared for this)
57 * Root-page PIDs to search in (rl0 field where clause, see initialize() function)
58 *
59 * If this value is set to less than zero (eg. -1) searching will happen
60 * in ALL of the page tree with no regard to branches at all.
61 * @var int|string
62 */
63 protected $searchRootPageIdList = 0;
64
65 /**
66 * @var int
67 */
68 protected $defaultResultNumber = 10;
69
70 /**
71 * @var int[]
72 */
73 protected $availableResultsNumbers = [];
74
75 /**
76 * Search repository
77 *
78 * @var \TYPO3\CMS\IndexedSearch\Domain\Repository\IndexSearchRepository
79 */
80 protected $searchRepository = null;
81
82 /**
83 * Lexer object
84 *
85 * @var \TYPO3\CMS\IndexedSearch\Lexer
86 */
87 protected $lexerObj;
88
89 /**
90 * External parser objects
91 * @var array
92 */
93 protected $externalParsers = [];
94
95 /**
96 * Will hold the first row in result - used to calculate relative hit-ratings.
97 *
98 * @var array
99 */
100 protected $firstRow = [];
101
102 /**
103 * sys_domain records
104 *
105 * @var array
106 */
107 protected $domainRecords = [];
108
109 /**
110 * Required fe_groups memberships for display of a result.
111 *
112 * @var array
113 */
114 protected $requiredFrontendUsergroups = [];
115
116 /**
117 * Page tree sections for search result.
118 *
119 * @var array
120 */
121 protected $resultSections = [];
122
123 /**
124 * Caching of page path
125 *
126 * @var array
127 */
128 protected $pathCache = [];
129
130 /**
131 * Storage of icons
132 *
133 * @var array
134 */
135 protected $iconFileNameCache = [];
136
137 /**
138 * Indexer configuration, coming from TYPO3's system configuration for EXT:indexed_search
139 *
140 * @var array
141 */
142 protected $indexerConfig = [];
143
144 /**
145 * Flag whether metaphone search should be enabled
146 *
147 * @var bool
148 */
149 protected $enableMetaphoneSearch = false;
150
151 /**
152 * @var \TYPO3\CMS\Core\TypoScript\TypoScriptService
153 */
154 protected $typoScriptService;
155
156 /**
157 * @var CharsetConverter
158 */
159 protected $charsetConverter;
160
161 /**
162 * @param \TYPO3\CMS\Core\TypoScript\TypoScriptService $typoScriptService
163 */
164 public function injectTypoScriptService(\TYPO3\CMS\Core\TypoScript\TypoScriptService $typoScriptService)
165 {
166 $this->typoScriptService = $typoScriptService;
167 }
168
169 /**
170 * sets up all necessary object for searching
171 *
172 * @param array $searchData The incoming search parameters
173 * @return array Search parameters
174 */
175 public function initialize($searchData = [])
176 {
177 $this->charsetConverter = GeneralUtility::makeInstance(CharsetConverter::class);
178 if (!is_array($searchData)) {
179 $searchData = [];
180 }
181
182 // check if TypoScript is loaded
183 if (!isset($this->settings['results'])) {
184 $this->redirect('noTypoScript');
185 }
186
187 // Sets availableResultsNumbers - has to be called before request settings are read to avoid DoS attack
188 $this->availableResultsNumbers = array_filter(GeneralUtility::intExplode(',', $this->settings['blind']['numberOfResults']));
189
190 // Sets default result number if at least one availableResultsNumbers exists
191 if (isset($this->availableResultsNumbers[0])) {
192 $this->defaultResultNumber = $this->availableResultsNumbers[0];
193 }
194
195 $this->loadSettings();
196
197 // setting default values
198 if (is_array($this->settings['defaultOptions'])) {
199 $searchData = array_merge($this->settings['defaultOptions'], $searchData);
200 }
201 // Indexer configuration from Extension Manager interface:
202 $this->indexerConfig = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('indexed_search');
203 $this->enableMetaphoneSearch = (bool)$this->indexerConfig['enableMetaphoneSearch'];
204 $this->initializeExternalParsers();
205 // If "_sections" is set, this value overrides any existing value.
206 if ($searchData['_sections']) {
207 $searchData['sections'] = $searchData['_sections'];
208 }
209 // If "_sections" is set, this value overrides any existing value.
210 if ($searchData['_freeIndexUid'] !== '' && $searchData['_freeIndexUid'] !== '_') {
211 $searchData['freeIndexUid'] = $searchData['_freeIndexUid'];
212 }
213 $searchData['numberOfResults'] = $this->getNumberOfResults($searchData['numberOfResults']);
214 // This gets the search-words into the $searchWordArray
215 $this->setSword($searchData['sword']);
216 // Add previous search words to current
217 if ($searchData['sword_prev_include'] && $searchData['sword_prev']) {
218 $this->setSword(trim($searchData['sword_prev']) . ' ' . $this->getSword());
219 }
220 $this->searchWords = $this->getSearchWords($searchData['defaultOperand']);
221 // This is the id of the site root.
222 // This value may be a commalist of integer (prepared for this)
223 $this->searchRootPageIdList = (int)$GLOBALS['TSFE']->config['rootLine'][0]['uid'];
224 // Setting the list of root PIDs for the search. Notice, these page IDs MUST
225 // have a TypoScript template with root flag on them! Basically this list is used
226 // to select on the "rl0" field and page ids are registered as "rl0" only if
227 // a TypoScript template record with root flag is there.
228 // This happens AFTER the use of $this->searchRootPageIdList above because
229 // the above will then fetch the menu for the CURRENT site - regardless
230 // of this kind of searching here. Thus a general search will lookup in
231 // the WHOLE database while a specific section search will take the current sections.
232 if ($this->settings['rootPidList']) {
233 $this->searchRootPageIdList = implode(',', GeneralUtility::intExplode(',', $this->settings['rootPidList']));
234 }
235 $this->searchRepository = GeneralUtility::makeInstance(\TYPO3\CMS\IndexedSearch\Domain\Repository\IndexSearchRepository::class);
236 $this->searchRepository->initialize($this->settings, $searchData, $this->externalParsers, $this->searchRootPageIdList);
237 $this->searchData = $searchData;
238 // Calling hook for modification of initialized content
239 if ($hookObj = $this->hookRequest('initialize_postProc')) {
240 $hookObj->initialize_postProc();
241 }
242 return $searchData;
243 }
244
245 /**
246 * Performs the search, the display and writing stats
247 *
248 * @param array $search the search parameters, an associative array
249 * @ignorevalidation $search
250 */
251 public function searchAction($search = [])
252 {
253 $searchData = $this->initialize($search);
254 // Find free index uid:
255 $freeIndexUid = $searchData['freeIndexUid'];
256 if ($freeIndexUid == -2) {
257 $freeIndexUid = $this->settings['defaultFreeIndexUidList'];
258 } elseif (!isset($searchData['freeIndexUid'])) {
259 // index configuration is disabled
260 $freeIndexUid = -1;
261 }
262 $indexCfgs = GeneralUtility::intExplode(',', $freeIndexUid);
263 $resultsets = [];
264 foreach ($indexCfgs as $freeIndexUid) {
265 // Get result rows
266 $tstamp1 = GeneralUtility::milliseconds();
267 if ($hookObj = $this->hookRequest('getResultRows')) {
268 $resultData = $hookObj->getResultRows($this->searchWords, $freeIndexUid);
269 } else {
270 $resultData = $this->searchRepository->doSearch($this->searchWords, $freeIndexUid);
271 }
272 // Display search results
273 $tstamp2 = GeneralUtility::milliseconds();
274 if ($hookObj = $this->hookRequest('getDisplayResults')) {
275 $resultsets[$freeIndexUid] = $hookObj->getDisplayResults($this->searchWords, $resultData, $freeIndexUid);
276 } else {
277 $resultsets[$freeIndexUid] = $this->getDisplayResults($this->searchWords, $resultData, $freeIndexUid);
278 }
279 $tstamp3 = GeneralUtility::milliseconds();
280 // Create header if we are searching more than one indexing configuration
281 if (count($indexCfgs) > 1) {
282 if ($freeIndexUid > 0) {
283 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
284 ->getQueryBuilderForTable('index_config');
285 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
286 $indexCfgRec = $queryBuilder
287 ->select('*')
288 ->from('index_config')
289 ->where(
290 $queryBuilder->expr()->eq(
291 'uid',
292 $queryBuilder->createNamedParameter($freeIndexUid, \PDO::PARAM_INT)
293 )
294 )
295 ->execute()
296 ->fetch();
297 $categoryTitle = $indexCfgRec['title'];
298 } else {
299 $categoryTitle = LocalizationUtility::translate('indexingConfigurationHeader.' . $freeIndexUid, 'IndexedSearch');
300 }
301 $resultsets[$freeIndexUid]['categoryTitle'] = $categoryTitle;
302 }
303 // Write search statistics
304 $this->writeSearchStat($searchData, $this->searchWords, $resultData['count'], [$tstamp1, $tstamp2, $tstamp3]);
305 }
306 $this->view->assign('resultsets', $resultsets);
307 $this->view->assign('searchParams', $searchData);
308 $this->view->assign('searchWords', $this->searchWords);
309 }
310
311 /****************************************
312 * functions to make the result rows and result sets
313 * ready for the output
314 ***************************************/
315 /**
316 * Compiles the HTML display of the incoming array of result rows.
317 *
318 * @param array $searchWords Search words array (for display of text describing what was searched for)
319 * @param array $resultData Array with result rows, count, first row.
320 * @param int $freeIndexUid Pointing to which indexing configuration you want to search in. -1 means no filtering. 0 means only regular indexed content.
321 * @return array
322 */
323 protected function getDisplayResults($searchWords, $resultData, $freeIndexUid = -1)
324 {
325 $result = [
326 'count' => $resultData['count'],
327 'searchWords' => $searchWords
328 ];
329 // Perform display of result rows array
330 if ($resultData) {
331 // Set first selected row (for calculation of ranking later)
332 $this->firstRow = $resultData['firstRow'];
333 // Result display here
334 $result['rows'] = $this->compileResultRows($resultData['resultRows'], $freeIndexUid);
335 $result['affectedSections'] = $this->resultSections;
336 // Browsing box
337 if ($resultData['count']) {
338 // could we get this in the view?
339 if ($this->searchData['group'] === 'sections' && $freeIndexUid <= 0) {
340 $resultSectionsCount = count($this->resultSections);
341 $result['sectionText'] = sprintf(LocalizationUtility::translate('result.' . ($resultSectionsCount > 1 ? 'inNsections' : 'inNsection'), 'IndexedSearch'), $resultSectionsCount);
342 }
343 }
344 }
345 // Print a message telling which words in which sections we searched for
346 if (substr($this->searchData['sections'], 0, 2) === 'rl') {
347 $result['searchedInSectionInfo'] = LocalizationUtility::translate('result.inSection', 'IndexedSearch') . ' "' . $this->getPathFromPageId(substr($this->searchData['sections'], 4)) . '"';
348 }
349
350 if ($hookObj = $this->hookRequest('getDisplayResults_postProc')) {
351 $result = $hookObj->getDisplayResults_postProc($result);
352 }
353
354 return $result;
355 }
356
357 /**
358 * Takes the array with resultrows as input and returns the result-HTML-code
359 * Takes the "group" var into account: Makes a "section" or "flat" display.
360 *
361 * @param array $resultRows Result rows
362 * @param int $freeIndexUid Pointing to which indexing configuration you want to search in. -1 means no filtering. 0 means only regular indexed content.
363 * @return array the result rows with additional information
364 */
365 protected function compileResultRows($resultRows, $freeIndexUid = -1)
366 {
367 $finalResultRows = [];
368 // Transfer result rows to new variable,
369 // performing some mapping of sub-results etc.
370 $newResultRows = [];
371 foreach ($resultRows as $row) {
372 $id = md5($row['phash_grouping']);
373 if (is_array($newResultRows[$id])) {
374 // swapping:
375 if (!$newResultRows[$id]['show_resume'] && $row['show_resume']) {
376 // Remove old
377 $subrows = $newResultRows[$id]['_sub'];
378 unset($newResultRows[$id]['_sub']);
379 $subrows[] = $newResultRows[$id];
380 // Insert new:
381 $newResultRows[$id] = $row;
382 $newResultRows[$id]['_sub'] = $subrows;
383 } else {
384 $newResultRows[$id]['_sub'][] = $row;
385 }
386 } else {
387 $newResultRows[$id] = $row;
388 }
389 }
390 $resultRows = $newResultRows;
391 $this->resultSections = [];
392 if ($freeIndexUid <= 0 && $this->searchData['group'] === 'sections') {
393 $rl2flag = substr($this->searchData['sections'], 0, 2) === 'rl';
394 $sections = [];
395 foreach ($resultRows as $row) {
396 $id = $row['rl0'] . '-' . $row['rl1'] . ($rl2flag ? '-' . $row['rl2'] : '');
397 $sections[$id][] = $row;
398 }
399 $this->resultSections = [];
400 foreach ($sections as $id => $resultRows) {
401 $rlParts = explode('-', $id);
402 if ($rlParts[2]) {
403 $theId = $rlParts[2];
404 $theRLid = 'rl2_' . $rlParts[2];
405 } elseif ($rlParts[1]) {
406 $theId = $rlParts[1];
407 $theRLid = 'rl1_' . $rlParts[1];
408 } else {
409 $theId = $rlParts[0];
410 $theRLid = '0';
411 }
412 $sectionName = $this->getPathFromPageId($theId);
413 $sectionName = ltrim($sectionName, '/');
414 if (!trim($sectionName)) {
415 $sectionTitleLinked = LocalizationUtility::translate('result.unnamedSection', 'IndexedSearch') . ':';
416 } else {
417 $onclick = 'document.forms[\'tx_indexedsearch\'][\'tx_indexedsearch_pi2[search][_sections]\'].value=' . GeneralUtility::quoteJSvalue($theRLid) . ';document.forms[\'tx_indexedsearch\'].submit();return false;';
418 $sectionTitleLinked = '<a href="#" onclick="' . htmlspecialchars($onclick) . '">' . $sectionName . ':</a>';
419 }
420 $resultRowsCount = count($resultRows);
421 $this->resultSections[$id] = [$sectionName, $resultRowsCount];
422 // Add section header
423 $finalResultRows[] = [
424 'isSectionHeader' => true,
425 'numResultRows' => $resultRowsCount,
426 'sectionId' => $id,
427 'sectionTitle' => $sectionTitleLinked
428 ];
429 // Render result rows
430 foreach ($resultRows as $row) {
431 $finalResultRows[] = $this->compileSingleResultRow($row);
432 }
433 }
434 } else {
435 // flat mode or no sections at all
436 foreach ($resultRows as $row) {
437 $finalResultRows[] = $this->compileSingleResultRow($row);
438 }
439 }
440 return $finalResultRows;
441 }
442
443 /**
444 * This prints a single result row, including a recursive call for subrows.
445 *
446 * @param array $row Search result row
447 * @param int $headerOnly 1=Display only header (for sub-rows!), 2=nothing at all
448 * @return array the result row with additional information
449 */
450 protected function compileSingleResultRow($row, $headerOnly = 0)
451 {
452 $specRowConf = $this->getSpecialConfigurationForResultRow($row);
453 $resultData = $row;
454 $resultData['headerOnly'] = $headerOnly;
455 $resultData['CSSsuffix'] = $specRowConf['CSSsuffix'] ? '-' . $specRowConf['CSSsuffix'] : '';
456 if ($this->multiplePagesType($row['item_type'])) {
457 $dat = unserialize($row['cHashParams']);
458 $pp = explode('-', $dat['key']);
459 if ($pp[0] != $pp[1]) {
460 $resultData['titleaddition'] = ', ' . LocalizationUtility::translate('result.page', 'IndexedSearch') . ' ' . $dat['key'];
461 } else {
462 $resultData['titleaddition'] = ', ' . LocalizationUtility::translate('result.pages', 'IndexedSearch') . ' ' . $pp[0];
463 }
464 }
465 $title = $resultData['item_title'] . $resultData['titleaddition'];
466 $title = GeneralUtility::fixed_lgd_cs($title, $this->settings['results.']['titleCropAfter'], $this->settings['results.']['titleCropSignifier']);
467 // If external media, link to the media-file instead.
468 if ($row['item_type']) {
469 if ($row['show_resume']) {
470 // Can link directly.
471 $targetAttribute = '';
472 if ($GLOBALS['TSFE']->config['config']['fileTarget']) {
473 $targetAttribute = ' target="' . htmlspecialchars($GLOBALS['TSFE']->config['config']['fileTarget']) . '"';
474 }
475 $title = '<a href="' . htmlspecialchars($row['data_filename']) . '"' . $targetAttribute . '>' . htmlspecialchars($title) . '</a>';
476 } else {
477 // Suspicious, so linking to page instead...
478 $copiedRow = $row;
479 unset($copiedRow['cHashParams']);
480 $title = $this->linkPageATagWrap(
481 htmlspecialchars($title),
482 $this->linkPage($row['page_id'], $copiedRow)
483 );
484 }
485 } else {
486 // Else the page:
487 // Prepare search words for markup in content:
488 $markUpSwParams = [];
489 if ($this->settings['forwardSearchWordsInResultLink']['_typoScriptNodeValue']) {
490 if ($this->settings['forwardSearchWordsInResultLink']['no_cache']) {
491 $markUpSwParams = ['no_cache' => 1];
492 }
493 foreach ($this->searchWords as $d) {
494 $markUpSwParams['sword_list'][] = $d['sword'];
495 }
496 }
497 $title = $this->linkPageATagWrap(
498 htmlspecialchars($title),
499 $this->linkPage($row['data_page_id'], $row, $markUpSwParams)
500 );
501 }
502 $resultData['title'] = $title;
503 $resultData['icon'] = $this->makeItemTypeIcon($row['item_type'], '', $specRowConf);
504 $resultData['rating'] = $this->makeRating($row);
505 $resultData['description'] = $this->makeDescription(
506 $row,
507 (bool)!($this->searchData['extResume'] && !$headerOnly),
508 $this->settings['results.']['summaryCropAfter']
509 );
510 $resultData['language'] = $this->makeLanguageIndication($row);
511 $resultData['size'] = GeneralUtility::formatSize($row['item_size']);
512 $resultData['created'] = $row['item_crdate'];
513 $resultData['modified'] = $row['item_mtime'];
514 $pI = parse_url($row['data_filename']);
515 if ($pI['scheme']) {
516 $targetAttribute = '';
517 if ($GLOBALS['TSFE']->config['config']['fileTarget']) {
518 $targetAttribute = ' target="' . htmlspecialchars($GLOBALS['TSFE']->config['config']['fileTarget']) . '"';
519 }
520 $resultData['pathTitle'] = $row['data_filename'];
521 $resultData['pathUri'] = $row['data_filename'];
522 $resultData['path'] = '<a href="' . htmlspecialchars($row['data_filename']) . '"' . $targetAttribute . '>' . htmlspecialchars($row['data_filename']) . '</a>';
523 } else {
524 $pathId = $row['data_page_id'] ?: $row['page_id'];
525 $pathMP = $row['data_page_id'] ? $row['data_page_mp'] : '';
526 $pathStr = $this->getPathFromPageId($pathId, $pathMP);
527 $pathLinkData = $this->linkPage(
528 $pathId,
529 [
530 'cHashParams' => $row['cHashParams'],
531 'data_page_type' => $row['data_page_type'],
532 'data_page_mp' => $pathMP,
533 'sys_language_uid' => $row['sys_language_uid']
534 ]
535 );
536
537 $resultData['pathTitle'] = $pathStr;
538 $resultData['pathUri'] = $pathLinkData['uri'];
539 $resultData['path'] = $this->linkPageATagWrap($pathStr, $pathLinkData);
540
541 // check if the access is restricted
542 if (is_array($this->requiredFrontendUsergroups[$pathId]) && !empty($this->requiredFrontendUsergroups[$pathId])) {
543 $lockedIcon = GeneralUtility::getFileAbsFileName('EXT:indexed_search/Resources/Public/Icons/FileTypes/locked.gif');
544 $lockedIcon = PathUtility::getAbsoluteWebPath($lockedIcon);
545 $resultData['access'] = '<img src="' . htmlspecialchars($lockedIcon) . '"'
546 . ' width="12" height="15" vspace="5" title="'
547 . sprintf(LocalizationUtility::translate('result.memberGroups', 'IndexedSearch'), implode(',', array_unique($this->requiredFrontendUsergroups[$pathId])))
548 . '" alt="" />';
549 }
550 }
551 // If there are subrows (eg. subpages in a PDF-file or if a duplicate page
552 // is selected due to user-login (phash_grouping))
553 if (is_array($row['_sub'])) {
554 $resultData['subresults'] = [];
555 if ($this->multiplePagesType($row['item_type'])) {
556 $resultData['subresults']['header'] = LocalizationUtility::translate('result.otherMatching', 'IndexedSearch');
557 foreach ($row['_sub'] as $subRow) {
558 $resultData['subresults']['items'][] = $this->compileSingleResultRow($subRow, 1);
559 }
560 } else {
561 $resultData['subresults']['header'] = LocalizationUtility::translate('result.otherMatching', 'IndexedSearch');
562 $resultData['subresults']['info'] = LocalizationUtility::translate('result.otherPageAsWell', 'IndexedSearch');
563 }
564 }
565 return $resultData;
566 }
567
568 /**
569 * Returns configuration from TypoScript for result row based
570 * on ID / location in page tree!
571 *
572 * @param array $row Result row
573 * @return array Configuration array
574 */
575 protected function getSpecialConfigurationForResultRow($row)
576 {
577 $pathId = $row['data_page_id'] ?: $row['page_id'];
578 $pathMP = $row['data_page_id'] ? $row['data_page_mp'] : '';
579 $rl = $GLOBALS['TSFE']->sys_page->getRootLine($pathId, $pathMP);
580 $specConf = $this->settings['specialConfiguration']['0'];
581 if (is_array($rl)) {
582 foreach ($rl as $dat) {
583 if (is_array($this->settings['specialConfiguration'][$dat['uid']])) {
584 $specConf = $this->settings['specialConfiguration'][$dat['uid']];
585 $specConf['_pid'] = $dat['uid'];
586 break;
587 }
588 }
589 }
590 return $specConf;
591 }
592
593 /**
594 * Return the rating-HTML code for the result row. This makes use of the $this->firstRow
595 *
596 * @param array $row Result row array
597 * @return string String showing ranking value
598 * @todo can this be a ViewHelper?
599 */
600 protected function makeRating($row)
601 {
602 switch ((string)$this->searchData['sortOrder']) {
603 case 'rank_count':
604 return $row['order_val'] . ' ' . LocalizationUtility::translate('result.ratingMatches', 'IndexedSearch');
605 break;
606 case 'rank_first':
607 return ceil(MathUtility::forceIntegerInRange((255 - $row['order_val']), 1, 255) / 255 * 100) . '%';
608 break;
609 case 'rank_flag':
610 if ($this->firstRow['order_val2']) {
611 // (3 MSB bit, 224 is highest value of order_val1 currently)
612 $base = $row['order_val1'] * 256;
613 // 15-3 MSB = 12
614 $freqNumber = $row['order_val2'] / $this->firstRow['order_val2'] * pow(2, 12);
615 $total = MathUtility::forceIntegerInRange($base + $freqNumber, 0, 32767);
616 return ceil(log($total) / log(32767) * 100) . '%';
617 }
618 break;
619 case 'rank_freq':
620 $max = 10000;
621 $total = MathUtility::forceIntegerInRange($row['order_val'], 0, $max);
622 return ceil(log($total) / log($max) * 100) . '%';
623 break;
624 case 'crdate':
625 return $GLOBALS['TSFE']->cObj->calcAge($GLOBALS['EXEC_TIME'] - $row['item_crdate'], 0);
626 break;
627 case 'mtime':
628 return $GLOBALS['TSFE']->cObj->calcAge($GLOBALS['EXEC_TIME'] - $row['item_mtime'], 0);
629 break;
630 default:
631 return ' ';
632 }
633 }
634
635 /**
636 * Returns the HTML code for language indication.
637 *
638 * @param array $row Result row
639 * @return string HTML code for result row.
640 */
641 protected function makeLanguageIndication($row)
642 {
643 $output = '&nbsp;';
644 // If search result is a TYPO3 page:
645 if ((string)$row['item_type'] === '0') {
646 // If TypoScript is used to render the flag:
647 if (is_array($this->settings['flagRendering'])) {
648 /** @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObj */
649 $cObj = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
650 $cObj->setCurrentVal($row['sys_language_uid']);
651 $typoScriptArray = $this->typoScriptService->convertPlainArrayToTypoScriptArray($this->settings['flagRendering']);
652 $output = $cObj->cObjGetSingle($this->settings['flagRendering']['_typoScriptNodeValue'], $typoScriptArray);
653 }
654 }
655 return $output;
656 }
657
658 /**
659 * Return icon for file extension
660 *
661 * @param string $imageType File extension / item type
662 * @param string $alt Title attribute value in icon.
663 * @param array $specRowConf TypoScript configuration specifically for search result.
664 * @return string <img> tag for icon
665 */
666 public function makeItemTypeIcon($imageType, $alt, $specRowConf)
667 {
668 // Build compound key if item type is 0, iconRendering is not used
669 // and specialConfiguration.[pid].pageIcon was set in TS
670 if ($imageType === '0' && $specRowConf['_pid'] && is_array($specRowConf['pageIcon']) && !is_array($this->settings['iconRendering'])) {
671 $imageType .= ':' . $specRowConf['_pid'];
672 }
673 if (!isset($this->iconFileNameCache[$imageType])) {
674 $this->iconFileNameCache[$imageType] = '';
675 // If TypoScript is used to render the icon:
676 if (is_array($this->settings['iconRendering'])) {
677 /** @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObj */
678 $cObj = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
679 $cObj->setCurrentVal($imageType);
680 $typoScriptArray = $this->typoScriptService->convertPlainArrayToTypoScriptArray($this->settings['iconRendering']);
681 $this->iconFileNameCache[$imageType] = $cObj->cObjGetSingle($this->settings['iconRendering']['_typoScriptNodeValue'], $typoScriptArray);
682 } else {
683 // Default creation / finding of icon:
684 $icon = '';
685 if ($imageType === '0' || substr($imageType, 0, 2) === '0:') {
686 if (is_array($specRowConf['pageIcon'])) {
687 $this->iconFileNameCache[$imageType] = $GLOBALS['TSFE']->cObj->cObjGetSingle('IMAGE', $specRowConf['pageIcon']);
688 } else {
689 $icon = 'EXT:indexed_search/Resources/Public/Icons/FileTypes/pages.gif';
690 }
691 } elseif ($this->externalParsers[$imageType]) {
692 $icon = $this->externalParsers[$imageType]->getIcon($imageType);
693 }
694 if ($icon) {
695 $fullPath = GeneralUtility::getFileAbsFileName($icon);
696 if ($fullPath) {
697 $info = @getimagesize($fullPath);
698 $iconPath = \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($fullPath);
699 $this->iconFileNameCache[$imageType] = is_array($info) ? '<img src="' . $iconPath . '" ' . $info[3] . ' title="' . htmlspecialchars($alt) . '" alt="" />' : '';
700 }
701 }
702 }
703 }
704 return $this->iconFileNameCache[$imageType];
705 }
706
707 /**
708 * Returns the resume for the search-result.
709 *
710 * @param array $row Search result row
711 * @param bool $noMarkup If noMarkup is FALSE, then the index_fulltext table is used to select the content of the page, split it with regex to display the search words in the text.
712 * @param int $length String length
713 * @return string HTML string
714 * @todo overwork this
715 */
716 protected function makeDescription($row, $noMarkup = false, $length = 180)
717 {
718 if ($row['show_resume']) {
719 if (!$noMarkup) {
720 $markedSW = '';
721 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_fulltext');
722 $ftdrow = $queryBuilder
723 ->select('*')
724 ->from('index_fulltext')
725 ->where(
726 $queryBuilder->expr()->eq(
727 'phash',
728 $queryBuilder->createNamedParameter($row['phash'], \PDO::PARAM_INT)
729 )
730 )
731 ->execute()
732 ->fetch();
733 if ($ftdrow !== false) {
734 // Cut HTTP references after some length
735 $content = preg_replace('/(http:\\/\\/[^ ]{' . $this->settings['results.']['hrefInSummaryCropAfter'] . '})([^ ]+)/i', '$1...', $ftdrow['fulltextdata']);
736 $markedSW = $this->markupSWpartsOfString($content);
737 }
738 }
739 if (!trim($markedSW)) {
740 $outputStr = GeneralUtility::fixed_lgd_cs($row['item_description'], $length, $this->settings['results.']['summaryCropSignifier']);
741 $outputStr = htmlspecialchars($outputStr);
742 }
743 $output = $outputStr ?: $markedSW;
744 } else {
745 $output = '<span class="noResume">' . LocalizationUtility::translate('result.noResume', 'IndexedSearch') . '</span>';
746 }
747 return $output;
748 }
749
750 /**
751 * Marks up the search words from $this->searchWords in the $str with a color.
752 *
753 * @param string $str Text in which to find and mark up search words. This text is assumed to be UTF-8 like the search words internally is.
754 * @return string Processed content
755 */
756 protected function markupSWpartsOfString($str)
757 {
758 $htmlParser = GeneralUtility::makeInstance(HtmlParser::class);
759 // Init:
760 $str = str_replace('&nbsp;', ' ', $htmlParser->bidir_htmlspecialchars($str, -1));
761 $str = preg_replace('/\\s\\s+/', ' ', $str);
762 $swForReg = [];
763 // Prepare search words for regex:
764 foreach ($this->searchWords as $d) {
765 $swForReg[] = preg_quote($d['sword'], '/');
766 }
767 $regExString = '(' . implode('|', $swForReg) . ')';
768 // Split and combine:
769 $parts = preg_split('/' . $regExString . '/i', ' ' . $str . ' ', 20000, PREG_SPLIT_DELIM_CAPTURE);
770 // Constants:
771 $summaryMax = $this->settings['results.']['markupSW_summaryMax'];
772 $postPreLgd = $this->settings['results.']['markupSW_postPreLgd'];
773 $postPreLgd_offset = $this->settings['results.']['markupSW_postPreLgd_offset'];
774 $divider = $this->settings['results.']['markupSW_divider'];
775 $occurencies = (count($parts) - 1) / 2;
776 if ($occurencies) {
777 $postPreLgd = MathUtility::forceIntegerInRange($summaryMax / $occurencies, $postPreLgd, $summaryMax / 2);
778 }
779 // Variable:
780 $summaryLgd = 0;
781 $output = [];
782 // Shorten in-between strings:
783 foreach ($parts as $k => $strP) {
784 if ($k % 2 == 0) {
785 // Find length of the summary part:
786 $strLen = mb_strlen($parts[$k], 'utf-8');
787 $output[$k] = $parts[$k];
788 // Possibly shorten string:
789 if (!$k) {
790 // First entry at all (only cropped on the frontside)
791 if ($strLen > $postPreLgd) {
792 $output[$k] = $divider . preg_replace('/^[^[:space:]]+[[:space:]]/', '', GeneralUtility::fixed_lgd_cs($parts[$k], -($postPreLgd - $postPreLgd_offset)));
793 }
794 } elseif ($summaryLgd > $summaryMax || !isset($parts[$k + 1])) {
795 // In case summary length is exceed OR if there are no more entries at all:
796 if ($strLen > $postPreLgd) {
797 $output[$k] = preg_replace('/[[:space:]][^[:space:]]+$/', '', GeneralUtility::fixed_lgd_cs($parts[$k], ($postPreLgd - $postPreLgd_offset))) . $divider;
798 }
799 } else {
800 if ($strLen > $postPreLgd * 2) {
801 $output[$k] = preg_replace('/[[:space:]][^[:space:]]+$/', '', GeneralUtility::fixed_lgd_cs($parts[$k], ($postPreLgd - $postPreLgd_offset))) . $divider . preg_replace('/^[^[:space:]]+[[:space:]]/', '', GeneralUtility::fixed_lgd_cs($parts[$k], -($postPreLgd - $postPreLgd_offset)));
802 }
803 }
804 $summaryLgd += mb_strlen($output[$k], 'utf-8');
805 // Protect output:
806 $output[$k] = htmlspecialchars($output[$k]);
807 // If summary lgd is exceed, break the process:
808 if ($summaryLgd > $summaryMax) {
809 break;
810 }
811 } else {
812 $summaryLgd += mb_strlen($strP, 'utf-8');
813 $output[$k] = '<strong class="tx-indexedsearch-redMarkup">' . htmlspecialchars($parts[$k]) . '</strong>';
814 }
815 }
816 // Return result:
817 return implode('', $output);
818 }
819
820 /**
821 * Write statistics information to database for the search operation if there was at least one search word.
822 *
823 * @param array $searchParams search params
824 * @param array $searchWords Search Word array
825 * @param int $count Number of hits
826 * @param array $pt Milliseconds the search took (start time DB query + end time DB query + end time to compile results)
827 */
828 protected function writeSearchStat($searchParams, $searchWords, $count, $pt)
829 {
830 $searchWord = $this->getSword();
831 if (empty($searchWord) && empty($searchWords)) {
832 return;
833 }
834
835 $insertFields = [
836 'searchstring' => $searchWord,
837 'searchoptions' => serialize([$searchParams, $searchWords, $pt]),
838 'feuser_id' => (int)$GLOBALS['TSFE']->fe_user->user['uid'],
839 // cookie as set or retrieved. If people has cookies disabled this will vary all the time
840 'cookie' => $GLOBALS['TSFE']->fe_user->id,
841 // Remote IP address
842 'IP' => GeneralUtility::getIndpEnv('REMOTE_ADDR'),
843 // Number of hits on the search
844 'hits' => (int)$count,
845 // Time stamp
846 'tstamp' => $GLOBALS['EXEC_TIME']
847 ];
848 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('index_search_stat');
849 $connection->insert(
850 'index_stat_search',
851 $insertFields,
852 ['searchoptions' => Connection::PARAM_LOB]
853 );
854 $newId = $connection->lastInsertId('index_stat_search');
855 if ($newId) {
856 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('index_stat_word');
857 foreach ($searchWords as $val) {
858 $insertFields = [
859 'word' => $val['sword'],
860 'index_stat_search_id' => $newId,
861 // Time stamp
862 'tstamp' => $GLOBALS['EXEC_TIME'],
863 // search page id for indexed search stats
864 'pageid' => $GLOBALS['TSFE']->id
865 ];
866 $connection->insert('index_stat_word', $insertFields);
867 }
868 }
869 }
870
871 /**
872 * Splits the search word input into an array where each word is represented by an array with key "sword"
873 * holding the search word and key "oper" holding the SQL operator (eg. AND, OR)
874 *
875 * Only words with 2 or more characters are accepted
876 * Max 200 chars total
877 * Space is used to split words, "" can be used search for a whole string
878 * AND, OR and NOT are prefix words, overruling the default operator
879 * +/|/- equals AND, OR and NOT as operators.
880 * All search words are converted to lowercase.
881 *
882 * $defOp is the default operator. 1=OR, 0=AND
883 *
884 * @param bool $defaultOperator If TRUE, the default operator will be OR, not AND
885 * @return array Search words if any found
886 */
887 protected function getSearchWords($defaultOperator)
888 {
889 // Shorten search-word string to max 200 bytes (does NOT take multibyte charsets into account - but never mind,
890 // shortening the string here is only a run-away feature!)
891 $searchWords = substr($this->getSword(), 0, 200);
892 // Convert to UTF-8 + conv. entities (was also converted during indexing!)
893 if ($GLOBALS['TSFE']->metaCharset && $GLOBALS['TSFE']->metaCharset !== 'utf-8') {
894 $searchWords = mb_convert_encoding($searchWords, 'utf-8', $GLOBALS['TSFE']->metaCharset);
895 $searchWords = html_entity_decode($searchWords);
896 }
897 $sWordArray = false;
898 if ($hookObj = $this->hookRequest('getSearchWords')) {
899 $sWordArray = $hookObj->getSearchWords_splitSWords($searchWords, $defaultOperator);
900 } else {
901 // sentence
902 if ($this->searchData['searchType'] == 20) {
903 $sWordArray = [
904 [
905 'sword' => trim($searchWords),
906 'oper' => 'AND'
907 ]
908 ];
909 } else {
910 // case-sensitive. Defines the words, which will be
911 // operators between words
912 $operatorTranslateTable = [
913 ['+', 'AND'],
914 ['|', 'OR'],
915 ['-', 'AND NOT'],
916 // Add operators for various languages
917 // Converts the operators to lowercase
918 [mb_strtolower(LocalizationUtility::translate('localizedOperandAnd', 'IndexedSearch'), 'utf-8'), 'AND'],
919 [mb_strtolower(LocalizationUtility::translate('localizedOperandOr', 'IndexedSearch'), 'utf-8'), 'OR'],
920 [mb_strtolower(LocalizationUtility::translate('localizedOperandNot', 'IndexedSearch'), 'utf-8'), 'AND NOT']
921 ];
922 $swordArray = \TYPO3\CMS\IndexedSearch\Utility\IndexedSearchUtility::getExplodedSearchString($searchWords, $defaultOperator == 1 ? 'OR' : 'AND', $operatorTranslateTable);
923 if (is_array($swordArray)) {
924 $sWordArray = $this->procSearchWordsByLexer($swordArray);
925 }
926 }
927 }
928 return $sWordArray;
929 }
930
931 /**
932 * Post-process the search word array so it will match the words that was indexed (including case-folding if any)
933 * If any words are splitted into multiple words (eg. CJK will be!) the operator of the main word will remain.
934 *
935 * @param array $searchWords Search word array
936 * @return array Search word array, processed through lexer
937 */
938 protected function procSearchWordsByLexer($searchWords)
939 {
940 $newSearchWords = [];
941 // Init lexer (used to post-processing of search words)
942 $lexerObjectClassName = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['lexer'] ?: \TYPO3\CMS\IndexedSearch\Lexer::class;
943 $this->lexerObj = GeneralUtility::makeInstance($lexerObjectClassName);
944 // Traverse the search word array
945 foreach ($searchWords as $wordDef) {
946 // No space in word (otherwise it might be a sentense in quotes like "there is").
947 if (strpos($wordDef['sword'], ' ') === false) {
948 // Split the search word by lexer:
949 $res = $this->lexerObj->split2Words($wordDef['sword']);
950 // Traverse lexer result and add all words again:
951 foreach ($res as $word) {
952 $newSearchWords[] = [
953 'sword' => $word,
954 'oper' => $wordDef['oper']
955 ];
956 }
957 } else {
958 $newSearchWords[] = $wordDef;
959 }
960 }
961 return $newSearchWords;
962 }
963
964 /**
965 * Sort options about the search form
966 *
967 * @param array $search The search data / params
968 * @ignorevalidation $search
969 */
970 public function formAction($search = [])
971 {
972 $searchData = $this->initialize($search);
973 // Adding search field value
974 $this->view->assign('sword', $this->getSword());
975 // Extended search
976 if (!empty($searchData['extendedSearch'])) {
977 // "Search for"
978 $allSearchTypes = $this->getAllAvailableSearchTypeOptions();
979 $this->view->assign('allSearchTypes', $allSearchTypes);
980 $allDefaultOperands = $this->getAllAvailableOperandsOptions();
981 $this->view->assign('allDefaultOperands', $allDefaultOperands);
982 $showTypeSearch = !empty($allSearchTypes) || !empty($allDefaultOperands);
983 $this->view->assign('showTypeSearch', $showTypeSearch);
984 // "Search in"
985 $allMediaTypes = $this->getAllAvailableMediaTypesOptions();
986 $this->view->assign('allMediaTypes', $allMediaTypes);
987 $allLanguageUids = $this->getAllAvailableLanguageOptions();
988 $this->view->assign('allLanguageUids', $allLanguageUids);
989 $showMediaAndLanguageSearch = !empty($allMediaTypes) || !empty($allLanguageUids);
990 $this->view->assign('showMediaAndLanguageSearch', $showMediaAndLanguageSearch);
991 // Sections
992 $allSections = $this->getAllAvailableSectionsOptions();
993 $this->view->assign('allSections', $allSections);
994 // Free Indexing Configurations
995 $allIndexConfigurations = $this->getAllAvailableIndexConfigurationsOptions();
996 $this->view->assign('allIndexConfigurations', $allIndexConfigurations);
997 // Sorting
998 $allSortOrders = $this->getAllAvailableSortOrderOptions();
999 $this->view->assign('allSortOrders', $allSortOrders);
1000 $allSortDescendings = $this->getAllAvailableSortDescendingOptions();
1001 $this->view->assign('allSortDescendings', $allSortDescendings);
1002 $showSortOrders = !empty($allSortOrders) || !empty($allSortDescendings);
1003 $this->view->assign('showSortOrders', $showSortOrders);
1004 // Limits
1005 $allNumberOfResults = $this->getAllAvailableNumberOfResultsOptions();
1006 $this->view->assign('allNumberOfResults', $allNumberOfResults);
1007 $allGroups = $this->getAllAvailableGroupOptions();
1008 $this->view->assign('allGroups', $allGroups);
1009 }
1010 $this->view->assign('searchParams', $searchData);
1011 }
1012
1013 /**
1014 * TypoScript was not loaded
1015 */
1016 public function noTypoScriptAction()
1017 {
1018 }
1019
1020 /****************************************
1021 * building together the available options for every dropdown
1022 ***************************************/
1023 /**
1024 * get the values for the "type" selector
1025 *
1026 * @return array Associative array with options
1027 */
1028 protected function getAllAvailableSearchTypeOptions()
1029 {
1030 $allOptions = [];
1031 $types = [0, 1, 2, 3, 10, 20];
1032 $blindSettings = $this->settings['blind'];
1033 if (!$blindSettings['searchType']) {
1034 foreach ($types as $typeNum) {
1035 $allOptions[$typeNum] = LocalizationUtility::translate('searchTypes.' . $typeNum, 'IndexedSearch');
1036 }
1037 }
1038 // Remove this option if metaphone search is disabled)
1039 if (!$this->enableMetaphoneSearch) {
1040 unset($allOptions[10]);
1041 }
1042 // disable single entries by TypoScript
1043 $allOptions = $this->removeOptionsFromOptionList($allOptions, $blindSettings['searchType']);
1044 return $allOptions;
1045 }
1046
1047 /**
1048 * get the values for the "defaultOperand" selector
1049 *
1050 * @return array Associative array with options
1051 */
1052 protected function getAllAvailableOperandsOptions()
1053 {
1054 $allOptions = [];
1055 $blindSettings = $this->settings['blind'];
1056 if (!$blindSettings['defaultOperand']) {
1057 $allOptions = [
1058 0 => LocalizationUtility::translate('defaultOperands.0', 'IndexedSearch'),
1059 1 => LocalizationUtility::translate('defaultOperands.1', 'IndexedSearch')
1060 ];
1061 }
1062 // disable single entries by TypoScript
1063 $allOptions = $this->removeOptionsFromOptionList($allOptions, $blindSettings['defaultOperand']);
1064 return $allOptions;
1065 }
1066
1067 /**
1068 * get the values for the "media type" selector
1069 *
1070 * @return array Associative array with options
1071 */
1072 protected function getAllAvailableMediaTypesOptions()
1073 {
1074 $allOptions = [];
1075 $mediaTypes = [-1, 0, -2];
1076 $blindSettings = $this->settings['blind'];
1077 if (!$blindSettings['mediaType']) {
1078 foreach ($mediaTypes as $mediaType) {
1079 $allOptions[$mediaType] = LocalizationUtility::translate('mediaTypes.' . $mediaType, 'IndexedSearch');
1080 }
1081 // Add media to search in:
1082 $additionalMedia = trim($this->settings['mediaList']);
1083 if ($additionalMedia !== '') {
1084 $additionalMedia = GeneralUtility::trimExplode(',', $additionalMedia, true);
1085 } else {
1086 $additionalMedia = [];
1087 }
1088 foreach ($this->externalParsers as $extension => $obj) {
1089 // Skip unwanted extensions
1090 if (!empty($additionalMedia) && !in_array($extension, $additionalMedia)) {
1091 continue;
1092 }
1093 if ($name = $obj->searchTypeMediaTitle($extension)) {
1094 $translatedName = LocalizationUtility::translate('mediaTypes.' . $extension, 'IndexedSearch');
1095 $allOptions[$extension] = $translatedName ?: $name;
1096 }
1097 }
1098 }
1099 // disable single entries by TypoScript
1100 $allOptions = $this->removeOptionsFromOptionList($allOptions, $blindSettings['mediaType']);
1101 return $allOptions;
1102 }
1103
1104 /**
1105 * get the values for the "language" selector
1106 *
1107 * @return array Associative array with options
1108 */
1109 protected function getAllAvailableLanguageOptions()
1110 {
1111 $allOptions = [
1112 '-1' => LocalizationUtility::translate('languageUids.-1', 'IndexedSearch'),
1113 '0' => LocalizationUtility::translate('languageUids.0', 'IndexedSearch')
1114 ];
1115 $blindSettings = $this->settings['blind'];
1116 if (!$blindSettings['languageUid']) {
1117 // Add search languages
1118 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
1119 ->getQueryBuilderForTable('sys_language');
1120 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
1121 $result = $queryBuilder
1122 ->select('uid', 'title')
1123 ->from('sys_language')
1124 ->execute();
1125
1126 while ($lang = $result->fetch()) {
1127 $allOptions[$lang['uid']] = $lang['title'];
1128 }
1129 // disable single entries by TypoScript
1130 $allOptions = $this->removeOptionsFromOptionList($allOptions, $blindSettings['languageUid']);
1131 } else {
1132 $allOptions = [];
1133 }
1134 return $allOptions;
1135 }
1136
1137 /**
1138 * get the values for the "section" selector
1139 * Here values like "rl1_" and "rl2_" + a rootlevel 1/2 id can be added
1140 * to perform searches in rootlevel 1+2 specifically. The id-values can even
1141 * be commaseparated. Eg. "rl1_1,2" would search for stuff inside pages on
1142 * menu-level 1 which has the uid's 1 and 2.
1143 *
1144 * @return array Associative array with options
1145 */
1146 protected function getAllAvailableSectionsOptions()
1147 {
1148 $allOptions = [];
1149 $sections = [0, -1, -2, -3];
1150 $blindSettings = $this->settings['blind'];
1151 if (!$blindSettings['sections']) {
1152 foreach ($sections as $section) {
1153 $allOptions[$section] = LocalizationUtility::translate('sections.' . $section, 'IndexedSearch');
1154 }
1155 }
1156 // Creating levels for section menu:
1157 // This selects the first and secondary menus for the "sections" selector - so we can search in sections and sub sections.
1158 if ($this->settings['displayLevel1Sections']) {
1159 $firstLevelMenu = $this->getMenuOfPages($this->searchRootPageIdList);
1160 $labelLevel1 = LocalizationUtility::translate('sections.rootLevel1', 'IndexedSearch');
1161 $labelLevel2 = LocalizationUtility::translate('sections.rootLevel2', 'IndexedSearch');
1162 foreach ($firstLevelMenu as $firstLevelKey => $menuItem) {
1163 if (!$menuItem['nav_hide']) {
1164 $allOptions['rl1_' . $menuItem['uid']] = trim($labelLevel1 . ' ' . $menuItem['title']);
1165 if ($this->settings['displayLevel2Sections']) {
1166 $secondLevelMenu = $this->getMenuOfPages($menuItem['uid']);
1167 foreach ($secondLevelMenu as $secondLevelKey => $menuItemLevel2) {
1168 if (!$menuItemLevel2['nav_hide']) {
1169 $allOptions['rl2_' . $menuItemLevel2['uid']] = trim($labelLevel2 . ' ' . $menuItemLevel2['title']);
1170 } else {
1171 unset($secondLevelMenu[$secondLevelKey]);
1172 }
1173 }
1174 $allOptions['rl2_' . implode(',', array_keys($secondLevelMenu))] = LocalizationUtility::translate('sections.rootLevel2All', 'IndexedSearch');
1175 }
1176 } else {
1177 unset($firstLevelMenu[$firstLevelKey]);
1178 }
1179 }
1180 $allOptions['rl1_' . implode(',', array_keys($firstLevelMenu))] = LocalizationUtility::translate('sections.rootLevel1All', 'IndexedSearch');
1181 }
1182 // disable single entries by TypoScript
1183 $allOptions = $this->removeOptionsFromOptionList($allOptions, $blindSettings['sections']);
1184 return $allOptions;
1185 }
1186
1187 /**
1188 * get the values for the "freeIndexUid" selector
1189 *
1190 * @return array Associative array with options
1191 */
1192 protected function getAllAvailableIndexConfigurationsOptions()
1193 {
1194 $allOptions = [
1195 '-1' => LocalizationUtility::translate('indexingConfigurations.-1', 'IndexedSearch'),
1196 '-2' => LocalizationUtility::translate('indexingConfigurations.-2', 'IndexedSearch'),
1197 '0' => LocalizationUtility::translate('indexingConfigurations.0', 'IndexedSearch')
1198 ];
1199 $blindSettings = $this->settings['blind'];
1200 if (!$blindSettings['indexingConfigurations']) {
1201 // add an additional index configuration
1202 if ($this->settings['defaultFreeIndexUidList']) {
1203 $uidList = GeneralUtility::intExplode(',', $this->settings['defaultFreeIndexUidList']);
1204 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
1205 ->getQueryBuilderForTable('index_config');
1206 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
1207 $result = $queryBuilder
1208 ->select('uid', 'title')
1209 ->from('index_config')
1210 ->where(
1211 $queryBuilder->expr()->in(
1212 'uid',
1213 $queryBuilder->createNamedParameter($uidList, Connection::PARAM_INT_ARRAY)
1214 )
1215 )
1216 ->execute();
1217
1218 while ($row = $result->fetch()) {
1219 $allOptions[$row['uid']]= $row['title'];
1220 }
1221 }
1222 // disable single entries by TypoScript
1223 $allOptions = $this->removeOptionsFromOptionList($allOptions, $blindSettings['indexingConfigurations']);
1224 } else {
1225 $allOptions = [];
1226 }
1227 return $allOptions;
1228 }
1229
1230 /**
1231 * get the values for the "section" selector
1232 * Here values like "rl1_" and "rl2_" + a rootlevel 1/2 id can be added
1233 * to perform searches in rootlevel 1+2 specifically. The id-values can even
1234 * be commaseparated. Eg. "rl1_1,2" would search for stuff inside pages on
1235 * menu-level 1 which has the uid's 1 and 2.
1236 *
1237 * @return array Associative array with options
1238 */
1239 protected function getAllAvailableSortOrderOptions()
1240 {
1241 $allOptions = [];
1242 $sortOrders = ['rank_flag', 'rank_freq', 'rank_first', 'rank_count', 'mtime', 'title', 'crdate'];
1243 $blindSettings = $this->settings['blind'];
1244 if (!$blindSettings['sortOrder']) {
1245 foreach ($sortOrders as $order) {
1246 $allOptions[$order] = LocalizationUtility::translate('sortOrders.' . $order, 'IndexedSearch');
1247 }
1248 }
1249 // disable single entries by TypoScript
1250 $allOptions = $this->removeOptionsFromOptionList($allOptions, $blindSettings['sortOrder.']);
1251 return $allOptions;
1252 }
1253
1254 /**
1255 * get the values for the "group" selector
1256 *
1257 * @return array Associative array with options
1258 */
1259 protected function getAllAvailableGroupOptions()
1260 {
1261 $allOptions = [];
1262 $blindSettings = $this->settings['blind'];
1263 if (!$blindSettings['groupBy']) {
1264 $allOptions = [
1265 'sections' => LocalizationUtility::translate('groupBy.sections', 'IndexedSearch'),
1266 'flat' => LocalizationUtility::translate('groupBy.flat', 'IndexedSearch')
1267 ];
1268 }
1269 // disable single entries by TypoScript
1270 $allOptions = $this->removeOptionsFromOptionList($allOptions, $blindSettings['groupBy.']);
1271 return $allOptions;
1272 }
1273
1274 /**
1275 * get the values for the "sortDescending" selector
1276 *
1277 * @return array Associative array with options
1278 */
1279 protected function getAllAvailableSortDescendingOptions()
1280 {
1281 $allOptions = [];
1282 $blindSettings = $this->settings['blind'];
1283 if (!$blindSettings['descending']) {
1284 $allOptions = [
1285 0 => LocalizationUtility::translate('sortOrders.descending', 'IndexedSearch'),
1286 1 => LocalizationUtility::translate('sortOrders.ascending', 'IndexedSearch')
1287 ];
1288 }
1289 // disable single entries by TypoScript
1290 $allOptions = $this->removeOptionsFromOptionList($allOptions, $blindSettings['descending.']);
1291 return $allOptions;
1292 }
1293
1294 /**
1295 * get the values for the "results" selector
1296 *
1297 * @return array Associative array with options
1298 */
1299 protected function getAllAvailableNumberOfResultsOptions()
1300 {
1301 $allOptions = [];
1302 if (count($this->availableResultsNumbers) > 1) {
1303 $allOptions = array_combine($this->availableResultsNumbers, $this->availableResultsNumbers);
1304 }
1305 // disable single entries by TypoScript
1306 $allOptions = $this->removeOptionsFromOptionList($allOptions, $this->settings['blind']['numberOfResults']);
1307 return $allOptions;
1308 }
1309
1310 /**
1311 * removes blinding entries from the option list of a selector
1312 *
1313 * @param array $allOptions associative array containing all options
1314 * @param array $blindOptions associative array containing the optionkey as they key and the value = 1 if it should be removed
1315 * @return array Options from $allOptions with some options removed
1316 */
1317 protected function removeOptionsFromOptionList($allOptions, $blindOptions)
1318 {
1319 if (is_array($blindOptions)) {
1320 foreach ($blindOptions as $key => $val) {
1321 if ($val == 1) {
1322 unset($allOptions[$key]);
1323 }
1324 }
1325 }
1326 return $allOptions;
1327 }
1328
1329 /**
1330 * Links the $linkText to page $pageUid
1331 *
1332 * @param int $pageUid Page id
1333 * @param array $row Result row
1334 * @param array $markUpSwParams Additional parameters for marking up search words
1335 * @return array
1336 */
1337 protected function linkPage($pageUid, $row = [], $markUpSwParams = [])
1338 {
1339 $pageLanguage = $GLOBALS['TSFE']->sys_language_content;
1340 // Parameters for link
1341 $urlParameters = (array)unserialize($row['cHashParams']);
1342 // Add &type and &MP variable:
1343 if ($row['data_page_mp']) {
1344 $urlParameters['MP'] = $row['data_page_mp'];
1345 }
1346 if (($pageLanguage === 0 && $row['sys_language_uid'] > 0) || $pageLanguage > 0) {
1347 $urlParameters['L'] = (int)$row['sys_language_uid'];
1348 }
1349 // markup-GET vars:
1350 $urlParameters = array_merge($urlParameters, $markUpSwParams);
1351 // This will make sure that the path is retrieved if it hasn't been
1352 // already. Used only for the sake of the domain_record thing.
1353 if (!is_array($this->domainRecords[$pageUid])) {
1354 $this->getPathFromPageId($pageUid);
1355 }
1356
1357 return $this->preparePageLink($pageUid, $row, $urlParameters);
1358 }
1359
1360 /**
1361 * Return the menu of pages used for the selector.
1362 *
1363 * @param int $pageUid Page ID for which to return menu
1364 * @return array Menu items (for making the section selector box)
1365 */
1366 protected function getMenuOfPages($pageUid)
1367 {
1368 if ($this->settings['displayLevelxAllTypes']) {
1369 $menu = [];
1370 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
1371 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
1372 $result = $queryBuilder
1373 ->select('uid', 'title')
1374 ->from('pages')
1375 ->where(
1376 $queryBuilder->expr()->eq(
1377 'pid',
1378 $queryBuilder->createNamedParameter($pageUid, \PDO::PARAM_INT)
1379 )
1380 )
1381 ->orderBy('sorting')
1382 ->execute();
1383
1384 while ($row = $result->fetch()) {
1385 $menu[$row['uid']] = $GLOBALS['TSFE']->sys_page->getPageOverlay($row);
1386 }
1387 } else {
1388 $menu = $GLOBALS['TSFE']->sys_page->getMenu($pageUid);
1389 }
1390 return $menu;
1391 }
1392
1393 /**
1394 * Returns the path to the page $id
1395 *
1396 * @param int $id Page ID
1397 * @param string $pathMP Content of the MP (mount point) variable
1398 * @return string Path (HTML-escaped)
1399 */
1400 protected function getPathFromPageId($id, $pathMP = '')
1401 {
1402 $identStr = $id . '|' . $pathMP;
1403 if (!isset($this->pathCache[$identStr])) {
1404 $this->requiredFrontendUsergroups[$id] = [];
1405 $this->domainRecords[$id] = [];
1406 $rl = $GLOBALS['TSFE']->sys_page->getRootLine($id, $pathMP);
1407 $path = '';
1408 $pageCount = count($rl);
1409 if (is_array($rl) && !empty($rl)) {
1410 $excludeDoktypesFromPath = GeneralUtility::trimExplode(
1411 ',',
1412 $this->settings['results']['pathExcludeDoktypes'] ?? '',
1413 true
1414 );
1415 $breadcrumbWrap = isset($this->settings['breadcrumbWrap']) ? $this->settings['breadcrumbWrap'] : '/';
1416 $breadcrumbWraps = GeneralUtility::makeInstance(TypoScriptService::class)
1417 ->explodeConfigurationForOptionSplit(['wrap' => $breadcrumbWrap], $pageCount);
1418 foreach ($rl as $k => $v) {
1419 if (in_array($v['doktype'], $excludeDoktypesFromPath, false)) {
1420 continue;
1421 }
1422 // Check fe_user
1423 if ($v['fe_group'] && ($v['uid'] == $id || $v['extendToSubpages'])) {
1424 $this->requiredFrontendUsergroups[$id][] = $v['fe_group'];
1425 }
1426 // Check sys_domain
1427 if ($this->settings['detectDomainRcords']) {
1428 $domainName = $this->getFirstSysDomainRecordForPage($v['uid']);
1429 if ($domainName) {
1430 $this->domainRecords[$id][] = $domainName;
1431 // Set path accordingly
1432 $path = $domainName . $path;
1433 break;
1434 }
1435 }
1436 // Stop, if we find that the current id is the current root page.
1437 if ($v['uid'] == $GLOBALS['TSFE']->config['rootLine'][0]['uid']) {
1438 array_pop($breadcrumbWraps);
1439 break;
1440 }
1441 $path = $GLOBALS['TSFE']->cObj->wrap(htmlspecialchars($v['title']), array_pop($breadcrumbWraps)['wrap']) . $path;
1442 }
1443 }
1444 $this->pathCache[$identStr] = $path;
1445 }
1446 return $this->pathCache[$identStr];
1447 }
1448
1449 /**
1450 * Gets the first sys_domain record for the page, $id
1451 *
1452 * @param int $id Page id
1453 * @return string Domain name
1454 */
1455 protected function getFirstSysDomainRecordForPage($id)
1456 {
1457 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_domain');
1458 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
1459 $row = $queryBuilder
1460 ->select('domainName')
1461 ->from('sys_domain')
1462 ->where(
1463 $queryBuilder->expr()->eq(
1464 'pid',
1465 $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)
1466 )
1467 )
1468 ->orderBy('sorting')
1469 ->setMaxResults(1)
1470 ->execute()
1471 ->fetch();
1472
1473 return rtrim($row['domainName'], '/');
1474 }
1475
1476 /**
1477 * simple function to initialize possible external parsers
1478 * feeds the $this->externalParsers array
1479 */
1480 protected function initializeExternalParsers()
1481 {
1482 // Initialize external document parsers for icon display and other soft operations
1483 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['external_parsers'] ?? [] as $extension => $className) {
1484 $this->externalParsers[$extension] = GeneralUtility::makeInstance($className);
1485 // Init parser and if it returns FALSE, unset its entry again
1486 if (!$this->externalParsers[$extension]->softInit($extension)) {
1487 unset($this->externalParsers[$extension]);
1488 }
1489 }
1490 }
1491
1492 /**
1493 * Returns an object reference to the hook object if any
1494 *
1495 * @param string $functionName Name of the function you want to call / hook key
1496 * @return object|null Hook object, if any. Otherwise NULL.
1497 */
1498 protected function hookRequest($functionName)
1499 {
1500 // Hook: menuConfig_preProcessModMenu
1501 if ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['pi1_hooks'][$functionName]) {
1502 $hookObj = GeneralUtility::makeInstance($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['pi1_hooks'][$functionName]);
1503 if (method_exists($hookObj, $functionName)) {
1504 $hookObj->pObj = $this;
1505 return $hookObj;
1506 }
1507 }
1508 return null;
1509 }
1510
1511 /**
1512 * Returns if an item type is a multipage item type
1513 *
1514 * @param string $item_type Item type
1515 * @return bool TRUE if multipage capable
1516 */
1517 protected function multiplePagesType($item_type)
1518 {
1519 return is_object($this->externalParsers[$item_type]) && $this->externalParsers[$item_type]->isMultiplePageExtension($item_type);
1520 }
1521
1522 /**
1523 * Load settings and apply stdWrap to them
1524 */
1525 protected function loadSettings()
1526 {
1527 if (!is_array($this->settings['results.'])) {
1528 $this->settings['results.'] = [];
1529 }
1530 $typoScriptArray = $this->typoScriptService->convertPlainArrayToTypoScriptArray($this->settings['results']);
1531
1532 $this->settings['results.']['summaryCropAfter'] = MathUtility::forceIntegerInRange(
1533 $GLOBALS['TSFE']->cObj->stdWrap($typoScriptArray['summaryCropAfter'], $typoScriptArray['summaryCropAfter.']),
1534 10,
1535 5000,
1536 180
1537 );
1538 $this->settings['results.']['summaryCropSignifier'] = $GLOBALS['TSFE']->cObj->stdWrap($typoScriptArray['summaryCropSignifier'], $typoScriptArray['summaryCropSignifier.']);
1539 $this->settings['results.']['titleCropAfter'] = MathUtility::forceIntegerInRange(
1540 $GLOBALS['TSFE']->cObj->stdWrap($typoScriptArray['titleCropAfter'], $typoScriptArray['titleCropAfter.']),
1541 10,
1542 500,
1543 50
1544 );
1545 $this->settings['results.']['titleCropSignifier'] = $GLOBALS['TSFE']->cObj->stdWrap($typoScriptArray['titleCropSignifier'], $typoScriptArray['titleCropSignifier.']);
1546 $this->settings['results.']['markupSW_summaryMax'] = MathUtility::forceIntegerInRange(
1547 $GLOBALS['TSFE']->cObj->stdWrap($typoScriptArray['markupSW_summaryMax'], $typoScriptArray['markupSW_summaryMax.']),
1548 10,
1549 5000,
1550 300
1551 );
1552 $this->settings['results.']['markupSW_postPreLgd'] = MathUtility::forceIntegerInRange(
1553 $GLOBALS['TSFE']->cObj->stdWrap($typoScriptArray['markupSW_postPreLgd'], $typoScriptArray['markupSW_postPreLgd.']),
1554 1,
1555 500,
1556 60
1557 );
1558 $this->settings['results.']['markupSW_postPreLgd_offset'] = MathUtility::forceIntegerInRange(
1559 $GLOBALS['TSFE']->cObj->stdWrap($typoScriptArray['markupSW_postPreLgd_offset'], $typoScriptArray['markupSW_postPreLgd_offset.']),
1560 1,
1561 50,
1562 5
1563 );
1564 $this->settings['results.']['markupSW_divider'] = $GLOBALS['TSFE']->cObj->stdWrap($typoScriptArray['markupSW_divider'], $typoScriptArray['markupSW_divider.']);
1565 $this->settings['results.']['hrefInSummaryCropAfter'] = MathUtility::forceIntegerInRange(
1566 $GLOBALS['TSFE']->cObj->stdWrap($typoScriptArray['hrefInSummaryCropAfter'], $typoScriptArray['hrefInSummaryCropAfter.']),
1567 10,
1568 400,
1569 60
1570 );
1571 $this->settings['results.']['hrefInSummaryCropSignifier'] = $GLOBALS['TSFE']->cObj->stdWrap($typoScriptArray['hrefInSummaryCropSignifier'], $typoScriptArray['hrefInSummaryCropSignifier.']);
1572 }
1573
1574 /**
1575 * Returns number of results to display
1576 *
1577 * @param int $numberOfResults Requested number of results
1578 * @return int
1579 */
1580 protected function getNumberOfResults($numberOfResults)
1581 {
1582 $numberOfResults = (int)$numberOfResults;
1583
1584 return (in_array($numberOfResults, $this->availableResultsNumbers)) ?
1585 $numberOfResults : $this->defaultResultNumber;
1586 }
1587
1588 /**
1589 * Internal method to build the page uri and link target.
1590 * @todo make use of the UriBuilder
1591 *
1592 * @param int $pageUid
1593 * @param array $row
1594 * @param array $urlParameters
1595 * @return array
1596 */
1597 protected function preparePageLink(int $pageUid, array $row, array $urlParameters): array
1598 {
1599 $target = '';
1600 // If external domain, then link to that:
1601 if (!empty($this->domainRecords[$pageUid])) {
1602 $scheme = GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://';
1603 $firstDomain = reset($this->domainRecords[$pageUid]);
1604 $additionalParams = '';
1605 if (is_array($urlParameters) && !empty($urlParameters)) {
1606 $additionalParams = GeneralUtility::implodeArrayForUrl('', $urlParameters);
1607 }
1608 $uri = $scheme . $firstDomain . '/index.php?id=' . $pageUid . $additionalParams;
1609 $target = $this->settings['detectDomainRecords.']['target'];
1610 } else {
1611 $uriBuilder = $this->controllerContext->getUriBuilder();
1612 $uri = $uriBuilder->setTargetPageUid($pageUid)
1613 ->setTargetPageType($row['data_page_type'])
1614 ->setUseCacheHash(true)
1615 ->setArguments($urlParameters)
1616 ->build();
1617 }
1618
1619 return ['uri' => $uri, 'target' => $target];
1620 }
1621
1622 /**
1623 * Create a tag for "path" key in search result
1624 *
1625 * @param string $linkText Link text (nodeValue)
1626 * @param array $linkData
1627 * @return string <A> tag wrapped title string.
1628 */
1629 protected function linkPageATagWrap(string $linkText, array $linkData): string
1630 {
1631 $target = !empty($linkData['target']) ? 'target="' . htmlspecialchars($linkData['target']) . '"' : '';
1632
1633 return '<a href="' . htmlspecialchars($linkData['uri']) . '" ' . $target . '>'
1634 . htmlspecialchars($linkText)
1635 . '</a>';
1636 }
1637
1638 /**
1639 * Set the search word
1640 * @param string $sword
1641 */
1642 public function setSword($sword)
1643 {
1644 $this->sword = (string)$sword;
1645 }
1646
1647 /**
1648 * Returns the search word
1649 * @return string
1650 */
1651 public function getSword()
1652 {
1653 return (string)$this->sword;
1654 }
1655 }