You are here

function taxonomy_edge_rebuild_edges in Taxonomy Edge 7

Same name and namespace in other branches
  1. 8 taxonomy_edge.rebuild.inc \taxonomy_edge_rebuild_edges()
  2. 6 taxonomy_edge.rebuild.inc \taxonomy_edge_rebuild_edges()
  3. 7.2 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_7101 in ./taxonomy_edge.install
Fixup schema version and broken upgrade paths.
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 79
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 ...
  $queue = DrupalQueue::get('taxonomy_edge', TRUE);
  $queue
    ->deleteQueue();

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

  // Clear out edge data for vocabulary.
  taxonomy_edge_taxonomy_vocabulary_insert($vocabulary);
  $total_rows = 1;
  $result = db_query("INSERT INTO {taxonomy_term_edge} (vid, tid, parent, distance)\n    SELECT DISTINCT d.vid, h.tid, h.tid, 0\n    FROM {taxonomy_term_hierarchy} h\n    INNER JOIN {taxonomy_term_data} d ON d.tid = h.tid\n    WHERE h.tid <> 0\n    AND h.tid <> h.parent\n    AND d.vid = :vid\n  ", array(
    ':vid' => $vid,
  ));
  $total_rows += $result
    ->rowCount();
  $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 {taxonomy_term_edge} (vid, tid, parent, distance)\n      SELECT e.vid, e.tid, h.parent, e.distance + 1\n      FROM {taxonomy_term_edge} e\n      INNER JOIN {taxonomy_term_hierarchy} h ON h.tid = e.parent\n      INNER JOIN {taxonomy_term_data} d ON d.tid = h.tid\n      WHERE e.distance = :depth\n      AND e.vid = :vid\n      AND h.tid <> 0\n      AND h.tid <> h.parent\n    ", array(
      ':vid' => $vid,
      ':depth' => $depth,
    ));
    $rows = $result
      ->rowCount();
    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['results'][] = 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,
  ));
  return $total_rows;
}