You are here

function taxonomy_edge_rebuild_edges in Taxonomy Edge 6

Same name and namespace in other branches
  1. 8 taxonomy_edge.rebuild.inc \taxonomy_edge_rebuild_edges()
  2. 7.2 taxonomy_edge.rebuild.inc \taxonomy_edge_rebuild_edges()
  3. 7 taxonomy_edge.rebuild.inc \taxonomy_edge_rebuild_edges()

Rebuild entire edge list.

Return value

integer Total number of rows inserted.

2 calls to taxonomy_edge_rebuild_edges()
taxonomy_edge_cron_rebuild in ./taxonomy_edge.module
Rebuild edges
taxonomy_edge_update_6101 in ./taxonomy_edge.install
Update edge table with serial, vocabulary id and indexes, and add order table.
2 string references to 'taxonomy_edge_rebuild_edges'
taxonomy_edge_rebuild_all_batch in ./taxonomy_edge.rebuild.inc
Start batch job for rebuild of edges and order
taxonomy_edge_rebuild_edges_batch in ./taxonomy_edge.rebuild.inc
Start batch job for rebuild of edges

File

./taxonomy_edge.rebuild.inc, line 84
This file contains the functions for reuilding various tables.

Code

function taxonomy_edge_rebuild_edges($vid, &$context) {
  $depth = 0;
  $max_depth = variable_get('taxonomy_edge_max_depth', TAXONOMY_EDGE_MAX_DEPTH);
  $vocabulary = taxonomy_vocabulary_load($vid);
  if (!$vocabulary) {
    $context['success'] = FALSE;
    $context['results'][] = t('Invalid vocabulary ID: %vid', array(
      '%vid' => $vid,
    ));
    return;
  }

  // Acquire lock to avoid conflicts with queue
  if (!lock_acquire('taxonomy_edge_rebuild_edges_' . $vid)) {
    $context['success'] = FALSE;
    $context['results'][] = t('Could not acquire lock!');
    return;
  }
  $time = microtime(TRUE);
  set_time_limit(86400);

  // Clear the queue, we're rebulding anyways ...
  if (module_exists('drupal_queue')) {
    drupal_queue_include();
    $queue = DrupalQueue::get('taxonomy_edge', TRUE);
    $queue
      ->deleteQueue();
  }

  // Please use a proper isolation level, so that transaction provides us with a
  // snapshot
  $tx = _taxonomy_edge_db_transaction();

  // Clear out edge data for vocabulary.
  taxonomy_edge_taxonomy_vocabulary_insert($vocabulary);
  $total_rows = 1;
  $result = db_query("INSERT INTO {term_edge} (vid, tid, parent, distance)\n    SELECT DISTINCT %d, h.tid, h.tid, 0\n    FROM {term_hierarchy} h\n    INNER JOIN {term_data} d ON d.tid = h.tid\n    WHERE h.tid <> 0\n    AND h.tid <> h.parent\n    AND d.vid = %d\n  ", $vid, $vid);
  $total_rows += db_affected_rows();
  $context['message'] = t('Processed %rows rows - current depth: %depth', array(
    '%rows' => $total_rows,
    '%depth' => $depth,
  ));
  $context['finished'] = 0.5;
  while ($max_depth-- > 0) {
    $result = db_query("INSERT INTO {term_edge} (vid, tid, parent, distance)\n      SELECT %d, e.tid, h.parent, %d\n      FROM {term_edge} e\n      INNER JOIN {term_hierarchy} h ON h.tid = e.parent\n      INNER JOIN {term_data} d ON d.tid = h.tid\n      WHERE e.distance = %d\n      AND e.vid = %d\n      AND h.tid <> 0\n      AND h.tid <> h.parent\n    ", $vid, $depth + 1, $depth, $vid);
    $rows = db_affected_rows();
    if ($rows <= 0) {
      break;
    }
    $depth++;
    $total_rows += $rows;
    $context['message'] = t('Processed %rows rows - current depth: %depth', array(
      '%rows' => $total_rows,
      '%depth' => $depth,
    ));
    $context['finished'] += (1 - $context['finished']) / 2;
  }
  taxonomy_edge_invalidate_order($vid);
  lock_release('taxonomy_edge_rebuild_edges_' . $vid);
  $context['success'] = TRUE;
  $context['finished'] = 1;
  $context['message'] = t('%name rebuilt: %rows processed with depth %depth in %time seconds', array(
    '%rows' => $total_rows,
    '%depth' => $depth,
    '%time' => sprintf("%.03f", microtime(TRUE) - $time),
    '%name' => $vocabulary->name,
  ));
  $context['results'][] = $context['message'];
  return $total_rows;
}