function tripal_pub_PMID_search_init

2.x tripal_pub.PMID.inc tripal_pub_PMID_search_init($search_str, $retmax)
3.x tripal_chado.pub_importer_PMID.inc tripal_pub_PMID_search_init($search_str, $retmax)
1.x PMID.inc tripal_pub_PMID_search_init($search_str, $retmax)
2 calls to tripal_pub_PMID_search_init()
tripal_pub_PMID_count in tripal_pub/includes/importers/PMID.inc
tripal_pub_PMID_range in tripal_pub/includes/importers/PMID.inc

File

tripal_pub/includes/importers/PMID.inc, line 190
Tripal Pub PubMed Interface

Code

function tripal_pub_PMID_search_init($search_str, $retmax) {

  // do a search for a single result so that we can establish a history, and get
  // the number of records. Once we have the number of records we can retrieve
  // those requested in the range.
  $query_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?" .
    "db=Pubmed" .
    "&retmax=$retmax" .
    "&usehistory=y" .
    "&term=" . urlencode($search_str);

  //dpm($query_url);
  $rfh = fopen($query_url, "r");
  if (!$rfh) {
    drupal_set_message('Could not perform Pubmed query. Cannot connect to Entrez.', 'error');
    watchdog('tpub_import', "Could not perform Pubmed query. Cannot connect to Entrez.", 
    array(), WATCHDOG_ERROR);
    return 0;
  }

  // retrieve the XML results
  $query_xml = '';
  while (!feof($rfh)) {
    $query_xml .= fread($rfh, 255);
  }
  fclose($rfh);
  //dpm("<pre>$query_xml</pre>");
  $xml = new XMLReader();
  $xml->xml($query_xml);

  // iterate though the child nodes of the <eSearchResult> tag and get the count, history and query_id
  $result = array();
  while ($xml->read()) {
    $element = $xml->name;

    if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'WebEnv') {
      // we've read as much as we need. If we go too much further our counts
      // will get messed up by other 'Count' elements.  so we're done.
      break;
    }
    if ($xml->nodeType == XMLReader::ELEMENT) {

      switch ($element) {
        case 'Count':
          $xml->read();
          $result['Count'] = $xml->value;
          break;
        case 'WebEnv':
          $xml->read();
          $result['WebEnv'] = $xml->value;
          break;
        case 'QueryKey':
          $xml->read();
          $result['QueryKey'] = $xml->value;
          break;
      }
    }
  }
  return $result;
}