Commit e2d208ac authored by Robert Lyon's avatar Robert Lyon
Browse files

Checking if elasticsearch can connect to host / port (bug #1256118)

Currently the getConnection() in Elastica/Client.php can give false
positive and also throws error if no connection.

What is needed is a true/false check to allow for things like notice
feedback on extensions page or giving a warning when switching to
'elasticsearch' on site options page when it is unreachable.

Change-Id: I43506f03b943d79eb019d39fe794037fc50bd05a
Signed-off-by: Robert Lyon's avatarRobert Lyon <>
parent 652e9f1f
......@@ -760,7 +760,13 @@ function siteoptions_submit(Pieform $form, $values) {
call_static_method(generate_class_name('search', $oldsearchplugin), 'cleanup_sitewide');
// Call the new search plugin's sitewide initialize method
safe_require('search', $values['searchplugin']);
call_static_method(generate_class_name('search', $values['searchplugin']), 'initialize_sitewide');
$initialize = call_static_method(generate_class_name('search', $values['searchplugin']), 'initialize_sitewide');
if (!$initialize) {
$form->reply(PIEFORM_ERR, array(
'message' => get_string('searchconfigerror', 'admin', $values['searchplugin']),
'goto' => '/admin/site/options.php',
// submitted sessionlifetime is in minutes; db entry session_timeout is in seconds
......@@ -339,6 +339,7 @@ $string['remoteavatars'] = 'Display remote avatars';
$string['remoteavatarsdescription'] = 'If checked, the <a href="">Gravatar</a> service will be used for users\' default profile pictures.';
$string['searchplugin'] = 'Search plugin';
$string['searchplugindescription'] = 'Search plugin to use';
$string['searchconfigerror'] = 'The configuration settings for search plugin "%s" are incorrect, please check the configuration settings under Extensions -> Plugin type: search. You may need to hit the search\'s \'reset\' button when done.';
$string['searchusernames'] = 'Search usernames';
$string['searchusernamesdescription'] = 'If checked, allow usernames to be searched on as part of "Search users".';
$string['sessionlifetime'] = 'Session lifetime';
......@@ -44,6 +44,7 @@ $string['indexnamedescription'] = 'Name of the Elasticsearch index. Default is m
$string['license'] = 'License';
$string['Media'] = 'Media';
$string['noticeenabled'] = 'The Elasticsearch plugin is currently active. To deactivate it, deselect it in the <a href="%s">site options search settings</a>.';
$string['noticenotactive'] = 'The ElasticSearch Server is unreachable on host: %s and port %s. Please make sure it is running.';
$string['noticenotenabled'] = 'The Elasticsearch plugin is not currently enabled. To activate it, select it in the <a href="%s">site options in the search settings</a>.';
$string['noticepostgresrequired'] = 'The Elasticsearch plugin only works with a PostgreSQL database at this time.';
$string['noticepostgresrequiredtitle'] = 'Feature not available';
......@@ -80,6 +80,20 @@ class PluginSearchElasticsearch extends PluginSearch {
return is_postgres();
* This function determines if we can connect to the supplied host and port
public static function can_connect() {
$host = get_config_plugin('search', 'elasticsearch', 'host');
$port = get_config_plugin('search', 'elasticsearch', 'port');
$fp = @fsockopen($host, $port, $errno, $errstr, 5);
if ($fp) {
return true;
return false;
* Generates the search form used in the page headers
* @return string
......@@ -147,6 +161,12 @@ class PluginSearchElasticsearch extends PluginSearch {
$enabledhtml = $smarty->fetch('Search:elasticsearch:configwarning.tpl');
if (!self::can_connect()) {
$smarty = smarty_core();
$smarty->assign('notice', get_string('noticenotactive', 'search.elasticsearch', get_config_plugin('search', 'elasticsearch', 'host'), get_config_plugin('search', 'elasticsearch', 'port')));
$enabledhtml .= $smarty->fetch('Search:elasticsearch:configwarning.tpl');
$config = array(
'elements' => array(
......@@ -393,7 +413,11 @@ class PluginSearchElasticsearch extends PluginSearch {
public static function initialize_sitewide() {
if (self::can_connect()) {
return true;
return false;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment