-
Notifications
You must be signed in to change notification settings - Fork 3
/
search_api.install
281 lines (249 loc) · 8.49 KB
/
search_api.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
<?php
/**
* @file
* Install, update and uninstall functions for the Search API module.
*/
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Link;
use Drupal\search_api\Entity\Server;
use Drupal\Core\Url;
/**
* Implements hook_schema().
*/
function search_api_schema() {
$schema['search_api_item'] = [
'description' => 'Stores the items which should be indexed for each index, and their state.',
'fields' => [
'index_id' => [
'description' => 'The ID of the index this item belongs to',
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
],
'datasource' => [
'description' => 'The plugin ID of the datasource this item belongs to',
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
],
'item_id' => [
'description' => 'The unique identifier of this item',
'type' => 'varchar',
'length' => 150,
'not null' => TRUE,
],
'changed' => [
'description' => 'A timestamp indicating when the item was last changed',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'status' => [
'description' => 'Boolean indicating the reindexation status, "1" when we need to reindex, "0" otherwise',
'type' => 'int',
'not null' => TRUE,
],
],
'indexes' => [
'indexing' => ['index_id', 'status', 'changed', 'item_id'],
],
'primary key' => ['index_id', 'item_id'],
];
return $schema;
}
/**
* Implements hook_uninstall().
*/
function search_api_uninstall() {
\Drupal::state()->delete('search_api_use_tracking_batch');
foreach (\Drupal::configFactory()->listAll('search_api.index.') as $index_id) {
\Drupal::state()->delete("search_api.index.$index_id.has_reindexed");
}
}
/**
* Implements hook_requirements().
*/
function search_api_requirements($phase) {
if ($phase == 'runtime') {
$requirements = [];
$message = _search_api_search_module_warning();
if ($message) {
$requirements += [
'search_api_core_search' => [
'title' => t('Search API'),
'value' => $message,
'severity' => REQUIREMENT_WARNING,
],
];
}
/** @var \Drupal\search_api\ServerInterface[] $servers */
$servers = Server::loadMultiple();
$unavailable_servers = [];
foreach ($servers as $server) {
if ($server->status() && !$server->isAvailable()) {
$unavailable_servers[] = $server->label();
}
}
if (!empty($unavailable_servers)) {
$requirements += [
'search_api_server_unavailable' => [
'title' => t('Search API'),
'value' => \Drupal::translation()->formatPlural(
count($unavailable_servers),
'The search server "@servers" is currently not available',
'The following search servers are not available: @servers',
['@servers' => implode(', ', $unavailable_servers)]
),
'severity' => REQUIREMENT_ERROR
]
];
}
$pending_tasks = \Drupal::getContainer()
->get('search_api.task_manager')
->getTasksCount();
if ($pending_tasks) {
$args['@link'] = '';
$url = Url::fromRoute('search_api.execute_tasks');
if ($url->access()) {
$link = new Link(t('Execute now'), $url);
$link = $link->toString();
$args['@link'] = $link;
$args['@link'] = new FormattableMarkup(' (@link)', $args);
}
$requirements['search_api_pending_tasks'] = [
'title' => t('Search API'),
'value' => \Drupal::translation()->formatPlural(
$pending_tasks,
'There is @count pending Search API task. @link',
'There are @count pending Search API tasks. @link',
$args
),
'severity' => REQUIREMENT_WARNING,
];
}
return $requirements;
}
return [];
}
/**
* Adapts index config schema to remove an unnecessary layer for plugins.
*/
function search_api_update_8101() {
// This update function updates search indexes for the change from
// https://www.drupal.org/node/2656052.
$config_factory = \Drupal::configFactory();
$plugin_types = [
'processor',
'datasource',
'tracker',
];
foreach ($config_factory->listAll('search_api.index.') as $index_id) {
$index = $config_factory->getEditable($index_id);
$changed = FALSE;
foreach ($plugin_types as $plugin_type) {
$property = $plugin_type . '_settings';
$plugins = $index->get($property);
foreach ($plugins as $id => $config) {
if (isset($config['plugin_id']) && isset($config['settings'])) {
$changed = TRUE;
$plugins[$id] = $config['settings'];
}
}
$index->set($property, $plugins);
}
if ($changed) {
// Mark the resulting configuration as trusted data. This avoids issues
// with future schema changes.
$index->save(TRUE);
}
}
return t('Index config schema updated.');
}
/**
* Removes unsupported cache plugins from Search API views.
*/
function search_api_update_8102() {
$config_factory = \Drupal::configFactory();
$changed = [];
foreach ($config_factory->listAll('views.view.') as $view_config_name) {
$view = $config_factory->getEditable($view_config_name);
$displays = $view->get('display');
if ($displays['default']['display_options']['query']['type'] === 'search_api_query') {
$change = FALSE;
foreach ($displays as $id => $display) {
if (!empty($display['display_options']['cache']['type']) && in_array($display['display_options']['cache']['type'], ['tag', 'time'])
) {
$displays[$id]['display_options']['cache']['type'] = 'none';
$change = TRUE;
}
}
if ($change) {
$view->set('display', $displays);
// Mark the resulting configuration as trusted data. This avoids issues
// with future schema changes.
$view->save(TRUE);
$changed[] = $view->get('id');
}
}
}
if (!empty($changed)) {
return \Drupal::translation()->translate('Removed incompatible cache options for the following Search API-based views: @ids', ['@ids' => implode(', ', array_unique($changed))]);
}
return NULL;
}
/**
* Switches from the old "Node status" to the new "Entity status" processor.
*/
function search_api_update_8103() {
// This update function updates search indexes for the change from
// https://www.drupal.org/node/2491175.
$config_factory = \Drupal::configFactory();
foreach ($config_factory->listAll('search_api.index.') as $index_id) {
$index = $config_factory->getEditable($index_id);
$processors = $index->get('processor_settings');
if (isset($processors['node_status'])) {
$processors['entity_status'] = $processors['node_status'];
unset($processors['node_status']);
$index->set('processor_settings', $processors);
// Mark the resulting configuration as trusted data. This avoids issues
// with future schema changes.
$index->save(TRUE);
}
}
// Clear the processor plugin cache so that if anything else indirectly tries
// to update Search API-related configuration, the plugin helper gets the most
// up-to-date plugin definitions.
\Drupal::getContainer()
->get('plugin.manager.search_api.processor')
->clearCachedDefinitions();
return t('Switched from old "Node status" to new "Entity status" processor.');
}
/**
* Update Views to use the time-based cache plugin for Search API.
*/
function search_api_update_8104() {
$config_factory = \Drupal::configFactory();
$changed = [];
foreach ($config_factory->listAll('views.view.') as $view_config_name) {
$view = $config_factory->getEditable($view_config_name);
$displays = $view->get('display');
$updated = FALSE;
foreach ($displays as $id => $display) {
if (!empty($display['display_options']['cache']['type']) && $display['display_options']['cache']['type'] === 'search_api') {
$displays[$id]['display_options']['cache']['type'] = 'search_api_time';
$updated = TRUE;
}
}
if ($updated) {
$view->set('display', $displays);
// Mark the resulting configuration as trusted data. This avoids issues
// with future schema changes.
$view->save(TRUE);
$changed[] = $view->get('id');
}
}
if (!empty($changed)) {
return \Drupal::translation()->translate('The following views have been updated to use the time-based cache plugin: @ids', ['@ids' => implode(', ', array_unique($changed))]);
}
return NULL;
}