# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: contributions/modules/millennium/millennium.import.inc
--- contributions/modules/millennium/millennium.import.inc Base (1.1.2.14)
+++ contributions/modules/millennium/millennium.import.inc Locally Modified (Based On 1.1.2.14)
@@ -2,6 +2,74 @@
 // $Id: millennium.import.inc,v 1.1.2.14 2010/02/17 20:36:06 janusman Exp $
 
 /**
+ * Recieves a keyword query, and returns scraped record numbers to later harvest
+ * via other functions like millennium_mass_fetch().
+ * 
+ * @param $query
+ *   a string with a keyword query, same as that typed on the /search/X URL on
+ *   the WebOPAC.
+ * @param $base_url
+ * @param $limit
+ *   Maximum number of records to return.
+ */
+function millennium_query_scrape($query, $base_url = false, $limit = 100) {
+  $records = array();
+
+  if ($base_url == false) {
+    $base_url = millennium_get_real_baseurl();
+  } else {
+    $base_url = millennium_get_real_baseurl($base_url);
+  }
+
+  // Build query URL
+  $path = "/search/X?" . drupal_urlencode($query);
+
+  // This will loop while more pages of results are found
+  while (TRUE) {
+    $url = $base_url . $path;
+    
+    // Launch
+    $result = millennium_http_request($url);
+    if ($result->code != 200) {
+      drupal_set_message(t('WebOPAC returned "Not found" on !link', array("!link" => l($url, $url))));
+      break;
+    }
+
+    // Look for <input type="checkbox" name="save" value="b1890629" >
+    $ok = preg_match_all(
+      '/name="save" value="(b[0-9]+)"[^>]+>/si',
+      $result->data,
+      $matches
+    );
+    if (!$ok) {
+      // Try second regexp: cart button
+      $ok = preg_match_all(
+        '/\?save=(b[0-9]+)/si',
+        $result->data,
+        $matches
+      );
+    }
+    if (!$ok) {
+      drupal_set_message(t("No records found on URL !link", array("!link" => l($url, $url))));
+      break;
+    }
+    $records = array_merge($records, $matches[1]);
+    if (sizeof($records) >= $limit) {
+      break;
+    }
+
+    // Look for "next" pager
+    $ok = preg_match('/<td .*?class=.browsePager.*?<strong>[0-9]+<\/strong>[^<]*<a href="(\/search[^"]+)">([0-9]+)/si', $result->data, $pager_link_matches);
+    if (!$ok) {
+      break;
+    }
+    // Set path for next call from "Next" pager
+    $path = $pager_link_matches[1];
+  }
+  return array_splice($records, 0, $limit);
+}
+
+/**
  * Gets a sequential number of records obeying PHP's max_execution_time setting.
  * @param $recnums 
  *   Array of item or bib numbers to fetch.
