Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Main function that imports, updates or deletes item or bibliographic records.
- * It can optionally take in a MARC record to bypass harvesting from the WebOPAC.
- * @param $data
- * An associative array of data for a record, with the following keys:
- * - 'bib_recnum' (optional)
- * Millennium record number (b123456) for the record to process.
- * - 'item_recnum' (optional)
- * A Millennium record number (i123456) for the record to process.
- * If bib_recnum is also given, it is assumed it is the bib record
- * associated with this item.
- * - 'marc' (optional)
- * MARC text as it is exported from the WebOPAC.
- * @param bool $force_update (Optional) States if the function should always update existing data.
- */
- function millennium_process_record(&$data, $force_update = false) {
- // Set defaults
- if (!isset($data['marc'])) {
- $data['marc'] = null;
- }
- if (!isset($data['item_recnum'])) {
- $data['item_recnum'] = null;
- }
- if (!isset($data['bib_recnum'])) {
- $data['bib_recnum'] = null;
- }
- #drupal_set_message("millennium_process_record():");
- #dpm($data);
- $type = "";
- // If only bib_recnum
- if ($data['bib_recnum'] && !$data['item_recnum']) {
- $type = "bib";
- }
- // If only item_recnum set, or if both record numbers set:
- if (
- (!$data['bib_recnum'] && $data['item_recnum']) ||
- ($data['bib_recnum'] && $data['item_recnum'])) {
- $type = "item";
- }
- switch($type) {
- case "bib":
- $result = millennium_process_bib_record(
- $data,
- $force_update
- );
- break;
- case "item":
- $result = millennium_process_item_record(
- $data,
- $force_update
- );
- break;
- default:
- watchdog("Millennium", "millennium_process_record() unknown record type: @data", array('@data' => var_export($data, TRUE)));
- return array("success" => false, "error" => "Could not determine function to call.");
- }
- if ($result["node"]) {
- $data["node"] = $result["node"];
- }
- #dpm($result);
- // Log errors
- if ($result['success'] !== true) {
- watchdog("Millennium", "millennium_process_record() failed with error @error", array('@error' => $result['error']));
- }
- return array("success" => $result['result'], 'status' => $result['status'], 'error' => $result['error']);
- }
- /**
- * Imports or updates Millennium item records in Drupal
- * @param array $data Millennium record information
- * @param bool $force_update (Optional) States if an existing node for that item record number should be updated.
- */
- function millennium_process_item_record($data, $force_update = true) {
- #drupal_set_message("millennium_process_item_record($item_recnum, $force_update, $marc_text, $bib_recnum)");
- if ($data['base_url']) {
- $base_url = $data['base_url'];
- } else {
- return array("success" => false, "error" => "No base_url in item #" . $data['item_recnum']);
- }
- // Had we already imported this item record?
- $item_import_history = db_fetch_object(db_query(
- "SELECT * FROM {millennium_bib_item} WHERE item_recnum='%s' AND base_url = '%s'",
- $data['item_recnum'],
- $base_url
- ));
- if ($item_import_history->nid) {
- #drupal_set_message("Item $item_recnum was imported before");
- // Item was imported before.
- $status = millennium_process_item_record_prev_imported($data, $item_import_history, $force_update = true);
- $node = node_load($item_import_history->nid);
- return array(
- "success" => true,
- "status" => $status['result'],
- "node" => $node,
- #"error" => t("This item #@item_number had already been imported under nid @nid", array("@item_number" => $item_recnum, "@nid" => $item_import_history->nid))
- );
- } else {
- // Item was not imported before.
- // If not given as the function's argument, get the item's bib record number.
- if ($bib_recnum == null) {
- $result = millennium_fetch_recordpage($data['item_recnum'], $base_url, 'plain');
- $record_html = $result->data;
- $ok = preg_match('/\/record=(b[0-9]+)/si', $record_html, $matches);
- if ($ok) {
- $bib_recnum = $matches[1];
- }
- }
- }
- if (!$bib_recnum) {
- return array("success" => false, "error" => "Could not get bib_recnum for item #" . $data['item_recnum']);
- }
- // We have a bib_number by now.
- // Is there an existing node with this bib_number?
- $bib_import_history = db_fetch_object(db_query(
- "SELECT * FROM {millennium_node_bib} WHERE bib_recnum='%s' AND base_url = '%s'",
- $bib_recnum,
- $base_url
- ));
- if (!$bib_import_history->nid) {
- // New item, new bib.
- // Must the node for the bib before the item can be imported.
- $data = array('bib_recnum' => $bib_recnum, 'marc' => $marc_text);
- $result = millennium_process_bib_record($data, $force_update);
- if ($result["success"] != true) {
- return array("success" => false, "error" => "Could not create a new node for item: " . $data['item_recnum'] . " and bib: " . $data['bib_recnum']);
- } else {
- $nid = $result["node"]->nid;
- }
- } else {
- $nid = $bib_import_history->nid;
- }
- // Do actual import of new item.
- $ok = millennium_process_item_record_new_import($data, $nid);
- if ($ok) {
- return array('success' => true, 'status' => 'item_created');
- } else {
- return array('success' => false, 'error' => "Could not create a new item for item $item_recnum");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement