|
1
|
|
|
<?php |
|
2
|
|
|
namespace ApacheSolrForTypo3\Solr\System\Solr\Service; |
|
3
|
|
|
|
|
4
|
|
|
/*************************************************************** |
|
5
|
|
|
* Copyright notice |
|
6
|
|
|
* |
|
7
|
|
|
* (c) 2009-2017 Timo Hund <[email protected]> |
|
8
|
|
|
* All rights reserved |
|
9
|
|
|
* |
|
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
|
11
|
|
|
* free software; you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* The GNU General Public License can be found at |
|
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
|
18
|
|
|
* |
|
19
|
|
|
* This script is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
|
25
|
|
|
***************************************************************/ |
|
26
|
|
|
|
|
27
|
|
|
use ApacheSolrForTypo3\Solr\PingFailedException; |
|
28
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
|
29
|
|
|
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager; |
|
30
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter; |
|
31
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
|
32
|
|
|
|
|
33
|
|
|
use Solarium\Client; |
|
34
|
|
|
use Solarium\Core\Client\Endpoint; |
|
35
|
|
|
use Solarium\Core\Client\Request; |
|
36
|
|
|
use Solarium\Core\Query\QueryInterface; |
|
37
|
|
|
use Solarium\Exception\HttpException; |
|
38
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
39
|
|
|
|
|
40
|
|
|
abstract class AbstractSolrService { |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected static $pingCache = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var TypoScriptConfiguration |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $configuration; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $logger = null; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var Client |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $client = null; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* SolrReadService constructor. |
|
64
|
|
|
*/ |
|
65
|
133 |
|
public function __construct(Client $client, $typoScriptConfiguration = null, $logManager = null) |
|
66
|
|
|
{ |
|
67
|
133 |
|
$this->client = $client; |
|
68
|
133 |
|
$this->configuration = $typoScriptConfiguration ?? Util::getSolrConfiguration(); |
|
69
|
133 |
|
$this->logger = $logManager ?? GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__); |
|
70
|
133 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns the path to the core solr path + core path. |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
2 |
|
*/ |
|
77
|
|
|
public function getCorePath() |
|
78
|
2 |
|
{ |
|
79
|
2 |
|
$endpoint = $this->getPrimaryEndpoint(); |
|
80
|
|
|
return is_null($endpoint) ? '' : $endpoint->getPath() .'/'. $endpoint->getCore(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Return a valid http URL given this server's host, port and path and a provided servlet name |
|
85
|
|
|
* |
|
86
|
2 |
|
* @param string $servlet |
|
87
|
|
|
* @param array $params |
|
88
|
2 |
|
* @return string |
|
89
|
2 |
|
*/ |
|
90
|
|
|
protected function _constructUrl($servlet, $params = []) |
|
91
|
|
|
{ |
|
92
|
|
|
$queryString = count($params) ? '?' . http_build_query($params, null, '&') : ''; |
|
93
|
|
|
return $this->__toString() . $servlet . $queryString; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
/** |
|
97
|
|
|
* Creates a string representation of the Solr connection. Specifically |
|
98
|
2 |
|
* will return the Solr URL. |
|
99
|
2 |
|
* |
|
100
|
|
|
* @return string The Solr URL. |
|
101
|
|
|
*/ |
|
102
|
|
|
public function __toString() |
|
103
|
|
|
{ |
|
104
|
|
|
$endpoint = $this->getPrimaryEndpoint(); |
|
105
|
|
|
if (!$endpoint instanceof Endpoint) { |
|
106
|
2 |
|
return ''; |
|
107
|
|
|
} |
|
108
|
2 |
|
|
|
109
|
|
|
return $endpoint->getScheme(). '://' . $endpoint->getHost() . ':' . $endpoint->getPort() . $endpoint->getPath() . '/' . $endpoint->getCore() . '/'; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return Endpoint|null |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getPrimaryEndpoint() |
|
116
|
2 |
|
{ |
|
117
|
|
|
return is_array($this->client->getEndpoints()) ? reset($this->client->getEndpoints()) : null; |
|
|
|
|
|
|
118
|
2 |
|
} |
|
119
|
2 |
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Central method for making a get operation against this Solr Server |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $url |
|
124
|
|
|
* @return ResponseAdapter |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function _sendRawGet($url) |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->_sendRawRequest($url, Request::METHOD_GET); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Central method for making a HTTP DELETE operation against the Solr server |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $url |
|
135
|
|
|
* @return ResponseAdapter |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function _sendRawDelete($url) |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->_sendRawRequest($url, Request::METHOD_DELETE); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Central method for making a post operation against this Solr Server |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $url |
|
146
|
|
|
* @param string $rawPost |
|
147
|
|
|
* @param string $contentType |
|
148
|
|
|
* @return ResponseAdapter |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function _sendRawPost($url, $rawPost, $contentType = 'text/xml; charset=UTF-8') |
|
151
|
|
|
{ |
|
152
|
|
|
$initializeRequest = function(Request $request) use ($rawPost, $contentType) { |
|
153
|
22 |
|
$request->setRawData($rawPost); |
|
154
|
|
|
$request->addHeader('Content-Type: ' . $contentType); |
|
155
|
22 |
|
return $request; |
|
156
|
22 |
|
}; |
|
157
|
|
|
|
|
158
|
|
|
return $this->_sendRawRequest($url, Request::METHOD_POST, $rawPost, $initializeRequest); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Method that performs an http request with the solarium client. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $url |
|
165
|
94 |
|
* @param string $method |
|
166
|
|
|
* @param string $body |
|
167
|
94 |
|
* @param \Closure $initializeRequest |
|
168
|
94 |
|
* @return ResponseAdapter |
|
169
|
2 |
|
*/ |
|
170
|
|
|
protected function _sendRawRequest($url, $method = Request::METHOD_GET, $body = '', \Closure $initializeRequest = null) |
|
171
|
|
|
{ |
|
172
|
92 |
|
$logSeverity = SolrLogManager::INFO; |
|
173
|
|
|
$exception = null; |
|
174
|
|
|
|
|
175
|
|
|
try { |
|
176
|
|
|
$request = $this->buildSolariumRequestFromUrl($url, $method); |
|
177
|
|
|
|
|
178
|
99 |
|
if($initializeRequest !== null) { |
|
179
|
|
|
$request = $initializeRequest($request); |
|
180
|
99 |
|
} |
|
181
|
|
|
|
|
182
|
|
|
$response = $this->executeRequest($request); |
|
183
|
|
|
} catch (HttpException $exception) { |
|
184
|
|
|
$logSeverity = SolrLogManager::ERROR; |
|
185
|
|
|
$response = new ResponseAdapter($exception->getBody(), $exception->getCode(), $exception->getMessage()); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
if ($this->configuration->getLoggingQueryRawPost() || $response->getHttpStatus() != 200) { |
|
189
|
16 |
|
$message = 'Querying Solr using '.$method; |
|
190
|
|
|
$this->writeLog($logSeverity, $message, $url, $response, $exception, $body); |
|
191
|
16 |
|
} |
|
192
|
|
|
|
|
193
|
|
|
return $response; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Build the log data and writes the message to the log |
|
198
|
|
|
* |
|
199
|
|
|
* @param integer $logSeverity |
|
200
|
4 |
|
* @param string $message |
|
201
|
|
|
* @param string $url |
|
202
|
4 |
|
* @param ResponseAdapter $solrResponse |
|
203
|
|
|
* @param \Exception $exception |
|
204
|
|
|
* @param string $contentSend |
|
205
|
|
|
*/ |
|
206
|
|
|
protected function writeLog($logSeverity, $message, $url, $solrResponse, $exception = null, $contentSend = '') |
|
207
|
|
|
{ |
|
208
|
|
|
$logData = $this->buildLogDataFromResponse($solrResponse, $exception, $url, $contentSend); |
|
209
|
|
|
$this->logger->log($logSeverity, $message, $logData); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Parses the solr information to build data for the logger. |
|
214
|
|
|
* |
|
215
|
4 |
|
* @param ResponseAdapter $solrResponse |
|
216
|
4 |
|
* @param \Exception $e |
|
217
|
4 |
|
* @param string $url |
|
218
|
4 |
|
* @param string $contentSend |
|
219
|
4 |
|
* @return array |
|
220
|
|
|
*/ |
|
221
|
4 |
|
protected function buildLogDataFromResponse(ResponseAdapter $solrResponse, \Exception $e = null, $url = '', $contentSend = '') |
|
222
|
|
|
{ |
|
223
|
|
|
$logData = ['query url' => $url, 'response' => (array)$solrResponse]; |
|
224
|
|
|
|
|
225
|
|
|
if ($contentSend !== '') { |
|
226
|
|
|
$logData['content'] = $contentSend; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
if (!empty($e)) { |
|
230
|
|
|
$logData['exception'] = $e->__toString(); |
|
231
|
|
|
return $logData; |
|
232
|
|
|
} else { |
|
233
|
17 |
|
// trigger data parsing |
|
234
|
|
|
// @extensionScannerIgnoreLine |
|
235
|
17 |
|
$solrResponse->response; |
|
236
|
17 |
|
$logData['response data'] = print_r($solrResponse, true); |
|
237
|
|
|
return $logData; |
|
238
|
|
|
} |
|
239
|
17 |
|
} |
|
240
|
|
|
|
|
241
|
17 |
|
/** |
|
242
|
5 |
|
* Call the /admin/ping servlet, can be used to quickly tell if a connection to the |
|
243
|
|
|
* server is available. |
|
244
|
|
|
* |
|
245
|
17 |
|
* Simply overrides the SolrPhpClient implementation, changing ping from a |
|
246
|
3 |
|
* HEAD to a GET request, see http://forge.typo3.org/issues/44167 |
|
247
|
3 |
|
* |
|
248
|
3 |
|
* Also does not report the time, see https://forge.typo3.org/issues/64551 |
|
249
|
|
|
* |
|
250
|
|
|
* @param boolean $useCache indicates if the ping result should be cached in the instance or not |
|
251
|
17 |
|
* @return bool TRUE if Solr can be reached, FALSE if not |
|
252
|
3 |
|
*/ |
|
253
|
3 |
|
public function ping($useCache = true) |
|
254
|
|
|
{ |
|
255
|
|
|
try { |
|
256
|
17 |
|
$httpResponse = $this->performPingRequest($useCache); |
|
257
|
|
|
} catch (HttpException $exception) { |
|
258
|
|
|
return false; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
return ($httpResponse->getHttpStatus() === 200); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Call the /admin/ping servlet, can be used to get the runtime of a ping request. |
|
266
|
|
|
* |
|
267
|
|
|
* @param boolean $useCache indicates if the ping result should be cached in the instance or not |
|
268
|
|
|
* @return double runtime in milliseconds |
|
269
|
3 |
|
* @throws \ApacheSolrForTypo3\Solr\PingFailedException |
|
270
|
|
|
*/ |
|
271
|
3 |
|
public function getPingRoundTripRuntime($useCache = true) |
|
272
|
3 |
|
{ |
|
273
|
3 |
|
try { |
|
274
|
|
|
$start = $this->getMilliseconds(); |
|
275
|
|
|
$httpResponse = $this->performPingRequest($useCache); |
|
276
|
|
|
$end = $this->getMilliseconds(); |
|
277
|
|
|
} catch (HttpException $e) { |
|
278
|
|
|
$message = 'Solr ping failed with unexpected response code: ' . $e->getCode(); |
|
279
|
|
|
/** @var $exception \ApacheSolrForTypo3\Solr\PingFailedException */ |
|
280
|
|
|
$exception = GeneralUtility::makeInstance(PingFailedException::class, /** @scrutinizer ignore-type */ $message); |
|
281
|
|
|
throw $exception; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
3 |
|
if ($httpResponse->getHttpStatus() !== 200) { |
|
285
|
|
|
$message = 'Solr ping failed with unexpected response code: ' . $httpResponse->getHttpStatus(); |
|
286
|
3 |
|
/** @var $exception \ApacheSolrForTypo3\Solr\PingFailedException */ |
|
287
|
|
|
$exception = GeneralUtility::makeInstance(PingFailedException::class, /** @scrutinizer ignore-type */ $message); |
|
288
|
3 |
|
throw $exception; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
return $end - $start; |
|
292
|
3 |
|
} |
|
293
|
3 |
|
|
|
294
|
3 |
|
/** |
|
295
|
|
|
* Performs a ping request and returns the result. |
|
296
|
|
|
* |
|
297
|
|
|
* @param boolean $useCache indicates if the ping result should be cached in the instance or not |
|
298
|
|
|
* @return ResponseAdapter |
|
299
|
|
|
*/ |
|
300
|
|
|
protected function performPingRequest($useCache = true) |
|
301
|
|
|
{ |
|
302
|
|
|
$cacheKey = (string)($this); |
|
303
|
|
|
if ($useCache && isset(static::$pingCache[$cacheKey])) { |
|
304
|
|
|
return static::$pingCache[$cacheKey]; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
$pingQuery = $this->client->createPing(); |
|
308
|
|
|
$pingResult = $this->createAndExecuteRequest($pingQuery); |
|
309
|
|
|
|
|
310
|
|
|
if ($useCache) { |
|
311
|
|
|
static::$pingCache[$cacheKey] = $pingResult; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
return $pingResult; |
|
315
|
74 |
|
} |
|
316
|
|
|
|
|
317
|
74 |
|
/** |
|
318
|
74 |
|
* Returns the current time in milliseconds. |
|
319
|
|
|
* |
|
320
|
|
|
* @return double |
|
321
|
|
|
*/ |
|
322
|
|
|
protected function getMilliseconds() |
|
323
|
|
|
{ |
|
324
|
|
|
return GeneralUtility::milliseconds(); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
3 |
|
* @param QueryInterface $query |
|
329
|
|
|
* @return ResponseAdapter |
|
330
|
|
|
*/ |
|
331
|
3 |
|
protected function createAndExecuteRequest(QueryInterface $query): ResponseAdapter |
|
332
|
3 |
|
{ |
|
333
|
2 |
|
$request = $this->client->createRequest($query); |
|
334
|
1 |
|
return $this->executeRequest($request); |
|
335
|
1 |
|
} |
|
336
|
|
|
|
|
337
|
1 |
|
/** |
|
338
|
1 |
|
* @param $request |
|
339
|
|
|
* @return ResponseAdapter |
|
340
|
|
|
*/ |
|
341
|
2 |
|
protected function executeRequest($request): ResponseAdapter |
|
342
|
|
|
{ |
|
343
|
|
|
$result = $this->client->executeRequest($request); |
|
344
|
|
|
return new ResponseAdapter($result->getBody(), $result->getStatusCode(), $result->getStatusMessage()); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
2 |
|
* @param string $url |
|
349
|
|
|
* @param string $httpMethod |
|
350
|
|
|
* @return Request |
|
351
|
|
|
*/ |
|
352
|
|
|
protected function buildSolariumRequestFromUrl($url, $httpMethod = Request::METHOD_GET): Request |
|
353
|
|
|
{ |
|
354
|
|
|
$params = []; |
|
355
|
|
|
parse_str(parse_url($url, PHP_URL_QUERY), $params); |
|
356
|
|
|
|
|
357
|
|
|
$path = parse_url($url, PHP_URL_PATH); |
|
358
|
|
|
$endpoint = $this->getPrimaryEndpoint(); |
|
359
|
|
|
$coreBasePath = $endpoint->getPath() . '/' . $endpoint->getCore() . '/'; |
|
360
|
|
|
$handler = str_replace($coreBasePath, '', $path); |
|
361
|
|
|
|
|
362
|
|
|
$request = new Request(); |
|
363
|
1 |
|
$request->setMethod($httpMethod); |
|
364
|
|
|
$request->setParams($params); |
|
365
|
|
|
$request->setHandler($handler); |
|
366
|
1 |
|
return $request; |
|
367
|
1 |
|
} |
|
368
|
|
|
} |
|
369
|
|
|
|