View source
<?php
function biblio_pm_form_biblio_node_form_alter(&$form, &$form_state) {
if (phpversion() > 5 && !isset($form_state['storage']) && !isset($form['#node']->nid)) {
$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' => '',
'#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']);
}
$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'])) {
module_load_include('php', 'biblio', 'pubmed/EntrezClient');
module_load_include('php', 'biblio', 'pubmed/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();
}
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'),
);
}
function biblio_pm_biblio_import($file, $terms = array(), $batch = FALSE, $session_id = NULL, $save = TRUE, $string = FALSE) {
$nids = array();
$dups = array();
$pmids = file($file->filepath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (empty($pmids)) {
drupal_set_message("Could not open PubMed ID file", 'error');
return;
}
return _biblio_pm_import_pmids($pmids, $terms, $batch, $session_id);
}
function _biblio_pm_import_pmids($pmids, $terms, $batch, $session_id) {
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 (($result = $Eclient
->fetchRecords($start)) && $start < $resmax) {
$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_biblio_xml_import($file, $terms, $batch, $session_id) {
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_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)
->getBiblio();
if (isset($node)) {
if (!empty($terms)) {
if (!isset($node['taxonomy'])) {
$node['taxonomy'] = array();
}
$node['taxonomy'] = array_merge($terms, $node['taxonomy']);
}
if (!($dup = biblio_pm_check_md5($node['biblio_pubmed_md5']))) {
$nid = biblio_save_node($node, $batch, $session_id);
if (isset($nid)) {
$nids[] = $nid;
}
}
else {
$dups[] = $dup;
}
$node = null;
}
$start++;
}
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) {
if ($type == 'node' && $node->type == 'biblio' && !empty($node->biblio_pubmed_id)) {
$link = 'http://www.ncbi.nlm.nih.gov/pubmed/' . $node->biblio_pubmed_id . '?dopt=Abstract';
return array(
'biblio_pubmed' => array(
'title' => t('PubMed'),
'href' => $link,
'attributes' => array(
'title' => t("Click to view the PubMed listing for this node"),
),
),
);
}
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)) {
$ret = $callback($node, $a3, $a4);
}
return $ret;
}
function _biblio_pm_delete($node) {
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)) {
drupal_write_record('biblio_pubmed', $node);
}
}
function _biblio_pm_update($node) {
if (isset($node->biblio_pubmed_id) && !empty($node->biblio_pubmed_id)) {
drupal_write_record('biblio_pubmed', $node, 'nid');
}
}
function _biblio_pm_load($node) {
static $result = array();
if (empty($result[$node->nid])) {
return $result[$node->nid] = db_fetch_array(db_query('SELECT biblio_pubmed_id FROM {biblio_pubmed} WHERE nid = %d', $node->nid));
}
return $result[$node->nid];
}
function biblio_pm_check_md5($md5) {
static $pm_md5s = 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;
}
}
if (isset($pm_md5s[$md5])) {
return $pm_md5s[$md5];
}
else {
$pm_md5s[$md5] = TRUE;
return;
}
}