Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.45 KB | None | 0 0
  1. /**
  2.  * Imports a bibliographic record into a node from a given bib_recnum, updating existing nodes if stale,
  3.  * and links the nodes, bib records and item record together.
  4.  * Requires the bibliographic record number to be known.
  5.  * Optionally, if the full marc record is passed, it is used directly instead of being fetched
  6.  * from the WebOPAC.
  7.  *
  8.  * @param array $data Millennium record information
  9.  * @param boolean $force_update
  10.  *   Optional. If true, forces the node to be updated when it already exists.
  11.  */
  12. function millennium_process_bib_record($data, $force_update = false) {
  13.   static $xml_disabled = false;
  14.   if ($data['base_url']) {
  15.     $base_url = millennium_get_real_baseurl($data['base_url']);
  16.   } else {
  17.     return array("success" => false, "error" => "Could not get bib_recnum for bib #" . $data['bib_recnum']);
  18.   }
  19.  
  20.   $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));
  21.  
  22.   // Has this bib record been imported before?
  23.   if ($bib_import_history->nid) {
  24.     // There is an existing node for this bib record
  25.     // Check if there is need to update the node.
  26.     $stale_record_age = (3600*24*30); // 30 days
  27.     $is_stale = (time() > (strtotime($bib_import_history->updated) + $stale_record_age));
  28.  
  29.     if ($force_update != true && !$is_stale) {
  30.       $node = node_load($bib_import_history->nid);
  31.       // Re-publish node in case it had been unpublished.
  32.       db_query('UPDATE {node} SET status = 1 WHERE nid = %d', $bib_import_history->nid);
  33.       return array("success" => true, 'status' => 'bib_untouched', "node" => $node);
  34.     }
  35.   }
  36.  
  37.   // We are here because an update is needed or was not previously imported.
  38.   // Further down decide if the node is updated or created; the previous steps are the same:
  39.   // - if no marc given, get marc is possible, if not handle deleted bib record
  40.   // - convert to node
  41.   // - Insert or update node
  42.   if (!$data['marc']) {
  43.     // No marc given, try to fetch.
  44.     $data['marc'] = millennium_fetch_marc($data['bib_recnum'], $base_url);
  45.     if (!$data['marc']) {
  46.       // Bib has been deleted!
  47.       millennium_handle_deleted_bib($data);
  48.       return array("success" => false, 'status' => 'bib_deleted', "error" => "Could not fetch MARC record for " . $data['bib_recnum']);
  49.     }
  50.   }
  51.  
  52.   // Re-create a nodeobject from the MARC record
  53.   $new_node = millennium_record_to_nodeobject($data['bib_recnum'], $base_url, $data['marc']);
  54.   if ($new_node->success === false) {
  55.     #drupal_set_message("millennium_process_bib_record(): millennium_record_to_nodeobject() returned error ". $result["error"]);
  56.    return array("success" => false, "error" => $new_node->error);
  57.   }
  58.  
  59.   // Give other modules the chance to cancel processing this data
  60.   // Function signature: millennium_continue_process_record($node, $data, $force_update)
  61.   // where:
  62.   //   $node is the node object generated from a Millennium record
  63.   //   $data is the incoming Millennium record array
  64.   //   $force_update is the current active setting to force updating existing
  65.   //     records.
  66.   $proceed = module_invoke_all('millennium_continue_process_record', $new_node, $data, $force_update);
  67.   if (is_array($proceed) && in_array(false, $proceed)) {
  68.     return array('success' => true, 'status' => 'bib_untouched');
  69.   }
  70.  
  71.   // Now, decide if we are going to insert a new node, or update the existing node.
  72.   if ($bib_import_history->nid) {
  73.     // insert existing node's nid into new_node and save node
  74.     $new_node->nid = $bib_import_history->nid;
  75.     // Clear node cache
  76.     $oldnode = node_load($new_node->nid, NULL, true);
  77.     // Get vid from existing node.
  78.     $new_node->vid = $oldnode->vid;
  79.     // Fill in missing node info, and allow other modules to make changes
  80.     node_submit($new_node);
  81.     // Store node in MySQL
  82.     node_save($new_node);
  83.  
  84.     // Update the node-bib relationship
  85.     $record = array('nid' => $bib_import_history->nid, 'bib_recnum' => $data['bib_recnum'],
  86.       'biblio_data' => serialize($new_node->millennium_biblio_data),
  87.       'updated' => date("Y-m-d H:i:s"), 'base_url' => $base_url);
  88.     #drupal_set_message("Will store this record:"); // DEBUG
  89.    #dpm($record); // DEBUG
  90.    // Update millennium_node_bib using key 'nid'
  91.     $result = drupal_write_record('millennium_node_bib', $record, array('nid'));
  92.     if (!$result) {
  93.       drupal_set_message("drupal_write_record('millennium_node_bib') returned false", "error");
  94.       #dpm($record);
  95.    }
  96.     return array("success" => true, 'status' => 'bib_updated', "node" => $new_node);
  97.   }
  98.   else {
  99.     // Not imported before, insert a new node
  100.     $new_node->is_new = 1;
  101.     // Add authored by information, if given in $data array.
  102.     if (isset($data['user'])) {
  103.       $user = $data['user'];
  104.     }
  105.     else {
  106.       global $user;
  107.     }
  108.     $new_node->uid = $user->uid;
  109.     $new_node->name = $user->name;
  110.     $new_node = node_submit($new_node);
  111.     node_save($new_node);
  112.  
  113.     // Add node-bib relationship
  114.     $record = array('nid' => $new_node->nid, 'bib_recnum' => $data['bib_recnum'],
  115.       'biblio_data' => serialize($new_node->millennium_biblio_data),
  116.       'updated' => date("Y-m-d H:i:s"), 'created' => date("Y-m-d H:i:s"), 'base_url' => $base_url);
  117.     $result = drupal_write_record('millennium_node_bib', $record);
  118.     if (!$result) {
  119.       drupal_set_message("millennium_process_bib_record drupal_write_record('millennium_node_bib') returned false");
  120.       #dpm($record);
  121.    }
  122.     return array("success" => true, 'status' => 'bib_created', "node" => $new_node);
  123.   }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement