Index: millennium.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/millennium/Attic/millennium.admin.inc,v
retrieving revision 1.1.2.2
diff -u -r1.1.2.2 millennium.admin.inc
--- millennium.admin.inc	15 Oct 2009 19:36:58 -0000	1.1.2.2
+++ millennium.admin.inc	15 Oct 2009 22:27:45 -0000
@@ -123,7 +123,6 @@
  * Callback for drupal_get_form for general settings form
  */
 function millennium_admin_settings() {
-
   // Check to see if allow_url_fopen is available, if not throw an error
   // so that the administrator knows that this will not work without it
   if (!ini_get('allow_url_fopen')) {
@@ -357,3 +356,165 @@
   }
   drupal_set_message( t('@count items queued successfully', array("@count" => $queued) ));
 }
+
+/**
+ * Form to batch import by item number range
+ */
+function millennium_admin_batch_form() {
+  $form["start"] = array(
+    '#type' => 'textfield',
+    '#default_value' => '100000',
+    '#size' => 7,
+    '#title' => t('Starting item number to import'),
+  );
+  $form["end"] = array(
+    '#type' => 'textfield',
+    '#default_value' => '100000',
+    '#size' => 7,
+    '#title' => t('Starting item number to import'),
+  );
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Import above items now'),
+  );
+  return $form;
+}
+
+function millennium_admin_batch_form_validate($form, &$form_state) {
+  foreach (array("start", "end") as $element) {
+    if (!is_int($form_state["values"][$element] + 0)) {
+      form_set_error($element, t("Must be a number"));
+    }
+    if ($form_state["values"][$element] + 0 <10000) {
+      form_set_error($element, t("Must be equal or higher than 10000"));
+    }
+  }
+  if ($form_state["values"]["end"] <= $form_state["values"]["start"]) {
+    form_set_error("end", t("Ending number must be higher than starting number."));
+  }
+}
+
+function millennium_admin_batch_form_submit($form, &$form_state) {
+  #dpm($form_state);
+  $start = $form_state["values"]["start"];
+  $end = $form_state["values"]["end"];
+
+  // Build an array of item numbers to batch load
+  $item_recnums = array();
+  for ($num = $start; $num < $end; $num++) {
+    $item_recnums["i{$num}"] = "i{$num}";
+  }
+  millennium_batch_import($item_recnums);
+  $form["#redirect"] = "admin/settings/millennium/batch_import";
+}
+
+/**
+ * Batch API implementation
+ */
+function millennium_batch_import($item_recnums) {
+  $batch = array(
+    'operations' => array(
+      array('millennium_batch_import_process', array($item_recnums)),
+    ),
+    'finished' => 'millennium_batch_import_finished',
+    'title' => t('Importing'),
+    'init_message' => t('Batch import is starting.'),
+    'file' => drupal_get_path('module', 'millennium') . '/millennium.admin.inc',
+    //'progress_message' => t('Reindexed @current out of @total.'),
+    'error_message' => t('Batch importing has encountered an error.'),
+  );
+  batch_set($batch);
+}
+
+/**
+* Batch Operation Callback
+*/
+function millennium_batch_import_process($item_recnums, &$context) {
+  timer_start("millennium_batch_import_process");
+  require_once(drupal_get_path("module", "millennium") . "/millennium.import.inc");
+
+  // We can safely process this limit without a timeout.
+  $limit = variable_get('millennium_webopac_maxrecords', 50);
+  $chunks = array_chunk($item_recnums, $limit);
+
+  if (!isset($context['sandbox']['progress'])) {
+    $context['sandbox']['progress'] = 0;
+    $context['sandbox']['current_item'] = 0;
+    $context['sandbox']['tot_items'] = sizeof($item_recnums);
+    $context['sandbox']['tot_attempted'] = 0;
+    $context['sandbox']['tot_imported'] = 0;
+    $context['sandbox']['tot_notfound'] = 0;
+    $context['sandbox']['tot_fail'] = 0;
+    $context['sandbox']['elapsed'] = 0;
+    $context['sandbox']['max'] = sizeof($chunks);
+  }
+
+  // Here we actually perform our processing on the current chunk.
+  $chunk = $chunks[$context['sandbox']['current_item']];
+  $context['sandbox']['tot_attempted'] += sizeof($chunk);
+  $fetched = millennium_mass_fetch($chunk);
+  $context['sandbox']['tot_notfound'] += sizeof($fetched['not_found']);
+  // Import successful fetches only
+  foreach ($fetched['found'] as $data) {
+    $result = millennium_import_update_item(
+      $data['item_recnum'],
+      $force_update[$data['item_recnum']],
+      $data['marc'],
+      $data['bib_recnum']
+    );
+
+    if ($result["success"] !== false) {
+      $context['sandbox']['tot_imported']++;
+      #$context['results'][] = l(check_plain($result["node"]->title), "node/" . $result["node"]->nid);
+    } else {
+      $context['sandbox']['tot_fail']++;
+      /*
+       watchdog("Millennium",
+        "Queue #@id: Failed to import node: item #@recnum, error: @error",
+        array("@id" => $ids[$data['item_recnum']], "@recnum" => $data['item_recnum'], "@error" => $result["error"])
+      );
+      */
+    }
+  }
+
+  // Update our progress information.
+  $context['sandbox']['elapsed'] += (timer_read("millennium_batch_import_process")/1000);
+  #timer_stop("millennium_batch_import_process");
+  $context['sandbox']['progress']++;
+  $context['sandbox']['current_item']++;
+  $context['message'] = t("<ul><li>@imported imported<li>@notfound not found on WebOpac<li>@failed could not import<li>@pending pending<li>@items items per second</ul>",
+    array(
+      "@notfound" => $context['sandbox']['tot_notfound'],
+      "@imported" => $context['sandbox']['tot_imported'],
+      "@failed" => $context['sandbox']['tot_fail'] ,
+      "@pending" => $context['sandbox']['tot_items'] - $context['sandbox']['tot_attempted'],
+      "@items" => sprintf("%2.1f", $context['sandbox']['tot_attempted'] / $context['sandbox']['elapsed'])
+      )
+  );
+  $context['results'] = $context['message'];
+
+  // Inform the batch engine that we are not finished,
+  // and provide an estimation of the completion level we reached.
+  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
+    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
+  }
+}
+
+/**
+* Batch 'finished' callback
+*/
+function millennium_batch_import_finished($success, $results, $operations) {
+  if ($success) {
+    // Here we do something meaningful with the results.
+    #$message = format_plural(count($results), '1 item successfully processed.', '@count items successfully imported.');
+    #$message .= theme('item_list', $results);
+    $message = $results;
+  }
+  else {
+    // An error occurred.
+    // $operations contains the operations that remained unprocessed.
+    $error_operation = reset($operations);
+    $message = t('An error occurred while processing @num with arguments :', array('@num' => $error_operation[0])) . print_r($error_operation[0], TRUE);
+  }
+  drupal_set_message($message, 'error');
+}
Index: millennium.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/millennium/millennium.module,v
retrieving revision 1.13.2.33.2.2.2.29
diff -u -r1.13.2.33.2.2.2.29 millennium.module
--- millennium.module	15 Oct 2009 20:14:12 -0000	1.13.2.33.2.2.2.29
+++ millennium.module	15 Oct 2009 22:26:40 -0000
@@ -126,6 +126,15 @@
     'type' => MENU_LOCAL_TASK,
     'file' => 'millennium.admin.inc',
   );
+  $items[MILLENNIUM_SETTINGS_PATH .'/batch_import'] = array(
+    'title' => 'Import immediately',
+    'description' => 'Add item record numbers to import now.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('millennium_admin_batch_form'),
+    'access arguments' => array('administer millennium'),
+    'type' => MENU_LOCAL_TASK,
+    'file' => 'millennium.admin.inc',
+  );
 
   $items['admin/reports/millennium'] = array(
     'title' => 'Millennium import statistics',
@@ -419,7 +428,7 @@
     // Add taxonomy to new node
     #millennium_add_taxonomy_to_node($nodeobject, $marc_parsed);
 
-    return array("success" => true);
+    return array("success" => true, "node" => $nodeobject);
   }
   else {
     // There is an existing node for this item's parent bibrecord
@@ -477,7 +486,7 @@
       // Update
       millennium_node_update($nodeobject, $item_recnum, $bib_recnum);
 
-      return array("success" => true);
+      return array("success" => true, "node" => $nodeobject);
     } else {
       // TODO: Use Success?
       return array("success" => true, "error" => "Item belongs to existing bib item ". $bib_import_history->bib_recnum ." in node ". $bib_import_history->nid);

