function chado_cleanup_orphaned_nodes_part

2.x tripal_core.chado_nodes.api.inc chado_cleanup_orphaned_nodes_part($table, $job_id = NULL, $nentries, $offset, $linking_table, $node_type)
3.x tripal_core.chado_nodes.api.inc chado_cleanup_orphaned_nodes_part($table, $job_id = NULL, $nentries, $offset, $linking_table, $node_type)

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

$table: The name of the table that corresonds to the node type we want to clean up.

$job_id: This should be the job id from the Tripal jobs system. This function will update the job status using the provided job ID.

Related topics

1 call to chado_cleanup_orphaned_nodes_part()
chado_cleanup_orphaned_nodes in tripal_core/api/tripal_core.chado_nodes.api.inc
This function is a wrapper for the chado_cleanup_orphaned_nodes function. It breaks up the work of chado_cleanup_orphaned_nodes into smaller pieces that are more managable for servers that may have low php memory settings.

File

tripal_core/api/tripal_core.chado_nodes.api.inc, line 1068
API to handle much of the common functionality implemented when creating a drupal node type.

Code

function chado_cleanup_orphaned_nodes_part($table, $job_id = NULL, $nentries, 
$offset, $linking_table, $node_type) {

  $count = 0;

  // Retrieve all of the entries in the linker table for a given node type
  // and place into an array.
  print "Verifying $linking_table records...\n";
  $cnodes = array();
  $clsql = "
    SELECT *
    FROM {" . $linking_table . "} LT
    ORDER BY LT.nid LIMIT $nentries OFFSET $offset";
  $res = db_query($clsql);
  foreach ($res as $node) {
    $cnodes[$count] = $node;
    $count++;
  }

  // Iterate through all of the $linking_table entries and remove those
  // that don't have a node or don't have a $table record.
  $deleted = 0;
  if ($count > 0) {
    $i = 0;
    $interval = intval($count * 0.01);
    if ($interval < 1) {
      $interval = 1;
    }
    foreach ($cnodes as $linker) {
      // Update the job status every 1% analyses
      if ($job_id and $i % $interval == 0) {
        $percent = sprintf("%.2f", ($i / $count) * 100);
        tripal_set_job_progress($job_id, intval($percent));
        print "Percent complete: $percent%. Memory: " . number_format(memory_get_usage()) . " bytes.\n";
      }

      // See if the node exits, if not remove the entry from linking table table.
      $nsql = "SELECT * FROM {node} WHERE nid = :nid AND type = :node_type";
      $results = db_query($nsql, array(':nid' => $linker->nid, ':node_type' => $node_type));
      $node = $results->fetchObject();
      if (!$node) {
        $deleted++;
        db_query("DELETE FROM {" . $linking_table . "} WHERE nid = :nid", array(':nid' => $linker->nid));
        //print "$linking_table missing node.... DELETING where nid=".$linker->nid." $linking_table entry.\n";
      }

      // Does record in chado exists, if not remove entry from $linking_table.
      $table_id = $table . "_id";
      $lsql = "SELECT * FROM {" . $table . "} where " . $table_id . " = :chado_id";
      $results = chado_query($lsql, array(":chado_id" => $linker->$table_id));
      $record = $results->fetchObject();
      if (!$record) {
        $deleted++;
        $sql = "DELETE FROM {" . $linking_table . "} WHERE " . $table_id . " = :chado_id";
        db_query($sql, array(":chado_id" => $linker->$table_id));
        //print "$linking_table missing $table.... DELETING where $table_id=".$linker->$table_id." $linking_table entry.\n";
      }
      $i++;
    }
    $percent = sprintf("%.2f", ($i / $count) * 100);
    tripal_set_job_progress($job_id, intval($percent));
    print "Percent complete: $percent%. Memory: " . number_format(memory_get_usage()) . " bytes.\n";
  }
  print "\nDeleted $deleted record(s) from $linking_table missing either a node or chado entry.\n";

  // Build the SQL statements needed to check if nodes point to valid record.
  print "Verifying nodes...\n";
  $dsql = "
    SELECT *
    FROM {node}
    WHERE type = :node_type
    ORDER BY nid
    LIMIT $nentries OFFSET $offset
  ";

  $dsql_args = array(':node_type' => $node_type);
  $nodes = array();
  $res = db_query($dsql, $dsql_args);
  $count = 0;
  foreach ($res as $node) {
    $nodes[$count] = $node;
    $count++;
  }

  // Iterate through all of the nodes and delete those that don't
  // have a corresponding entry in the linking table.
  $deleted = 0;
  if ($count > 0) {
    $i = 0;
    $interval = intval($count * 0.01);
    if ($interval < 1) {
      $interval = 1;
    }
    foreach ($nodes as $node) {
      // update the job status every 1%
      if ($job_id and $i % $interval == 0) {
        $percent = sprintf("%.2f", ($i / $count) * 100);
        tripal_set_job_progress($job_id, intval($percent));
        print "Percent complete: $percent%. Memory: " . number_format(memory_get_usage()) . " bytes.\r";
      }

      // check to see if the node has a corresponding entry
      // in the $linking_table table. If not then delete the node.
      $csql = "SELECT * FROM {" . $linking_table . "} WHERE nid = :nid ";
      $results = db_query($csql, array(':nid' => $node->nid));
      $link = $results->fetchObject();
      if (!$link) {
        // Checking node_access creates a memory leak. Commenting out for now
        // assuming that this code can only be run by a site administrator
        // anyway.
        //         if (node_access('delete', $node)) {
        $deleted++;
        node_delete($node->nid);
        //         }
        //         else {
        //           print "\nNode missing in $linking_table table.... but cannot delete due to improper permissions (node $node->nid)\n";
        //         }
      }
      $i++;
    }
    $percent = sprintf("%.2f", ($i / $count) * 100);
    tripal_set_job_progress($job_id, intval($percent));
    print "Percent complete: $percent%. Memory: " . number_format(memory_get_usage()) . " bytes.\r";
    print "\nDeleted $deleted node(s) that did not have corresponding $linking_table entries.\n";
  }

  return '';
}