1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the TYPO3 CMS project. |
5
|
|
|
* |
6
|
|
|
* It is free software; you can redistribute it and/or modify it under |
7
|
|
|
* the terms of the GNU General Public License, either version 2 |
8
|
|
|
* of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please read the |
11
|
|
|
* LICENSE.txt file that was distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* The TYPO3 project - inspiring people to share! |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace ApacheSolrForTypo3\Solr\Controller; |
17
|
|
|
|
18
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Suggest\SuggestService; |
19
|
|
|
|
20
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\SolrUnavailableException; |
21
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
22
|
|
|
use Psr\Http\Message\ResponseInterface; |
23
|
|
|
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException; |
24
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class SuggestController |
28
|
|
|
* |
29
|
|
|
* @author Frans Saris <[email protected]> |
30
|
|
|
* @author Timo Hund <[email protected]> |
31
|
|
|
* @copyright (c) 2017 Timo Hund <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class SuggestController extends AbstractBaseController |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* This method creates a suggest json response that can be used in a suggest layer. |
37
|
|
|
* |
38
|
|
|
* @param string $queryString |
39
|
|
|
* @param string|null $callback |
40
|
|
|
* @param array|null $additionalFilters |
41
|
|
|
* @return ResponseInterface |
42
|
|
|
* @throws AspectNotFoundException |
43
|
|
|
*/ |
44
|
3 |
|
public function suggestAction(string $queryString, ?string $callback = null, ?array $additionalFilters = []): ResponseInterface |
45
|
|
|
{ |
46
|
|
|
// Get suggestions |
47
|
3 |
|
$rawQuery = htmlspecialchars(mb_strtolower(trim($queryString))); |
48
|
|
|
|
49
|
3 |
|
if ($this->searchService === null) { |
50
|
|
|
return $this->handleSolrUnavailable(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
/** @var SuggestService $suggestService */ |
55
|
3 |
|
$suggestService = GeneralUtility::makeInstance( |
56
|
3 |
|
SuggestService::class, |
57
|
|
|
/** @scrutinizer ignore-type */ |
58
|
3 |
|
$this->typoScriptFrontendController, |
59
|
|
|
/** @scrutinizer ignore-type */ |
60
|
3 |
|
$this->searchService, |
61
|
|
|
/** @scrutinizer ignore-type */ |
62
|
3 |
|
$this->typoScriptConfiguration |
63
|
3 |
|
); |
64
|
|
|
|
65
|
3 |
|
$additionalFilters = is_array($additionalFilters) ? array_map('htmlspecialchars', $additionalFilters) : []; |
|
|
|
|
66
|
3 |
|
$pageId = $this->typoScriptFrontendController->getRequestedId(); |
|
|
|
|
67
|
3 |
|
$languageId = Util::getLanguageUid(); |
68
|
3 |
|
$arguments = $this->request->getArguments(); |
69
|
|
|
|
70
|
3 |
|
$searchRequest = $this->getSearchRequestBuilder()->buildForSuggest($arguments, $rawQuery, $pageId, $languageId); |
71
|
3 |
|
$result = $suggestService->getSuggestions($searchRequest, $additionalFilters); |
72
|
|
|
} catch (SolrUnavailableException $e) { |
73
|
|
|
return $this->handleSolrUnavailable(); |
74
|
|
|
} |
75
|
3 |
|
if ($callback) { |
76
|
2 |
|
return $this->htmlResponse(htmlspecialchars($callback) . '(' . json_encode($result, JSON_UNESCAPED_SLASHES) . ')'); |
77
|
|
|
} |
78
|
1 |
|
return $this->htmlResponse(json_encode($result, JSON_UNESCAPED_SLASHES)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function handleSolrUnavailable(): ResponseInterface |
82
|
|
|
{ |
83
|
|
|
$this->logSolrUnavailable(); |
84
|
|
|
$result = ['status' => false]; |
85
|
|
|
return $this->htmlResponse(json_encode($result, JSON_UNESCAPED_SLASHES))->withStatus(503, self::STATUS_503_MESSAGE); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|