You are here

biblio_pm.module in Bibliography Module 6.2

Same filename and directory in other branches
  1. 7 modules/pubmed/biblio_pm.module
  2. 7.2 modules/pubmed/biblio_pm.module

File

modules/pubmed/biblio_pm.module
View source
<?php

/*
 * @file pubmed.module
 *
 */
function biblio_pm_cron() {
  if (variable_get('biblio_pm_auto_update', 0)) {
    $interval = variable_get('biblio_pm_update_interval', 3600);

    // defaults to once per hour
    $count_limit = variable_get('biblio_pm_update_limit', 100);

    // only update 100 at a time
    $age = variable_get('biblio_pm_age_limit', 2419200);

    // defaults to one month since last update
    $age_limit = time() - $age;
    if (time() >= variable_get('biblio_pm_update_next_execution', 0)) {
      $ids = array();
      $sql = 'SELECT nid, biblio_pubmed_id FROM {biblio_pubmed} WHERE biblio_pm_changed < %d ORDER BY nid ASC LIMIT %d';
      $result = db_query($sql, $age_limit, $count_limit);
      while ($pm = db_fetch_object($result)) {
        $ids[$pm->nid] = $pm->biblio_pubmed_id;
      }
      if (count($ids)) {
        list($nids, $dups) = biblio_pm_import_ids($ids);
        if (count($nids)) {
          foreach ($nids as $nid) {
            $message = '';
            $message = t('!nid was updated due to changes originating at !url', array(
              '!nid' => l($nid, 'node/' . $nid),
              '!url' => l('PubMed', 'http://www.ncbi.nlm.nih.gov/pubmed/' . $ids[$nid]),
            ));
            watchdog('biblio_pm', $message, array(), WATCHDOG_WARNING);
          }
        }
        if (count($dups)) {
          $count = count($dups);
          $message = format_plural($count, 'One duplicate PubMed entry was checked, but no changes were found.', '@count PubMed entries were checked, but no changes were found.');
          watchdog('biblio_pm', $message, array(
            '@count' => $count,
          ), WATCHDOG_INFO);
          $now = time();
          $sql = 'UPDATE {biblio_pubmed} SET biblio_pm_changed=%d WHERE nid IN (' . implode(',', $dups) . ')';
          db_query($sql, $now);
        }
      }
      $message = t('There were no PubMed entries older than @age to check.', array(
        '@age' => format_interval($age),
      ));
      watchdog('biblio_pm', $message, array(), WATCHDOG_INFO);
      variable_set('biblio_pm_update_next_execution', time() + $interval);
    }
  }
}
function biblio_pm_form_biblio_admin_settings_alter(&$form, &$form_state) {
  module_load_include('inc', 'biblio_pm', 'biblio_pm.admin');
  $form += biblio_pm_settings_form();
}
function biblio_pm_form_biblio_node_form_alter(&$form, &$form_state) {
  if (phpversion() > 5 && !$form_state['submitted'] && !isset($form['#node']->nid) && $form['biblio_type']['#default_value'] < 1) {
    $form['biblio_pubmed_lookup'] = array(
      '#type' => 'fieldset',
      '#title' => t('PubMed Lookup'),
      '#weight' => -20,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['biblio_pubmed_lookup']['PMID'] = array(
      '#type' => 'textfield',
      '#title' => t('PubMed ID'),
      '#required' => FALSE,
      '#default_value' => $form_state['values']['PMID'],
      '#description' => t('Enter a PubMed ID</b>'),
      '#size' => 60,
      '#maxlength' => 255,
      '#weight' => -4,
    );
    $form['biblio_pubmed_lookup']['pubmed_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Populate using PubMed'),
    );
    $form['#validate'] = array_merge(array(
      'biblio_pm_form_biblio_node_form_validate',
    ), $form['#validate']);

    // put my validator first
  }
  $form['biblio_pubmed_id'] = array(
    '#type' => 'value',
    '#value' => $form_state['values']['biblio_pubmed_id'],
  );
  $form['biblio_pubmed_md5'] = array(
    '#type' => 'value',
    '#value' => $form_state['values']['biblio_pubmed_md5'],
  );
}
function biblio_pm_form_biblio_node_form_validate($form, &$form_state) {
  $node_data = array();
  if (strlen($pmid = $form_state['values']['PMID'])) {
    if (!($dup = biblio_pm_check_pmid($pmid))) {
      module_load_include('php', 'biblio_pm', 'EntrezClient');
      module_load_include('php', 'biblio_pm', 'EntrezPubmedArticle');
      $Eclient = new BiblioEntrezClient();
      try {
        $result = $Eclient
          ->fetch($pmid);
      } catch (Exception $e) {
        form_set_error($e
          ->getMessage());
      }
      if (!isset($result->PubmedArticle)) {
        unset($form_state['values']['biblio_type']);
        unset($form_state['post']['biblio_type']);
        form_set_error('PMID', 'No data available for PubMed ID: ' . check_plain($pmid));
        return;
      }
      $data = new BiblioEntrezPubmedArticle($result->PubmedArticle);
      $node_data = $data
        ->getBiblio();
    }
    else {
      $message = t('The PubMed ID that you are trying to import already exists in the database, see ');
      $message .= l('node/' . $dup, 'node/' . $dup);
      form_set_error('PMID', $message);
      $form_state['rebuild'] = TRUE;
      $form_state['submitted'] = FALSE;
      unset($form_state['values']['biblio_type']);
    }
  }
  if (!empty($node_data)) {
    $form_state['values'] = array_merge($form_state['values'], $node_data);
    $form_state['storage']['biblio_type'] = $node_data['biblio_type'];
    return;
  }
}
function biblio_pm_biblio_import_options() {
  return array(
    'biblio_pm' => t('PubMed ID List'),
    'biblio_pm_xml' => t('PubMed XML'),
  );
}
function biblio_pm_biblio_import($file, $terms = array(), $batch = FALSE, $session_id = NULL) {
  $nids = array();
  $dups = array();
  $pmids = file($file->filepath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  if (empty($pmids)) {
    drupal_set_message(t("Could not open PubMed ID file"), 'error');
    return;
  }
  return biblio_pm_import_ids($pmids, $terms, $batch, $session_id);
}
function biblio_pm_xml_biblio_import($file, $terms = array(), $batch = FALSE, $session_id = NULL) {
  libxml_use_internal_errors(true);
  $xml = @simplexml_load_file($file->filepath);
  if (empty($xml) || isset($xml->body->pre->ERROR)) {
    drupal_set_message("Could not parse file as PubMed XML", 'error');
    return;
  }
  return _biblio_pm_create_node_from_xml($xml, $terms, $batch, $session_id);
}
function biblio_pm_import_ids($pmids, $terms = array(), $batch = FALSE, $session_id = NULL) {
  module_load_include('php', 'biblio_pm', 'EntrezClient');
  module_load_include('php', 'biblio_pm', 'EntrezPubmedArticle');
  $retmax = 100;
  $resmax = count($pmids);
  $start = 0;
  $Eclient = new BiblioEntrezClient();
  $Eclient
    ->post($pmids);
  $Eclient
    ->setReturnMax($retmax);
  $nids = array();
  $dups = array();
  while ($start < $resmax && ($result = $Eclient
    ->fetchRecords($start))) {
    $start += count($result
      ->xpath('//PubmedArticle'));
    list($nid, $dup) = _biblio_pm_create_node_from_xml($result, $terms, $batch, $session_id);
    $nids = array_merge($nids, $nid);
    $dups = array_merge($dups, $dup);
  }
  return array(
    $nids,
    $dups,
  );
}
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');
      if (!empty($terms)) {
        if (!isset($node->taxonomy)) {
          $node->taxonomy = array();
        }
        $node->taxonomy = array_merge($terms, $node->taxonomy);
      }
      if ($dup < 0 && $action == 'newrev') {

        //entry has be imported before, but may have changed

        // 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 on !date due to changes at source", array(
          '!date' => $curr_date,
        ));
        $dup = NULL;
      }
      if ($dup < 0 && $action == 'replace') {

        //entry has be imported before, but may have changed
        $node->nid = -$dup;
        $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, $batch, $session_id);
        if (!empty($node->nid)) {
          $nids[] = $node->nid;
        }
      }
      else {
        $dups[] = $dup;
      }
      $node = null;
    }
  }
  return array(
    $nids,
    $dups,
  );
}
function biblio_pm_check_pmid($pmid) {
  return db_result(db_query("SELECT nid FROM {biblio_pubmed} WHERE biblio_pubmed_id = %d", $pmid));
}
function biblio_pm_biblio_lookup_link_settings() {
  return array(
    'pubmed' => t('PubMed'),
  );
}
function biblio_pm_biblio_lookup_link($node) {
  return biblio_pm_link('node', $node);
}
function biblio_pm_link($type, $node = NULL, $teaser = FALSE) {
  $show_link = variable_get('biblio_lookup_links', array(
    'pubmed' => TRUE,
  ));
  if (!isset($show_link['pubmed']) || !$show_link['pubmed'] || !isset($node) || $node->type != 'biblio' || !isset($node->biblio_pubmed_id)) {
    return array();
  }
  if (isset($node) && $type == 'node' && $node->type == 'biblio' && !empty($node->biblio_pubmed_id)) {
    $link = 'http://www.ncbi.nlm.nih.gov/pubmed/' . $node->biblio_pubmed_id . '?dopt=Abstract';
    $attrs = array(
      'title' => t("Click to view the PubMed listing for this node"),
    );
    if (variable_get('biblio_links_target_new_window', null)) {
      $attrs = array_merge($attrs, array(
        'target' => '_blank',
      ));
    }
    return array(
      'biblio_pubmed' => array(
        'title' => t('PubMed'),
        'href' => $link,
        'attributes' => $attrs,
      ),
    );
  }
  return;
}
function biblio_pm_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($node->type != 'biblio') {
    return;
  }
  $callback = '_biblio_pm_' . str_replace(' ', '_', $op);
  if (function_exists($callback)) {
    return $callback($node, $a3, $a4);
  }
  return;
}
function _biblio_pm_delete($node) {
  if ($node->type == 'biblio') {
    db_query('DELETE FROM {biblio_pubmed} WHERE nid = %d', $node->nid);
  }
}
function _biblio_pm_view($node) {
}
function _biblio_pm_insert($node) {
  if (isset($node->biblio_pubmed_id) && !empty($node->biblio_pubmed_id)) {
    $node->biblio_pm_changed = time();
    drupal_write_record('biblio_pubmed', $node);
  }
}
function _biblio_pm_update($node) {
  if (isset($node->biblio_pubmed_id) && !empty($node->biblio_pubmed_id)) {
    $node->biblio_pm_changed = time();
    drupal_write_record('biblio_pubmed', $node, 'nid');
  }
}
function _biblio_pm_load($node) {
  if ($node->type == 'biblio') {
    return db_fetch_array(db_query('SELECT  biblio_pubmed_id, biblio_pubmed_md5  FROM {biblio_pubmed} WHERE nid = %d', $node->nid));
  }
}
function biblio_pm_check_md5($pmid, $md5) {
  static $pm_md5s = array();
  static $pm_nids = array();
  if (empty($pm_md5s)) {
    $result = db_query("SELECT * FROM {biblio_pubmed} ");
    while ($row = db_fetch_object($result)) {
      $pm_md5s[$row->biblio_pubmed_md5] = $row->nid;
      $pm_nids[$row->biblio_pubmed_id] = $row->nid;
    }
  }
  if (isset($pm_nids[$pmid]) && isset($pm_md5s[$md5])) {

    // must be an exact duplicate of an existing node (return the nid)
    return $pm_md5s[$md5];
  }
  elseif (isset($pm_nids[$pmid]) && !isset($pm_md5s[$md5])) {

    //pmid has been save previously but content must have changed (return negative nid)
    return -$pm_nids[$pmid];
  }
  else {
    $pm_md5s[$md5] = TRUE;

    // gaurd against duplicates in the same import
    $pm_nids[$pmid] = TRUE;
    return;
  }
}
function biblio_pm_get_biblio_field_info() {
  return array(
    'biblio_pm' => array(
      'name' => 'biblio_pubmed_id',
      'type' => 'textfield',
      'title' => 'PubMed ID',
    ),
  );
}