Index: millennium.import.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/millennium/Attic/millennium.import.inc,v
retrieving revision 1.1.2.3
diff -u -r1.1.2.3 millennium.import.inc
--- millennium.import.inc	16 Oct 2009 17:09:06 -0000	1.1.2.3
+++ millennium.import.inc	16 Oct 2009 20:16:53 -0000
@@ -36,8 +36,9 @@
  * Gets item information (bib number & MARC record) using the III's book cart.
  * Recieves an array of item numbers and returns an array of found item data including bib number and MARC keyed by item number, and an unkeyed array of not found items.
  * @param array $item_recnums An unkeyed array of item numbers = array('i100000', 'i100002', ...)
+ * @param bool $complete_holdings A flag that specifies if an extra request should be done if the holdings table is incomplete.
  */
-function millennium_fetch_records_via_bookcart($item_recnums) {
+function millennium_fetch_records_via_bookcart($item_recnums, $complete_holdings = true) {
   static $headers = array();
 
   // Start timer to measure average performance
@@ -46,26 +47,34 @@
   $baseurl = millennium_get_real_baseurl();
   // If called for first time, initiate a session and store the III_SESSION_ID cookie
   if (sizeof($headers) == 0 ) {
-    // Get a session cookie
-    $result = drupal_http_request("{$baseurl}/search/X?test");
-    #dpm($result);
-    if (empty($result->headers["Set-Cookie"])) {
-      return array('found' => array(), 'not_found' => array());
+    // Try to get from cache first
+    $cached = cache_get("millennium_fetch_records_via_bookcart_headers");
+    if ($cached->data) {
+      $headers = $cached->data;
+    } else {
+      // Get a session cookie
+      $result = drupal_http_request("{$baseurl}/search/X");
+      #dpm($result);
+      if (empty($result->headers["Set-Cookie"])) {
+        return array('found' => array(), 'not_found' => array());
+      }
+      $session_cookie = preg_replace(
+        '/.*(III_SESSION_ID=[a-zA-Z0-9\.]+).*/',
+        '\1',
+        $result->headers["Set-Cookie"]
+      );
+      $headers = array('Cookie' => $session_cookie);
+      // Cache this session cookie for 5 minutes
+      cache_set("millennium_fetch_records_via_bookcart_headers", $headers, 'cache', time() + 300);
     }
-    $session_cookie = preg_replace(
-      '/.*(III_SESSION_ID=[a-zA-Z0-9\.]+).*/',
-      '\1',
-      $result->headers["Set-Cookie"]
-    );
-    $headers = array('Cookie' => $session_cookie);
   }
 
   // Issue N requests to add items to the bookcart, 25 records at a time
   $chunks = array_chunk($item_recnums, 25);
   foreach ($chunks as $chunk) {
     // Add items from $item_recnums array to the cart
-    $path = '/search?/Xtest&searchscope=0&SORT=D/Xtest&searchscope=0&SORT=D&SUBKEY=test/1%2C7175%2C7175%2CE/2browse';
-    $post = "jumpref=Xtest&save=" . implode("&save=", $chunk) . "&save_func=save_marked";
+    $path = '/search?/X&searchscope=0&SORT=D/X&searchscope=0&SORT=D&SUBKEY=/1%2C7175%2C7175%2CE/2browse';
+    $post = "jumpref=X&save=" . implode("&save=", $chunk) . "&save_func=save_marked";
     $result = drupal_http_request("{$baseurl}{$path}", $headers, 'POST', $post);
   }
   #dpm($result);
@@ -103,12 +112,13 @@
   #dpm($item_to_bib);
 
   // Get MARC for all!
-  $path = "/search/?.i100000/++export/1%2C-1%2C-1%2CB/export/";
+  $path = "/search/?/++export/1%2C-1%2C-1%2CB/export/";
   $post = "email_addx=&email_subj=&ex_device=43&ex_format=50";
   $result = drupal_http_request("{$baseurl}{$path}", $headers, 'POST', $post);
   #dpm($result->data);
+  // Split records
   $ok = preg_match_all(
-    '/<pre>(.*?)<\/pre>/s',
+    '/<div class="exportHeading">.*?<pre>(.*?)<\/pre>.*?(<table.*?bibItems.*?<\/table>)(?:.<center>.<form .*? action="(.*?)".*?<\/form>|)/si',
     $result->data,
     $matches,
     PREG_SET_ORDER
@@ -118,12 +128,20 @@
   $index = 0;
   foreach ($found_items as $item => $dummy) {
     $found_items[$item]['marc'] = $matches[$index][1];
+
+    // Add holdings information; determine if an extra request is needed
+    $this_bib_recnum = $found_items[$item]['bib_recnum'];
+    if ($matches[$index][3] != "" && $complete_holdings == true) {
+      $found_items[$item]['holdings'] = millennium_get_holdings_info($this_bib_recnum);
+    } else {
+      $found_items[$item]['holdings'] = millennium_get_holdings_info($this_bib_recnum, $matches[$index][2]);
+    }
     $index++;
   }
   #dpm($found_items);
 
   // Clear the cart
-  $path = "/search?/X/X/1,-1,-1,B/browse?clear_saves=1";
+  $path = "/search?///1,-1,-1,B/browse?clear_saves=1";
   $dummy = drupal_http_request("{$baseurl}{$path}", $headers, 'GET');
 
   // Read timer.

