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\Plugin\Eid; |
14
|
|
|
|
15
|
|
|
use Kitodo\Dlf\Common\Helper; |
16
|
|
|
use Kitodo\Dlf\Common\Solr; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use TYPO3\CMS\Core\Http\Response; |
20
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* eID search suggestions for plugin 'Search' of the 'dlf' extension |
24
|
|
|
* |
25
|
|
|
* @author Henrik Lochmann <[email protected]> |
26
|
|
|
* @author Sebastian Meyer <[email protected]> |
27
|
|
|
* @package TYPO3 |
28
|
|
|
* @subpackage dlf |
29
|
|
|
* @access public |
30
|
|
|
*/ |
31
|
|
|
class SearchSuggest |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* The main method of the eID script |
35
|
|
|
* |
36
|
|
|
* @param ServerRequestInterface $request |
37
|
|
|
* @return ResponseInterface XML response of search suggestions |
38
|
|
|
*/ |
39
|
|
|
public function main(ServerRequestInterface $request) |
40
|
|
|
{ |
41
|
|
|
$output = []; |
42
|
|
|
// Get input parameters and decrypt core name. |
43
|
|
|
$parameters = $request->getParsedBody(); |
44
|
|
|
$encrypted = (string) $parameters['encrypted']; |
45
|
|
|
$hashed = (string) $parameters['hashed']; |
46
|
|
|
if (empty($encrypted) || empty($hashed)) { |
47
|
|
|
throw new \InvalidArgumentException('No valid parameter passed!', 1580585079); |
48
|
|
|
} |
49
|
|
|
$core = Helper::decrypt($encrypted, $hashed); |
50
|
|
|
// Perform Solr query. |
51
|
|
|
$solr = Solr::getInstance($core); |
52
|
|
|
if ($solr->ready) { |
53
|
|
|
$query = $solr->service->createSelect(); |
54
|
|
|
$query->setHandler('suggest'); |
55
|
|
|
$query->setQuery(Solr::escapeQuery((string) $parameters['q'])); |
56
|
|
|
$query->setRows(0); |
57
|
|
|
$results = $solr->service->select($query)->getResponse()->getBody(); |
58
|
|
|
$result = json_decode($results); |
59
|
|
|
foreach ($result->spellcheck->suggestions as $suggestions) { |
60
|
|
|
if (is_object($suggestions)) { |
61
|
|
|
foreach ($suggestions->suggestion as $suggestion) { |
62
|
|
|
$output[] = $suggestion; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
// Create response object. |
68
|
|
|
/** @var Response $response */ |
69
|
|
|
$response = GeneralUtility::makeInstance(Response::class); |
70
|
|
|
$response->getBody()->write(json_encode($output)); |
71
|
|
|
return $response; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|