View source
<?php
define('ELASTICSEARCH_CONNECTOR_VERSION', '1.0');
use Symfony\Component\HttpFoundation\Request;
use Drupal\Component\Utility\Url;
function elasticsearch_connector_help($route_name) {
switch ($route_name) {
case 'admin/help#elasticsearch_connector':
return '<p>' . t('Abstraction of making connection to the elasticsearch
server. This module is API for a whole bunch of functionality connected
with this module. Provides an interface to connect to a elasticsearch
cluster and implements the official Elasticsearch-php library.') . '</p>';
}
}
function elasticsearch_connector_cron() {
}
function elasticsearch_connector_theme() {
return array(
'elasticsearch_connector_page' => array(
'render element' => 'page',
'template' => 'elasticsearch-connector-dialog-page',
),
);
}
function elasticsearch_connector_element_info() {
return array(
'ec_clusters' => array(
'#input' => TRUE,
'#multiple' => FALSE,
'#theme' => 'select',
'#theme_wrappers' => array(
'form_element',
),
'#process' => array(
'_elasticsearch_ec_clusters_process',
),
),
'ec_index' => array(
'#input' => TRUE,
'#tree' => TRUE,
'#multiple' => FALSE,
'#theme_wrappers' => array(
'form_element',
),
'#process' => array(
'_elasticsearch_ec_index_process',
),
'#attached' => _elasticsearch_ec_index_attached(),
),
);
}
function _elasticsearch_ec_clusters_process(array $element, array &$form_state, array $form) {
$element = form_process_select($element);
if (empty($element['#skip_default_options'])) {
$element['#only_active'] = isset($element['#only_active']) ? $element['#only_active'] : TRUE;
$element['#empty_option'] = isset($element['#empty_option']) ? $element['#empty_option'] : TRUE;
$clusters = elasticsearch_cluster_load_all($element['#only_active'], $element['#empty_option']);
$element['#options'] = $clusters;
}
return $element;
}
function _elasticsearch_ec_index_attached() {
return array(
'js' => array(
drupal_get_path('module', 'elasticsearch') . '/js/ec-index.js',
),
'css' => array(
drupal_get_path('module', 'elasticsearch') . '/css/ec-index.css',
),
'library' => array(
array(
'system',
'ui.dialog',
),
),
);
}
function _elasticsearch_check_if_cluster_locked($cluster) {
$locked = array();
if (!empty($cluster)) {
$type = 'cluster';
foreach (module_implements('elasticsearch_edit_lock') as $module) {
$function = $module . '_elasticsearch_edit_lock';
$locked_result = $function($type, $cluster, NULL);
if (!empty($locked_result)) {
$locked[] = $module;
}
}
}
return $locked;
}
function _elasticsearch_check_if_index_locked($cluster, $index) {
$locked = array();
if (!empty($cluster)) {
$type = 'index';
foreach (module_implements('elasticsearch_edit_lock') as $module) {
$function = $module . '_elasticsearch_edit_lock';
$locked_result = $function($type, $cluster, $index);
if (!empty($locked_result)) {
$locked[] = $module;
}
}
}
return $locked;
}
function elasticsearch_connector_elasticsearch_edit_lock($type, $cluster, $index = NULL) {
if ($type == 'cluster' && $cluster->cluster_id == elasticsearch_connector_get_default()) {
return TRUE;
}
return FALSE;
}
function _elasticsearch_ec_index_process(array $element, array &$form_state, array $form) {
$element['#tree'] = TRUE;
$element_id = $element['#id'];
$wrapper_id = $element_id . '-index-wrapper';
$element['cluster_id'] = array(
'#type' => 'select',
'#id' => $element_id . '-cluster-id',
'#title' => t('Select cluster'),
'#required' => $element['#required'],
'#default_value' => isset($element['#default_value']) && is_array($element['#default_value']) && isset($element['#default_value']['cluster_id']) ? $element['#default_value']['cluster_id'] : '',
'#description' => t('Select the cluster.'),
'#ajax' => array(
'callback' => '_elasticsearch_ec_index_ajax',
'wrapper' => $wrapper_id,
'method' => 'replace',
'effect' => 'fade',
),
);
if (!isset($element['cluster_id']['#current_path'])) {
$element['cluster_id']['#current_path'] = current_path();
}
if (empty($element['#skip_default_options'])) {
$element['#only_active'] = isset($element['#only_active']) ? $element['#only_active'] : TRUE;
$element['#empty_option'] = isset($element['#empty_option']) ? $element['#empty_option'] : TRUE;
$clusters = elasticsearch_cluster_load_all($element['#only_active'], $element['#empty_option']);
$element['cluster_id']['#options'] = $clusters;
}
$links = array();
$index_options = array(
'' => t('Select index'),
);
if (is_array($element['#value']) && !empty($element['#value']['cluster_id'])) {
$index_options = array();
try {
$index_options = elasticsearch_get_indices_options($element['#value']['cluster_id'], TRUE);
} catch (\Exception $e) {
if (!empty($element['#throw_exp'])) {
throw $e;
}
}
$links[] = array(
'title' => t('Add index'),
'href' => 'admin/config/elasticsearch/clusters/' . $element['#value']['cluster_id'] . '/indices/add',
'attributes' => array(
'target' => '_blank',
'class' => 'ec-index-dialog',
),
'query' => array(
'render' => 'elasticsearch-dialog',
'index_element_id' => $element_id . '-index',
'cluster_element_id' => $element_id . '-cluster-id',
),
);
}
$element['index'] = array(
'#type' => 'select',
'#title' => t('Select index'),
'#id' => $element_id . '-index',
'#required' => $element['#required'],
'#default_value' => isset($element['#default_value']) && is_array($element['#default_value']) && isset($element['#default_value']['index']) ? $element['#default_value']['index'] : '',
'#description' => t('Select the index.'),
'#options' => $index_options,
'#prefix' => '<div id="' . $wrapper_id . '">',
'#suffix' => '<div class="dialog-links ' . $element['#id'] . '">' . theme('links__es_index_links', array(
'links' => $links,
'attributes' => array(
'class' => 'index-dialog-links',
),
)) . '</div></div>',
);
unset($element['#title']);
$context = array(
'form' => $form,
);
drupal_alter('ec_index_process', $element, $form_state, $context);
return $element;
}
function elasticsearch_connector_page_alter(&$page) {
if (elasticsearch_in_dialog()) {
unset($page['page_top']);
unset($page['page_bottom']);
$page['#theme'] = 'elasticsearch_page';
}
}
function elasticsearch_connector_in_dialog() {
return isset($_GET['render']) && $_GET['render'] == 'elasticsearch-dialog';
}
function elasticsearch_connector_close_on_submit() {
return !isset($_GET['closeonsubmit']) || $_GET['closeonsubmit'];
}
function elasticsearch_connector_close_on_redirect($cluster_id, $index_name) {
$_GET['destination'] = 'elasticsearch-dialog/redirect/' . $cluster_id . '/' . $index_name . '?elasticsearch-dialog-close=1&render=elasticsearch-dialog';
if (isset($_GET['cluster_element_id'])) {
$_GET['destination'] .= '&index_element_id=' . $_GET['index_element_id'];
}
if (isset($_GET['cluster_element_id'])) {
$_GET['destination'] .= '&cluster_element_id=' . $_GET['cluster_element_id'];
}
}
function elasticsearch_connector_redirect_page($cluster, $index_name) {
if (elasticsearch_in_dialog() && isset($_GET['elasticsearch-dialog-close'])) {
drupal_add_js(drupal_get_path('module', 'elasticsearch') . '/js/ec-index-child.js');
drupal_add_js(array(
'elasticsearch' => array(
'dialog' => array(
'cluster_id' => $cluster->cluster_id,
'index_name' => $index_name,
'index_element_id' => (string) $_GET['index_element_id'],
'cluster_element_id' => (string) $_GET['cluster_element_id'],
),
),
), 'setting');
}
return '';
}
function _elasticsearch_ec_index_ajax(array $form, array $form_state) {
$parents = $form_state['triggering_element']['#parents'];
$search_key = array_search('cluster_id', $parents);
$parents[$search_key] = 'index';
$index_element = drupal_array_get_nested_value($form, $parents);
return $index_element;
}
function elasticsearch_connector_main_settings_path() {
$settings_path = 'admin/config/elasticsearch';
return $settings_path;
}
function elasticsearch_connector_set_breadcrumb($links = array()) {
$breadcrumb = array(
l(t('Home'), '<front>'),
l(t('Administration'), 'admin'),
l(t('Configuration'), 'admin/config'),
l(t('Elasticsearch'), elasticsearch_main_settings_path()),
);
if (!empty($links)) {
foreach ($links as $link) {
$breadcrumb[] = $link;
}
}
drupal_set_breadcrumb($breadcrumb);
}
function elasticsearch_connector_get_indices_options($cluster_id, $empty_option = FALSE) {
$result = array();
$client = elasticsearch_get_client_by_id($cluster_id);
if ($client) {
$indices = $client
->indices()
->stats();
drupal_alter('elasticsearch_indices', $indices);
if ($empty_option) {
$result[''] = t('Select index');
}
if (!empty($indices['indices'])) {
foreach ($indices['indices'] as $index_name => $index_info) {
$result[$index_name] = $index_name;
}
}
}
return $result;
}
function elasticsearch_connector_index_valid_load($index_name) {
if (preg_match('/^[a-z][a-z0-9_]*$/i', $index_name)) {
return $index_name;
}
return FALSE;
}
function elasticsearch_connector_get_cluster_nodes_stat(\Elasticsearch\Client $client) {
try {
return $client
->nodes()
->stats();
} catch (\Exception $e) {
drupal_set_message($e
->getMessage(), 'error');
}
}
function elasticsearch_connector_check_plugin_exists(\Elasticsearch\Client $client, $plugin_name) {
$nodes_plugins = array();
$result = FALSE;
try {
$plugins = $client
->nodes()
->info(array(
'node_id' => '_all',
));
foreach ($plugins['nodes'] as $elastic_node_id => $elastic_node) {
$nodes_plugins[$elastic_node_id][$plugin_name] = FALSE;
foreach ($elastic_node['plugins'] as $plugin) {
if ($plugin['name'] == $plugin_name) {
$nodes_plugins[$elastic_node_id][$plugin_name] = TRUE;
}
}
if (empty($nodes_plugins[$elastic_node_id][$plugin_name])) {
$result = FALSE;
break;
}
else {
$result = TRUE;
}
}
return $result;
} catch (\Exception $e) {
drupal_set_message($e
->getMessage(), 'error');
return FALSE;
}
}
function template_process_elasticsearch_page(&$variables) {
if (!isset($variables['messages'])) {
$variables['messages'] = $variables['page']['#show_messages'] ? theme('status_messages') : '';
}
}
function _elasticsearch_validate_ttl_field(array $element, array &$form_state, array $form) {
if (!empty($element['#value']) && !preg_match('/^([\\d]+)(d|m|h|ms|w)$/', $element['#value'])) {
form_error($element, t('Invalid elasticsearch TTL value. Please use the proper syntax e.g. 1d (d (days), m (minutes), h (hours), ms (milliseconds) or w (weeks)).'));
}
}
function elasticsearch_connector_site_hash() {
if (!($hash = \Drupal::config('elasticsearch.settings')
->get('site_hash'))) {
global $base_url;
$hash = substr(base_convert(sha1(uniqid($base_url, TRUE)), 16, 36), 0, 6);
\Drupal::config('elasticsearch.settings')
->set('site_hash', $hash)
->save();
}
return $hash;
}