Tripal v1.0 (6.x-1.0)
Core Module API

Modules

 AHAH API
 Chado API
 Custom Tables API
 Files API
 Jobs API
 Materalized Views API

Functions

 tripal_get_chado_custom_schema ($table)
 tripal_core_get_chado_tables ($include_custom=NULL)
 tripal_core_set_chado_version ()
 tripal_core_get_chado_version ($exact=FALSE, $warn_if_unsupported=FALSE)
 tripal_core_get_chado_table_schema ($table)
 tripal_core_clean_orphaned_nodes ($table, $job_id)

Function Documentation

tripal_core_clean_orphaned_nodes ( table,
job_id 
)

This function will delete Drupal nodes for any sync'ed table (e.g. feature, organism, analysis, stock, library) if the chado record has been deleted or the entry in the chado_[table] table has been removed.

Parameters:
$tableThe name of the table that corresonds to the node type we want to clean up.
$job_idThis should be the job id from the Tripal jobs system. This function will update the job status using the provided job ID.

Definition at line 3647 of file tripal_core_chado.api.inc.

                                                           {
  $count = 0;

  // build the SQL statments needed to check if nodes point to valid analyses
  $dsql = "SELECT * FROM {node} WHERE type = 'chado_%s' order by nid";
  $nsql = "SELECT * FROM {node} WHERE nid = %d";
  $csql = "SELECT * FROM {chado_%s} where nid = %d ";
  $clsql= "SELECT * FROM {chado_%s}";
  $lsql = "SELECT * FROM %s where %s_id = %d ";

  // load into nodes array
  print "Getting nodes\n";
  $nodes = array();
  $res = db_query($dsql, $table);
  while ($node = db_fetch_object($res)) {
    $nodes[$count] = $node;
    $count++;
  }

  // load the chado_$table into an array
  print "Getting chado_$table\n";
  $cnodes = array();
  $res = db_query($clsql, $table);
  while ($node = db_fetch_object($res)) {
    $cnodes[$count] = $node;
    $count++;
  }
  $interval = intval($count * 0.01);
  if ($interval < 1) {
    $interval = 1;
  }

  // iterate through all of the chado_$table entries and remove those
  // that don't have a node or don't have a $table record in chado.libary
  print "Verifying all chado_$table Entries\n";
  $deleted = 0;
  foreach ($cnodes as $nid) {

    // update the job status every 1% analyses
    if ($job_id and $i % $interval == 0) {
      tripal_job_set_progress($job_id, intval(($i / $count) * 100));
    }

    // see if the node exits, if not remove the entry from the chado_$table table
    $node = db_fetch_object(db_query($nsql, $nid->nid));
    if (!$node) {
      $deleted++;
      db_query("DELETE FROM {chado_%s} WHERE nid = %d", $table, $nid->nid);
      $message = "chado_$table missing node.... DELETING: $nid->nid";
      watchdog('tripal_core', $message, array(), WATCHDOG_WARNING);
    }

    // see if the record in chado exist, if not remove the entry from the chado_$table
    $table_id = $table . "_id";
    $record = db_fetch_object(chado_query($lsql, $table, $table, $nid->$table_id));
    if (!$record) {
      $deleted++;
      db_query("DELETE FROM {chado_%s} WHERE %s_id = '%d'", $table, $table, $nid->$table_id);
      $message = "chado_$table missing $table.... DELETING entry.";
      watchdog('tripal_core', $message, array(), WATCHDOG_WARNING);
    }
    $i++;
  }
  print "\t$deleted chado_$table entries missing either a node or chado entry.\n";

  // iterate through all of the nodes and delete those that don't
  // have a corresponding entry in chado_$table
  $deleted = 0;
  foreach ($nodes as $node) {

    // update the job status every 1% libraries
    if ($job_id and $i % $interval == 0) {
      tripal_job_set_progress($job_id, intval(($i / $count) * 100));
    }

    // check to see if the node has a corresponding entry
    // in the chado_$table table. If not then delete the node.
    $link = db_fetch_object(db_query($csql, $table, $node->nid));
    if (!$link) {
      if (node_access('delete', $node)) {
        $deleted++;
        $message = "Node missing in chado_$table table.... DELETING node $node->nid";
        watchdog("tripal_core", $message, array(), WATCHDOG_WARNING);
        node_delete($node->nid);
      }
      else {
        $message = "Node missing in chado_$table table.... but cannot delete due to improper permissions (node $node->nid)";
        watchdog("tripal_core", $message, array(), WATCHDOG_WARNING);
      }
    }
    $i++;
  }
  print "\t$deleted nodes did not have corresponding chado_$table entries.\n";

  return '';
}
tripal_core_get_chado_table_schema ( table)

Retrieves the chado tables Schema API array.

Parameters:
$tableThe name of the table to retrieve. The function will use the appopriate Tripal chado schema API hooks (e.g. v1.11 or v1.2).
Returns:
A Drupal Schema API array defining the table.

Definition at line 3618 of file tripal_core_chado.api.inc.

                                                    {

  // first get the chado version that is installed
  $v = tripal_core_get_chado_version();

  // get the table array from the proper chado schema
  $v = preg_replace("/\./", "_", $v); // reformat version for hook name
  $table_arr = module_invoke_all("chado_schema_v" . $v . "_" . $table);

  // if the table_arr is empty then maybe this is a custom table
  if (!is_array($table_arr) or count($table_arr) == 0) {
    $table_arr = tripal_get_chado_custom_schema($table);
  }

  return $table_arr;
}
tripal_core_get_chado_tables ( include_custom = NULL)

Retrieves the list tables in the Chado schema. By default it only retursn the default Chado tables, but may also return custom tables added to the Chado schema as well.

Parameters:
$include_customOptional. Set as TRUE to include any custom tables created in the Chado schema. Custom tables are added to Chado using the tripal_core_chado_create_table() function.
Returns:
An associative array where the key and value pairs are the Chado table names.

Definition at line 3463 of file tripal_core_chado.api.inc.

                                                              {


  // first get the chado version that is installed
  $v = tripal_core_get_chado_version();

  $tables = array();
  if ($v == '1.2') {
    $tables_v1_2 = tripal_core_chado_get_v1_2_tables();
    foreach ($tables_v1_2 as $table) {
      $tables[$table] = $table;
    }
  }
  if ($v == '1.11' or $v == '1.11 or older') {
    $tables_v1_11 = tripal_core_chado_get_v1_11_tables();
    foreach ($tables_v1_11 as $table) {
      $tables[$table] = $table;
    }
  }

  // now add in the custom tables too if requested
  if ($include_custom) {
    $sql = "SELECT table_name FROM {tripal_custom_tables}";
    $resource = db_query($sql);

    while ($r = db_fetch_object($resource)) {
      $tables[$r->table_name] = $r->table_name;
    }
  }

  asort($tables);
  return $tables;
}
tripal_core_get_chado_version ( exact = FALSE,
warn_if_unsupported = FALSE 
)

Returns the version number of the currently installed Chado instance. It can return the real or effective version.

Parameters:
$exactSet this argument to 1 to retrieve the exact version that is installed. Otherwise, this function will set the version to the nearest 'tenth'. Chado versioning numbers in the hundreds represent changes to the software and not the schema. Changes in the tenth's represent changes in the schema.
$warn_if_unsupportedIf the currently installed version of Chado is not supported by Tripal the generatea a Drupal warning.
Returns:
The version of Chado

Definition at line 3571 of file tripal_core_chado.api.inc.

                                                                                     {
  // first get the chado version that is installed
  $exact_version = variable_get('chado_version', '');
  if (!$exact_version) {
    $exact_version = tripal_core_set_chado_version();
  }

  // Tripal only supports v1.11 or newer.. really this is the same as v1.1
  // but at the time the v1.11 schema API was written we didn't know that so
  // we'll return the version 1.11 so the schema API will work.
  if (strcmp($exact_version, '1.11 or older') == 0) {
    $exact_version = "1.11";
    if ($warn_if_unsupported) {
      drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.11.  If you are certain this is v1.11
         or if Chado was installed using an earlier version of Tripal then all is well. If not please upgrade to v1.11 or later"),
         'warning');
    }
  }

  // if not returing an exact version, return the version to the nearest 10th.
  // return 1.2 for all versions of 1.2x
  $effective_version = $exact_version;
  if (preg_match('/^1\.2\d+$/', $effective_version)) {
    $effective_version = "1.2";
  }
  if ($warn_if_unsupported and ($effective_version != 1.11 and $effective_version != 1.2 and $effective_version != 'not installed')) {
    drupal_set_message(t("WARNING: The currently installed version of Chado, v$exact_version, is not fully compatible with Tripal."), 'warning');
  }
  // if the callee has requested the exact version then return it
  if ($exact) {
    return $exact_version;
  }

  return $effective_version;
}
tripal_core_set_chado_version ( )

Sets a Drupal variable with the current version of Chado. This variable can then be queried later using the tripal_core_get_chado_Version

Returns:
The version of Chado

Definition at line 3505 of file tripal_core_chado.api.inc.

                                         {
  global $db_url;

  // check that Chado is installed if not return 'uninstalled as the version'
  $chado_exists = tripal_core_chado_schema_exists();
  if (!$chado_exists) {
    // if it's not in the drupal database check to see if it's specified in the $db_url
    // in the settings.php
    if (!is_array($db_url) or !array_key_exists('chado', $db_url)) {
      // if it's not in the drupal database or specified in the $db_url then
      // return uninstalled as the version
      return 'not installed';
    }
  }

  // if the table doesn't exist then we don't know what version but we know
  // it must be 1.11 or older.
  $previous_db = tripal_db_set_active('chado');
  $prop_exists = db_table_exists('chadoprop');
  tripal_db_set_active($previous_db);
  if (!$prop_exists) {
    return "1.11 or older";
  }

  // we can't use the Tripal API to query this table
  // because the Tripal API depends on this function to
  // tell it the version. So, we need a typical SQL statement
  $sql = "SELECT value "
  ."FROM chadoprop CP "
  ."  INNER JOIN cvterm CVT on CVT.cvterm_id = CP.type_id "
  ."  INNER JOIN cv CV on CVT.cv_id = CV.cv_id "
  ."WHERE CV.name = 'chado_properties' and CVT.name = 'version'";
  $previous_db = tripal_db_set_active('chado');
  $v = db_fetch_object(db_query($sql));
  tripal_db_set_active($previous_db);

  // if we don't have a version in the chadoprop table then it must be
  // v1.11 or older
  if (!$v->value) {
    variable_set('chado_version', "1.11 or older");
    return "1.11 or older";
  }

  variable_set('chado_version', $v->value);
  return $v->value;
}
tripal_get_chado_custom_schema ( table)

Retrieves the schema in an array for the specified custom table.

Parameters:
$tableThe name of the table to create.
Returns:
A Drupal-style Schema API array definition of the table. Returns FALSE on failure.

Definition at line 3386 of file tripal_core_chado.api.inc.

                                                {

  $sql = "SELECT schema FROM {tripal_custom_tables} WHERE table_name = '%s'";
  $custom = db_fetch_object(db_query($sql, $table));
  if (!$custom) {
    return FALSE;
  }
  else {
    return unserialize($custom->schema);
  }
}
 All Classes Files Functions Variables