1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2017 <[email protected]> |
8
|
|
|
* All rights reserved |
9
|
|
|
* |
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
11
|
|
|
* free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* The GNU General Public License can be found at |
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
18
|
|
|
* |
19
|
|
|
* This script is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
25
|
|
|
***************************************************************/ |
26
|
|
|
|
27
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The Highlighting ParameterProvider is responsible to build the solr query parameters |
31
|
|
|
* that are needed for the highlighting. |
32
|
|
|
* |
33
|
|
|
* @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder |
34
|
|
|
*/ |
35
|
|
|
class Highlighting implements ParameterBuilder |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
protected $isEnabled = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
protected $fragmentSize = 200; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $highlightingFieldList = ''; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $prefix = ''; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $postfix = ''; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Highlighting constructor. |
65
|
|
|
* |
66
|
|
|
* private constructor should only be created with the from* methods |
67
|
|
|
* |
68
|
|
|
* @param bool $isEnabled |
69
|
|
|
* @param int $fragmentSize |
70
|
|
|
* @param string $highlightingFieldList |
71
|
|
|
* @param string $prefix |
72
|
|
|
* @param string $postfix |
73
|
|
|
*/ |
74
|
|
|
private function __construct($isEnabled = false, $fragmentSize = 200, $highlightingFieldList = '', $prefix = '', $postfix = '') |
75
|
|
|
{ |
76
|
|
|
$this->isEnabled = $isEnabled; |
77
|
|
|
$this->fragmentSize = $fragmentSize; |
78
|
|
|
$this->highlightingFieldList = $highlightingFieldList; |
79
|
|
|
$this->prefix = $prefix; |
80
|
|
|
$this->postfix = $postfix; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
|
|
public function getFragmentSize(): int |
87
|
|
|
{ |
88
|
|
|
return $this->fragmentSize; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param int $fragmentSize |
93
|
|
|
*/ |
94
|
|
|
public function setFragmentSize(int $fragmentSize) |
95
|
|
|
{ |
96
|
|
|
$this->fragmentSize = $fragmentSize; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getHighlightingFieldList(): string |
103
|
|
|
{ |
104
|
|
|
return $this->highlightingFieldList; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $highlightingFieldList |
109
|
|
|
*/ |
110
|
|
|
public function setHighlightingFieldList(string $highlightingFieldList) |
111
|
|
|
{ |
112
|
|
|
$this->highlightingFieldList = $highlightingFieldList; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getPrefix(): string |
119
|
|
|
{ |
120
|
|
|
return $this->prefix; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $prefix |
125
|
|
|
*/ |
126
|
|
|
public function setPrefix(string $prefix) |
127
|
|
|
{ |
128
|
|
|
$this->prefix = $prefix; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getPostfix(): string |
135
|
|
|
{ |
136
|
|
|
return $this->postfix; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $postfix |
141
|
|
|
*/ |
142
|
|
|
public function setPostfix(string $postfix) |
143
|
|
|
{ |
144
|
|
|
$this->postfix = $postfix; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return boolean |
149
|
|
|
*/ |
150
|
|
|
public function getIsEnabled() |
151
|
|
|
{ |
152
|
|
|
return $this->isEnabled; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param boolean $isEnabled |
157
|
|
|
*/ |
158
|
|
|
public function setIsEnabled($isEnabled) |
159
|
|
|
{ |
160
|
|
|
$this->isEnabled = $isEnabled; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function build() |
167
|
|
|
{ |
168
|
|
|
if (!$this->isEnabled) { |
169
|
|
|
return []; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$highlightingParameter = []; |
173
|
|
|
$highlightingParameter['hl'] = 'true'; |
174
|
|
|
$highlightingParameter['hl.fragsize'] = (int)$this->fragmentSize; |
175
|
|
|
|
176
|
|
|
if ($this->highlightingFieldList != '') { |
177
|
|
|
$highlightingParameter['hl.fl'] = $this->highlightingFieldList; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// the fast vector highlighter can only be used, when the fragmentSize is |
181
|
|
|
// higher then 17 otherwise solr throws an exception |
182
|
|
|
$useFastVectorHighlighter = ($this->fragmentSize >= 18); |
183
|
|
|
|
184
|
|
|
if ($useFastVectorHighlighter) { |
185
|
|
|
$highlightingParameter['hl.useFastVectorHighlighter'] = 'true'; |
186
|
|
|
$highlightingParameter['hl.tag.pre'] = $this->prefix; |
187
|
|
|
$highlightingParameter['hl.tag.post'] = $this->postfix; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if ($this->prefix !== '' && $this->postfix !== '') { |
191
|
|
|
$highlightingParameter['hl.simple.pre'] = $this->prefix; |
192
|
|
|
$highlightingParameter['hl.simple.post'] = $this->postfix; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $highlightingParameter; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param TypoScriptConfiguration $solrConfiguration |
200
|
|
|
* @return Highlighting |
201
|
|
|
*/ |
202
|
|
|
public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration) |
203
|
|
|
{ |
204
|
|
|
$isEnabled = $solrConfiguration->getSearchResultsHighlighting(); |
205
|
|
|
if (!$isEnabled) { |
206
|
|
|
return new Highlighting(false); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$fragmentSize = $solrConfiguration->getSearchResultsHighlightingFragmentSize(); |
210
|
|
|
$highlightingFields = $solrConfiguration->getSearchResultsHighlightingFields(); |
211
|
|
|
$wrap = explode('|', $solrConfiguration->getSearchResultsHighlightingWrap()); |
212
|
|
|
$prefix = isset($wrap[0]) ? $wrap[0] : ''; |
213
|
|
|
$postfix = isset($wrap[1]) ? $wrap[1] : ''; |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
return new Highlighting($isEnabled, $fragmentSize, $highlightingFields, $prefix, $postfix); |
217
|
|
|
} |
218
|
|
|
} |