This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdkan_acquia_search_solr.module
135 lines (125 loc) · 4.16 KB
/
dkan_acquia_search_solr.module
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
<?php
/**
* @file
* Code for the HHS Search feature.
*/
/**
* Queries the search_api_server for dkan_acquia_solr.
*/
function dkan_acquia_search_solr_query_dkan_server($server) {
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'search_api_server', '=')
->propertyCondition('machine_name', $server)
->execute();
if (isset($result['search_api_server'])) {
return $result['search_api_server'];
}
return FALSE;
}
/*
* Implements hook_environment_switch.
*/
function dkan_acquia_search_solr_environment_switch($target_env, $source_env) {
features_revert_module('dkan_dataset_groups');
features_revert_module('dkan_sitewide_search_db');
features_revert_module('dkan_data_story');
}
/**
* Implements hook_default_search_api_server().
*/
function dkan_acquia_search_solr_default_search_api_server() {
return array(
'dkan_acquia_solr' => entity_create(
'search_api_server',
array(
'name' => 'DKAN Acquia Solr',
'machine_name' => 'dkan_acquia_solr',
'description' => 'Custom Search API server for DKAN SOLR integration with Acquia',
'class' => 'acquia_search_service',
'enabled' => 0,
'options' => array(
'port' => 443,
'scheme' => 'https',
'http_method' => 'AUTO',
'clean_ids' => true,
'site_hash' => true,
),
)
),
);
}
/**
* Implements hook_default_search_api_index_alter().
*/
function dkan_acquia_search_solr_default_search_api_index_alter(array &$defaults) {
// List of the possible search api indexes.
$index_list = array(
"stories_index",
"datasets",
"groups_di",
);
// Check if a custom server has been set in $settings.php or elsewhere.
if (!$server_override = variable_get('dkan_acquia_search_solr_override', false)) {
// No override is set so set to acquia by default.
$server_override = "dkan_acquia_solr";
}
$cores_available = _acquia_solr_cores_available();
$env = $_ENV['AH_SITE_ENVIRONMENT'];
// Only alter the indexes if there's a matching core for the environment.
if (isset($cores_available[$env])) {
foreach($index_list as $index) {
if (isset($defaults[$index])) {
$defaults[$index]->server = $server_override;
}
}
}
}
/**
* Implements hook_default_search_api_server_alter().
*/
function dkan_acquia_search_solr_default_search_api_server_alter(array &$defaults) {
// List of the possible search api indexes.
$server_list = array(
"groups_server",
"datasets",
"dkan_acquia_solr",
"local_solr_server",
"stories_server"
);
// Only enable the solr server if there's a matching core for the environment.
$cores_available = _acquia_solr_cores_available();
$env = $_ENV['AH_SITE_ENVIRONMENT'];
if (isset($cores_available[$env])) {
// Disable ALL servers by default.
foreach($server_list as $server) {
if (isset($defaults[$server])) {
$defaults[$server]->enabled = false;
}
}
// Check if a custom server has been set in $settings.php or elsewhere.
if (!$server_override = variable_get('dkan_acquia_search_solr_override', false)) {
// No override is set so enable the acquia server.
$defaults['dkan_acquia_solr']->enabled = true;
}
else {
$defaults[$server_override]->enabled = true;
}
}
}
function _acquia_solr_cores_available() {
$identifier = acquia_agent_settings('acquia_identifier');
$subscription = acquia_agent_settings('acquia_subscription_data');
$key = acquia_agent_settings('acquia_key');
$derived_key_salt = $subscription['derived_key_salt'];
$search_cores = $subscription['heartbeat_data']['search_cores'];
foreach ($search_cores as $key => $core) {
$derivation_string = $core['core_id'] . 'solr' . $derived_key_salt;
$derived_key = hash_hmac('sha1', str_pad($derivation_string, 80, $derivation_string), $key);
$search_cores[$key]['derived_key'] = $derived_key;
$search_env = explode('.', $core['core_id']);
$search_env = count($search_env) > 1 ? $search_env[1] : 'prod';
$search_cores[$search_env] = $search_cores[$key];
unset($search_cores[$key]);
}
return $search_cores;
}