You are here

function biblio_import_batch_operations in Bibliography Module 7.2

Same name and namespace in other branches
  1. 6.2 includes/biblio.import.export.inc \biblio_import_batch_operations()
  2. 6 biblio.import.export.inc \biblio_import_batch_operations()
  3. 7 includes/biblio.import.export.inc \biblio_import_batch_operations()
1 string reference to 'biblio_import_batch_operations'
biblio_import_form_submit in includes/biblio.import.export.inc
Implementation of hook_submit() for the biblio_import_form.

File

includes/biblio.import.export.inc, line 296
Functions that are used to import and export biblio data.

Code

function biblio_import_batch_operations($session_id, $user, $userid, $terms, &$context) {
  $limit = 10;
  if (empty($context['sandbox'])) {

    // Initiate multistep processing.
    $context['results']['session_id'] = $session_id;
    $context['results']['userid'] = $userid;
    $context['results']['user'] = $user;
    $context['results']['terms'] = $terms;
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_id'] = 0;
    $context['results']['bids'] = array();
    $context['sandbox']['max'] = db_query("SELECT COUNT(DISTINCT(id)) FROM {biblio_import_cache} WHERE session_id = :sessid", array(
      ':sessid' => $session_id,
    ))
      ->fetchField();
    $context['sandbox']['itters'] = $context['sandbox']['max'] / $limit;
    $context['sandbox']['eta'] = 0;
  }

  // Bail out if the cache is empty.
  if ($context['sandbox']['max'] == 0) {
    return;
  }

  // Process the next 20 nodes.
  timer_start('biblio_import');
  $result = db_query_range("SELECT id, data FROM {biblio_import_cache} WHERE id > :id AND session_id = :sessid ORDER BY id ASC", 0, $limit, array(
    ':id' => $context['sandbox']['current_id'],
    ':sessid' => $session_id,
  ));
  foreach ($result as $row) {
    if ($biblio = unserialize(base64_decode($row->data))) {
      biblio_save_node($biblio, $terms);
      $context['results']['bids'][] = $biblio->bid;
    }
    $context['sandbox']['progress']++;
    $context['sandbox']['current_id'] = $row->id;
  }
  $looptime = timer_stop('biblio_import');
  $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>Nodes saved: %progress <br> Time remaining: %eta min.<br>', array(
      '%progress' => $progress,
      '%eta' => $eta,
    ));
  }

  // Multistep processing : report progress.
  if ($context['sandbox']['progress'] <= $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}