1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 CMS project. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
10
|
|
|
* of the License, or any later version. |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please read the |
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* The TYPO3 project - inspiring people to share! |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ApacheSolrForTypo3\Solr\System\Solr; |
19
|
|
|
|
20
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
21
|
|
|
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager; |
22
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Parser\SchemaParser; |
23
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Parser\StopWordParser; |
24
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Parser\SynonymParser; |
25
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Service\SolrAdminService; |
26
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Service\SolrReadService; |
27
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Service\SolrWriteService; |
28
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
29
|
|
|
use Psr\Container\ContainerExceptionInterface; |
30
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
31
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
32
|
|
|
use Psr\Http\Client\ClientInterface; |
33
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
34
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
35
|
|
|
use Solarium\Client; |
36
|
|
|
use Solarium\Core\Client\Adapter\Psr18Adapter; |
37
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Solr Service Access |
41
|
|
|
* |
42
|
|
|
* @author Ingo Renner <[email protected]> |
43
|
|
|
*/ |
44
|
|
|
class SolrConnection |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var SolrAdminService|null |
48
|
|
|
*/ |
49
|
|
|
protected ?SolrAdminService $adminService = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var SolrReadService|null |
53
|
|
|
*/ |
54
|
|
|
protected ?SolrReadService $readService= null; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var SolrWriteService|null |
58
|
|
|
*/ |
59
|
|
|
protected ?SolrWriteService $writeService = null; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var TypoScriptConfiguration |
63
|
|
|
*/ |
64
|
|
|
protected TypoScriptConfiguration $configuration; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var SynonymParser|null |
68
|
|
|
*/ |
69
|
|
|
protected ?SynonymParser $synonymParser = null; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var StopWordParser|null |
73
|
|
|
*/ |
74
|
|
|
protected ?StopWordParser $stopWordParser = null; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var SchemaParser|null |
78
|
|
|
*/ |
79
|
|
|
protected ?SchemaParser $schemaParser = null; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var Node[] |
83
|
|
|
*/ |
84
|
|
|
protected array $nodes = []; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var SolrLogManager|null |
88
|
|
|
*/ |
89
|
|
|
protected ?SolrLogManager $logger = null; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var ClientInterface|Client[] |
93
|
|
|
*/ |
94
|
|
|
protected array $clients = []; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var ClientInterface |
98
|
|
|
*/ |
99
|
|
|
protected ClientInterface $psr7Client; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var RequestFactoryInterface |
103
|
|
|
*/ |
104
|
|
|
protected RequestFactoryInterface $requestFactory; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var StreamFactoryInterface |
108
|
|
|
*/ |
109
|
|
|
protected StreamFactoryInterface $streamFactory; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @var EventDispatcherInterface |
113
|
|
|
*/ |
114
|
|
|
protected EventDispatcherInterface $eventDispatcher; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Constructor |
118
|
|
|
* |
119
|
|
|
* @param Node $readNode |
120
|
|
|
* @param Node $writeNode |
121
|
|
|
* @param ?TypoScriptConfiguration $configuration |
122
|
|
|
* @param ?SynonymParser $synonymParser |
123
|
|
|
* @param ?StopWordParser $stopWordParser |
124
|
|
|
* @param ?SchemaParser $schemaParser |
125
|
|
|
* @param ?SolrLogManager $logManager |
126
|
|
|
* @param ?ClientInterface $psr7Client |
127
|
|
|
* @param ?RequestFactoryInterface $requestFactory |
128
|
|
|
* @param ?StreamFactoryInterface $streamFactory |
129
|
|
|
* @param ?EventDispatcherInterface $eventDispatcher |
130
|
|
|
* |
131
|
|
|
* @throws ContainerExceptionInterface |
132
|
|
|
* @throws NotFoundExceptionInterface |
133
|
|
|
*/ |
134
|
246 |
|
public function __construct( |
135
|
|
|
Node $readNode, |
136
|
|
|
Node $writeNode, |
137
|
|
|
TypoScriptConfiguration $configuration = null, |
138
|
|
|
SynonymParser $synonymParser = null, |
139
|
|
|
StopWordParser $stopWordParser = null, |
140
|
|
|
SchemaParser $schemaParser = null, |
141
|
|
|
SolrLogManager $logManager = null, |
142
|
|
|
ClientInterface $psr7Client = null, |
143
|
|
|
RequestFactoryInterface $requestFactory = null, |
144
|
|
|
StreamFactoryInterface $streamFactory = null, |
145
|
|
|
EventDispatcherInterface $eventDispatcher = null |
146
|
|
|
) { |
147
|
246 |
|
$this->nodes['read'] = $readNode; |
148
|
246 |
|
$this->nodes['write'] = $writeNode; |
149
|
246 |
|
$this->nodes['admin'] = $writeNode; |
150
|
246 |
|
$this->configuration = $configuration ?? Util::getSolrConfiguration(); |
151
|
246 |
|
$this->synonymParser = $synonymParser; |
152
|
246 |
|
$this->stopWordParser = $stopWordParser; |
153
|
246 |
|
$this->schemaParser = $schemaParser; |
154
|
246 |
|
$this->logger = $logManager; |
155
|
246 |
|
$this->psr7Client = $psr7Client ?? GeneralUtility::getContainer()->get(ClientInterface::class); |
156
|
246 |
|
$this->requestFactory = $requestFactory ?? GeneralUtility::getContainer()->get(RequestFactoryInterface::class); |
157
|
246 |
|
$this->streamFactory = $streamFactory ?? GeneralUtility::getContainer()->get(StreamFactoryInterface::class); |
158
|
246 |
|
$this->eventDispatcher = $eventDispatcher ?? GeneralUtility::getContainer()->get(EventDispatcherInterface::class); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $key |
163
|
|
|
* @return Node |
164
|
|
|
*/ |
165
|
226 |
|
public function getNode(string $key): Node |
166
|
|
|
{ |
167
|
226 |
|
return $this->nodes[$key]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return SolrAdminService |
172
|
|
|
*/ |
173
|
14 |
|
public function getAdminService(): SolrAdminService |
174
|
|
|
{ |
175
|
14 |
|
if ($this->adminService === null) { |
176
|
14 |
|
$this->adminService = $this->buildAdminService(); |
177
|
|
|
} |
178
|
|
|
|
179
|
14 |
|
return $this->adminService; |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return SolrAdminService |
184
|
|
|
*/ |
185
|
14 |
|
protected function buildAdminService(): SolrAdminService |
186
|
|
|
{ |
187
|
14 |
|
$endpointKey = 'admin'; |
188
|
14 |
|
$client = $this->getClient($endpointKey); |
189
|
14 |
|
$this->initializeClient($client, $endpointKey); |
190
|
14 |
|
return GeneralUtility::makeInstance( |
191
|
14 |
|
SolrAdminService::class, |
192
|
14 |
|
$client, |
193
|
14 |
|
$this->configuration, |
194
|
14 |
|
$this->logger, |
195
|
14 |
|
$this->synonymParser, |
196
|
14 |
|
$this->stopWordParser, |
197
|
14 |
|
$this->schemaParser |
198
|
14 |
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return SolrReadService |
203
|
|
|
*/ |
204
|
92 |
|
public function getReadService(): SolrReadService |
205
|
|
|
{ |
206
|
92 |
|
if ($this->readService === null) { |
207
|
92 |
|
$this->readService = $this->buildReadService(); |
208
|
|
|
} |
209
|
|
|
|
210
|
92 |
|
return $this->readService; |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return SolrReadService |
215
|
|
|
*/ |
216
|
92 |
|
protected function buildReadService(): SolrReadService |
217
|
|
|
{ |
218
|
92 |
|
$endpointKey = 'read'; |
219
|
92 |
|
$client = $this->getClient($endpointKey); |
220
|
92 |
|
$this->initializeClient($client, $endpointKey); |
221
|
92 |
|
return GeneralUtility::makeInstance(SolrReadService::class, $client); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return SolrWriteService |
226
|
|
|
*/ |
227
|
200 |
|
public function getWriteService(): SolrWriteService |
228
|
|
|
{ |
229
|
200 |
|
if ($this->writeService === null) { |
230
|
200 |
|
$this->writeService = $this->buildWriteService(); |
231
|
|
|
} |
232
|
|
|
|
233
|
200 |
|
return $this->writeService; |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return SolrWriteService |
238
|
|
|
*/ |
239
|
200 |
|
protected function buildWriteService(): SolrWriteService |
240
|
|
|
{ |
241
|
200 |
|
$endpointKey = 'write'; |
242
|
200 |
|
$client = $this->getClient($endpointKey); |
243
|
200 |
|
$this->initializeClient($client, $endpointKey); |
244
|
200 |
|
return GeneralUtility::makeInstance(SolrWriteService::class, $client); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param Client $client |
249
|
|
|
* @param string $endpointKey |
250
|
|
|
* @return Client |
251
|
|
|
*/ |
252
|
218 |
|
protected function initializeClient(Client $client, string $endpointKey): Client |
253
|
|
|
{ |
254
|
218 |
|
if (trim($this->getNode($endpointKey)->getUsername()) === '') { |
255
|
218 |
|
return $client; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$username = $this->getNode($endpointKey)->getUsername(); |
259
|
|
|
$password = $this->getNode($endpointKey)->getPassword(); |
260
|
|
|
$this->setAuthenticationOnAllEndpoints($client, $username, $password); |
261
|
|
|
|
262
|
|
|
return $client; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param Client $client |
267
|
|
|
* @param string $username |
268
|
|
|
* @param string $password |
269
|
|
|
*/ |
270
|
|
|
protected function setAuthenticationOnAllEndpoints(Client $client, string $username, string $password) |
271
|
|
|
{ |
272
|
|
|
foreach ($client->getEndpoints() as $endpoint) { |
273
|
|
|
$endpoint->setAuthentication($username, $password); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param string $endpointKey |
279
|
|
|
* @return Client |
280
|
|
|
*/ |
281
|
218 |
|
protected function getClient(string $endpointKey): Client |
282
|
|
|
{ |
283
|
218 |
|
if (isset($this->clients[$endpointKey])) { |
284
|
|
|
return $this->clients[$endpointKey]; |
285
|
|
|
} |
286
|
|
|
|
287
|
218 |
|
$adapter = new Psr18Adapter($this->psr7Client, $this->requestFactory, $this->streamFactory); |
288
|
|
|
|
289
|
218 |
|
$client = new Client($adapter, $this->eventDispatcher); |
290
|
218 |
|
$client->getPlugin('postbigrequest'); |
291
|
218 |
|
$client->clearEndpoints(); |
292
|
|
|
|
293
|
218 |
|
$newEndpointOptions = $this->getNode($endpointKey)->getSolariumClientOptions(); |
294
|
218 |
|
$newEndpointOptions['key'] = $endpointKey; |
295
|
218 |
|
$client->createEndpoint($newEndpointOptions, true); |
296
|
|
|
|
297
|
218 |
|
$this->clients[$endpointKey] = $client; |
298
|
218 |
|
return $client; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param Client $client |
303
|
|
|
* @param ?string $endpointKey |
304
|
|
|
*/ |
305
|
|
|
public function setClient(Client $client, ?string $endpointKey = 'read') |
306
|
|
|
{ |
307
|
|
|
$this->clients[$endpointKey] = $client; |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|