|
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 Solarium\Core\Client\Endpoint; |
|
21
|
|
|
use UnexpectedValueException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Represent a server node of solr, in the most setups you would only have one, but sometimes |
|
25
|
|
|
* multiple for reading and writing. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Timo Hund <[email protected]> |
|
28
|
|
|
* @copyright Copyright (c) 2009-2020 Timo Hund <[email protected]> |
|
29
|
|
|
* |
|
30
|
|
|
* @deprecated Class will be removed with Ext:solr 12.x. Use class \Solarium\Core\Client\Endpoint instead. |
|
31
|
|
|
*/ |
|
32
|
|
|
class Node extends Endpoint |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Node constructor. |
|
36
|
|
|
* @param string $scheme |
|
37
|
|
|
* @param string $host |
|
38
|
|
|
* @param int $port |
|
39
|
|
|
* @param string $path |
|
40
|
|
|
* @param ?string $username |
|
41
|
|
|
* @param ?string $password |
|
42
|
|
|
*/ |
|
43
|
128 |
|
public function __construct( |
|
44
|
|
|
string $scheme = 'http', |
|
45
|
|
|
string $host = 'localhost', |
|
46
|
|
|
int $port = 8983, |
|
47
|
|
|
string $path = '/solr/core_en/', |
|
48
|
|
|
?string $username = null, |
|
49
|
|
|
?string $password = null |
|
50
|
|
|
) { |
|
51
|
128 |
|
$path = (string)$path; |
|
52
|
128 |
|
$elements = explode('/', trim($path, '/')); |
|
53
|
128 |
|
$coreName = (string)array_pop($elements); |
|
54
|
|
|
// Remove API version |
|
55
|
128 |
|
array_pop($elements); |
|
56
|
|
|
|
|
57
|
|
|
// The path should always have the same format! |
|
58
|
128 |
|
$path = trim(implode('/', $elements), '/'); |
|
59
|
|
|
|
|
60
|
128 |
|
$options = [ |
|
61
|
|
|
'scheme' => $scheme, |
|
62
|
|
|
'host' => $host, |
|
63
|
|
|
'port' => $port, |
|
64
|
128 |
|
'path' => '/' . $path, |
|
65
|
|
|
'collection' => null, |
|
66
|
|
|
'core' => $coreName, |
|
67
|
|
|
'leader' => false, |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
128 |
|
parent::__construct($options); |
|
71
|
128 |
|
$this->setAuthentication($username, $password); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array $configuration |
|
76
|
|
|
* @return Node |
|
77
|
|
|
*/ |
|
78
|
129 |
|
public static function fromArray(array $configuration): Node |
|
79
|
|
|
{ |
|
80
|
129 |
|
static::checkIfRequiredKeyIsSet($configuration, 'scheme'); |
|
81
|
128 |
|
static::checkIfRequiredKeyIsSet($configuration, 'host'); |
|
82
|
128 |
|
static::checkIfRequiredKeyIsSet($configuration, 'port'); |
|
83
|
128 |
|
static::checkIfRequiredKeyIsSet($configuration, 'path'); |
|
84
|
|
|
|
|
85
|
128 |
|
$scheme = $configuration['scheme']; |
|
86
|
128 |
|
$host = $configuration['host']; |
|
87
|
128 |
|
$port = $configuration['port']; |
|
88
|
128 |
|
$path = $configuration['path']; |
|
89
|
|
|
|
|
90
|
128 |
|
$username = $configuration['username'] ?? ''; |
|
91
|
128 |
|
$password = $configuration['password'] ?? ''; |
|
92
|
128 |
|
return new Node($scheme, $host, $port, $path, $username, $password); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Checks if the required configuration option is set. |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $configuration |
|
99
|
|
|
* @param string $name |
|
100
|
|
|
* @throws UnexpectedValueException |
|
101
|
|
|
*/ |
|
102
|
129 |
|
protected static function checkIfRequiredKeyIsSet(array $configuration, string $name) |
|
103
|
|
|
{ |
|
104
|
129 |
|
if (empty($configuration[$name])) { |
|
105
|
1 |
|
throw new UnexpectedValueException('Required solr connection property ' . $name . ' is missing.'); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
113 |
|
public function getUsername(): string |
|
113
|
|
|
{ |
|
114
|
113 |
|
return (string)$this->getOption('username'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function getPassword(): string |
|
121
|
|
|
{ |
|
122
|
1 |
|
return (string)$this->getOption('password'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Returns the path including api path. |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getCoreBasePath(): string |
|
131
|
|
|
{ |
|
132
|
|
|
$pathWithoutLeadingAndTrailingSlashes = trim(trim($this->getPath()), '/'); |
|
|
|
|
|
|
133
|
|
|
$pathWithoutLastSegment = substr($pathWithoutLeadingAndTrailingSlashes, 0, strrpos($pathWithoutLeadingAndTrailingSlashes, '/')); |
|
134
|
|
|
return ($pathWithoutLastSegment === '') ? '/' : '/' . $pathWithoutLastSegment . '/'; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Returns the core name from the configured path. |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
* @deprecated Will be removed with Ext:solr 12.x. Use method getCore() instead. |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getCoreName(): string |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->getCore(); |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return array |
|
150
|
|
|
*/ |
|
151
|
111 |
|
public function getSolariumClientOptions(): array |
|
152
|
|
|
{ |
|
153
|
|
|
return [ |
|
154
|
111 |
|
'host' => $this->getHost(), |
|
155
|
111 |
|
'port' => $this->getPort(), |
|
156
|
111 |
|
'scheme' => $this->getScheme(), |
|
157
|
111 |
|
'path' => $this->getPath(), |
|
158
|
111 |
|
'core' => $this->getCore(), |
|
159
|
|
|
]; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return string |
|
164
|
|
|
* @deprecated Will be removed with Ext:solr 12.x. Use methods getCoreBaseUri() for API version 1 instead |
|
165
|
|
|
*/ |
|
166
|
11 |
|
public function __toString(): string |
|
167
|
|
|
{ |
|
168
|
11 |
|
return $this->getCoreBaseUri(); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|