diff --git a/includes/admin_index.form.inc b/includes/admin_index.form.inc deleted file mode 100644 index 9ab5b484..00000000 --- a/includes/admin_index.form.inc +++ /dev/null @@ -1,9 +0,0 @@ -ello!!!!"; - - return $ret_val; -} diff --git a/tripal_elasticsearch.module b/tripal_elasticsearch.module index 132ecb3d..b4998aad 100644 --- a/tripal_elasticsearch.module +++ b/tripal_elasticsearch.module @@ -21,277 +21,273 @@ tripal_elasticsearch_auto_discover_classes(); /** * Implements hook_init(). */ -function tripal_elasticsearch_init() -{ - global $user; - - $library = libraries_detect('elasticsearch-php'); - if (user_access('administer tripal elasticsearch', $user)) { - if (! $library) { - drupal_set_message(t('The Elastichsearch-PHP library is not installed. +function tripal_elasticsearch_init() { + global $user; + + $library = libraries_detect('elasticsearch-php'); + if (user_access('administer tripal elasticsearch', $user)) { + if (!$library) { + drupal_set_message(t('The Elastichsearch-PHP library is not installed. Please install this library first.'), 'warning'); - } + } - // Try to load the library and check if that worked. - $library = libraries_load('elasticsearch-php'); - if (empty($library['loaded'])) { - drupal_set_message(t('The Elasticsearch-PHP library loading failed!'), 'warning'); - } + // Try to load the library and check if that worked. + $library = libraries_load('elasticsearch-php'); + if (empty($library['loaded'])) { + drupal_set_message(t('The Elasticsearch-PHP library loading failed!'), 'warning'); } + } } /** * Implements hook_menu(). */ -function tripal_elasticsearch_menu() -{ - $admin_url_base = 'admin/tripal/extension/tripal_elasticsearch'; - - $items[$admin_url_base] = [ - 'title' => 'Tripal Elasticsearch', - 'description' => t('Administration pages for Tripal Elasticsearch'), - 'access arguments' => ['administer tripal elasticsearch'], - ]; - - $items[$admin_url_base.'/elasticsearch_connection'] = [ - 'title' => 'Connections', - 'access arguments' => ['administer tripal elasticsearch'], - 'page callback' => 'drupal_get_form', - 'page arguments' => ['elasticsearch_connection_form'], - 'access arguments' => ['administer tripal elasticsearch'], - 'description' => t('Add or edit Elasticsearch connections for this site or remote sites.'), - 'file' => 'includes/elasticsearch_connection.form.inc', - 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), - ]; - - // Create routing for editing and deleting individual remote entries - $items[$admin_url_base.'/elasticsearch_connection/edit/%'] = [ - 'title' => t('Edit Remote Server'), - 'page callback' => 'drupal_get_form', - 'page arguments' => ['remote_edit_confirm', 5], - 'access arguments' => ['administer tripal elasticsearch'], - 'file' => 'includes/elasticsearch_connection.form.inc', - 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), - ]; - - $items[$admin_url_base.'/elasticsearch_connection/delete/%'] = [ - 'title' => t('Delete Remote Server'), - 'page callback' => 'drupal_get_form', - 'page arguments' => ['remote_delete_confirm', 5], - 'access arguments' => ['administer tripal elasticsearch'], - 'file' => 'includes/elasticsearch_connection.form.inc', - 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), - ]; - - $items[$admin_url_base.'/indices_management'] = [ - 'title' => 'Indices', - 'page callback' => 'tripal_elasticsearch_indices_list_page', - //'page arguments' => ['tripal_elasticsearch_indexing_form'], - 'access arguments' => ['administer tripal elasticsearch'], - 'file' => 'includes/indices_management.form.inc', - 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), - 'description' => t('Manage your Elasticsearch indices. Create, edit, or delete indices, check their status, or manage settings such as cross-site querying.') - - ]; - - $items[$admin_url_base.'/indices_management/list'] = [ - 'title' => 'List Indices', - 'type' => MENU_DEFAULT_LOCAL_TASK, - 'weight' => 1, - ]; - - $items[$admin_url_base.'/indices_management/create'] = [ - 'title' => 'Create Index', - 'page callback' => 'drupal_get_form', - 'page arguments' => ['tripal_elasticsearch_indexing_form'], - 'access arguments' => ['administer tripal elasticsearch'], - 'file' => 'includes/indices_management.form.inc', - 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), - 'type' => MENU_LOCAL_TASK, - 'weight' => 2, - ]; - - $items[$admin_url_base.'/indices_management/edit/%'] = [ - 'title' => 'Edit Index', - 'page callback' => 'drupal_get_form', - 'page arguments' => ['tripal_elasticsearch_index_edit_confirm', 6], - 'access arguments' => ['administer tripal elasticsearch'], - 'file' => 'includes/indices_management.form.inc', - 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), - 'type' => MENU_CALLBACK, - 'weight' => 1, - ]; - - $items[$admin_url_base.'/indices_management/delete/%'] = [ - 'title' => 'Delete Index', - 'page callback' => 'drupal_get_form', - 'page arguments' => ['tripal_elasticsearch_index_delete_confirm', 6], - 'access arguments' => ['administer tripal elasticsearch'], - 'file' => 'includes/indices_management.form.inc', - 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), - 'type' => MENU_CALLBACK, - 'weight' => 1, - ]; - - $items[$admin_url_base.'/search_form_management'] = [ - 'title' => 'Search Forms', - 'page callback' => 'drupal_get_form', - 'page arguments' => ['table_search_interface_building_form'], - 'access arguments' => ['administer tripal elasticsearch'], - 'file' => 'includes/search_form_management.form.inc', - 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), - 'description' => t('Create or edit search forms for custom database table indices.') - - ]; - - - $items[$admin_url_base.'/progress'] = [ - 'title' => 'Progress', - 'page callback' => 'tripal_elasticsearch_progress_page', - 'access arguments' => ['administer tripal elasticsearch'], - 'file' => 'includes/indices_management.form.inc', - 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), - 'description' => t('View the progress of Elasticsearch jobs.') - ]; - - $items[$admin_url_base.'/progress/all'] = [ - 'title' => 'Progress', - 'page callback' => 'tripal_elasticsearch_get_progress', - 'access arguments' => ['administer tripal elasticsearch'], - 'file' => 'includes/indices_management.form.inc', - 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), - ]; - - $items[$admin_url_base.'/tuning'] = [ - 'title' => 'Tuning', - 'description' => t('Tuning'), - 'page callback' => 'drupal_get_form', - 'page arguments' => ['tripal_elasticsearch_tuning_form'], - 'access arguments' => ['administer tripal elasticsearch'], - 'description' => t('Make fine changes to what fields are indexed. Applies to custom indices only.') - ]; - - - // USER PATHS - //----------- - - // Page to display website search results for all node types. - $items['tripal_elasticsearch/search_website'] = [ - 'title' => t('Search results'), - 'page callback' => 'tripal_elasticsearch_web_search_results_page_callback', - 'access callback' => true, - ]; - - $items['tripal_elasticsearch/search_website/%'] = [ - 'title' => t('Search results'), - 'page callback' => 'tripal_elasticsearch_web_search_results_page_callback', - 'page arguments' => [2], - 'access callback' => true, - ]; - - // Page to display table search result. - $items['tripal_elasticsearch/search_table'] = [ - 'title' => '', - 'page callback' => 'tripal_elasticsearch_table_search_page_callback', - 'access callback' => user_access('access content'), - ]; - - $items['tripal_elasticsearch/download/results'] = [ - 'page callback' => 'tripal_elasticsearch_table_search_download', - 'access callback' => user_access('access content'), - 'type' => MENU_CALLBACK, - ]; - - // API Endpoints - // ------------- - - $items['elasticsearch/api/v1/status'] = [ - 'title' => t('Status'), - 'page callback' => 'tripal_elasticsearch_api_v1_status', - 'access arguments' => ['access content'], - 'type' => MENU_CALLBACK, - ]; - - $items['elasticsearch/api/v1/remote/status/%'] = [ - 'title' => t('Status'), - 'page callback' => 'tripal_elasticsearch_api_v1_remote_status', - 'access arguments' => ['access content'], - 'page arguments' => [5], - 'type' => MENU_CALLBACK, - ]; - - $items['elasticsearch/api/v1/search/%'] = [ - 'title' => t('Search'), - 'page callback' => 'tripal_elasticsearch_api_v1_search', - 'access arguments' => ['access content'], - 'page arguments' => [4], - 'type' => MENU_CALLBACK, - ]; - - $items['elasticsearch/api/v1/local-search'] = [ - 'title' => t('Local Search'), - 'page callback' => 'tripal_elasticsearch_api_v1_local_search', - 'access arguments' => ['access content'], - //'page arguments' => [4], - 'type' => MENU_CALLBACK, - ]; - - $items['elasticsearch/api/v1/categories'] = [ - 'title' => t('Categories'), - 'page callback' => 'tripal_elasticsearch_api_v1_categories', - 'access arguments' => ['access content'], - 'type' => MENU_CALLBACK, - ]; - - $items['elasticsearch/api/v1/index-search/%'] = [ - 'title' => t('Search'), - 'page callback' => 'tripal_elasticsearch_api_v1_table_index_local_search', - 'access arguments' => ['access content'], - 'page arguments' => [4], - 'type' => MENU_CALLBACK, - ]; - - $items['elasticsearch/api/v1/%/search/%'] = [ - 'title' => t('Search'), - 'page callback' => 'tripal_elasticsearch_api_v1_table_index_search', - 'access arguments' => ['access content'], - 'page arguments' => [3, 5], - 'type' => MENU_CALLBACK, - ]; - - return $items; +function tripal_elasticsearch_menu() { + $admin_url_base = 'admin/tripal/extension/tripal_elasticsearch'; + + $items[$admin_url_base] = [ + 'title' => 'Tripal Elasticsearch', + 'description' => t('Administration pages for Tripal Elasticsearch'), + 'access arguments' => ['administer tripal elasticsearch'], + ]; + + $items[$admin_url_base . '/elasticsearch_connection'] = [ + 'title' => 'Connections', + 'access arguments' => ['administer tripal elasticsearch'], + 'page callback' => 'drupal_get_form', + 'page arguments' => ['elasticsearch_connection_form'], + 'access arguments' => ['administer tripal elasticsearch'], + 'description' => t('Add or edit Elasticsearch connections for this site or remote sites.'), + 'file' => 'includes/elasticsearch_connection.form.inc', + 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), + ]; + + // Create routing for editing and deleting individual remote entries + $items[$admin_url_base . '/elasticsearch_connection/edit/%'] = [ + 'title' => t('Edit Remote Server'), + 'page callback' => 'drupal_get_form', + 'page arguments' => ['remote_edit_confirm', 5], + 'access arguments' => ['administer tripal elasticsearch'], + 'file' => 'includes/elasticsearch_connection.form.inc', + 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), + ]; + + $items[$admin_url_base . '/elasticsearch_connection/delete/%'] = [ + 'title' => t('Delete Remote Server'), + 'page callback' => 'drupal_get_form', + 'page arguments' => ['remote_delete_confirm', 5], + 'access arguments' => ['administer tripal elasticsearch'], + 'file' => 'includes/elasticsearch_connection.form.inc', + 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), + ]; + + $items[$admin_url_base . '/indices_management'] = [ + 'title' => 'Indices', + 'page callback' => 'tripal_elasticsearch_indices_list_page', + //'page arguments' => ['tripal_elasticsearch_indexing_form'], + 'access arguments' => ['administer tripal elasticsearch'], + 'file' => 'includes/indices_management.form.inc', + 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), + 'description' => t('Manage your Elasticsearch indices. Create, edit, or delete indices, check their status, or manage settings such as cross-site querying.'), + + ]; + + $items[$admin_url_base . '/indices_management/list'] = [ + 'title' => 'List Indices', + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'weight' => 1, + ]; + + $items[$admin_url_base . '/indices_management/create'] = [ + 'title' => 'Create Index', + 'page callback' => 'drupal_get_form', + 'page arguments' => ['tripal_elasticsearch_indexing_form'], + 'access arguments' => ['administer tripal elasticsearch'], + 'file' => 'includes/indices_management.form.inc', + 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), + 'type' => MENU_LOCAL_TASK, + 'weight' => 2, + ]; + + $items[$admin_url_base . '/indices_management/edit/%'] = [ + 'title' => 'Edit Index', + 'page callback' => 'drupal_get_form', + 'page arguments' => ['tripal_elasticsearch_index_edit_confirm', 6], + 'access arguments' => ['administer tripal elasticsearch'], + 'file' => 'includes/indices_management.form.inc', + 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), + 'type' => MENU_CALLBACK, + 'weight' => 1, + ]; + + $items[$admin_url_base . '/indices_management/delete/%'] = [ + 'title' => 'Delete Index', + 'page callback' => 'drupal_get_form', + 'page arguments' => ['tripal_elasticsearch_index_delete_confirm', 6], + 'access arguments' => ['administer tripal elasticsearch'], + 'file' => 'includes/indices_management.form.inc', + 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), + 'type' => MENU_CALLBACK, + 'weight' => 1, + ]; + + $items[$admin_url_base . '/search_form_management'] = [ + 'title' => 'Search Forms', + 'page callback' => 'drupal_get_form', + 'page arguments' => ['table_search_interface_building_form'], + 'access arguments' => ['administer tripal elasticsearch'], + 'file' => 'includes/search_form_management.form.inc', + 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), + 'description' => t('Create or edit search forms for custom database table indices.'), + + ]; + + + $items[$admin_url_base . '/progress'] = [ + 'title' => 'Progress', + 'page callback' => 'tripal_elasticsearch_progress_page', + 'access arguments' => ['administer tripal elasticsearch'], + 'file' => 'includes/indices_management.form.inc', + 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), + 'description' => t('View the progress of Elasticsearch jobs.'), + ]; + + $items[$admin_url_base . '/progress/all'] = [ + 'title' => 'Progress', + 'page callback' => 'tripal_elasticsearch_get_progress', + 'access arguments' => ['administer tripal elasticsearch'], + 'file' => 'includes/indices_management.form.inc', + 'file_path' => drupal_get_path('module', 'tripal_elasticsearch'), + ]; + + $items[$admin_url_base . '/tuning'] = [ + 'title' => 'Tuning', + 'description' => t('Tuning'), + 'page callback' => 'drupal_get_form', + 'page arguments' => ['tripal_elasticsearch_tuning_form'], + 'access arguments' => ['administer tripal elasticsearch'], + 'description' => t('Make fine changes to what fields are indexed. Applies to custom indices only.'), + ]; + + + // USER PATHS + //----------- + + // Page to display website search results for all node types. + $items['tripal_elasticsearch/search_website'] = [ + 'title' => t('Search results'), + 'page callback' => 'tripal_elasticsearch_web_search_results_page_callback', + 'access callback' => TRUE, + ]; + + $items['tripal_elasticsearch/search_website/%'] = [ + 'title' => t('Search results'), + 'page callback' => 'tripal_elasticsearch_web_search_results_page_callback', + 'page arguments' => [2], + 'access callback' => TRUE, + ]; + + // Page to display table search result. + $items['tripal_elasticsearch/search_table'] = [ + 'title' => '', + 'page callback' => 'tripal_elasticsearch_table_search_page_callback', + 'access callback' => user_access('access content'), + ]; + + $items['tripal_elasticsearch/download/results'] = [ + 'page callback' => 'tripal_elasticsearch_table_search_download', + 'access callback' => user_access('access content'), + 'type' => MENU_CALLBACK, + ]; + + // API Endpoints + // ------------- + + $items['elasticsearch/api/v1/status'] = [ + 'title' => t('Status'), + 'page callback' => 'tripal_elasticsearch_api_v1_status', + 'access arguments' => ['access content'], + 'type' => MENU_CALLBACK, + ]; + + $items['elasticsearch/api/v1/remote/status/%'] = [ + 'title' => t('Status'), + 'page callback' => 'tripal_elasticsearch_api_v1_remote_status', + 'access arguments' => ['access content'], + 'page arguments' => [5], + 'type' => MENU_CALLBACK, + ]; + + $items['elasticsearch/api/v1/search/%'] = [ + 'title' => t('Search'), + 'page callback' => 'tripal_elasticsearch_api_v1_search', + 'access arguments' => ['access content'], + 'page arguments' => [4], + 'type' => MENU_CALLBACK, + ]; + + $items['elasticsearch/api/v1/local-search'] = [ + 'title' => t('Local Search'), + 'page callback' => 'tripal_elasticsearch_api_v1_local_search', + 'access arguments' => ['access content'], + //'page arguments' => [4], + 'type' => MENU_CALLBACK, + ]; + + $items['elasticsearch/api/v1/categories'] = [ + 'title' => t('Categories'), + 'page callback' => 'tripal_elasticsearch_api_v1_categories', + 'access arguments' => ['access content'], + 'type' => MENU_CALLBACK, + ]; + + $items['elasticsearch/api/v1/index-search/%'] = [ + 'title' => t('Search'), + 'page callback' => 'tripal_elasticsearch_api_v1_table_index_local_search', + 'access arguments' => ['access content'], + 'page arguments' => [4], + 'type' => MENU_CALLBACK, + ]; + + $items['elasticsearch/api/v1/%/search/%'] = [ + 'title' => t('Search'), + 'page callback' => 'tripal_elasticsearch_api_v1_table_index_search', + 'access arguments' => ['access content'], + 'page arguments' => [3, 5], + 'type' => MENU_CALLBACK, + ]; + + return $items; } /** * Implements hook_permission(). */ -function tripal_elasticsearch_permission() -{ - return [ - 'administer tripal elasticsearch' => [ - 'title' => t('Administer Tripal Elasticsearch module'), - 'description' => t('Perform administration tasks for Tripal Elasticsearch'), - ], - ]; +function tripal_elasticsearch_permission() { + return [ + 'administer tripal elasticsearch' => [ + 'title' => t('Administer Tripal Elasticsearch module'), + 'description' => t('Perform administration tasks for Tripal Elasticsearch'), + ], + ]; } /** * Implements hook_libraries_info(). */ -function tripal_elasticsearch_libraries_info() -{ - // Register the Elasticsearch-PHP library. - $libraries['elasticsearch-php'] = [ - 'name' => 'Elasticsearch-PHP', - 'vendor url' => 'https://www.elastic.co/guide/en/elasticsearch/client/php-api/current', - 'download url' => 'https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_quickstart.html', - 'version' => '5.0~', - 'files' => [ - 'php' => ['vendor/autoload.php'], - ], - ]; - - return $libraries; +function tripal_elasticsearch_libraries_info() { + // Register the Elasticsearch-PHP library. + $libraries['elasticsearch-php'] = [ + 'name' => 'Elasticsearch-PHP', + 'vendor url' => 'https://www.elastic.co/guide/en/elasticsearch/client/php-api/current', + 'download url' => 'https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_quickstart.html', + 'version' => '5.0~', + 'files' => [ + 'php' => ['vendor/autoload.php'], + ], + ]; + + return $libraries; } /** @@ -299,34 +295,33 @@ function tripal_elasticsearch_libraries_info() * * Registers template files/functions used by this module. */ -function tripal_elasticsearch_theme($existing, $type, $theme, $path) -{ - $themes = [ - 'elastic_modal' => [ - 'template' => 'elastic_modal', - 'render element' => 'elements', - 'path' => "$path/theme/templates", - 'variables' => [ - // The trigger to open the modal - // Please supply a css selector such as #trigger-btn or .trigger-btn - 'trigger' => null, - // What happens when the backdrop is clicked - // So far, we support "close" or "none" - 'backdrop_click' => 'close', - // Content array. This value will be supplied to drupal_render(). - 'content' => [], - // Submit button text - 'submit' => 'Submit', - // Cancel button text - // Specify NULL if you don't want the cancel button to appear - 'cancel' => 'Cancel', - // The modal's title text - 'title' => null, - ], - ], - ]; - - return $themes; +function tripal_elasticsearch_theme($existing, $type, $theme, $path) { + $themes = [ + 'elastic_modal' => [ + 'template' => 'elastic_modal', + 'render element' => 'elements', + 'path' => "$path/theme/templates", + 'variables' => [ + // The trigger to open the modal + // Please supply a css selector such as #trigger-btn or .trigger-btn + 'trigger' => NULL, + // What happens when the backdrop is clicked + // So far, we support "close" or "none" + 'backdrop_click' => 'close', + // Content array. This value will be supplied to drupal_render(). + 'content' => [], + // Submit button text + 'submit' => 'Submit', + // Cancel button text + // Specify NULL if you don't want the cancel button to appear + 'cancel' => 'Cancel', + // The modal's title text + 'title' => NULL, + ], + ], + ]; + + return $themes; } /** @@ -334,123 +329,119 @@ function tripal_elasticsearch_theme($existing, $type, $theme, $path) * * Add JS file when the elastic modal theme function is called. */ -function tripal_elasticsearch_preprocess_elastic_modal(&$variables) -{ - drupal_add_js(drupal_get_path('module', 'tripal_elasticsearch').'/js/ESModal.js'); +function tripal_elasticsearch_preprocess_elastic_modal(&$variables) { + drupal_add_js(drupal_get_path('module', 'tripal_elasticsearch') . '/js/ESModal.js'); } /** * Implements hook_daemon_api_info(). * Registers our Daemon with the Daemon API */ -function tripal_elasticsearch_daemon_api_info() -{ - $daemon = []; - - // This is an example daemon which just sleeps for random amounts of time. - $daemon['elasticsearch'] = [ - // The machine name of the daemon (same as key above). - 'machine_name' => 'elasticsearch', - // A human-readable name for your daemon. - 'name' => 'Elasticsearch Daemon', - // This module (ie: the module implementing the daemon). - 'module' => 'tripal_elasticsearch', - // The class extending DrushDaemon and implementing your daemon-specific - // functionality. This class should be in a [classname].inc file in your - // modules base directory. - 'class' => 'ESDaemon', - // OPTIONAL: Define this if your module doesn't follow the rule mentioned - // in the above comment. The name and path to the file containing the - // daemon class assuming your module folder as the root. - 'class_file' => 'includes/ESDaemon.inc', - ]; - - return $daemon; +function tripal_elasticsearch_daemon_api_info() { + $daemon = []; + + // This is an example daemon which just sleeps for random amounts of time. + $daemon['elasticsearch'] = [ + // The machine name of the daemon (same as key above). + 'machine_name' => 'elasticsearch', + // A human-readable name for your daemon. + 'name' => 'Elasticsearch Daemon', + // This module (ie: the module implementing the daemon). + 'module' => 'tripal_elasticsearch', + // The class extending DrushDaemon and implementing your daemon-specific + // functionality. This class should be in a [classname].inc file in your + // modules base directory. + 'class' => 'ESDaemon', + // OPTIONAL: Define this if your module doesn't follow the rule mentioned + // in the above comment. The name and path to the file containing the + // daemon class assuming your module folder as the root. + 'class_file' => 'includes/ESDaemon.inc', + ]; + + return $daemon; } /** * Implements hook_cron_queue_info(). */ -function tripal_elasticsearch_cron_queue_info() -{ - // Define N = 5 cron queues - $queues = []; - $queue_number = 5; - - for ($n = 1; $n <= $queue_number; $n++) { - $queues['elasticsearch_queue_'.$n] = [ - 'worker callback' => 'ESQueue::run', - 'time' => 60 * 2, - ]; - } +function tripal_elasticsearch_cron_queue_info() { + // Define N = 5 cron queues + $queues = []; + $queue_number = 5; - $queues['elasticsearch_dispatcher'] = [ - 'worker callback' => 'ESQueue::run', - // Let worker take as much time required to dispatch all jobs - 'time' => 60 * 20, + for ($n = 1; $n <= $queue_number; $n++) { + $queues['elasticsearch_queue_' . $n] = [ + 'worker callback' => 'ESQueue::run', + 'time' => 60 * 2, ]; + } + + $queues['elasticsearch_dispatcher'] = [ + 'worker callback' => 'ESQueue::run', + // Let worker take as much time required to dispatch all jobs + 'time' => 60 * 20, + ]; - return $queues; + return $queues; } /** * Implements hook_block_info(). */ -function tripal_elasticsearch_block_info() -{ - $blocks = []; - // Define block for website search box. - $blocks['elasticsearch_website_search_box'] = [ - 'info' => t('Tripal Elasticsearch website search box'), - 'status' => 1, - 'region' => 'header', +function tripal_elasticsearch_block_info() { + $blocks = []; + // Define block for website search box. + $blocks['elasticsearch_website_search_box'] = [ + 'info' => t('Tripal Elasticsearch website search box'), + 'status' => 1, + 'region' => 'header', + 'cache' => DRUPAL_NO_CACHE, + ]; + + // Define block for website search results by category. + $blocks['website_search_category'] = [ + 'info' => t('Tripal Elasticsearch website search category'), + 'cache' => DRUPAL_NO_CACHE, + 'status' => 1, + 'region' => 'sidebar_first', + 'visibility' => BLOCK_VISIBILITY_LISTED, + 'page' => "tripal_elasticsearch/search_website\ntripal_elasticsearch/search_website/*", + ]; + + $blocks['cross_site_search_block'] = [ + 'info' => t('Tripal Elasticsearch cross site search form'), + 'visibility' => BLOCK_VISIBILITY_LISTED, + 'cache' => DRUPAL_NO_CACHE, + 'page' => "elasticsearch/cross-site", + ]; + + $blocks['elasticsearch_gene_search_block'] = [ + 'info' => t('Tripal Elasticsearch cross site gene search form'), + 'visibility' => BLOCK_VISIBILITY_LISTED, + 'cache' => DRUPAL_NO_CACHE, + 'page' => "elasticsearch/cross-site-gene-search", + ]; + + $blocks['es_local_gene_search_block'] = [ + 'info' => t('Tripal Elasticsearch local gene search form'), + 'visibility' => BLOCK_VISIBILITY_LISTED, + 'cache' => DRUPAL_NO_CACHE, + 'page' => "elasticsearch/gene-search", + ]; + + // Define blocks for table search forms. + $sql = "SELECT index_name FROM {tripal_elasticsearch}"; + $index_names = db_query($sql)->fetchCol('index_name'); + if (!empty($index_names)) { + foreach ($index_names as $index_name) { + $blocks['tes_' . $index_name] = [ + 'info' => t('Search block for index: ' . $index_name), 'cache' => DRUPAL_NO_CACHE, - ]; - - // Define block for website search results by category. - $blocks['website_search_category'] = [ - 'info' => t('Tripal Elasticsearch website search category'), - 'cache' => DRUPAL_NO_CACHE, - 'status' => 1, - 'region' => 'sidebar_first', - 'visibility' => BLOCK_VISIBILITY_LISTED, - 'page' => "tripal_elasticsearch/search_website\ntripal_elasticsearch/search_website/*", - ]; - - $blocks['cross_site_search_block'] = [ - 'info' => t('Tripal Elasticsearch cross site search form'), - 'visibility' => BLOCK_VISIBILITY_LISTED, - 'cache' => DRUPAL_NO_CACHE, - 'page' => "elasticsearch/cross-site", - ]; - - $blocks['elasticsearch_gene_search_block'] = [ - 'info' => t('Tripal Elasticsearch cross site gene search form'), - 'visibility' => BLOCK_VISIBILITY_LISTED, - 'cache' => DRUPAL_NO_CACHE, - 'page' => "elasticsearch/cross-site-gene-search", - ]; - - $blocks['es_local_gene_search_block'] = [ - 'info' => t('Tripal Elasticsearch local gene search form'), - 'visibility' => BLOCK_VISIBILITY_LISTED, - 'cache' => DRUPAL_NO_CACHE, - 'page' => "elasticsearch/gene-search", - ]; - - // Define blocks for table search forms. - $sql = "SELECT index_name FROM {tripal_elasticsearch}"; - $index_names = db_query($sql)->fetchCol('index_name'); - if (! empty($index_names)) { - foreach ($index_names as $index_name) { - $blocks['tes_'.$index_name] = [ - 'info' => t('Search block for index: '.$index_name), - 'cache' => DRUPAL_NO_CACHE, - ]; - } + ]; } + } - return $blocks; + return $blocks; } /** @@ -458,14 +449,13 @@ function tripal_elasticsearch_block_info() * * @return string */ -function tripal_elasticsearch_build_download_url() -{ - $query = ['query' => $_GET]; - if (array_key_exists('q', $query['query'])) { - unset($query['query']['q']); - } +function tripal_elasticsearch_build_download_url() { + $query = ['query' => $_GET]; + if (array_key_exists('q', $query['query'])) { + unset($query['query']['q']); + } - return url('tripal_elasticsearch/download/results', $query); + return url('tripal_elasticsearch/download/results', $query); } /** @@ -475,48 +465,48 @@ function tripal_elasticsearch_build_download_url() * * @return array */ -function tripal_elasticsearch_generate_block($delta) -{ - $block = []; - - $index_name = preg_replace('/^(tes_)/', '', $delta); - - // Use index name obtained from block name and query the database. - $sql = "SELECT DISTINCT index_name FROM {tripal_elasticsearch} WHERE index_name = :index_name"; - $result = db_query($sql, [':index_name' => $index_name])->fetchCol('index_name'); - - // If query result is not empty, display the block. - if (! empty($result)) { - $block['subject'] = t('Search block form for index: '.$index_name.''); - - $page['form'] = drupal_get_form('tripal_elasticsearch_build_search_block_form', $index_name); - - if (isset($_GET['op'])) { - drupal_add_js(drupal_get_path('module', 'tripal_elasticsearch').'/js/table_search_results_datatable.js'); - $search_results = tripal_elasticsearch_paginate(10); - if (empty($search_results)) { - $page['content'] = ['#markup' => '0 results found.']; - } else { - $markup = get_table_search_result_table($search_results['results'], $index_name, $search_results['total']); - $page_number = $search_results['page']; - $total_pages = ceil($search_results['total'] / 10); - - $page['download'] = [ - '#markup' => '
'.'Download all results in csv format'.'
', - ]; - $page['count'] = [ - '#markup' => "Showing page {$page_number} out of {$total_pages} pages.
"."Found {$search_results['total']} results.
"."' . 'Download all results in csv format' . '
', + ]; + $page['count'] = [ + '#markup' => "Showing page {$page_number} out of {$total_pages} pages.
" . "Found {$search_results['total']} results.
" . "'.$results['total'].' results found Page '.$current_page.' out of '.$pages.'
'; - $content .= get_website_search_result_table($results['results'], false); - $content .= $results['pager']; - - $form = drupal_get_form('tripal_elasticsearch_collections_form'); - try { - $content .= theme('elastic_modal', [ - 'trigger' => '#create-collection-btn', - 'title' => 'Create Collection', - 'submit' => 'Save Collection', - 'content' => [ - 'form' => $form, - ], - ]); - } catch (Exception $exception) { + if (empty($index_name)) { + return 'Searching is not available. Please try later.'; } - return $content; + $per_page = 10; + $results = $es->setWebsiteSearchParams($keyword, $node_type, implode(',', $index_name), '') + ->paginate($per_page); + } catch (\Exception $e) { + tripal_report_error('tripal_elasticsearch', TRIPAL_ERROR, $e->getMessage()); + + return 'Searching is not available. Please try later.'; + } + + if (count($results) == 0) { + return "Your search -" . $keyword . "- didn't match any content."; + } + + $current_page = $results['page']; + $pages = $results['pages']; + $content = ''; + if (count($results['results'])) { + $content = tripal_elasticsearch_create_collection_button(); + } + $content .= '' . $results['total'] . ' results found Page ' . $current_page . ' out of ' . $pages . '
'; + $content .= get_website_search_result_table($results['results'], FALSE); + $content .= $results['pager']; + + $form = drupal_get_form('tripal_elasticsearch_collections_form'); + try { + $content .= theme('elastic_modal', [ + 'trigger' => '#create-collection-btn', + 'title' => 'Create Collection', + 'submit' => 'Save Collection', + 'content' => [ + 'form' => $form, + ], + ]); + } catch (Exception $exception) { + + } + + return $content; } /** @@ -638,19 +626,18 @@ function tripal_elasticsearch_web_search_results_page_callback($node_type = '') * * @return string */ -function tripal_elasticsearch_create_collection_button() -{ - // Make sure Tripal collections are supported - if (! function_exists('tripal_create_collection')) { - return ''; - } - - return ''.l('Create Downloadable Collection', 'tripal_elasticsearch/collections/create', [ - 'attributes' => [ - 'id' => 'create-collection-btn', - 'class' => 'btn btn-primary', - ], - ]).'
'; +function tripal_elasticsearch_create_collection_button() { + // Make sure Tripal collections are supported + if (!function_exists('tripal_create_collection')) { + return ''; + } + + return '' . l('Create Downloadable Collection', 'tripal_elasticsearch/collections/create', [ + 'attributes' => [ + 'id' => 'create-collection-btn', + 'class' => 'btn btn-primary', + ], + ]) . '
'; } /** @@ -658,135 +645,132 @@ function tripal_elasticsearch_create_collection_button() * * @return string */ -function tripal_elasticsearch_table_search_page_callback() -{ - // create an empty page to host table search blocks. - return ''; +function tripal_elasticsearch_table_search_page_callback() { + // create an empty page to host table search blocks. + return ''; } /** * Implements hook_form_FORM_ID_alter(). */ function tripal_elasticsearch_form_tripal_elasticsearch_build_search_block_form_alter( - &$form, - &$form_state, - $form_id + &$form, + &$form_state, + $form_id ) { - if (! isset($form_state['values']['op'])) { - return; - } elseif ($form_state['values']['op'] !== 'Download') { - // add js and css files - drupal_add_css(drupal_get_path('module', 'tripal_elasticsearch').'/css/jquery.dataTables.min.css'); - drupal_add_js(drupal_get_path('module', 'tripal_elasticsearch').'/js/jquery.dataTables.min.js'); - drupal_add_js(drupal_get_path('module', 'tripal_elasticsearch').'/js/table_search_results_datatable.js'); - - /** - * build an associated array in which keys are field names and values are user input contents. - */ - $sql = "SELECT * FROM {tripal_elasticsearch} WHERE index_name = :index_name"; - $index_name = $form_state['values']['index_name']; - $result = db_query($sql, [':index_name' => $index_name])->fetchAll(); - $index_fields = []; - foreach ($result as $record) { - $field_value = isset($form_state['values'][$record->index_field]) ? $form_state['values'][$record->index_field] : ''; - if (! empty($field_value)) { - $index_fields[$record->index_field] = $field_value; - } - } - $record = end($results); - // get index type - $index_type = $record->table_name; - // Build search query for table search. - $query = build_search_query_from_field_content_pairs($index_fields); - // Build table search params. - $select_window = isset($form_state['values']['select_window']) ? $form_state['values']['select_window'] : ''; - $from = empty($select_window) ? 0 : 1000 * ($select_window - 1); - // Run Elasticsearch and return search results into an array. - $results = []; - try { - $es = new ESInstance(); - $results = $es->setTableSearchParams($index_name, $index_type, $query, [ - $from, - 1000, - ])->search(); - } catch (\Exception $e) { - tripal_report_error('tripal_elasticsearch', 'TRIPAL_ERROR', $e->getMessage()); - } - - // Theme search results - $output = 'Your search did not match any record'; - $total = count($results); - if ($total > 0) { - $output = '