-
Notifications
You must be signed in to change notification settings - Fork 52
/
search_api_solr.install
2336 lines (2181 loc) · 88.1 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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Install, update and uninstall functions for the Search API Solr module.
*/
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\search_api_solr\Controller\SolrConfigSetController;
use Drupal\search_api_solr\SearchApiSolrConflictingEntitiesException;
use Drupal\search_api_solr\SearchApiSolrException;
use Drupal\search_api_solr\Utility\Utility as SearchApiSolrUtility;
use Solarium\Client;
/**
* Implements hook_requirements().
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\search_api\SearchApiException
*/
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') {
// @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 it might really be the desired
// feature.
$servers = search_api_solr_get_servers();
$module_extension_list = \Drupal::service('extension.list.module');
foreach ($servers as $server_id => $server) {
if ($server->status()) {
/** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
$backend = $server->getBackend();
$connector = $backend->getSolrConnector();
$configuration = $backend->getConfiguration();
if ($backend->isAvailable() && $connector->pingCore()) {
if (!$backend->isNonDrupalOrOutdatedConfigSetAllowed()) {
$config_set_controller = new SolrConfigSetController($module_extension_list);
$config_set_controller->setServer($server);
$new_config_set = [];
try {
// The freshly generated files.
$new_config_set = $config_set_controller->getConfigFiles();
}
catch (\Exception $e) {
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['title'] = t('Solr Server %server', ['%server' => $server->label()]);
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['value'] = t('Schema could not be generated');
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['severity'] = REQUIREMENT_ERROR;
if ($e instanceof SearchApiSolrConflictingEntitiesException) {
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['description'] = t(
'Some enabled parts of the configuration conflict with others: @conflicts', [
'@conflicts' => new FormattableMarkup(str_replace('core_issue_2919648_workaround', $server_id, $e), []),
]
);
}
else {
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['description'] = t(
'The config-set for the Solr server <a href=":url">@server</a> could not be generated.', [
':url' => $server->toUrl('canonical')->toString(),
'@server' => $server->label(),
]
);
}
}
if (!empty($new_config_set)) {
try {
$server_files_list = SearchApiSolrUtility::getServerFiles($server);
}
catch (SearchApiSolrException $e) {
$server_files_list = [];
}
// The files that are already on the server.
$server_file_names = array_keys($server_files_list);
$new_config_file_names = array_keys($new_config_set);
$extra_release_files = array_diff($new_config_file_names, $server_file_names);
if (!empty($extra_release_files)) {
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['title'] = t('Solr Server %server', ['%server' => $server->label()]);
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['value'] = t('Schema incomplete');
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['severity'] = REQUIREMENT_WARNING;
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['description'] = t(
'There are some files missing in the Solr server schema <a href=":url">@server</a>: @files. An updated config.zip should be downloaded and deployed to your Solr server.', [
':url' => $server->toUrl('canonical')->toString(),
'@server' => $server->label(),
'@files' => implode(', ', $extra_release_files),
]
);
}
foreach ($new_config_set as $new_file_name => $new_file_body) {
if (stripos(strrev($new_file_name), 'lmx.') === 0) {
$exception = FALSE;
try {
$server_file_data = $connector->getFile($new_file_name);
$server_file_body = $server_file_data->getBody();
}
catch (SearchApiSolrException $e) {
$server_file_body = '';
$exception = $e;
}
[
$version_number_server,
$xml_server,
] = SearchApiSolrUtility::normalizeXml($server_file_body);
[
$version_number_new,
$xml_new,
] = SearchApiSolrUtility::normalizeXml($new_file_body);
if (strcmp($xml_server, $xml_new) !== 0) {
if ($version_number_server !== $version_number_new) {
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['title'] = t('Solr Server %server', ['%server' => $server->label()]);
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['value'] = t('Schema not up to date');
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['severity'] = REQUIREMENT_ERROR;
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['description'] = t(
'There are some configuration elements missing in the Solr server schema <a href=":url">@server</a>. This is likely due to using an outdated version of either Drupal or Solr. The recommended version is: @version. An updated config.zip should be downloaded and deployed to your Solr server.', [
':url' => $server->toUrl('canonical')->toString(),
'@server' => $server->label(),
'@version' => $version_number_new,
]
);
break;
}
elseif ($version_number_server === $version_number_new && !$connector->isJumpStartConfigSet()) {
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['title'] = t('Solr Server %server', ['%server' => $server->label()]);
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['severity'] = REQUIREMENT_WARNING;
if (!$exception) {
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['value'] = t('Schema modified');
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['description'] = t(
'Your config-set contains manually added customizations. Be aware that these will be lost when the config-set needs to be regenerated.', []);
}
else {
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['value'] = t('Schema validation');
$requirements['search_api_solr_schema_' . $server_id . '_modifications']['description'] = t(
'File %filename could not be fetched from Solr server for validation: @message', [
'%filename' => $new_file_name,
'@message' => $exception->getMessage(),
]);
}
break;
}
}
}
}
}
if ($indexes = $server->getIndexes()) {
foreach ($indexes as $index) {
$endpoint = $backend->getCollectionEndpoint($index);
if ($connector->pingEndpoint($endpoint)) {
if (!$configuration['suppress_missing_languages']) {
$stats = $backend->getSchemaLanguageStatistics($endpoint);
if ($missing_languages = array_filter($stats, function ($state) {
return !$state;
})) {
$requirements['search_api_solr_schema_' . $server_id . $endpoint->getKey() . '_languages'] = [
'title' => t('Solr Server %server', ['%server' => $server->label()]),
'value' => t('Schema incomplete'),
'severity' => REQUIREMENT_WARNING,
'description' => t('There are some language-specific field types missing in schema of index <a href=":index_url">@index</a> on server <a href=":server_url">@server</a>: %missing. These languages will use the language-undefined fallback field type. In case of language variations like "de-at" the fallback will be the non-variation language "de" if it exists. You can suppress this warning by checking the corresponding checkbox in the advanced section on the server <a href=":server_edit_url">edit page</a>. But before doing this you should whether the missing language-specific field type exists or not. If it is available and you get this warning you simply need to generate and deploy a new Solr config-set.', [
':index_url' => $index->toUrl('canonical')
->toString(),
'@index' => $index->label(),
':server_url' => $server->toUrl('canonical')
->toString(),
'@server' => $server->label(),
'%missing' => implode(', ', array_keys($missing_languages)),
':server_edit_url' => $server->toUrl('edit-form')
->toString(),
]),
];
}
}
}
else {
$requirements['search_api_solr_' . $server_id . $endpoint->getKey() . '_endpoint'] = [
'title' => t('Solr Server %server', ['%server' => $server->label()]),
'value' => t('Endpoint not reachable'),
'description' => \Drupal::translation()
->translate('The endpoint for Index %index on Solr server <a href=":url">@server</a> could not be reached.',
[
'%index' => $index->label(),
':url' => Url::fromRoute('entity.search_api_server.canonical', ['search_api_server' => $server_id])
->toString(),
'@server' => $server->label(),
]),
'severity' => REQUIREMENT_ERROR,
];
}
}
}
}
}
else {
$requirements['search_api_solr_' . $server_id . '_modifications'] = [
'title' => t('Solr Server %server', ['%server' => $server->label()]),
'value' => t('Solr not reachable'),
'description' => t('Solr server <a href=":url">@server</a> is not reachable.', [
':url' => $server->toUrl('canonical')->toString(),
'@server' => $server->label(),
]),
'severity' => REQUIREMENT_ERROR,
];
}
}
}
$filename = __DIR__ . '/composer.json';
$composer = json_decode(file_get_contents($filename), TRUE);
if (
!preg_match('/([0123456789.]+)/', $composer['require']['solarium/solarium'], $matches) ||
!Client::checkMinimal($matches[1])
) {
$requirements['search_api_solr_solarium'] = [
'title' => t('Solarium library'),
'value' => t('Solarium library'),
'description' => t('Solarium @version is too old! Version required: @required', ['@version' => Client::getVersion(), '@required' => $matches[1] ?? t('Error reading composer.json')]),
'severity' => REQUIREMENT_ERROR,
];
}
}
return $requirements;
}
/**
* Implements hook_uninstall().
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\search_api\SearchApiException
*/
function search_api_solr_uninstall() {
\Drupal::state()->delete('search_api_solr.last_optimize');
\Drupal::state()->delete('search_api_solr.endpoint.data');
\Drupal::state()->delete('search_api_solr.site_hash');
\Drupal::state()->delete('search_api_solr.search_all_rows');
foreach (search_api_solr_get_servers() as $server) {
foreach ($server->getIndexes() as $index) {
\Drupal::state()
->delete('search_api_solr.' . $index->id() . '.last_update');
}
\Drupal::state()
->delete('search_api_solr.' . $server->id() . '.schema_parts');
}
}
/**
* Gets all backend configs for active Solr servers.
*
* @param string $pattern
* Pattern to filter the backend configs.
*
* @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
* Name of the solr server.
* @param array $backend_config
* Solr backend configuration.
* @param bool $trusted_data
* Boolean to indicate if the data is trusted.
*/
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 Solr servers.
*
* @param string $pattern
* Pattern to filter the server configs.
*
* @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') {
$backend_configs = search_api_solr_update_helper_get_backend_configs($pattern);
$indexes = search_api_solr_update_helper_get_indexes($pattern);
$index_third_party_settings = [];
foreach ($indexes as $index_id => $index) {
$config_id = 'search_api.server.' . $index->get('server');
if (isset($backend_configs[$config_id])) {
// 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
* Id of the solr index.
* @param array $third_party_settings
* Third party settings array.
* @param bool $trusted_data
* Boolean to indicate if the data is trusted.
*/
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 index settings for Solr servers.
*
* @param string $pattern
* Pattern to filter the index configs.
*
* @return array
* All index configs for Solr servers keyed by index name.
*/
function search_api_solr_update_helper_get_indexes($pattern = 'solr') {
$backend_configs = search_api_solr_update_helper_get_backend_configs($pattern);
$config_factory = \Drupal::configFactory();
$indexes = [];
foreach ($config_factory->listAll('search_api.index.') as $index_id) {
$index = $config_factory->get($index_id);
$config_id = 'search_api.server.' . $index->get('server');
if (isset($backend_configs[$config_id])) {
// This index belongs to a Solr server.
$indexes[$index_id] = $index;
}
}
return $indexes;
}
/**
* Saves a modified index config.
*
* @param string $index_id
* Id of the Solr Index.
* @param array $settings
* Settings array.
* @param bool $trusted_data
* Boolean to indicate if the data is trusted.
*/
function search_api_solr_update_helper_save_indexes($index_id, array $settings, $trusted_data = TRUE) {
\Drupal::configFactory()->getEditable($index_id)
->setData($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
* The field type config name.
* @param array $field_type_config
* The field type config.
* @param bool $trusted_data
* Boolean to indicate if the data is trusted.
*/
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
* The configs directory.
*/
function search_api_solr_update_helper_install_configs($directory = InstallStorage::CONFIG_OPTIONAL_DIRECTORY) {
/** @var \Drupal\Core\Config\ConfigInstallerInterface $config_installer */
$config_installer = \Drupal::service('config.installer');
$config_installer->installDefaultConfig('module', 'search_api_solr');
$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);
$config_installer->installOptionalConfig($storage);
}
$restrict_by_dependency = [
'module' => 'search_api_solr',
];
$config_installer->installOptionalConfig(NULL, $restrict_by_dependency);
}
/**
* Replace installed field type config with module version.
*
* @param string $field_type_name
* The field type config name.
* @param string $path
* The config path.
*/
function search_api_solr_update_helper_replace_field_type($field_type_name, $path = 'optional') {
$filename = __DIR__ . '/config/' . $path . '/' . $field_type_name . '.yml';
$field_type_config = Yaml::decode(file_get_contents($filename));
foreach (search_api_solr_update_helper_get_field_type_configs() as $name => $config) {
if ($name === $field_type_name) {
search_api_solr_update_helper_save_field_type_config($field_type_name, ['uuid' => $config['uuid']] + $field_type_config);
break;
}
}
}
/**
* 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);
}
}
/**
* Install Solr Field Types.
*/
function search_api_solr_update_8200() {
// 8.x-1.x to 8.x-2.x migration path is obsolete and therefore removed. Just
// install the latest field types.
\Drupal::entityDefinitionUpdateManager()
->installEntityType(new ConfigEntityType([
'id' => 'solr_field_type',
'label' => new TranslatableMarkup('Solr Field Type'),
'handlers' => [
'list_builder' => 'Drupal\search_api_solr\Controller\SolrFieldTypeListBuilder',
'form' => [
'add' => 'Drupal\search_api_solr\Form\SolrFieldTypeForm',
'edit' => 'Drupal\search_api_solr\Form\SolrFieldTypeForm',
'delete' => 'Drupal\search_api_solr\Form\SolrFieldTypeDeleteForm',
],
],
'config_prefix' => 'solr_field_type',
'admin_permission' => 'administer search_api',
'entity_keys' => [
'id' => 'id',
'label' => 'label',
'uuid' => 'uuid',
],
'links' => [
'edit-form' => '/admin/config/search/search-api/solr_field_type/{solr_field_type}',
'delete-form' => '/admin/config/search/search-api/solr_field_type/{solr_field_type}/delete',
'disable-for-server' => '/admin/config/search/search-api/server/{search_api_server}/solr_field_type/{solr_field_type}/disable',
'enable-for-server' => '/admin/config/search/search-api/server/{search_api_server}/solr_field_type/{solr_field_type}/enable',
'collection' => '/admin/config/search/search-api/server/{search_api_server}/solr_field_type',
],
]));
search_api_solr_update_helper_install_configs();
}
/**
* 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 &$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);
}
}
}
/**
* Removed search_api_solr_update_8203().
*/
/**
* 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) {
$save = FALSE;
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);
$save = TRUE;
}
}
}
}
}
if ($save) {
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);
}
/**
* Removed search_api_solr_update_8209().
*/
/**
* Removed search_api_solr_update_8210().
*/
/**
* 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();
}
/**
* Configure index prefixes individually per server and index.
*/
function search_api_solr_update_8213() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('search_api_solr.settings');
$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['advanced']) || !isset($third_party_settings['advanced']['index_prefix'])) {
$prefix = 'index_prefix_' . $index_id;
$third_party_settings['advanced']['index_prefix'] = $data[$prefix] ?? '';
unset($data[$prefix]);
search_api_solr_update_helper_save_index_third_party_settings($index_id, $third_party_settings);
}
}
foreach (search_api_solr_update_helper_get_backend_configs() as $server_name => $backend_config) {
if (!isset($backend_config['server_prefix'])) {
$backend_config['server_prefix'] = $data['index_prefix'] ?? '';
search_api_solr_update_helper_save_backend_config($server_name, $backend_config);
}
}
unset($data['index_prefix']);
$config->setData($data);
$config->save(TRUE);
}
/**
* Migrate Solr backends to the new unified Solr backend.
*/
function search_api_solr_update_8300() {
$config_factory = \Drupal::configFactory();
foreach ($config_factory->listAll('search_api_solr.solr_field_type.m_') as $field_type_name) {
$config_factory->getEditable($field_type_name)->delete();
}
foreach (search_api_solr_update_helper_get_field_type_configs() as $field_type_name => $field_type_config) {
unset($field_type_config['managed_schema']);
search_api_solr_update_helper_save_field_type_config($field_type_name, $field_type_config);
}
foreach ($config_factory->listAll('search_api.server.') as $server_name) {
$server = $config_factory->getEditable($server_name);
$backend_config = $server->get('backend_config');
switch ($server->get('backend')) {
case 'search_api_solr':
$backend_config['sasm_limit_search_page_to_content_language'] = FALSE;
$backend_config['sasm_search_page_include_language_independent'] = TRUE;
break;
case 'search_api_solr_multilingual':
case 'search_api_solr_multilingual_managed_schema':
unset($backend_config['sasm_language_unspecific_fallback_on_schema_issues']);
$server->set('backend', 'search_api_solr');
break;
case 'search_api_solr_any_schema':
$backend_config['sasm_limit_search_page_to_content_language'] = FALSE;
$backend_config['sasm_search_page_include_language_independent'] = TRUE;
$server->set('backend', 'search_api_solr');
break;
default:
continue(2);
}
$dependencies = $server->get('dependencies');
$dependencies['module'] = ['search_api_solr'];
$server
->set('backend_config', $backend_config)
->set('dependencies', $dependencies)
->save(TRUE);
}
foreach ($config_factory->listAll('search_api.index.') as $index_name) {
$index = $config_factory->getEditable($index_name);
$field_settings = $index->get('field_settings');
foreach ($field_settings as &$field_setting) {
if ('solr_string_doc_values' === $field_setting['type']) {
$field_setting['type'] = 'string';
}
}
$index->set('field_settings', $field_settings)->save(TRUE);
}
}
/**
* Field types clean-up.
*/
function search_api_solr_update_8301() {
$config_factory = \Drupal::configFactory();
foreach ($config_factory->listAll('search_api.index.') as $index_name) {
$index = $config_factory->getEditable($index_name);
$field_settings = $index->get('field_settings');
foreach ($field_settings as &$field_setting) {
if ('solr_text_ngram' === $field_setting['type']) {
$field_setting['type'] = 'solr_text_custom:edge';
}
if ('solr_string_ngram' === $field_setting['type']) {
$field_setting['type'] = 'solr_text_custom:edgestring';
}
if ('solr_text_phonetic' === $field_setting['type']) {
$field_setting['type'] = 'solr_text_custom:phonetic';
}
}
$index->set('field_settings', $field_settings)->save(TRUE);
}
}
/**
* Re-install language-specific field types to enable the new spellcheckers.
*/
function search_api_solr_update_8302() {
$config_factory = \Drupal::configFactory();
foreach ($config_factory->listAll('search_api_solr.solr_field_type.text_') as $field_type_name) {
if (preg_match('/^search_api_solr\.solr_field_type\.text_[a-z]{2}[_-]{1}/', $field_type_name)) {
$config_factory->getEditable($field_type_name)->delete();
}
}
}
/**
* Remove obsolete setting in config.
*/
function search_api_solr_update_8303() {
// Obsolete because of search_api_solr_update_8304().
}
/**
* Convert site_hash from setting to state.
*/
function search_api_solr_update_8304() {
$config_factory = \Drupal::configFactory();
$settings = $config_factory->getEditable('search_api_solr.settings');
\Drupal::state()
->set('search_api_solr.site_hash', $settings->get('site_hash') ?? '');
$settings->delete();
foreach ($config_factory->listAll('search_api.server.') as $server_name) {
$server = $config_factory->getEditable($server_name);
$backend_config = $server->get('backend_config');
$backend_config['optimize'] = FALSE;
$server->set('backend_config', $backend_config)->save(TRUE);
}
}
/**
* Add Dutch nouns and improve stemming for Dutch language.
*/
function search_api_solr_update_8305() {
$nouns = '';
foreach (search_api_solr_update_helper_get_field_type_configs() as $field_type_name => $field_type_config) {
if (strpos($field_type_name, 'text_nl') !== FALSE) {
$save = FALSE;
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) {
if ($filter['class'] === 'solr.SnowballPorterFilterFactory') {
if ($filter['language'] === 'Dutch') {
$filter['language'] = 'Kp';
$save = TRUE;
}
}
}
}
}
if (!empty($field_type_config['text_files'])) {
if (empty($field_type_config['text_files']['nouns']) || $field_type_config['text_files']['nouns'] == PHP_EOL) {
if (!$nouns) {
// We always use this hardcoded source file to have valid one for
// different domains created by users, example:
// text_nl_scientific_6_0_0.
$filename = __DIR__ . '/config/optional/search_api_solr.solr_field_type.text_nl_7_0_0.yml';
$nouns = Yaml::decode(file_get_contents($filename));
}
$field_type_config['text_files']['nouns'] = $nouns['text_files']['nouns'];
$save = TRUE;
}
}
if ($save) {
search_api_solr_update_helper_save_field_type_config($field_type_name, $field_type_config);
}
}
}
}
/**
* Replace deprecated Solr filters by their successors for Solr 7.
*/
function search_api_solr_update_8306() {
foreach (search_api_solr_update_helper_get_field_type_configs() as $field_type_name => $field_type_config) {
if (
version_compare($field_type_config['minimum_solr_version'], '7.0.0', '>=') &&
!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) {
switch ($filter['class']) {
case 'solr.WordDelimiterFilterFactory':
$filter['class'] = 'solr.WordDelimiterGraphFilterFactory';
break;
case 'solr.SynonymFilterFactory':
$filter['class'] = 'solr.SynonymGraphFilterFactory';
break;
case 'solr.StopFilterFactory':
unset($filter['enablePositionIncrements']);
break;