forked from mkalkbrenner/search_api_solr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_api_solr.install
538 lines (497 loc) · 18.8 KB
/
search_api_solr.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<?php
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\search_api_solr\SolrMultilingualBackendInterface;
use Drupal\Core\Url;
/**
* Implements hook_requirements().
*/
function search_api_solr_requirements($phase) {
$requirements = [];
if ($phase == 'install') {
if (!class_exists('\Solarium\Core\Client\Request')) {
$requirements['search_api_solr_library'] = [
'description' => t('Solr search requires the solarium/solarium library.'),
'severity' => REQUIREMENT_ERROR,
];
}
}
elseif ($phase == 'runtime') {
$servers = search_api_solr_get_servers();
$count = 0;
$unavailable = 0;
$multiple_indexes = 0;
$last = NULL;
foreach ($servers as $server_id => $server) {
if (!$server->getBackend()->isAvailable()) {
++$unavailable;
$last = $server;
}
$indexes = $server->getIndexes();
if (count($indexes) > 1) {
$active = 0;
foreach ($indexes as $index) {
if ($index->status()) {
++$active;
}
}
if ($active > 1) {
++$multiple_indexes;
}
}
++$count;
}
if (!$count) {
return [];
}
$requirements['search_api_solr'] = [
'title' => \Drupal::translation()->translate('Solr servers'),
'value' => \Drupal::translation()->formatPlural($count, '1 server', '@count servers'),
];
if ($unavailable) {
if ($unavailable == 1) {
$requirements['search_api_solr']['description'] = \Drupal::translation()->translate('The Solr server of <a href=":url">%name</a> could not be reached.',
[':url' => Url::fromRoute('entity.search_api_server.canonical', ['search_api_server' => $last->id()])->toString(), '%name' => $last->label()]);
}
else {
$requirements['search_api_solr']['description'] = \Drupal::translation()->translate('@count Solr servers could not be reached.', ['@count' => $unavailable]);
}
$requirements['search_api_solr']['severity'] = REQUIREMENT_ERROR;
}
else {
$requirements['search_api_solr']['description'] = \Drupal::translation()->formatPlural($count, 'The Solr server could be reached.', 'All @count Solr servers could be reached.');
$requirements['search_api_solr']['severity'] = REQUIREMENT_OK;
}
$requirements['search_api_solr_multiple_indexes'] = [
'title' => \Drupal::translation()->translate('Solr servers with multiple indexes'),
'value' => \Drupal::translation()->formatPlural($count, '1 server', '@count servers'),
];
if ($multiple_indexes) {
// @todo Warn if autocomplete uses an implementation that is not suitable
// for multiple indexes per core. But we need the possibility to turn that
// off if the user acknowledged it because he might really want to have
// it like this as a feature.
}
foreach ($servers as $server) {
$backend = $server->getBackend();
if ($backend instanceof SolrMultilingualBackendInterface) {
$key = 'search_api_solr_multilingual_' . $server->id();
$requirements[$key] = [
'title' => t('Multilingual Solr Server'),
'value' => t('Schema complete'),
'severity' => REQUIREMENT_OK,
'description' => t('Schema on server <a href=":url">@server</a> contains all required language-specific fields.', [
':url' => $server->toUrl('canonical')->toString(),
'@server' => $server->label(),
]),
];
if ($backend->isAvailable()) {
$stats = $backend->getSchemaLanguageStatistics();
if ($missing_languages = array_filter($stats, function ($state) {
return !$state;
})) {
$requirements[$key]['value'] = t('Schema incomplete');
$requirements[$key]['severity'] = $backend->hasLanguageUndefinedFallback() ? REQUIREMENT_WARNING : REQUIREMENT_ERROR;
$requirements[$key]['description'] = t(
'There are some language-specific field types missing in schema of Solr server <a href=":url">@server</a>: @languages.', [
':url' => $server->toUrl('canonical')->toString(),
'@server' => $server->label(),
'@languages' => implode(', ', array_keys($missing_languages)),
]);
}
}
else {
$requirements[$key]['value'] = t('Not reachable');
$requirements[$key]['severity'] = REQUIREMENT_ERROR;
$requirements[$key]['description'] = t(
'Solr server <a href=":url">@server</a> is not reachable.', [
':url' => $server->toUrl('canonical')->toString(),
'@server' => $server->label(),
]);
}
}
}
}
return $requirements;
}
/**
* Implements hook_uninstall().
*/
function search_api_solr_uninstall() {
\Drupal::state()->delete('search_api_solr.last_optimize');
\Drupal::state()->delete('search_api_solr.endpoint.data');
}
/**
* Gets all backend configs for active Solr servers.
*
* @param string $pattern
*
* @return array
* All backend configs for active Solr servers keyed by server name.
*/
function search_api_solr_update_helper_get_backend_configs($pattern = 'solr') {
$config_factory = \Drupal::configFactory();
$backend_configs = [];
foreach ($config_factory->listAll('search_api.server.') as $server_name) {
$server = $config_factory->get($server_name);
// Covers search_api_solr_multilingual, too.
if (strpos($server->get('backend'), $pattern) !== FALSE) {
$backend_configs[$server_name] = $server->get('backend_config');
}
}
return $backend_configs;
}
/**
* Saves a modified backend config for a given Solr server.
*
* @param string $server_name
* @param array $backend_config
* @param bool $trusted_data
*/
function search_api_solr_update_helper_save_backend_config($server_name, array $backend_config, $trusted_data = TRUE) {
\Drupal::configFactory()->getEditable($server_name)
->set('backend_config', $backend_config)
->save($trusted_data);
}
/**
* Gets all index third party settings for active Solr servers.
*
* @param string $pattern
*
* @return array
* All backend configs for active Solr servers keyed by server name.
*/
function search_api_solr_update_helper_get_index_third_party_settings($pattern = 'solr') {
$config_factory = \Drupal::configFactory();
$index_third_party_settings = [];
foreach ($config_factory->listAll('search_api.index.') as $index_id) {
$index = $config_factory->get($index_id);
$backend_configs = search_api_solr_update_helper_get_backend_configs($pattern);
if (isset($backend_configs[$index->get('server')])) {
// This index belongs to a Solr server.
$index_third_party_settings[$index_id] = $index->get('third_party_settings.search_api_solr');
}
}
return $index_third_party_settings;
}
/**
* Saves a modified backend config for a given Solr server.
*
* @param string $index_id
* @param array $third_party_settings
* @param bool $trusted_data
*/
function search_api_solr_update_helper_save_index_third_party_settings($index_id, array $third_party_settings, $trusted_data = TRUE) {
\Drupal::configFactory()->getEditable($index_id)
->set('third_party_settings.search_api_solr', $third_party_settings)
->save($trusted_data);
}
/**
* Gets all solr field type configs.
*
* @return array
* All solr field type configs.
*/
function search_api_solr_update_helper_get_field_type_configs() {
$config_factory = \Drupal::configFactory();
$field_type_configs = [];
foreach ($config_factory->listAll('search_api_solr.solr_field_type.') as $field_type_name) {
$field_type_configs[$field_type_name] = $config_factory->get($field_type_name)->getRawData();
}
return $field_type_configs;
}
/**
* Saves a modified solr field type config.
*
* @param string $field_type_name
* @param array $field_type_config
* @param bool $trusted_data
*/
function search_api_solr_update_helper_save_field_type_config($field_type_name, array $field_type_config, $trusted_data = TRUE) {
\Drupal::configFactory()->getEditable($field_type_name)
->setData($field_type_config)
->save($trusted_data);
}
/**
* Helper function to install all new configs.
*
* @param string $directory
*/
function search_api_solr_update_helper_install_configs($directory = InstallStorage::CONFIG_OPTIONAL_DIRECTORY) {
$optional_install_path = \Drupal::moduleHandler()->getModule('search_api_solr')->getPath() . '/' . $directory;
if (is_dir($optional_install_path)) {
// Install any optional config the module provides.
$storage = new FileStorage($optional_install_path, StorageInterface::DEFAULT_COLLECTION);
/** @var \Drupal\Core\Config\ConfigInstallerInterface $config_installer */
$config_installer = \Drupal::service('config.installer');
$config_installer->installOptionalConfig($storage);
}
}
/**
* Split Solr paths stored in configurations into server and core parts.
*/
function search_api_solr_update_8001() {
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
$parts = explode('/', $backend_config['path']);
if (count($parts) > 2) {
$backend_config['core'] = array_pop($parts);
$backend_config['path'] = implode('/', $parts);
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
}
}
/**
* Convert http_user and http_pass to username and password config for Solarium.
*/
function search_api_solr_update_8002() {
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
$backend_config['username'] = $backend_config['http_user'];
$backend_config['password'] = $backend_config['http_pass'];
unset($backend_config['http_user'], $backend_config['http_pass']);
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
}
/**
* Add default timeout settings to existing configs.
*/
function search_api_solr_update_8003() {
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
$backend_config['timeout'] = 5;
$backend_config['index_timeout'] = 5;
$backend_config['optimize_timeout'] = 10;
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
}
/**
* Migrate existing backend configurations to the basic auth connector plugin.
*/
function search_api_solr_update_8004() {
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
$backend_config['connector'] = 'basic_auth';
$backend_config['connector_config'] = [];
foreach (['scheme', 'host', 'port', 'path', 'core', 'timeout', 'index_timeout', 'optimize_timeout', 'solr_version', 'http_method', 'username', 'password'] as $key) {
$backend_config['connector_config'][$key] = $backend_config[$key];
unset($backend_config[$key]);
}
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
}
/**
* Add commit_within settings to existing connector configs.
*/
function search_api_solr_update_8005() {
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
$backend_config['connector_config']['commit_within'] = 1000;
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
}
/**
* Add autocomplete settings to existing configs.
*/
function search_api_solr_update_8006() {
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
$backend_config['suggest_suffix'] = TRUE;
$backend_config['suggest_corrections'] = TRUE;
$backend_config['suggest_words'] = FALSE;
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
}
/**
* Remove old autocomplete settings in existing configs.
*/
function search_api_solr_update_8007() {
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
unset($backend_config['autocorrect_spell']);
unset($backend_config['autocorrect_suggest_words']);
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
}
/**
* Remove obsolete settings in existing configs.
*/
function search_api_solr_update_8008() {
$config_factory = \Drupal::configFactory();
foreach ($config_factory->listAll('search_api_solr.settings.') as $setting) {
$config = $config_factory->getEditable($setting);
$data = $config->getRawData();
unset($data['autocomplete_max_occurrences']);
unset($data['http_get_max_length']);
$config->setData($data);
$config->save(TRUE);
}
}
/**
* Remove Search API Multilingual Solr Search left-overs.
*/
function search_api_solr_update_8200() {
$database = Drupal::database();
$database->delete('key_value')
->condition('collection', 'system.schema')
->condition('name', 'search_api_solr_multilingual')
->execute();
}
/**
* Fix suggester field type.
*/
function search_api_solr_update_8201() {
foreach (search_api_solr_update_helper_get_field_type_configs() as $field_type_name => $field_type_config) {
if (!empty($field_type_config['solr_configs']) && !empty($field_type_config['solr_configs']['searchComponents'])) {
foreach ($field_type_config['solr_configs']['searchComponents'] as &$component) {
if ($component['name'] == 'suggest') {
foreach ($component['lst'] as &$lst) {
foreach ($lst['str'] as &$str) {
foreach ($lst['str'] as &$entry) {
if ($entry['name'] == 'field') {
$entry['VALUE'] = 'twm_suggest';
search_api_solr_update_helper_save_field_type_config($field_type_name, $field_type_config);
break;
}
}
}
}
}
}
}
}
}
/**
* Enable support for targeted domains for all backends and add custom codes.
*/
function search_api_solr_update_8202() {
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
if (!isset($backend_config['domain'])) {
if (isset($backend_config['sasm_domain'])) {
$backend_config['domain'] = $backend_config['sasm_domain'];
unset($backend_config['sasm_domain']);
}
else {
$backend_config['domain'] = 'generic';
}
}
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
foreach (search_api_solr_update_helper_get_field_type_configs() as $field_type_name => $field_type_config) {
if (!isset($field_type_config['custom_code'])) {
$field_type_config['custom_code'] = '';
}
search_api_solr_update_helper_save_field_type_config($field_type_name, $field_type_config);
}
}
/**
* Install new essential Solr field types.
*/
function search_api_solr_update_8203() {
// Removed.
}
/**
* Enable phrase suggestions support.
*/
function search_api_solr_update_8204() {
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
if (!isset($backend_config['suggest_phrases'])) {
$backend_config['suggest_phrases'] = FALSE;
}
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
}
/**
* Drop never implemented word suggestions support.
*/
function search_api_solr_update_8205() {
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
if (isset($backend_config['suggest_words'])) {
unset($backend_config['suggest_words']);
}
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
}
/**
* Remove obsolete autocomplete settings.
*/
function search_api_solr_update_8206() {
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
unset($backend_config['suggest_suffix']);
unset($backend_config['suggest_corrections']);
unset($backend_config['suggest_phrases']);
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
}
/**
* Fix language undefined field types.
*/
function search_api_solr_update_8207() {
foreach (search_api_solr_update_helper_get_field_type_configs() as $field_type_name => $field_type_config) {
if (strpos($field_type_name, 'text_und') === 0) {
if (!empty($field_type_config['field_type']) && !empty($field_type_config['field_type']['analyzers'])) {
foreach ($field_type_config['field_type']['analyzers'] as &$component) {
foreach ($component['filters'] as &$filter) {
foreach ($filter as &$entry) {
if ($entry['class'] == 'solr.SnowballPorterFilterFactory') {
unset($entry);
}
}
}
}
}
search_api_solr_update_helper_save_field_type_config($field_type_name, $field_type_config);
}
}
}
/**
* Enable new highlighter.
*/
function search_api_solr_update_8208() {
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
if (isset($backend_config['excerpt'])) {
unset($backend_config['excerpt']);
}
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('search_api_solr.standard_highlighter');
$data = $config->getRawData();
unset($data['excerpt']);
$data['highlight']['snippets'] = 3;
$data['highlight']['fragsize'] = 0;
$config->setData($data);
$config->save(TRUE);
}
/**
* Install missing configs for 8.x-2.0-alpha3.
*/
function search_api_solr_update_8209() {
// search_api_solr_update_helper_install_configs(InstallStorage::CONFIG_INSTALL_DIRECTORY);
}
/**
* Install missing configs for 8.x-2.0-beta1.
*/
function search_api_solr_update_8210() {
search_api_solr_update_helper_install_configs(InstallStorage::CONFIG_OPTIONAL_DIRECTORY);
}
/**
* Add default finalize timeout settings to existing configs.
*/
function search_api_solr_update_8211() {
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
if (!isset($backend_config['finalize_timeout'])) {
$backend_config['finalize_timeout'] = 30;
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
}
}
/**
* Configure highlighter individually per index and remove global config.
*/
function search_api_solr_update_8212() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('search_api_solr.standard_highlighter');
$data = $config->getRawData();
foreach (search_api_solr_update_helper_get_index_third_party_settings() as $index_id => $third_party_settings) {
if (!isset($third_party_settings['highlighter'])) {
$third_party_settings['highlighter'] = $data;
}
search_api_solr_update_helper_save_index_third_party_settings($index_id, $third_party_settings);
}
$config->delete();
}