-
Notifications
You must be signed in to change notification settings - Fork 14
/
search_api_solr.module
115 lines (105 loc) · 4.2 KB
/
search_api_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
<?php
/**
* @file
* Provides a Solr-based service class for the Search API.
*/
use Drupal\Core\Form\FormStateInterface;
use \Drupal\Core\Routing\RouteMatchInterface;
use Drupal\search_api\Entity\Server;
use Drupal\search_api\SearchApiException;
use Drupal\search_api\ServerInterface;
use Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend;
/**
* Implements hook_help().
*/
function search_api_solr_help($route_name, RouteMatchInterface $route_match) {
if ($route_name == 'search_api.overview') {
// Included because we need the REQUIREMENT_* constants.
include_once(DRUPAL_ROOT . '/core/includes/install.inc');
module_load_include('install', 'search_api_solr');
$reqs = search_api_solr_requirements('runtime');
foreach ($reqs as $req) {
if (isset($req['description'])) {
$type = $req['severity'] == REQUIREMENT_ERROR ? 'error' : ($req['severity'] == REQUIREMENT_WARNING ? 'warning' : 'status');
drupal_set_message($req['description'], $type);
}
}
}
}
/**
* Implements hook_cron().
*
* Used to execute an optimization operation on all enabled Solr servers once a
* day.
*/
function search_api_solr_cron() {
$action = \Drupal::config('search_api_solr.settings')->get('cron_action');
// We treat all unknown action settings as "none". However, we turn a blind
// eye for Britons and other people who can spell.
if (!in_array($action, array('spellcheck', 'optimize', 'optimise'))) {
return;
}
// 86400 seconds is one day. We use slightly less here to allow for some
// variation in the request time of the cron run, so that the time of day will
// (more or less) stay the same.
if (REQUEST_TIME - \Drupal::state()->get('search_api_solr.last_optimize') > 86340) {
\Drupal::state()->set('search_api_solr.last_optimize', REQUEST_TIME);
// Get the IDs of all enabled servers which use the Solr backend.
$ids = \Drupal::entityQuery('search_api_server')
->condition('backend', 'search_api_solr')
->condition('status', TRUE)
->execute();
$count = 0;
foreach (Server::loadMultiple($ids) as $server) {
try {
/** @var \Solarium\Client $solarium_client */
$solarium_client = $server->getBackend()->getSolrConnection();
if ($action != 'spellcheck') {
$update = $solarium_client->createUpdate();
$update->addOptimize(TRUE, FALSE);
$solarium_client->update($update);
}
else {
$solarium_query = $solarium_client->createSelect();
$solarium_query->setRows(0);
$spellcheck = $solarium_query->getSpellcheck();
$spellcheck->setBuild(TRUE);
$solarium_client->select($solarium_query);
}
++$count;
}
catch(SearchApiException $e) {
watchdog_exception('search_api_solr', $e, '%type while optimizing Solr server @server: !message in %function (line %line of %file).', array('@server' => $server->label()));
}
}
if ($count) {
$vars['@count'] = $count;
if ($action != 'spellcheck') {
\Drupal::logger('search_api_solr')->info('Optimized @count Solr server(s).', $vars);
}
else {
\Drupal::logger('search_api_solr')->info('Rebuilt spellcheck dictionary on @count Solr server(s).', $vars);
}
}
}
}
/**
* Implements hook_search_api_server_update().
*/
function search_api_solr_search_api_server_update(ServerInterface $server) {
// @todo Do we still need to keep static and persistent caches?
// if ($server->getBackendId() == 'search_api_solr') {
// $server->getSolrConnection()->clearCache();
// }
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function search_api_solr_form_search_api_index_edit_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\search_api\IndexInterface $index */
$index = $form_state->getFormObject()->getEntity();
$backend = $index->hasValidServer() ? $index->getServerInstance()->getBackend() : NULL;
if ($backend instanceof SearchApiSolrBackend) {
$form['options']['index_directly']['#description'] .= ' ' . t('This feature is well supported for Solr 4 and above, but could cause latency and timeout problems with Solr 3. Not recommended with that version.');
}
}