1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Traits; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Firesphere\SolrSearch\Helpers\SolrLogger; |
9
|
|
|
use Firesphere\SolrSearch\Interfaces\ConfigStore; |
10
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
11
|
|
|
use Solarium\Client; |
12
|
|
|
use Solarium\QueryType\Server\CoreAdmin\Query\Query; |
13
|
|
|
use Solarium\QueryType\Server\CoreAdmin\Result\StatusResult; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait CoreAdminTrait is the trait that helps with Admin operations. |
17
|
|
|
|
18
|
|
|
* Core operations, such as reload, load, create, unload etc. are supplied by this trait. |
19
|
|
|
* |
20
|
|
|
* @package Firesphere\SolrSearch\Traits |
21
|
|
|
*/ |
22
|
|
|
trait CoreAdminTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Query A core admin object |
26
|
|
|
*/ |
27
|
|
|
protected $admin; |
28
|
|
|
/** |
29
|
|
|
* @var Client The current client |
30
|
|
|
*/ |
31
|
|
|
protected $client; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a new core |
35
|
|
|
* |
36
|
|
|
* @param $core string - The name of the core |
37
|
|
|
* @param ConfigStore $configStore |
38
|
|
|
* @return bool |
39
|
|
|
* @throws Exception |
40
|
|
|
* @throws GuzzleException |
41
|
|
|
*/ |
42
|
|
|
public function coreCreate($core, $configStore): bool |
43
|
|
|
{ |
44
|
|
|
$action = $this->admin->createCreate(); |
45
|
|
|
|
46
|
|
|
$action->setCore($core); |
47
|
|
|
$action->setInstanceDir($configStore->instanceDir($core)); |
48
|
|
|
$this->admin->setAction($action); |
49
|
|
|
try { |
50
|
|
|
$response = $this->client->coreAdmin($this->admin); |
51
|
|
|
|
52
|
|
|
return $response->getWasSuccessful(); |
53
|
|
|
} catch (Exception $e) { |
54
|
|
|
$solrLogger = new SolrLogger(); |
55
|
|
|
$solrLogger->saveSolrLog('Config'); |
56
|
|
|
|
57
|
|
|
throw new Exception($e); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Reload the given core |
63
|
|
|
* |
64
|
|
|
* @param $core |
65
|
|
|
* @return StatusResult|null |
66
|
|
|
*/ |
67
|
|
|
public function coreReload($core) |
68
|
|
|
{ |
69
|
|
|
$reload = $this->admin->createReload(); |
70
|
|
|
$reload->setCore($core); |
71
|
|
|
|
72
|
|
|
$this->admin->setAction($reload); |
73
|
|
|
|
74
|
|
|
$response = $this->client->coreAdmin($this->admin); |
75
|
|
|
|
76
|
|
|
return $response->getStatusResult(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Check the status of a core |
81
|
|
|
* |
82
|
|
|
* @deprecated backward compatibility stub |
83
|
|
|
* @param string $core |
84
|
|
|
* @return StatusResult|null |
85
|
|
|
*/ |
86
|
|
|
public function coreIsActive($core) |
87
|
|
|
{ |
88
|
|
|
return $this->coreStatus($core); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the core status |
93
|
|
|
* |
94
|
|
|
* @param string $core |
95
|
|
|
* @return StatusResult|null |
96
|
|
|
*/ |
97
|
|
|
public function coreStatus($core) |
98
|
|
|
{ |
99
|
|
|
$status = $this->admin->createStatus(); |
100
|
|
|
$status->setCore($core); |
101
|
|
|
|
102
|
|
|
$this->admin->setAction($status); |
103
|
|
|
$response = $this->client->coreAdmin($this->admin); |
104
|
|
|
|
105
|
|
|
return $response->getStatusResult(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Remove a core from Solr |
110
|
|
|
* |
111
|
|
|
* @param string $core core name |
112
|
|
|
* @return StatusResult|null A result is successful |
113
|
|
|
*/ |
114
|
|
|
public function coreUnload($core) |
115
|
|
|
{ |
116
|
|
|
$unload = $this->admin->createUnload(); |
117
|
|
|
$unload->setCore($core); |
118
|
|
|
|
119
|
|
|
$this->admin->setAction($unload); |
120
|
|
|
$response = $this->client->coreAdmin($this->admin); |
121
|
|
|
|
122
|
|
|
return $response->getStatusResult(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|