1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CWP\Search\Solr; |
4
|
|
|
|
5
|
|
|
use SilverStripe\FullTextSearch\Solr\Solr; |
6
|
|
|
use SilverStripe\FullTextSearch\Solr\Stores\SolrConfigStore; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class CwpSolrConfigStore |
10
|
|
|
* |
11
|
|
|
* Uploads configuration to Solr via the PHP proxy CWP uses to filter requests |
12
|
|
|
*/ |
13
|
|
|
class CwpSolrConfigStore implements SolrConfigStore |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $remote = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $url = ''; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param array $config |
27
|
|
|
*/ |
28
|
|
|
public function __construct($config) |
29
|
|
|
{ |
30
|
|
|
$options = Solr::solr_options(); |
31
|
|
|
|
32
|
|
|
$this->url = implode('', [ |
33
|
|
|
'http://', |
34
|
|
|
isset($config['auth']) ? $config['auth'] . '@' : '', |
35
|
|
|
$options['host'] . ':' . $options['port'], |
36
|
|
|
$config['path'] |
37
|
|
|
]); |
38
|
|
|
$this->remote = $config['remotepath']; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $index |
43
|
|
|
* @param string $file |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function uploadFile($index, $file) |
47
|
|
|
{ |
48
|
|
|
$this->uploadString($index, basename($file), file_get_contents($file)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
* @param string $index |
54
|
|
|
* @param string $filename |
55
|
|
|
* @param string $string |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function uploadString($index, $filename, $string) |
59
|
|
|
{ |
60
|
|
|
$targetDir = "{$this->url}/config/$index"; |
61
|
|
|
|
62
|
|
|
file_get_contents($targetDir . '/' . $filename, false, stream_context_create([ |
63
|
|
|
'http' => [ |
64
|
|
|
'method' => 'POST', |
65
|
|
|
'header' => 'Content-type: application/octet-stream', |
66
|
|
|
'content' => (string) $string |
67
|
|
|
] |
68
|
|
|
])); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $index |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function instanceDir($index) |
76
|
|
|
{ |
77
|
|
|
return $this->remote ? "{$this->remote}/$index" : $index; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|