1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
7
|
|
|
* |
8
|
|
|
* @license GNU General Public License version 3 or later. |
9
|
|
|
* For the full copyright and license information, please read the |
10
|
|
|
* LICENSE.txt file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Kitodo\Dlf\Middleware; |
14
|
|
|
|
15
|
|
|
use Kitodo\Dlf\Common\Solr\Solr; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
18
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
19
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
20
|
|
|
use TYPO3\CMS\Core\Core\Environment; |
21
|
|
|
use TYPO3\CMS\Core\Http\Response; |
22
|
|
|
use TYPO3\CMS\Core\Information\Typo3Version; |
23
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Search suggestions Middleware for plugin 'Search' of the 'dlf' extension |
27
|
|
|
* |
28
|
|
|
* @author Henrik Lochmann <[email protected]> |
29
|
|
|
* @author Sebastian Meyer <[email protected]> |
30
|
|
|
* @author Beatrycze Volk <[email protected]> |
31
|
|
|
* @package TYPO3 |
32
|
|
|
* @subpackage dlf |
33
|
|
|
* @access public |
34
|
|
|
*/ |
35
|
|
|
class SearchSuggest implements MiddlewareInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* The process method of the middleware. |
39
|
|
|
* |
40
|
|
|
* @access public |
41
|
|
|
* |
42
|
|
|
* @param ServerRequestInterface $request |
43
|
|
|
* @param RequestHandlerInterface $handler |
44
|
|
|
* |
45
|
|
|
* @return ResponseInterface XML response of search suggestions |
46
|
|
|
*/ |
47
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
48
|
|
|
{ |
49
|
|
|
$response = $handler->handle($request); |
50
|
|
|
// Get input parameters and decrypt core name. |
51
|
|
|
$parameters = $request->getParsedBody(); |
52
|
|
|
// Return if not this middleware |
53
|
|
|
if (!isset($parameters['middleware']) || ($parameters['middleware'] != 'dlf/search-suggest')) { |
54
|
|
|
return $response; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$output = []; |
58
|
|
|
$solrCore = (string) $parameters['solrcore']; |
59
|
|
|
$uHash = (string) $parameters['uHash']; |
60
|
|
|
if (hash_equals(GeneralUtility::hmac((string) (new Typo3Version()) . Environment::getExtensionsPath(), 'SearchSuggest'), $uHash) === false) { |
61
|
|
|
throw new \InvalidArgumentException('No valid parameter passed!', 1580585079); |
62
|
|
|
} |
63
|
|
|
// Perform Solr query. |
64
|
|
|
$solr = Solr::getInstance($solrCore); |
65
|
|
|
if ($solr->ready) { |
66
|
|
|
$query = $solr->service->createSelect(); |
67
|
|
|
$query->setHandler('suggest'); |
68
|
|
|
$query->setQuery(Solr::escapeQuery((string) $parameters['q'])); |
69
|
|
|
$query->setRows(0); |
70
|
|
|
$results = $solr->service->select($query)->getResponse()->getBody(); |
71
|
|
|
$result = json_decode($results); |
72
|
|
|
if (is_iterable($result->spellcheck->suggestions)) { |
73
|
|
|
foreach ($result->spellcheck->suggestions as $suggestions) { |
74
|
|
|
if (is_object($suggestions)) { |
75
|
|
|
foreach ($suggestions->suggestion as $suggestion) { |
76
|
|
|
$output[] = $suggestion; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Create response object. |
84
|
|
|
/** @var Response $response */ |
85
|
|
|
$response = GeneralUtility::makeInstance(Response::class); |
86
|
|
|
$response->getBody()->write(json_encode($output)); |
87
|
|
|
return $response; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|