Skip to content

Commit

Permalink
Test and fix all db indices problems
Browse files Browse the repository at this point in the history
  • Loading branch information
almasaeed2010 committed Dec 14, 2017
1 parent 2db0576 commit eb34bd2
Show file tree
Hide file tree
Showing 10 changed files with 606 additions and 76 deletions.
2 changes: 1 addition & 1 deletion includes/Jobs/GeneSearchIndexJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected function loadBlastData($keys) {
* @return array
*/
protected function loadAnnotations($keys) {
$query = "SELECT db.name AS db_name, dbxref.accession, cv.name AS cv_name, feature_id
$query = "SELECT db.name AS db_name, dbxref.accession, cv.name AS cv_name, feature_id
FROM chado.dbxref
INNER JOIN chado.cvterm ON dbxref.dbxref_id = cvterm.dbxref_id
INNER JOIN chado.feature_cvterm ON cvterm.cvterm_id = feature_cvterm.cvterm_id
Expand Down
19 changes: 4 additions & 15 deletions includes/Jobs/TableIndexJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct($index, $table, $fields) {
$this->index = $index;
$this->type = ucwords($index);
$this->table = $table;
$this->fields = $fields;
$this->fields = array_map('db_escape_field', $fields);
}

/**
Expand All @@ -69,17 +69,6 @@ public function handle() {
elseif ($this->total > 0) {
$es->createEntry($this->index, $this->table, FALSE, $records[0]);
}

$sql = "
SELECT F.uniquename,
F.feature_id,
BLAST.hit_description,
CVT.cvterm_id
FROM chado.feature F
FULL OUTER JOIN chado.blast_hit_data BLAST ON F.feature_id = BLAST.feature_id
FULL OUTER JOIN chado.feature_cvterm CVT ON F.feature_id = CVT.feature_id
WHERE BLAST.hit_description IS NOT NULL OR CVT.cvterm_id IS NOT NULL
";
}

/**
Expand All @@ -96,8 +85,8 @@ protected function get() {
$this->offset(0);
}

$select_fields = implode(',', $this->fields);
$query = "SELECT {$select_fields} FROM {{$this->table}} ORDER BY {$this->fields[0]} ASC OFFSET :offset LIMIT :limit";
$select_fields = implode(', ', $this->fields);
$query = "SELECT {$select_fields} FROM {" . db_escape_table($this->table) . "} ORDER BY {$this->fields[0]} ASC OFFSET :offset LIMIT :limit";

return db_query($query, [
':offset' => $this->offset,
Expand All @@ -121,6 +110,6 @@ public function total() {
* @return int
*/
public function count() {
return db_query('SELECT COUNT(*) FROM {' . $this->table . '}')->fetchField();
return db_query('SELECT COUNT(*) FROM {' . db_escape_table($this->table) . '}')->fetchField();
}
}
128 changes: 128 additions & 0 deletions includes/TripalFields/local__feature_search/local__feature_search.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
/**
* @class
* Purpose:
*
* Data:
* Assumptions:
*/
class local__feature_search extends ChadoField {

// --------------------------------------------------------------------------
// EDITABLE STATIC CONSTANTS
//
// The following constants SHOULD be set for each descendant class. They are
// used by the static functions to provide information to Drupal about
// the field and it's default widget and formatter.
// --------------------------------------------------------------------------

// The default label for this field.
public static $default_label = 'Feature Search';

// The default description for this field.
public static $default_description = 'Search features related to an organism (search by property)';

// The default widget for this field.
public static $default_widget = 'local__feature_search_widget';

// The default formatter for this field.
public static $default_formatter = 'local__feature_search_formatter';

// The module that manages this field.
public static $module = 'tripal_elasticsearch';

// A list of global settings. These can be accessed within the
// globalSettingsForm. When the globalSettingsForm is submitted then
// Drupal will automatically change these settings for all fields.
// Once instances exist for a field type then these settings cannot be
// changed.
public static $default_settings = array(
'storage' => 'field_chado_storage',
// It is expected that all fields set a 'value' in the load() function.
// In many cases, the value may be an associative array of key/value pairs.
// In order for Tripal to provide context for all data, the keys should
// be a controlled vocabulary term (e.g. rdfs:type). Keys in the load()
// function that are supported by the query() function should be
// listed here.
'searchable_keys' => array(),
);

// Provide a list of instance specific settings. These can be access within
// the instanceSettingsForm. When the instanceSettingsForm is submitted
// then Drupal with automatically change these settings for the instance.
// It is recommended to put settings at the instance level whenever possible.
// If you override this variable in a child class be sure to replicate the
// term_name, term_vocab, term_accession and term_fixed keys as these are
// required for all TripalFields.
public static $default_instance_settings = array(
// The DATABASE name, as it appears in chado.db. This also builds the link-out url. In most cases this will simply be the CV name. In some cases (EDAM) this will be the SUBONTOLOGY.
'term_vocabulary' => 'local',
// The name of the term.
'term_name' => 'feature_search',
// The unique ID (i.e. accession) of the term.
'term_accession' => 'feature_search',
// Set to TRUE if the site admin is not allowed to change the term
// type, otherwise the admin can change the term mapped to a field.
'term_fixed' => FALSE,
// Indicates if this field should be automatically attached to display
// or web services or if this field should be loaded separately. This
// is convenient for speed. Fields that are slow should for loading
// should have auto_attach set to FALSE so tha their values can be
// attached asynchronously.
'auto_attach' => FALSE,
// The table in Chado that the instance maps to.
'chado_table' => 'organism',
// The column of the table in Chado where the value of the field comes from.
'chado_column' => 'organism_id',
// The base table.
'base_table' => 'organism',
);

// A boolean specifying that users should not be allowed to create
// fields and instances of this field type through the UI. Such
// fields can only be created programmatically with field_create_field()
// and field_create_instance().
public static $no_ui = FALSE;

// A boolean specifying that the field will not contain any data. This
// should exclude the field from web services or downloads. An example
// could be a quick search field that appears on the page that redirects
// the user but otherwise provides no data.
public static $no_data = FALSE;

/**
* Loads the field values from the underlying data store.
*
* @param $entity
*
* @return
* An array of the following format:
* $entity->{$field_name}['und'][0]['value'] = $value;
* where:
* - $entity is the entity object to which this field is attached.
* - $field_name is the name of this field
* - 'und' is the language code (in this case 'und' == undefined)
* - 0 is the cardinality. Increment by 1 when more than one item is
* available.
* - 'value' is the key indicating the value of this field. It should
* always be set. The value of the 'value' key will be the contents
* used for web services and for downloadable content. The value
* should be of the follow format types: 1) A single value (text,
* numeric, etc.) 2) An array of key value pair. 3) If multiple entries
* then cardinality should incremented and format types 1 and 2 should
* be used for each item.
* The array may contain as many other keys at the same level as 'value'
* but those keys are for internal field use and are not considered the
* value of the field.
*
*
*/
public function load($entity) {

// ChadoFields automatically load the chado column specified in the
// default settings above. If that is all you need then you don't even
// need to implement this function. However, if you need to add any
// additional data to be used in the display, you should add it here.
parent::load($entity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/**
* @class
* Purpose:
*
* Display:
* Configuration:
*/
class local__feature_search_formatter extends ChadoFieldFormatter {

// The default label for this field.
public static $default_label = 'Feature Search';

// The list of field types for which this formatter is appropriate.
public static $field_types = ['local__feature_search'];

// The list of default settings for this formatter.
public static $default_settings = [
'setting1' => 'default_value',
];

/**
* Provides the field's setting form.
*
* This function corresponds to the hook_field_formatter_settings_form()
* function of the Drupal Field API.
*
* The settings form appears on the 'Manage Display' page of the content
* type administration page. This function provides the form that will
* appear on that page.
*
* To add a validate function, please create a static function in the
* implementing class, and indicate that this function should be used
* in the form array that is returned by this function.
*
* This form will not be displayed if the formatter_settings_summary()
* function does not return anything.
*
* param $field
* The field structure being configured.
* param $instance
* The instance structure being configured.
* param $view_mode
* The view mode being configured.
* param $form
* The (entire) configuration form array, which will usually have no use
* here. Typically for reference only.
* param $form_state
* The form state of the (entire) configuration form.
*
* @return
* A Drupal Form array containing the settings form for this field.
*/
public function settingsForm($view_mode, $form, &$form_state) {

}

/**
* Provides the display for a field
*
* This function corresponds to the hook_field_formatter_view()
* function of the Drupal Field API.
*
* This function provides the display for a field when it is viewed on
* the web page. The content returned by the formatter should only include
* what is present in the $items[$delta]['values] array. This way, the
* contents that are displayed on the page, via webservices and downloaded
* into a CSV file will always be identical. The view need not show all
* of the data in the 'values' array.
*
* @param $element
* @param $entity_type
* @param $entity
* @param $langcode
* @param $items
* @param $display
*
* @return
* An element array compatible with that returned by the
* hook_field_formatter_view() function.
*/
public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
// Get the settings
$settings = $display['settings'];

$organism = $entity->chado_record;
$form = drupal_get_form('tripal_elasticsearch_gene_search_form', TRUE, "$organism->genus $organism->species", [
'ds_pane' => 'Feature Search',
]);

$content = '<p>You can search this organism’s features in the database by entering search terms in the box. You can search by feature name or annotation, for example, (Heat Shock, IPR020575, GO:0016049, etc.)</p>';
$content .= drupal_render($form);
$element[] = [
'#type' => 'markup',
'#markup' => $content,
];
}

/**
* Provides a summary of the formatter settings.
*
* This function corresponds to the hook_field_formatter_settings_summary()
* function of the Drupal Field API.
*
* On the 'Manage Display' page of the content type administration page,
* fields are allowed to provide a settings form. This settings form can
* be used to allow the site admin to define how the field should be
* formatted. The settings are then available for the formatter()
* function of this class. This function provides a text-based description
* of the settings for the site developer to see. It appears on the manage
* display page inline with the field. A field must always return a
* value in this function if the settings form gear button is to appear.
*
* See the hook_field_formatter_settings_summary() function for more
* information.
*
* @param $field
* @param $instance
* @param $view_mode
*
* @return string
* A string that provides a very brief summary of the field settings
* to the user.
*
*/
public function settingsSummary($view_mode) {
return '';
}

}
Loading

0 comments on commit eb34bd2

Please sign in to comment.