123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- <?php
- function tripal_core_search_include_chado_fields() {
- return array(
- 'organism.genus',
- 'organism.species',
- );
- }
- function tripal_core_entity_property_info_alter(&$info) {
-
-
- $fields_to_include = module_invoke_all('search_include_chado_fields');
- $fields_to_include = array_unique($fields_to_include);
-
-
-
- $node_info = module_invoke_all('node_info');
- foreach ($node_info as $n) {
-
-
-
-
-
- if (isset($n['chado_node_api'])) {
- $schema = chado_get_schema($n['chado_node_api']['base_table']);
-
-
-
- foreach ($schema['fields'] as $field_name => $details) {
- $machine_name = $n['chado_node_api']['base_table'] . '.' . $field_name;
-
- $label = ucwords(str_replace(array('.','_'),' ',$machine_name));
-
- if (preg_match('/name/', $field_name) OR in_array($machine_name, $fields_to_include)) {
- if (!isset($info['node']['bundles'][ $n['base'] ]['properties'][$machine_name])) {
- $info['node']['bundles'][ $n['base'] ]['properties'][$machine_name] = array(
- 'label' => $label,
- 'description' => (isset($details['description'])) ? $details['description'] : '',
- 'type' => ($details['type'] == 'varchar') ? 'text' : $details['type'],
- 'schema field' => '[' . $machine_name . ']',
-
-
- 'getter callback' => 'tripal_search_chado_token_getter_callback'
- );
- }
- }
- }
-
-
-
- foreach ($schema['foreign keys'] as $table => $fk_details) {
- foreach ($fk_details['columns'] as $left_field => $right_field) {
- $machine_name = $n['chado_node_api']['base_table'] . '.' . $left_field;
- $field_details = $schema['fields'][$left_field];
-
- $label = $table . ' (' . $machine_name . ')';
- if (preg_match('/(\w+)_id/',$left_field,$matches)) {
-
- $key_label = ucwords(str_replace('_', ' ', $matches[1]));
-
- $label = str_replace('_', ' ', $n['chado_node_api']['base_table']);
- $label .= ' ' . str_replace('_', ' ', $matches[1]);
- $label = ucwords($label);
- }
- $keytoken = '[BASE.' . $left_field . '>' . $table . '.' . $right_field . ']';
- $format = chado_node_get_readable_format($keytoken);
-
-
-
- $info['node']['properties'][$table . '.' . $right_field .' key'] = array(
- 'label' => $key_label . ' (All Content Types)',
- 'description' => (isset($field_details['description'])) ? $field_details['description'] : '',
- 'type' => 'text',
-
-
-
- 'schema field' => $format,
-
-
- 'getter callback' => 'tripal_search_chado_token_across_nodetypes_getter_callback'
- );
- $pretoken = '[' . $n['chado_node_api']['base_table'] . '.' . $left_field . '>' . $table . '.' . $right_field . ']';
- $format = chado_node_get_readable_format($pretoken);
-
-
- $info['node']['bundles'][ $n['base'] ]['properties'][$machine_name .' expanded'] = array(
- 'label' => $label . ' (Expanded)',
- 'description' => (isset($field_details['description'])) ? $field_details['description'] : '',
- 'type' => 'text',
- 'schema field' => $format,
-
-
- 'getter callback' => 'tripal_search_chado_token_getter_callback'
- );
- }
- }
- }
- }
-
- drupal_alter('tripal_search_properties', $info);
- }
- function hook_tripal_search_properties_alter(&$info) { }
- function tripal_search_chado_token_getter_callback($data, $options, $field_name, $type, $info) {
- if (isset($data->nid)) {
- if (isset($info['schema field'])) {
- $format = $info['schema field'];
-
- if (preg_match('/\[(\w+)\.(\w+)/',$format, $matches)) {
- $base_table = $matches[1];
- $field_name = $matches[2];
-
-
-
- if (!isset($data->{$base_table})) return NULL;
- $format = tripal_core_get_token_value_for_property($base_table, $field_name, $format, $data, $info);
- return $format;
- }
- else {
-
- tripal_report_error(
- 'tripal_search',
- TRIPAL_ERROR,
- 'Unable to extract the base table from the format (:format) for :field because it didn\'t match the expected format: [tablename.field...',
- array(':field' => $field_name, ':format' => $format)
- );
- return NULL;
- }
- }
- else {
- tripal_report_error(
- 'tripal_search',
- TRIPAL_ERROR,
- 'Unable to get value for :field because the schema field was not set.',
- array(':field' => $field_name)
- );
- return NULL;
- }
- }
- }
- function tripal_search_chado_token_across_nodetypes_getter_callback($data, $options, $field_name, $type, $info) {
-
-
- if (isset($data->nid)) {
- if (preg_match('/^chado_(\w+)/',$data->type,$matches)) {
- if (isset($info['schema field'])) {
-
- $base_table = $matches[1];
-
- $format = str_replace('BASE', $base_table, $info['schema field']);
-
- $format = tripal_core_get_token_value_for_property($base_table, $field_name, $format, $data, $info);
- return $format;
- }
- else {
-
- tripal_report_error(
- 'tripal_search',
- TRIPAL_ERROR,
- 'Unable to extract the base table from the format (:format) for :field because it didn\'t match the expected format: [tablename.field...',
- array(':field' => $field_name, ':format' => $format)
- );
- }
- }
- else {
- tripal_report_error(
- 'tripal_search',
- TRIPAL_ERROR,
- 'Unable to get value for :field because the schema field was not set.',
- array(':field' => $field_name)
- );
- }
- }
- return NULL;
- }
- function tripal_core_get_token_value_for_property($base_table, $field_name, $format, $data, $info) {
-
- if (preg_match_all('/\[[^]]+\]/', $format, $used_tokens)) {
- $used_tokens = $used_tokens[0];
-
- if (empty($used_tokens)) {
- tripal_report_error(
- 'tripal_search',
- TRIPAL_NOTICE,
- 'Returned static text for :field since there were no tokens in the supplied format: :format',
- array(':field' => $field_name, ':format' => $format)
- );
- return $format;
- }
-
- $null_tokens = array();
- foreach ($used_tokens as $token) {
- $token_info = array(
- 'name' => $info['label'],
- 'table' => $base_table,
- 'field' => $field_name,
- 'token' => $token,
- 'description' => $info['description'],
- 'location' => chado_node_get_location_from_token($token),
- );
- $value = chado_get_token_value($token_info, $data, array('supress_errors' => TRUE));
- if (empty($value)) $null_tokens[] = $token;
-
- $format = str_replace($token, $value, $format);
- }
-
-
- if (sizeof($used_tokens) == sizeof($null_tokens)) return NULL;
- }
- else {
- tripal_report_error(
- 'tripal_search',
- TRIPAL_NOTICE,
- 'Returned static text for :field since there were no tokens of a recognized format in the supplied format: :format',
- array(':field' => $field_name, ':format' => $format)
- );
- }
- return $format;
- }
- function tripal_core_modules_enabled($modules) {
- if (module_exists('search_api')) {
- $index_enabled = db_query('SELECT enabled FROM search_api_index WHERE machine_name=:name', array(':name' => 'default_node_index'))->fetchField();
- if ($index_enabled) {
- tripal_search_update_default_index();
- }
- }
- }
- function tripal_search_update_default_index() {
-
- $index_id = db_query('SELECT id FROM search_api_index WHERE machine_name=:name',
- array(':name' => 'default_node_index'))->fetchField();
- if (!$index_id) {
-
- return FALSE;
- }
- $index = search_api_index_load($index_id);
-
- $changes = array('options' => $index->options);
-
-
- if ($index->enabled == FALSE AND $index->server == NULL) {
-
-
-
- $info = entity_get_property_info('node');
-
-
-
- foreach ($info['bundles'] as $node_type => $details) {
- if (preg_match('/^chado_/', $node_type)) {
-
- foreach ($details['properties'] as $field_name => $field_details) {
- if (!isset($changes['options']['fields'][$field_name])) {
- $changes['options']['fields'][$field_name]['type'] = ($field_details['type'] == 'varchar') ? 'text' : $field_details['type'];
-
-
- if (preg_match('/name/',$field_name)) {
- $changes['options']['fields'][$field_name]['boost'] = '3.0';
- }
- }
- }
- }
- }
-
-
- if (!isset($index->options['processors']['search_api_highlighting'])) {
- $changes['options']['processors']['search_api_highlighting'] = array(
- 'status' => 1,
- 'weight' => 35,
- 'settings' => array(
- 'prefix' => '<strong>',
- 'suffix' => '</strong>',
- 'excerpt' => 1,
- 'excerpt_length' => 256,
- 'exclude_fields' => array(),
- 'highlight' => 'always',
- ),
- );
- }
- else {
- $changes['options']['processors']['search_api_highlighting']['status'] = 1;
- $changes['options']['processors']['search_api_highlighting']['settings']['excerpt'] = 1;
- }
-
- search_api_index_edit($index_id, $changes);
- drupal_set_message('The Search API "Default Node Index" was updated.');
- }
- else {
- tripal_report_error(
- 'tripal_search',
- TRIPAL_NOTICE,
- 'The Search API "Default Node Index" was not updated with Tripal Fields. If you would like to enable more Tripal/Chado fields to be indexed, edit the Field Listing for the "Default Node Index" now.'
- );
- }
- }
|