function chado_update_existing_node_urls

2.x tripal_core.chado_nodes.title_and_path.api.inc chado_update_existing_node_urls($content_type, $job_id = 0)
3.x tripal_core.chado_nodes.title_and_path.inc chado_update_existing_node_urls($content_type, $job_id = 0)

Tripal Job for updating all node URLs for a particular node type.

Parameters

$content_type: The machine name of the node type to update URLs for.

$job_id: The ID of the tripal job calling this function.

1 string reference to 'chado_update_existing_node_urls'
chado_add_admin_form_set_url_form_submit in tripal_core/api/tripal_core.chado_nodes.title_and_path.api.inc
Implements hook_form_submit(). SUBMIT: Actually add the format specified by chado_add_admin_form_set_title()

File

tripal_core/api/tripal_core.chado_nodes.title_and_path.api.inc, line 655
Contains API functions to set titles and paths for all chado nodes

Code

function chado_update_existing_node_urls($content_type, $job_id = 0) {
  $transaction = db_transaction();

  print "\nNOTE: Setting of URLs is performed using a database transaction. \n" .
    "If the load fails or is terminated prematurely then the entire set of \n" .
    "new URLs will be rolled back and no changes will be made.\n\n";

  try {
    // Get the number of records we need to set URLs for.
    $num_nodes = db_query('SELECT count(*) FROM {' . $content_type . '}')->fetchField();

    // Calculate the interval at which we will print an update on the screen.
    $num_set = 0;
    $num_per_interval = 100;

    if ($num_nodes > 0) {

      print "There are $num_nodes nodes to update URLs for. You will be \n" .
      "updated on the progress every 100 nodes.\n\n";

      // Get the list of nodes of this particular content type.
      $query = new EntityFieldQuery();
      $result = $query->entityCondition('entity_type', 'node')
        ->entityCondition('bundle', $content_type)
        ->execute();

      if (isset($result['node'])) {
        $nids = array_keys($result['node']);

        foreach ($nids as $nid) {

          // Load the current node. Normally here we would use the node_load()
          // function that is part of the Drupal API.  However, this seems
          // to have memory leaks that have not yet been identified, so
          // we'll create the object by hand:
          $node = new stdClass();
          $node->nid = $nid;
          $node->type = $content_type;
          $table = preg_replace('/chado_/', '', $content_type);
          $id = chado_get_id_from_nid($table, $nid);
          $node->$table = chado_generate_var($table, array($table . "_id" => $id));

          // Now set the URL for the current node.
          $alias = chado_set_node_url($node);

          // update the job status every 1% nodes
          if ($num_set % $num_per_interval == 0) {
            $percent = ($num_set / $num_nodes) * 100;

            // Update the user on progress.
            $percent = sprintf("%.2f", $percent);
            print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";

            // Update the tripal job.
            if ($job_id) {
              tripal_set_job_progress($job_id, intval($percent));
            }
          }
          $num_set++;
        }
      }
      $percent = ($num_set / $num_nodes) * 100;
      tripal_set_job_progress($job_id, intval($percent));
      $percent = sprintf("%.2f", $percent);
      print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
      print "\nDone. Set " . number_format($num_set) . " URLs\n";
    }
    else {
      print "\nThere are no $content_type nodes to update. If you know there\n"
      . "are records of this type in chado, try sync'ing them to Drupal.\n";
    }
  }
  catch (Exception $e) {
    $transaction->rollback();
    print "\n"; // make sure we start errors on new line
    watchdog_exception('chado_node_api', $e);
    tripal_report_error(
    'chado_node_api', 
    TRIPAL_WARNING, 
    'Unable to update URLs for the :content-type Node Type. Specifically, the last URL attempted was NID=:nid and Alias=:alias', 
    array(':content-type' => $content_type, ':nid' => $node->nid, ':alias' => $alias)
    );
  }
}