You are here

taxonomy_edge.rebuild.inc in Taxonomy Edge 6

This file contains the functions for reuilding various tables.

File

taxonomy_edge.rebuild.inc
View source
<?php

/**
 * @file
 *
 * This file contains the functions for reuilding various tables.
 */

/**
 * Start batch job for rebuild of edges
 */
function taxonomy_edge_rebuild_edges_batch($vids) {
  $batch = array(
    'finished' => 'taxonomy_edge_rebuild_finished',
    'file' => drupal_get_path('module', 'taxonomy_edge') . '/taxonomy_edge.rebuild.inc',
    'title' => t('Rebuilding taxonomy edges'),
    'init_message' => t('Rebuilding taxonomy edges'),
  );
  foreach ($vids as $vid) {
    $batch['operations'][] = array(
      'taxonomy_edge_rebuild_edges',
      array(
        $vid,
      ),
    );
  }
  batch_set($batch);
}

/**
 * Start batch job for rebuild of order
 */
function taxonomy_edge_rebuild_order_batch($vids) {
  $batch = array(
    'finished' => 'taxonomy_edge_rebuild_finished',
    'file' => drupal_get_path('module', 'taxonomy_edge') . '/taxonomy_edge.rebuild.inc',
    'title' => t('Rebuilding taxonomy edge order'),
    'init_message' => t('Rebuilding taxonomy edge order'),
  );
  foreach ($vids as $vid) {
    $batch['operations'][] = array(
      'taxonomy_edge_rebuild_order',
      array(
        $vid,
      ),
    );
  }
  batch_set($batch);
}

/**
 * Start batch job for rebuild of edges and order
 */
function taxonomy_edge_rebuild_all_batch($vids) {
  $batch = array(
    'finished' => 'taxonomy_edge_rebuild_finished',
    'file' => drupal_get_path('module', 'taxonomy_edge') . '/taxonomy_edge.rebuild.inc',
    'title' => t('Rebuilding taxonomy edges and order'),
    'init_message' => t('Rebuilding taxonomy edges and order'),
  );
  foreach ($vids as $vid) {
    $batch['operations'][] = array(
      'taxonomy_edge_rebuild_edges',
      array(
        $vid,
      ),
    );
    $batch['operations'][] = array(
      'taxonomy_edge_rebuild_order',
      array(
        $vid,
      ),
    );
  }
  batch_set($batch);
}

/**
 * Finished function for rebuild tree batch operation.
 *
 * @param type $success
 * @param type $result
 * @param type $operations
 */
function taxonomy_edge_rebuild_finished($success, $results, $operations) {
  if ($success) {

    // Here we do something meaningful with the results.
    $message = theme('item_list', $results);
  }
  else {

    // An error occurred.
    // $operations contains the operations that remained unprocessed.
    $error_operation = reset($operations);
    $message = t('An error occurred while processing %error_operation with arguments: @arguments', array(
      '%error_operation' => $error_operation[0],
      '@arguments' => print_r($error_operation[1], TRUE),
    ));
  }
  drupal_set_message($message, 'status');
}

/**
 * Rebuild entire edge list.
 *
 * @return integer
 *   Total number of rows inserted.
 */
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;
}

/**
 * Rebuild the sorted tree.
 */
function taxonomy_edge_rebuild_order($vid, &$context) {
  $vocabulary = taxonomy_vocabulary_load($vid);
  if (!$vocabulary) {
    $context['success'] = FALSE;
    $context['message'] = t('Invalid vocabulary ID: %vid', array(
      '%vid' => $vid,
    ));
    return;
  }

  // Acquire lock
  if (!lock_acquire('taxonomy_edge_rebuild_edges_' . $vid)) {
    $context['success'] = FALSE;
    $context['message'] = t('Could not acquire lock!');
    return;
  }
  $tx = _taxonomy_edge_db_transaction();
  $time = microtime(TRUE);
  taxonomy_edge_invalidate_order($vid);
  db_query("DELETE FROM {term_edge_order} WHERE vid = %d", $vid);
  db_query("INSERT INTO {term_edge_order} (vid, parent, eid)\n    SELECT %d, op.parent, op.eid\n    FROM {term_edge} op\n    WHERE op.vid = %d\n    ORDER BY " . _taxonomy_edge_generate_term_path_query('op.tid'), $vid, $vid);
  $total_rows = db_affected_rows();
  $context['success'] = TRUE;
  $context['finished'] = 1;
  $context['message'] = t('%name sorted: %rows edges in %time seconds', array(
    '%rows' => $total_rows,
    '%time' => sprintf("%.03f", microtime(TRUE) - $time),
    '%name' => $vocabulary->name,
  ));
  $context['results'][] = $context['message'];
  db_query("INSERT INTO {term_edge_order} (vid) VALUES(%d)", -$vid);
  lock_release('taxonomy_edge_rebuild_edges_' . $vid);
}

Functions

Namesort descending Description
taxonomy_edge_rebuild_all_batch Start batch job for rebuild of edges and order
taxonomy_edge_rebuild_edges Rebuild entire edge list.
taxonomy_edge_rebuild_edges_batch Start batch job for rebuild of edges
taxonomy_edge_rebuild_finished Finished function for rebuild tree batch operation.
taxonomy_edge_rebuild_order Rebuild the sorted tree.
taxonomy_edge_rebuild_order_batch Start batch job for rebuild of order