You are here

function biblio_batch_uninstall in Bibliography Module 7

Same name and namespace in other branches
  1. 6.2 biblio.install \biblio_batch_uninstall()
1 string reference to 'biblio_batch_uninstall'
biblio_uninstall in ./biblio.install

File

./biblio.install, line 117

Code

function biblio_batch_uninstall(&$context) {
  $limit = 5;
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_node'] = 0;
    $context['sandbox']['max'] = db_query("SELECT COUNT(DISTINCT nid) FROM {node} WHERE type='biblio'")
      ->fetchField();
    $context['sandbox']['itters'] = $context['sandbox']['max'] / $limit;
    $context['sandbox']['eta'] = 0;
  }

  // Bail out if the cache is empty.
  if ($context['sandbox']['max'] == 0) {
    $context['finished'] = 1;
    return;
  }
  timer_start('biblio_delete');
  $result = db_select('node')
    ->fields('node', array(
    'nid',
  ))
    ->condition('nid', $context['sandbox']['current_node'], '>')
    ->condition('type', 'biblio')
    ->orderBy('nid')
    ->range(0, $limit)
    ->execute();
  foreach ($result as $row) {
    $node = node_delete($row->nid);
    if (function_exists('search_wipe')) {
      search_wipe($row->nid, 'node');
    }
    $context['sandbox']['progress']++;
    $context['sandbox']['current_node'] = $row->nid;
  }
  $looptime = timer_stop('biblio_delete');
  $context['sandbox']['eta'] += $looptime['time'];
  $itters = $context['sandbox']['progress'] / $limit;
  if ($itters) {
    $average_time = $context['sandbox']['eta'] / $itters;
    $eta = ($context['sandbox']['itters'] * $average_time - $average_time * $itters) / 1000;
    if ($eta >= 60) {
      $min = (int) $eta / 60;
    }
    else {
      $min = 0;
    }
    $sec = $eta % 60;
    $eta = sprintf("%d:%02d", $min, $sec);
    $progress = sprintf("%d / %d", $context['sandbox']['progress'], $context['sandbox']['max']);
    $context['message'] = t('<br>Biblio nodes deleted: %progress <br> Time remaining: %eta min.<br>', array(
      '%progress' => $progress,
      '%eta' => $eta,
    ));
  }
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}