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