1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 CMS project. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
10
|
|
|
* of the License, or any later version. |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please read the |
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* The TYPO3 project - inspiring people to share! |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Document; |
19
|
|
|
|
20
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\SearchResult; |
21
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet; |
22
|
|
|
use ApacheSolrForTypo3\Solr\ViewHelpers\AbstractSolrFrontendViewHelper; |
23
|
|
|
use Closure; |
24
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
25
|
|
|
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
26
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class HighlightResultViewHelper |
30
|
|
|
* |
31
|
|
|
* @author Frans Saris <[email protected]> |
32
|
|
|
* @author Timo Hund <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class HighlightResultViewHelper extends AbstractSolrFrontendViewHelper |
35
|
|
|
{ |
36
|
|
|
use CompileWithRenderStatic; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
protected $escapeOutput = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Initializes the arguments |
45
|
|
|
*/ |
46
|
22 |
|
public function initializeArguments() |
47
|
|
|
{ |
48
|
22 |
|
parent::initializeArguments(); |
49
|
22 |
|
$this->registerArgument('resultSet', SearchResultSet::class, 'The context searchResultSet', true); |
50
|
22 |
|
$this->registerArgument('document', SearchResult::class, 'The document to highlight', true); |
51
|
22 |
|
$this->registerArgument('fieldName', 'string', 'The fieldName', true); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $arguments |
56
|
|
|
* @param Closure $renderChildrenClosure |
57
|
|
|
* @param RenderingContextInterface $renderingContext |
58
|
|
|
* @return string |
59
|
|
|
* @noinspection PhpMissingReturnTypeInspection |
60
|
|
|
*/ |
61
|
25 |
|
public static function renderStatic( |
62
|
|
|
array $arguments, |
63
|
|
|
Closure $renderChildrenClosure, |
64
|
|
|
RenderingContextInterface $renderingContext |
65
|
|
|
) { |
66
|
|
|
/* @var SearchResultSet $resultSet */ |
67
|
25 |
|
$resultSet = $arguments['resultSet']; |
68
|
25 |
|
$fieldName = $arguments['fieldName']; |
69
|
25 |
|
$document = $arguments['document']; |
70
|
25 |
|
$highlightedContent = self::getHighlightedContent($resultSet, $document, $fieldName); |
71
|
25 |
|
if (is_string($highlightedContent)) { |
72
|
25 |
|
return self::escapeEverythingExceptAllowedTags($resultSet, $highlightedContent); |
73
|
|
|
} |
74
|
|
|
return ''; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param SearchResultSet $resultSet |
79
|
|
|
* @param SearchResult $document |
80
|
|
|
* @param string $fieldName |
81
|
|
|
* @return mixed|string |
82
|
|
|
*/ |
83
|
25 |
|
protected static function getHighlightedContent(SearchResultSet $resultSet, SearchResult $document, string $fieldName) |
84
|
|
|
{ |
85
|
25 |
|
$fragmentSeparator = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchResultsHighlightingFragmentSeparator(); |
86
|
|
|
|
87
|
25 |
|
$content = call_user_func([$document, 'get' . $fieldName]); |
88
|
25 |
|
$highlightedContent = $resultSet->getUsedSearch()->getHighlightedContent(); |
89
|
25 |
|
if (!empty($highlightedContent->{$document->getId()}->{$fieldName}[0])) { |
90
|
3 |
|
return implode(' ' . $fragmentSeparator . ' ', $highlightedContent->{$document->getId()}->{$fieldName}); |
91
|
|
|
} |
92
|
22 |
|
return $content; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param SearchResultSet $resultSet |
97
|
|
|
* @param string $content |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
25 |
|
protected static function escapeEverythingExceptAllowedTags(SearchResultSet $resultSet, string $content): string |
101
|
|
|
{ |
102
|
25 |
|
$wrap = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchResultsHighlightingWrap(); |
103
|
25 |
|
if ($wrap === '') { |
104
|
|
|
return htmlspecialchars($content); |
105
|
|
|
} |
106
|
|
|
|
107
|
25 |
|
$wrapParts = GeneralUtility::trimExplode('|', $wrap); |
108
|
25 |
|
if (count($wrapParts) !== 2) { |
109
|
1 |
|
return htmlspecialchars($content); |
110
|
|
|
} |
111
|
|
|
|
112
|
24 |
|
$substitutedContent = str_replace($wrapParts[0], '___highlight_begin___', $content); |
113
|
24 |
|
$substitutedContent = str_replace($wrapParts[1], '___highlight_end___', $substitutedContent); |
114
|
24 |
|
$output = htmlspecialchars($substitutedContent); |
115
|
24 |
|
$output = str_replace('___highlight_begin___', $wrapParts[0], $output); |
116
|
24 |
|
return str_replace('___highlight_end___', $wrapParts[1], $output); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|