|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CWP\Search\Solr; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use SilverStripe\Core\Config\Configurable; |
|
7
|
|
|
use SilverStripe\Core\Environment; |
|
8
|
|
|
use SilverStripe\FullTextSearch\Solr\Solr; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* CwpSolr configures Solr in a CWP-compatible manner. |
|
12
|
|
|
*/ |
|
13
|
|
|
class CwpSolr |
|
14
|
|
|
{ |
|
15
|
|
|
use Configurable; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private static $options; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Configure Solr. |
|
25
|
|
|
* |
|
26
|
|
|
* $options - An array consisting of: |
|
27
|
|
|
* |
|
28
|
|
|
* 'extraspath' - (String) Where to find Solr core configuartion files. |
|
29
|
|
|
* Defaults to '<BASE_PATH>/mysite/conf/extras'. |
|
30
|
|
|
* 'version' - select the Solr configuration to use when in CWP. One of: |
|
31
|
|
|
* * 'cwp-4': preferred version, uses secured 4.x service available on CWP |
|
32
|
|
|
* * 'local-4': this can be use for development using silverstripe-localsolr package, 4.x branch |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function configure() |
|
35
|
|
|
{ |
|
36
|
|
|
if (!class_exists(Solr::class)) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// get options from configuration |
|
41
|
|
|
$options = static::config()->get('options'); |
|
42
|
|
|
|
|
43
|
|
|
// get version specific options |
|
44
|
|
|
switch ($options['version']) { |
|
45
|
|
|
case 'cwp-4': |
|
46
|
|
|
$solrOptions = self::options_for_cwp($options); |
|
47
|
|
|
break; |
|
48
|
|
|
case 'local-4': |
|
49
|
|
|
$solrOptions = self::options_for_local($options); |
|
50
|
|
|
break; |
|
51
|
|
|
default: |
|
52
|
|
|
throw new InvalidArgumentException(sprintf( |
|
53
|
|
|
'Solr version "%s" is not supported on CWP. Please use "local-4" on local ' . |
|
54
|
|
|
'and "cwp-4" on production. For preferred configuration see ' . |
|
55
|
|
|
'https://www.cwp.govt.nz/developer-docs/.', |
|
56
|
|
|
$options['version'] |
|
57
|
|
|
)); |
|
58
|
|
|
break; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Allow users to override extras path. |
|
62
|
|
|
// CAUTION: CWP does not permit usage of customised solrconfig.xml. |
|
63
|
|
|
if (isset($options['extraspath']) && file_exists($options['extraspath'])) { |
|
64
|
|
|
$solrOptions['extraspath'] = $options['extraspath']; |
|
65
|
|
|
} elseif (file_exists(BASE_PATH . '/mysite/conf/extras')) { |
|
66
|
|
|
$solrOptions['extraspath'] = BASE_PATH . '/mysite/conf/extras'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
Solr::configure_server($solrOptions); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $options |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function options_for_cwp($options) |
|
77
|
|
|
{ |
|
78
|
|
|
$version = $options['version']; |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
return [ |
|
81
|
|
|
'host' => Environment::getEnv('SOLR_SERVER'), |
|
82
|
|
|
'port' => Environment::getEnv('SOLR_PORT'), |
|
83
|
|
|
'path' => '/v4/', |
|
84
|
|
|
'version' => 4, |
|
85
|
|
|
'indexstore' => [ |
|
86
|
|
|
'mode' => CwpSolrConfigStore::class, |
|
87
|
|
|
'path' => '/v4', |
|
88
|
|
|
], |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $options |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function options_for_local($options) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
return [ |
|
100
|
|
|
'host' => Environment::getEnv('SOLR_SERVER') ? Environment::getEnv('SOLR_SERVER') : 'localhost', |
|
101
|
|
|
'port' => Environment::getEnv('SOLR_PORT') ? Environment::getEnv('SOLR_PORT') : 8983, |
|
102
|
|
|
'path' => Environment::getEnv('SOLR_PATH') ? Environment::getEnv('SOLR_PATH') : '/solr/', |
|
103
|
|
|
'version' => 4, |
|
104
|
|
|
'indexstore' => [ |
|
105
|
|
|
'mode' => Environment::getEnv('SOLR_MODE') ? Environment::getEnv('SOLR_MODE') : 'file', |
|
106
|
|
|
'auth' => Environment::getEnv('SOLR_AUTH') ? Environment::getEnv('SOLR_AUTH') : null, |
|
107
|
|
|
// Allow storing the solr index and config data in an arbitrary location, |
|
108
|
|
|
// e.g. outside of the webroot |
|
109
|
|
|
'path' => Environment::getEnv('SOLR_INDEXSTORE_PATH') |
|
110
|
|
|
? Environment::getEnv('SOLR_INDEXSTORE_PATH') |
|
111
|
|
|
: BASE_PATH . '/.solr', |
|
112
|
|
|
'remotepath' => Environment::getEnv('SOLR_REMOTE_PATH') |
|
113
|
|
|
? Environment::getEnv('SOLR_REMOTE_PATH') |
|
114
|
|
|
: null |
|
115
|
|
|
] |
|
116
|
|
|
]; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|