Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Imports a bibliographic record into a node from a given bib_recnum, updating existing nodes if stale,
- * and links the nodes, bib records and item record together.
- * Requires the bibliographic record number to be known.
- * Optionally, if the full marc record is passed, it is used directly instead of being fetched
- * from the WebOPAC.
- *
- * @param array $data Millennium record information
- * @param boolean $force_update
- * Optional. If true, forces the node to be updated when it already exists.
- */
- function millennium_process_bib_record($data, $force_update = false) {
- static $xml_disabled = false;
- if ($data['base_url']) {
- $base_url = millennium_get_real_baseurl($data['base_url']);
- } else {
- return array("success" => false, "error" => "Could not get bib_recnum for bib #" . $data['bib_recnum']);
- }
- $bib_import_history = db_fetch_object(db_query("SELECT * FROM {millennium_node_bib} WHERE bib_recnum='%s' AND base_url = '%s'", $data['bib_recnum'], $base_url));
- // Has this bib record been imported before?
- if ($bib_import_history->nid) {
- // There is an existing node for this bib record
- // Check if there is need to update the node.
- $stale_record_age = (3600*24*30); // 30 days
- $is_stale = (time() > (strtotime($bib_import_history->updated) + $stale_record_age));
- if ($force_update != true && !$is_stale) {
- $node = node_load($bib_import_history->nid);
- // Re-publish node in case it had been unpublished.
- db_query('UPDATE {node} SET status = 1 WHERE nid = %d', $bib_import_history->nid);
- return array("success" => true, 'status' => 'bib_untouched', "node" => $node);
- }
- }
- // We are here because an update is needed or was not previously imported.
- // Further down decide if the node is updated or created; the previous steps are the same:
- // - if no marc given, get marc is possible, if not handle deleted bib record
- // - convert to node
- // - Insert or update node
- if (!$data['marc']) {
- // No marc given, try to fetch.
- $data['marc'] = millennium_fetch_marc($data['bib_recnum'], $base_url);
- if (!$data['marc']) {
- // Bib has been deleted!
- millennium_handle_deleted_bib($data);
- return array("success" => false, 'status' => 'bib_deleted', "error" => "Could not fetch MARC record for " . $data['bib_recnum']);
- }
- }
- // Re-create a nodeobject from the MARC record
- $new_node = millennium_record_to_nodeobject($data['bib_recnum'], $base_url, $data['marc']);
- if ($new_node->success === false) {
- #drupal_set_message("millennium_process_bib_record(): millennium_record_to_nodeobject() returned error ". $result["error"]);
- return array("success" => false, "error" => $new_node->error);
- }
- // Give other modules the chance to cancel processing this data
- // Function signature: millennium_continue_process_record($node, $data, $force_update)
- // where:
- // $node is the node object generated from a Millennium record
- // $data is the incoming Millennium record array
- // $force_update is the current active setting to force updating existing
- // records.
- $proceed = module_invoke_all('millennium_continue_process_record', $new_node, $data, $force_update);
- if (is_array($proceed) && in_array(false, $proceed)) {
- return array('success' => true, 'status' => 'bib_untouched');
- }
- // Now, decide if we are going to insert a new node, or update the existing node.
- if ($bib_import_history->nid) {
- // insert existing node's nid into new_node and save node
- $new_node->nid = $bib_import_history->nid;
- // Clear node cache
- $oldnode = node_load($new_node->nid, NULL, true);
- // Get vid from existing node.
- $new_node->vid = $oldnode->vid;
- // Fill in missing node info, and allow other modules to make changes
- node_submit($new_node);
- // Store node in MySQL
- node_save($new_node);
- // Update the node-bib relationship
- $record = array('nid' => $bib_import_history->nid, 'bib_recnum' => $data['bib_recnum'],
- 'biblio_data' => serialize($new_node->millennium_biblio_data),
- 'updated' => date("Y-m-d H:i:s"), 'base_url' => $base_url);
- #drupal_set_message("Will store this record:"); // DEBUG
- #dpm($record); // DEBUG
- // Update millennium_node_bib using key 'nid'
- $result = drupal_write_record('millennium_node_bib', $record, array('nid'));
- if (!$result) {
- drupal_set_message("drupal_write_record('millennium_node_bib') returned false", "error");
- #dpm($record);
- }
- return array("success" => true, 'status' => 'bib_updated', "node" => $new_node);
- }
- else {
- // Not imported before, insert a new node
- $new_node->is_new = 1;
- // Add authored by information, if given in $data array.
- if (isset($data['user'])) {
- $user = $data['user'];
- }
- else {
- global $user;
- }
- $new_node->uid = $user->uid;
- $new_node->name = $user->name;
- $new_node = node_submit($new_node);
- node_save($new_node);
- // Add node-bib relationship
- $record = array('nid' => $new_node->nid, 'bib_recnum' => $data['bib_recnum'],
- 'biblio_data' => serialize($new_node->millennium_biblio_data),
- 'updated' => date("Y-m-d H:i:s"), 'created' => date("Y-m-d H:i:s"), 'base_url' => $base_url);
- $result = drupal_write_record('millennium_node_bib', $record);
- if (!$result) {
- drupal_set_message("millennium_process_bib_record drupal_write_record('millennium_node_bib') returned false");
- #dpm($record);
- }
- return array("success" => true, 'status' => 'bib_created', "node" => $new_node);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement