search_api_elasticsearch_elastica.drush.inc in Search API Elasticsearch 7
Drush integration for Search API elasticsearch.
File
modules/elastica/search_api_elasticsearch_elastica.drush.inc
View source
<?php
const SEARCH_API_ELASTICSEARCH_ELASTICA_VERSION = '1.3.4.0';
define('SEARCH_API_ELASTICSEARCH_ELASTICA_TARBALL', 'https://github.com/ruflin/Elastica/archive/v' . SEARCH_API_ELASTICSEARCH_ELASTICA_VERSION . '.tar.gz');
function search_api_elasticsearch_elastica_drush_command() {
$items['download-elastica'] = array(
'description' => dt("Downloads the Elastica PHP Library."),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
'callback' => 'drush_download_elastica',
);
return $items;
}
function drush_download_elastica() {
module_load_include('module', 'search_api_elasticsearch_elastica');
spl_autoload_register('_search_api_elasticsearch_elastica_autoload');
if (class_exists('Elastica\\Client')) {
drush_log(dt('Elastica library already present. No download required.'), 'ok');
return;
}
if (module_exists('composer_manager')) {
drush_shell_exec('drush composer-manager install');
}
else {
if (!drush_shell_exec('type tar')) {
return drush_set_error(dt('Missing dependency: tar. Install it before using this command.'));
}
$args = func_get_args();
if (isset($args[0])) {
$path = $args[0];
}
else {
$path = drush_get_context('DRUSH_DRUPAL_ROOT');
if (module_exists('libraries') && ($library_path = dirname(libraries_get_path('Elastica')))) {
$path .= '/' . $library_path;
}
else {
$path .= '/sites/all/libraries';
}
}
if (!is_dir($path)) {
drush_op('mkdir', $path);
drush_log(dt('Directory @path was created', array(
'@path' => $path,
)), 'notice');
}
$olddir = getcwd();
chdir($path);
_search_api_elasticsearch_elastica_fetch_tar(SEARCH_API_ELASTICSEARCH_ELASTICA_TARBALL);
chdir($olddir);
if (is_dir($path . '/Elastica')) {
drush_log(dt('Elastica has been downloaded to @path', array(
'@path' => $path,
)), 'success');
}
else {
drush_log(dt('Drush was unable to download Elastica to @path', array(
'@path' => $path,
)), 'error');
}
}
}
function _search_api_elasticsearch_elastica_fetch_tar($url) {
$filename = basename($url);
if (is_file($filename)) {
drush_op('unlink', $filename);
}
if (!drush_shell_exec('wget ' . $url)) {
drush_shell_exec('curl -O ' . $url);
}
if (is_file($filename)) {
print $filename;
drush_shell_exec('tar xf ' . $filename);
drush_op('unlink', $filename);
drush_shell_exec('mv Elastica-' . SEARCH_API_ELASTICSEARCH_ELASTICA_VERSION . ' Elastica');
}
}
function drush_search_api_elasticsearch_elastica_pre_pm_enable() {
$modules = func_get_args();
if (in_array('search_api_elasticsearch_elastica', $modules)) {
drush_download_elastica();
}
}