1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet; |
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 ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacet; |
18
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\FacetRegistry; |
19
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\RequirementsService; |
20
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Sorting\Sorting; |
21
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Spellchecking\Suggestion; |
22
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
23
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManager; |
24
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This processor is used to transform the solr response into a |
28
|
|
|
* domain object hierarchy that can be used in the application (controller and view). |
29
|
|
|
* |
30
|
|
|
* @author Frans Saris <[email protected]> |
31
|
|
|
* @author Timo Hund <[email protected]> |
32
|
|
|
* @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet |
33
|
|
|
*/ |
34
|
|
|
class ResultSetReconstitutionProcessor implements SearchResultSetProcessor |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var ObjectManagerInterface |
38
|
|
|
*/ |
39
|
|
|
protected $objectManager; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return ObjectManagerInterface |
43
|
|
|
*/ |
44
|
60 |
|
public function getObjectManager() |
45
|
|
|
{ |
46
|
60 |
|
if ($this->objectManager === null) { |
47
|
32 |
|
$this->objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
48
|
|
|
} |
49
|
60 |
|
return $this->objectManager; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ObjectManagerInterface $objectManager |
54
|
|
|
*/ |
55
|
28 |
|
public function setObjectManager($objectManager) |
56
|
|
|
{ |
57
|
28 |
|
$this->objectManager = $objectManager; |
58
|
28 |
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return FacetRegistry |
63
|
|
|
*/ |
64
|
60 |
|
protected function getFacetRegistry() |
65
|
|
|
{ |
66
|
60 |
|
return $this->getObjectManager()->get(FacetRegistry::class); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The implementation can be used to influence a SearchResultSet that is |
71
|
|
|
* created and processed in the SearchResultSetService. |
72
|
|
|
* |
73
|
|
|
* @param SearchResultSet $resultSet |
74
|
|
|
* @return SearchResultSet |
75
|
|
|
*/ |
76
|
71 |
|
public function process(SearchResultSet $resultSet) |
77
|
|
|
{ |
78
|
71 |
|
if (!$resultSet instanceof SearchResultSet) { |
|
|
|
|
79
|
|
|
return $resultSet; |
80
|
|
|
} |
81
|
|
|
|
82
|
71 |
|
$resultSet = $this->parseSpellCheckingResponseIntoObjects($resultSet); |
83
|
71 |
|
$resultSet = $this->parseSortingIntoObjects($resultSet); |
84
|
|
|
|
85
|
|
|
// here we can reconstitute other domain objects from the solr response |
86
|
71 |
|
$resultSet = $this->parseFacetsIntoObjects($resultSet); |
87
|
|
|
|
88
|
71 |
|
return $resultSet; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param SearchResultSet $resultSet |
93
|
|
|
* @return SearchResultSet |
94
|
|
|
*/ |
95
|
71 |
|
protected function parseSortingIntoObjects(SearchResultSet $resultSet) |
96
|
|
|
{ |
97
|
71 |
|
$configuration = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration(); |
98
|
71 |
|
$hasSorting = $resultSet->getUsedSearchRequest()->getHasSorting(); |
99
|
71 |
|
$activeSortingName = $resultSet->getUsedSearchRequest()->getSortingName(); |
100
|
71 |
|
$activeSortingDirection = $resultSet->getUsedSearchRequest()->getSortingDirection(); |
101
|
|
|
|
102
|
|
|
// no configuration available |
103
|
71 |
|
if (!isset($configuration)) { |
104
|
7 |
|
return $resultSet; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// no sorting enabled |
108
|
64 |
|
if (!$configuration->getSearchSorting()) { |
109
|
31 |
|
return $resultSet; |
110
|
|
|
} |
111
|
33 |
|
foreach ($configuration->getSearchSortingOptionsConfiguration() as $sortingKeyName => $sortingOptions) { |
112
|
33 |
|
$sortingName = rtrim($sortingKeyName, '.'); |
113
|
33 |
|
$selected = false; |
114
|
33 |
|
$direction = $configuration->getSearchSortingDefaultOrderBySortOptionName($sortingName); |
115
|
|
|
|
116
|
|
|
// when we have an active sorting in the request we compare the sortingName and mark is as active and |
117
|
|
|
// use the direction from the request |
118
|
33 |
|
if ($hasSorting && $activeSortingName == $sortingName) { |
119
|
1 |
|
$selected = true; |
120
|
1 |
|
$direction = $activeSortingDirection; |
121
|
|
|
} |
122
|
|
|
|
123
|
33 |
|
$field = $sortingOptions['field']; |
124
|
33 |
|
$label = $sortingOptions['label']; |
125
|
|
|
|
126
|
33 |
|
$isResetOption = $field === 'relevance'; |
127
|
|
|
// @todo allow stdWrap on label |
128
|
33 |
|
$sorting = new Sorting($resultSet, $sortingName, $field, $direction, $label, $selected, $isResetOption); |
129
|
33 |
|
$resultSet->addSorting($sorting); |
130
|
|
|
} |
131
|
|
|
|
132
|
33 |
|
return $resultSet; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param SearchResultSet $resultSet |
137
|
|
|
* @return SearchResultSet |
138
|
|
|
*/ |
139
|
71 |
|
private function parseSpellCheckingResponseIntoObjects(SearchResultSet $resultSet) |
140
|
|
|
{ |
141
|
|
|
//read the response |
142
|
71 |
|
$response = $resultSet->getResponse(); |
143
|
71 |
|
if (!is_object($response->spellcheck->suggestions)) { |
144
|
63 |
|
return $resultSet; |
145
|
|
|
} |
146
|
|
|
|
147
|
8 |
|
foreach ($response->spellcheck->suggestions as $key => $suggestionData) { |
148
|
5 |
|
if (!isset($suggestionData->suggestion) && !is_array($suggestionData->suggestion)) { |
149
|
1 |
|
continue; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// the key contains the misspelled word expect the internal key "collation" |
153
|
5 |
|
if ($key === 'collation') { |
154
|
|
|
continue; |
155
|
|
|
} |
156
|
|
|
//create the spellchecking object structure |
157
|
5 |
|
$misspelledTerm = $key; |
158
|
5 |
|
foreach ($suggestionData->suggestion as $suggestedTerm) { |
159
|
5 |
|
$suggestion = $this->createSuggestionFromResponseFragment($suggestionData, $suggestedTerm, $misspelledTerm); |
160
|
|
|
|
161
|
|
|
//add it to the resultSet |
162
|
5 |
|
$resultSet->addSpellCheckingSuggestion($suggestion); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
8 |
|
return $resultSet; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param \stdClass $suggestionData |
171
|
|
|
* @param string $suggestedTerm |
172
|
|
|
* @param string $misspelledTerm |
173
|
|
|
* @return Suggestion |
174
|
|
|
*/ |
175
|
5 |
|
private function createSuggestionFromResponseFragment($suggestionData, $suggestedTerm, $misspelledTerm) |
176
|
|
|
{ |
177
|
5 |
|
$numFound = isset($suggestionData->numFound) ? $suggestionData->numFound : 0; |
178
|
5 |
|
$startOffset = isset($suggestionData->startOffset) ? $suggestionData->startOffset : 0; |
179
|
5 |
|
$endOffset = isset($suggestionData->endOffset) ? $suggestionData->endOffset : 0; |
180
|
|
|
|
181
|
|
|
// by now we avoid to use GeneralUtility::makeInstance, since we only create a value object |
182
|
|
|
// and the usage might be a overhead. |
183
|
5 |
|
$suggestion = new Suggestion($suggestedTerm, $misspelledTerm, $numFound, $startOffset, $endOffset); |
184
|
5 |
|
return $suggestion; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Parse available facets into objects |
189
|
|
|
* |
190
|
|
|
* @param SearchResultSet $resultSet |
191
|
|
|
* @return SearchResultSet |
192
|
|
|
*/ |
193
|
71 |
|
private function parseFacetsIntoObjects(SearchResultSet $resultSet) |
194
|
|
|
{ |
195
|
|
|
// Make sure we can access the facet configuration |
196
|
71 |
|
if (!$resultSet->getUsedSearchRequest() || !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()) { |
197
|
7 |
|
return $resultSet; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
// Read the response |
201
|
64 |
|
$response = $resultSet->getResponse(); |
202
|
64 |
|
if (!is_object($response->facet_counts) && !is_object($response->facets)) { |
203
|
4 |
|
return $resultSet; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** @var FacetRegistry $facetRegistry */ |
207
|
60 |
|
$facetRegistry = $this->getFacetRegistry(); |
208
|
60 |
|
$facetsConfiguration = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingFacets(); |
209
|
|
|
|
210
|
60 |
|
foreach ($facetsConfiguration as $name => $options) { |
211
|
58 |
|
if (!is_array($options)) { |
212
|
|
|
continue; |
213
|
|
|
} |
214
|
58 |
|
$facetName = rtrim($name, '.'); |
215
|
58 |
|
$type = !empty($options['type']) ? $options['type'] : ''; |
216
|
|
|
|
217
|
58 |
|
$parser = $facetRegistry->getPackage($type)->getParser(); |
218
|
58 |
|
$facet = $parser->parse($resultSet, $facetName, $options); |
219
|
58 |
|
if ($facet !== null) { |
220
|
58 |
|
$resultSet->addFacet($facet); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
60 |
|
$this->applyRequirements($resultSet); |
225
|
|
|
|
226
|
60 |
|
return $resultSet; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param SearchResultSet $resultSet |
231
|
|
|
*/ |
232
|
60 |
|
protected function applyRequirements(SearchResultSet $resultSet) |
233
|
|
|
{ |
234
|
60 |
|
$requirementsService = $this->getRequirementsService(); |
235
|
60 |
|
$facets = $resultSet->getFacets(); |
236
|
60 |
|
foreach ($facets as $facet) { |
237
|
|
|
/** @var $facet AbstractFacet */ |
238
|
56 |
|
$requirementsMet = $requirementsService->getAllRequirementsMet($facet); |
239
|
56 |
|
$facet->setAllRequirementsMet($requirementsMet); |
240
|
|
|
} |
241
|
60 |
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return RequirementsService |
245
|
|
|
*/ |
246
|
60 |
|
protected function getRequirementsService() |
247
|
|
|
{ |
248
|
60 |
|
return $this->getObjectManager()->get(RequirementsService::class); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|