You are here

function _biblio_pm_create_node_from_xml in Bibliography Module 7

Same name and namespace in other branches
  1. 6.2 modules/pubmed/biblio_pm.module \_biblio_pm_create_node_from_xml()
  2. 6 pubmed/biblio_pm.module \_biblio_pm_create_node_from_xml()
  3. 7.2 modules/pubmed/biblio_pm.module \_biblio_pm_create_node_from_xml()
2 calls to _biblio_pm_create_node_from_xml()
biblio_pm_import_ids in modules/pubmed/biblio_pm.module
Imports multiple PMIDs.
biblio_pm_xml_biblio_import in modules/pubmed/biblio_pm.module
Imports article(s) from an XML file (exported) from PubMed Central.

File

modules/pubmed/biblio_pm.module, line 321

Code

function _biblio_pm_create_node_from_xml($xml, $terms, $batch, $session_id) {
  module_load_include('php', 'biblio_pm', 'EntrezPubmedArticle');
  $nids = array();
  $dups = array();
  $node = new stdClass();
  $data = new BiblioEntrezPubmedArticle();
  foreach ($xml
    ->xpath('//PubmedArticle') as $article) {
    $node = $data
      ->setArticle($article)
      ->getBiblioAsObject();
    if (isset($node)) {
      $dup = biblio_pm_check_md5($node->biblio_pubmed_id, $node->biblio_pubmed_md5);
      $action = variable_get('biblio_pm_dup_action', 'newrev');

      // Entry has be imported before, but may have changed.
      if ($dup < 0 && $action == 'newrev') {

        // Load the node in order to preserve all its data and merge the new
        // data from pubmed.
        $node = (object) array_merge((array) node_load(-$dup), (array) $node);
        $node->nid = -$dup;
        $node->revision = 1;
        $curr_date = format_date(time());
        $node->log = t('Automatically updated by the Biblio PubMed module on !date due to changes at !url', array(
          '!date' => $curr_date,
          '!url' => l('PubMed', 'https://www.ncbi.nlm.nih.gov/pubmed/' . $node->biblio_pubmed_id),
        ));
        $dup = NULL;
      }

      // Entry has be imported before, but may have changed.
      if ($dup < 0 && $action == 'replace') {
        $node->nid = -$dup;
        $existing_node = db_query("SELECT * FROM {node} WHERE nid=:nid", array(
          ':nid' => $node->nid,
        ))
          ->fetchObject();
        $node = (object) array_merge((array) $existing_node, (array) $node);
        $dup = NULL;
      }
      if (!$dup) {

        // Allows other modules to alter the node before it is being saved. (Note: $article is a SimpleXML object)
        drupal_alter('biblio_pm_node', $node, $article);
        biblio_save_node($node, $terms, $batch, $session_id);
        if (!empty($node->nid)) {
          $nids[] = $node->nid;
        }
      }
      else {
        $dups[] = $dup;
      }
      $node = NULL;
    }
  }
  return array(
    $nids,
    $dups,
  );
}