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