pubmed.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. <?php
  2. /**
  3. * @file
  4. * Tripal Pub PubMed Interface
  5. *
  6. * @defgroup tripal_pub_PMID PubMed Interface
  7. * @ingroup tripal_pub
  8. */
  9. /**
  10. *
  11. */
  12. function tripal_pub_remote_search_PMID($search_array, $num_to_retrieve, $pager_id) {
  13. // convert the terms list provicded by the caller into a string with words
  14. // separated by a '+' symbol.
  15. $num_criteria = $search_array['num_criteria'];
  16. $days = $search_array['days'];
  17. $search_str = '';
  18. for ($i = 0; $i <= $num_criteria; $i++) {
  19. $search_terms = $search_array['criteria'][$i]['search_terms'];
  20. $scope = $search_array['criteria'][$i]['scope'];
  21. $op = $search_array['criteria'][$i]['operation'];
  22. if ($op) {
  23. $search_str .= "$op";
  24. }
  25. $search_str .= '(';
  26. if($scope == 'title') {
  27. $search_str .= '"' . implode("+", preg_split('/\s+/', trim($search_terms))) . '"[Title]';
  28. }
  29. elseif($scope == 'author') {
  30. $search_str .= '"' . implode("+", preg_split('/\s+/', trim($search_terms))) . '"[Author]';
  31. }
  32. elseif($scope == 'abstract') {
  33. $search_str .= '"' . implode("+", preg_split('/\s+/', trim($search_terms))) . '"[Title/Abstract]';
  34. }
  35. elseif($scope == 'id') {
  36. $search_str .= '' . implode("+", preg_split('/\s+/', trim($search_terms))) . '[Uid]';
  37. }
  38. $search_str .= ')';
  39. }
  40. $search_array['limit'] = $num_to_retrieve;
  41. $search_array['search_string'] = $search_str;
  42. // we want to get the list of pubs using the search terms but using a Drupal style pager
  43. $pubs = tripal_pager_callback('tripal_pub_PMID_range', $num_to_retrieve, $pager_id,
  44. 'tripal_pub_PMID_count', $search_array);
  45. return $pubs;
  46. }
  47. /*
  48. * This function is used as the callback function when used with the
  49. * tripal_pager_callback function. This function returns a count of
  50. * the dataset to be paged.
  51. */
  52. function tripal_pub_PMID_count($search_array) {
  53. $terms = $search_array['search_string'];
  54. $days = $search_array['days'];
  55. $limit = $search_array['limit'];
  56. $results = tripal_pub_PMID_search_init($terms, $limit, $days);
  57. $_SESSION['tripal_pub_PMID_query'][$terms]['Count'] = $results['Count'];
  58. $_SESSION['tripal_pub_PMID_query'][$terms]['WebEnv'] = $results['WebEnv'];
  59. $_SESSION['tripal_pub_PMID_query'][$terms]['QueryKey'] = $results['QueryKey'];
  60. return $results['Count'];
  61. }
  62. /*
  63. * This function is used as the callback function when used with the
  64. * tripal_pager_callback function. This function returns the results
  65. * within the specified range
  66. */
  67. function tripal_pub_PMID_range($search_array, $start = 0, $limit = 10) {
  68. $terms = $search_array['search_string'];
  69. $days = $search_array['days'];
  70. $limit = $search_array['limit'];
  71. $count = $_SESSION['tripal_pub_PMID_query'][$terms]['Count'];
  72. if ($count == 0) {
  73. return array();
  74. }
  75. // get the query_key and the web_env from the previous count query.
  76. $query_key = $_SESSION['tripal_pub_PMID_query'][$terms]['QueryKey'];
  77. $web_env = $_SESSION['tripal_pub_PMID_query'][$terms]['WebEnv'];
  78. // if this function has been called without calling the count function
  79. // then we need to do the query.
  80. if (!$query_key) {
  81. $results = tripal_pub_PMID_search_init($terms, $limit, $days);
  82. $_SESSION['tripal_pub_PMID_query']['WebEnv'] = $results['WebEnv'];
  83. $_SESSION['tripal_pub_PMID_query']['QueryKey'] = $results['QueryKey'];
  84. $query_key = $results['QueryKey'];
  85. $web_env = $results['WebEnv'];
  86. }
  87. // now get the list of PMIDs from the previous search
  88. $pmids_txt = tripal_pub_PMID_fetch($query_key, $web_env, 'uilist', 'text', $start, $limit);
  89. // iterate through each PMID and get the publication record. This requires a new search and new fetch
  90. $pmids = explode("\n", trim($pmids_txt));
  91. $pubs = array();
  92. foreach ($pmids as $pmid) {
  93. // now retrieve the individual record
  94. $pub_xml = tripal_pub_PMID_fetch($query_key, $web_env, 'null', 'xml', 0, 1, array('id' => $pmid));
  95. $pub = tripal_pub_PMID_parse_pubxml($pub_xml);
  96. $pubs[] = $pub;
  97. }
  98. return $pubs;
  99. }
  100. /*
  101. *
  102. */
  103. function tripal_pub_PMID_search_init($terms, $retmax, $days = 0){
  104. // do a search for a single result so that we can establish a history, and get
  105. // the number of records. Once we have the number of records we can retrieve
  106. // those requested in the range.
  107. $query_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?" .
  108. "db=Pubmed" .
  109. "&retmax=$retmax" .
  110. "&usehistory=y".
  111. "&term=$terms";
  112. if($days) {
  113. $query_url .= "&reldate=$days&datetype=edat";
  114. }
  115. //dpm($query_url);
  116. $rfh = fopen($query_url, "r");
  117. if (!$rfh) {
  118. drupal_set_message('Could not perform Pubmed query. Cannot connect to Entrez.', 'error');
  119. return 0;
  120. }
  121. // retrieve the XML results
  122. $query_xml = '';
  123. while (!feof($rfh)) {
  124. $query_xml .= fread($rfh, 255);
  125. }
  126. fclose($rfh);
  127. //dpm("<pre>$query_xml</pre>");
  128. $xml = new XMLReader();
  129. $xml->xml($query_xml);
  130. // iterate though the child nodes of the <eSearchResult> tag and get the count, history and query_id
  131. $result = array();
  132. while ($xml->read()) {
  133. $element = $xml->name;
  134. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'WebEnv') {
  135. // we've read as much as we need. If we go too much further our counts
  136. // will get messed up by other 'Count' elements. so we're done.
  137. break;
  138. }
  139. if ($xml->nodeType == XMLReader::ELEMENT) {
  140. switch ($element) {
  141. case 'Count':
  142. $xml->read();
  143. $result['Count'] = $xml->value;
  144. break;
  145. case 'WebEnv':
  146. $xml->read();
  147. $result['WebEnv'] = $xml->value;
  148. break;
  149. case 'QueryKey':
  150. $xml->read();
  151. $result['QueryKey'] = $xml->value;
  152. break;
  153. }
  154. }
  155. }
  156. return $result;
  157. }
  158. /*
  159. *
  160. */
  161. function tripal_pub_PMID_fetch($query_key, $web_env, $rettype = 'null',
  162. $retmod = 'null', $start = 0, $limit = 10, $args = array()){
  163. // repeat the search performed previously (using WebEnv & QueryKey) to retrieve
  164. // the PMID's within the range specied. The PMIDs will be returned as a text list
  165. $fetch_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?".
  166. "rettype=$rettype" .
  167. "&retmode=$retmod" .
  168. "&retstart=$start" .
  169. "&retmax=$limit" .
  170. "&db=Pubmed" .
  171. "&query_key=$query_key".
  172. "&WebEnv=$web_env";
  173. foreach ($args as $key => $value) {
  174. if(is_array($value)) {
  175. $fetch_url .= "&$key=";
  176. foreach ($value as $item) {
  177. $fetch_url .= "$item,";
  178. }
  179. $fetch_url = substr($fetch_url, 0, -1); // remove trailing comma
  180. }
  181. else {
  182. $fetch_url .= "&$key=$value";
  183. }
  184. }
  185. //dpm($fetch_url);
  186. $rfh = fopen($fetch_url, "r");
  187. if (!$rfh) {
  188. drupal_set_message('ERROR: Could not perform PubMed query.', 'error');
  189. return '';
  190. }
  191. $results = '';
  192. if($rfh) {
  193. while (!feof($rfh)) {
  194. $results .= fread($rfh, 255);
  195. }
  196. fclose($rfh);
  197. }
  198. return $results;
  199. }
  200. /*
  201. * This function parses the XML containing details of a publication and
  202. * converts it into an associative array of where keys are Tripal Pub
  203. * ontology terms and the values are extracted from the XML. The
  204. * XML should contain only a single publication record.
  205. *
  206. * Information about the valid elements in the PubMed XML can be found here:
  207. * http://www.nlm.nih.gov/bsd/licensee/elements_descriptions.html
  208. *
  209. * Information about PubMed's citation format can be found here
  210. * http://www.nlm.nih.gov/bsd/policy/cit_format.html
  211. */
  212. function tripal_pub_PMID_parse_pubxml($pub_xml) {
  213. $pub = array();
  214. if (!$pub_xml) {
  215. return $pub;
  216. }
  217. // read the XML and iterate through it.
  218. $xml = new XMLReader();
  219. $xml->xml($pub_xml);
  220. while ($xml->read()) {
  221. $element = $xml->name;
  222. if ($xml->nodeType == XMLReader::ELEMENT) {
  223. switch ($element) {
  224. case 'PMID':
  225. // thre are multiple places where a PMID is present in the XML and
  226. // since this code does not descend into every branch of the XML tree
  227. // we will encounter many of them here. Therefore, we only want the
  228. // PMID that we first encounter. If we already have the PMID we will
  229. // just skip it. Examples of other PMIDs are in the articles that
  230. // cite this one.
  231. $xml->read(); // get the value for this element
  232. if(!$pub['Publication Dbxref']) {
  233. $pub['Publication Dbxref'] = 'PMID:' . $xml->value;
  234. }
  235. break;
  236. case 'Article':
  237. $pub_model = $xml->getAttribute('PubModel');
  238. $pub['Publication Model'] = $pub_model;
  239. tripal_pub_PMID_parse_article($xml, $pub);
  240. break;
  241. case 'MedlineJournalInfo':
  242. tripal_pub_PMID_parse_medline_journal_info($xml, $pub);
  243. break;
  244. case 'ChemicalList':
  245. // TODO: handle this
  246. break;
  247. case 'SupplMeshList':
  248. // TODO: meant for protocol list
  249. break;
  250. case 'CitationSubset':
  251. // TODO: not sure this is needed.
  252. break;
  253. case 'CommentsCorrections':
  254. // TODO: handle this
  255. break;
  256. case 'GeneSymbolList':
  257. // TODO: handle this
  258. break;
  259. case 'MeshHeadingList':
  260. // TODO: Medical subject headings
  261. break;
  262. case 'NumberOfReferences':
  263. // TODO: not sure we should keep this as it changes frequently.
  264. break;
  265. case 'PersonalNameSubjectList':
  266. // TODO: for works about an individual or with biographical note/obituary.
  267. break;
  268. case 'OtherID':
  269. // TODO: ID's from another NLM partner.
  270. break;
  271. case 'OtherAbstract':
  272. // TODO: when the journal does not contain an abstract for the publication.
  273. break;
  274. case 'KeywordList':
  275. // TODO: handle this
  276. break;
  277. case 'InvestigatorList':
  278. // TODO: personal names of individuals who are not authors (can be used with collection)
  279. break;
  280. case 'GeneralNote':
  281. // TODO: handle this
  282. break;
  283. case 'DeleteCitation':
  284. // TODO: need to know how to handle this
  285. break;
  286. default:
  287. break;
  288. }
  289. }
  290. }
  291. $pub['Citation'] = $pub['Authors'] . '. ' . $pub['Title'] . '. ' .
  292. $pub['Journal ISO Abbreviation'] . '. ' . $pub['Publication Date'];
  293. if ($pub['Volume'] or $pub['Issue']) {
  294. $pub['Citation'] .= '; ';
  295. }
  296. if ($pub['Volume']) {
  297. $pub['Citation'] .= $pub['Volume'];
  298. }
  299. if ($pub['Issue']) {
  300. $pub['Citation'] .= '(' . $pub['Issue'] . ')';
  301. }
  302. if ($pub['Pages']) {
  303. $pub['Citation'] .= ':' . $pub['Pages'];
  304. }
  305. $pub['raw'] = $pub_xml;
  306. return $pub;
  307. }
  308. /*
  309. *
  310. */
  311. function tripal_pub_PMID_parse_medline_journal_info($xml, &$pub) {
  312. while ($xml->read()) {
  313. // get this element name
  314. $element = $xml->name;
  315. // if we're at the </Article> element then we're done with the article...
  316. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'MedlineJournalInfo') {
  317. return;
  318. }
  319. if ($xml->nodeType == XMLReader::ELEMENT) {
  320. switch ($element) {
  321. case 'Country':
  322. // the place of publication of the journal
  323. $xml->read();
  324. $pub['Journal Country'] = $xml->value;
  325. break;
  326. case 'MedlineTA':
  327. // TODO: not sure how this is different from ISOAbbreviation
  328. break;
  329. case 'NlmUniqueID':
  330. // TODO: the journal's unique ID in medline
  331. break;
  332. case 'ISSNLinking':
  333. // TODO: not sure how this is different from ISSN
  334. break;
  335. default:
  336. break;
  337. }
  338. }
  339. }
  340. }
  341. /*
  342. *
  343. */
  344. function tripal_pub_PMID_parse_article($xml, &$pub) {
  345. while ($xml->read()) {
  346. // get this element name
  347. $element = $xml->name;
  348. // if we're at the </Article> element then we're done with the article...
  349. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'Article') {
  350. return;
  351. }
  352. if ($xml->nodeType == XMLReader::ELEMENT) {
  353. switch ($element) {
  354. case 'Journal':
  355. tripal_pub_PMID_parse_journal($xml, $pub);
  356. break;
  357. case 'ArticleTitle':
  358. $xml->read();
  359. // remoave any trailing period from the title
  360. $pub['Title'] = preg_replace('/\.$/', '', $xml->value);
  361. break;
  362. case 'Abstract':
  363. tripal_pub_PMID_parse_abstract($xml, $pub);
  364. break;
  365. case 'Pagination':
  366. tripal_pub_PMID_parse_pagination($xml, $pub);
  367. break;
  368. case 'ELocationID':
  369. $type = $xml->getAttribute('EIdType');
  370. $valid = $xml->getAttribute('ValidYN');
  371. $xml->read();
  372. $elocation = $xml->value;
  373. if ($type == 'doi' and $valid == 'Y') {
  374. $pub['DOI'] = $elocation;
  375. }
  376. if ($type == 'pii' and $valid == 'Y') {
  377. $pub['PII'] = $elocation;
  378. }
  379. $pub['Elocation'] = $elocation;
  380. break;
  381. case 'Affiliation':
  382. // the affiliation tag at this level is meant solely for the first author
  383. $xml->read();
  384. $pub['Author List'][0]['Affiliation'] = $xml->value;
  385. break;
  386. case 'AuthorList':
  387. $complete = $xml->getAttribute('CompleteYN');
  388. tripal_pub_PMID_parse_authorlist($xml, $pub);
  389. break;
  390. case 'InvestigatorList':
  391. // TODO: perhaps handle this one day. The investigator list is to list the names of people who
  392. // are members of a collective or corporate group that is an author in the paper.
  393. break;
  394. case 'Language':
  395. $xml->read();
  396. $lang_abbr = $xml->value;
  397. // there may be multiple languages so we store these in an array
  398. $pub['Language'][] = tripal_pub_remote_search_get_language($lang_abbr);
  399. $pub['Language Abbr'][] = $lang_abbr;
  400. break;
  401. case 'DataBankList':
  402. // TODO: handle this case
  403. break;
  404. case 'GrantList':
  405. // TODO: handle this case
  406. break;
  407. case 'PublicationTypeList':
  408. tripal_pub_PMID_parse_publication_type($xml, $pub);
  409. break;
  410. case 'VernacularTitle':
  411. $xml->read();
  412. $pub['Vernacular Title'][] = $xml->value;
  413. break;
  414. case 'ArticleDate':
  415. // TODO: figure out what to do with this element. We already have the
  416. // published date in the <PubDate> field, but this date should be in numeric
  417. // form and may have more information.
  418. break;
  419. default:
  420. break;
  421. }
  422. }
  423. }
  424. }
  425. /*
  426. * A full list of publication types can be found here:
  427. * http://www.nlm.nih.gov/mesh/pubtypes.html.
  428. *
  429. * The Tripal Pub ontology doesn't yet have terms for all of the
  430. * publication types so we store the value in the 'publication_type' term.
  431. */
  432. function tripal_pub_PMID_parse_publication_type($xml, &$pub) {
  433. while ($xml->read()) {
  434. $element = $xml->name;
  435. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'PublicationTypeList') {
  436. // we've reached the </PublicationTypeList> element so we're done.
  437. return;
  438. }
  439. if ($xml->nodeType == XMLReader::ELEMENT) {
  440. switch ($element) {
  441. case 'PublicationType':
  442. $xml->read();
  443. $pub_cvterm = tripal_cv_get_cvterm_by_name($xml->value, NULL, 'tripal_pub');
  444. if (!$pub_cvterm) {
  445. // see if this we can find the name using a synonym
  446. $pub_cvterm = tripal_cv_get_cvterm_by_synonym($xml->value, NULL, 'tripal_pub');
  447. if (!$pub_cvterm) {
  448. watchdog('tpub_pubmed', 'Cannot find a valid vocabulary term for the publication type: "%term".',
  449. array('%term' => $xml->value), WATCHDOG_ERROR);
  450. }
  451. }
  452. else {
  453. $pub['Publication Type'][] = $pub_cvterm->name;
  454. }
  455. break;
  456. default:
  457. break;
  458. }
  459. }
  460. }
  461. }
  462. /*
  463. *
  464. */
  465. function tripal_pub_PMID_parse_abstract($xml, &$pub) {
  466. $abstract = '';
  467. while ($xml->read()) {
  468. $element = $xml->name;
  469. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'Abstract') {
  470. // we've reached the </Abstract> element so return
  471. $pub['Abstract'] = $abstract;
  472. return;
  473. }
  474. // the abstract text can be just a singe paragraph or be broken into multiple
  475. // abstract texts for structured abstracts. Here we will just combine then
  476. // into a single element in the order that they arrive in HTML format
  477. if ($xml->nodeType == XMLReader::ELEMENT) {
  478. switch ($element) {
  479. case 'AbstractText':
  480. $label = $xml->getAttribute('Label');
  481. $xml->read();
  482. if ($label) {
  483. $part = "<p><b>$label</b></br>" . $xml->value . '</p>';
  484. $abstract .= $part;
  485. $pub['Structured Abstract Part'][] = $part;
  486. }
  487. else {
  488. $abstract .= '<p>' . $xml->value . '</p>';
  489. }
  490. break;
  491. case 'CopyrightInformation':
  492. $xml->read();
  493. $pub['Copyright'] = $xml->value;
  494. break;
  495. default:
  496. break;
  497. }
  498. }
  499. }
  500. }
  501. /*
  502. *
  503. */
  504. function tripal_pub_PMID_parse_pagination($xml, &$pub) {
  505. while ($xml->read()) {
  506. $element = $xml->name;
  507. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'Pagination') {
  508. // we've reached the </Pagination> element so we're done.
  509. return;
  510. }
  511. if ($xml->nodeType == XMLReader::ELEMENT) {
  512. switch ($element) {
  513. case 'MedlinePgn':
  514. $xml->read();
  515. if(trim($xml->value)) {
  516. $pub['Pages'] = $xml->value;
  517. }
  518. break;
  519. default:
  520. break;
  521. }
  522. }
  523. }
  524. }
  525. /*
  526. *
  527. */
  528. function tripal_pub_PMID_parse_journal($xml, &$pub) {
  529. while ($xml->read()) {
  530. $element = $xml->name;
  531. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'Journal') {
  532. return;
  533. }
  534. if ($xml->nodeType == XMLReader::ELEMENT) {
  535. switch ($element) {
  536. case 'ISSN':
  537. $issn_type = $xml->getAttribute('IssnType');
  538. $xml->read();
  539. $issn = $xml->value;
  540. $pub['ISSN'] = $issn;
  541. if ($issn_type == 'Electronic') {
  542. $pub['eISSN'] = $issn;
  543. }
  544. if ($issn_type == 'Print') {
  545. $pub['pISSN'] = $issn;
  546. }
  547. break;
  548. case 'JournalIssue':
  549. // valid values of cited_medium are 'Internet' and 'Print'
  550. $cited_medium = $xml->getAttribute('CitedMedium');
  551. tripal_pub_PMID_parse_journal_issue($xml, $pub);
  552. break;
  553. case 'Title':
  554. $xml->read();
  555. $pub['Journal Name'] = $xml->value;
  556. break;
  557. case 'ISOAbbreviation':
  558. $xml->read();
  559. $pub['Journal ISO Abbreviation'] = $xml->value;
  560. break;
  561. default:
  562. break;
  563. }
  564. }
  565. }
  566. }
  567. /*
  568. *
  569. */
  570. function tripal_pub_PMID_parse_journal_issue($xml, &$pub) {
  571. while ($xml->read()) {
  572. $element = $xml->name;
  573. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'JournalIssue'){
  574. // if we're at the </JournalIssue> element then we're done
  575. return;
  576. }
  577. if ($xml->nodeType == XMLReader::ELEMENT) {
  578. switch ($element) {
  579. case 'Volume':
  580. $xml->read();
  581. $pub['Volume'] = $xml->value;
  582. break;
  583. case 'Issue':
  584. $xml->read();
  585. $pub['Issue'] = $xml->value;
  586. break;
  587. case 'PubDate':
  588. $date = tripal_pub_PMID_parse_date($xml, 'PubDate');
  589. $year = $date['year'];
  590. $month = $date['month'];
  591. $day = $date['day'];
  592. $medline = $date['medline'];
  593. $pub['Year'] = $year;
  594. if ($month and $day and $year) {
  595. $pub['Publication Date'] = "$year $month $day";
  596. }
  597. elseif ($month and !$day and $year) {
  598. $pub['Publication Date'] = "$year $month";
  599. }
  600. elseif (!$month and !$day and $year) {
  601. $pub['Publication Date'] = $year;
  602. }
  603. elseif ($medline) {
  604. $pub['Publication Date'] = $medline;
  605. }
  606. else {
  607. $pub['Publication Date'] = "Date Unknown";
  608. }
  609. break;
  610. default:
  611. break;
  612. }
  613. }
  614. }
  615. }
  616. /*
  617. *
  618. */
  619. function tripal_pub_PMID_parse_date ($xml, $element_name) {
  620. $date = array();
  621. while ($xml->read()) {
  622. $element = $xml->name;
  623. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == $element_name){
  624. // if we're at the </$element_name> then we're done
  625. return $date;
  626. }
  627. if ($xml->nodeType == XMLReader::ELEMENT) {
  628. switch ($element) {
  629. case 'Year':
  630. $xml->read();
  631. $date['year'] = $xml->value;
  632. break;
  633. case 'Month':
  634. $xml->read();
  635. $month =
  636. $date['month'] = $xml->value;
  637. break;
  638. case 'Day':
  639. $xml->read();
  640. $date['day'] = $xml->value;
  641. break;
  642. case 'MedlineDate':
  643. // the medline date is when the date cannot be broken into distinct month day year.
  644. $xml->read();
  645. $date['medline'] = $xml->value;
  646. break;
  647. default:
  648. break;
  649. }
  650. }
  651. }
  652. }
  653. /*
  654. *
  655. */
  656. function tripal_pub_PMID_parse_authorlist($xml, &$pub) {
  657. $num_authors = 0;
  658. while ($xml->read()) {
  659. $element = $xml->name;
  660. if ($xml->nodeType == XMLReader::END_ELEMENT){
  661. // if we're at the </AuthorList> element then we're done with the article...
  662. if($element == 'AuthorList') {
  663. // build the author list before returning
  664. $authors = '';
  665. foreach ($pub['Author List'] as $author) {
  666. if ($author['valid'] == 'N') {
  667. // skip non-valid entries. A non-valid entry should have
  668. // a corresponding corrected entry so we can saftely skip it.
  669. continue;
  670. }
  671. if ($author['Collective']) {
  672. $authors .= $author['Collective'] . ', ';
  673. }
  674. else {
  675. $authors .= $author['Surname'] . ' ' . $author['First Initials'] . ', ';
  676. }
  677. }
  678. $authors = substr($authors, 0, -2);
  679. $pub['Authors'] = $authors;
  680. return;
  681. }
  682. // if we're at the end </Author> element then we're done with the author and we can
  683. // start a new one.
  684. if($element == 'Author') {
  685. $num_authors++;
  686. }
  687. }
  688. if ($xml->nodeType == XMLReader::ELEMENT) {
  689. switch ($element) {
  690. case 'Author':
  691. $valid = $xml->getAttribute('ValidYN');
  692. $pub['Author List'][$num_authors]['valid'] = $valid;
  693. break;
  694. case 'LastName':
  695. $xml->read();
  696. $pub['Author List'][$num_authors]['Surname'] = $xml->value;
  697. break;
  698. case 'ForeName':
  699. $xml->read();
  700. $pub['Author List'][$num_authors]['Given Name'] = $xml->value;
  701. break;
  702. case 'Initials':
  703. $xml->read();
  704. $pub['Author List'][$num_authors]['First Initials'] = $xml->value;
  705. break;
  706. case 'Suffix':
  707. $xml->read();
  708. $pub['Author List'][$num_authors]['Suffix'] = $xml->value;
  709. break;
  710. case 'CollectiveName':
  711. $xml->read();
  712. $pub['Author List'][$num_authors]['Collective'] = $xml->value;
  713. break;
  714. case 'Identifier':
  715. // according to the specification, this element is not yet used.
  716. break;
  717. default:
  718. break;
  719. }
  720. }
  721. }
  722. }
  723. /*
  724. * Language abbreviations were obtained here:
  725. * http://www.nlm.nih.gov/bsd/language_table.html
  726. */
  727. function tripal_pub_remote_search_get_language($lang_abbr) {
  728. $languages = array(
  729. 'afr' => 'Afrikaans',
  730. 'alb' => 'Albanian',
  731. 'amh' => 'Amharic',
  732. 'ara' => 'Arabic',
  733. 'arm' => 'Armenian',
  734. 'aze' => 'Azerbaijani',
  735. 'ben' => 'Bengali',
  736. 'bos' => 'Bosnian',
  737. 'bul' => 'Bulgarian',
  738. 'cat' => 'Catalan',
  739. 'chi' => 'Chinese',
  740. 'cze' => 'Czech',
  741. 'dan' => 'Danish',
  742. 'dut' => 'Dutch',
  743. 'eng' => 'English',
  744. 'epo' => 'Esperanto',
  745. 'est' => 'Estonian',
  746. 'fin' => 'Finnish',
  747. 'fre' => 'French',
  748. 'geo' => 'Georgian',
  749. 'ger' => 'German',
  750. 'gla' => 'Scottish Gaelic',
  751. 'gre' => 'Greek, Modern',
  752. 'heb' => 'Hebrew',
  753. 'hin' => 'Hindi',
  754. 'hrv' => 'Croatian',
  755. 'hun' => 'Hungarian',
  756. 'ice' => 'Icelandic',
  757. 'ind' => 'Indonesian',
  758. 'ita' => 'Italian',
  759. 'jpn' => 'Japanese',
  760. 'kin' => 'Kinyarwanda',
  761. 'kor' => 'Korean',
  762. 'lat' => 'Latin',
  763. 'lav' => 'Latvian',
  764. 'lit' => 'Lithuanian',
  765. 'mac' => 'Macedonian',
  766. 'mal' => 'Malayalam',
  767. 'mao' => 'Maori',
  768. 'may' => 'Malay',
  769. 'mul' => 'Multiple languages',
  770. 'nor' => 'Norwegian',
  771. 'per' => 'Persian',
  772. 'pol' => 'Polish',
  773. 'por' => 'Portuguese',
  774. 'pus' => 'Pushto',
  775. 'rum' => 'Romanian, Rumanian, Moldovan',
  776. 'rus' => 'Russian',
  777. 'san' => 'Sanskrit',
  778. 'slo' => 'Slovak',
  779. 'slv' => 'Slovenian',
  780. 'spa' => 'Spanish',
  781. 'srp' => 'Serbian',
  782. 'swe' => 'Swedish',
  783. 'tha' => 'Thai',
  784. 'tur' => 'Turkish',
  785. 'ukr' => 'Ukrainian',
  786. 'und' => 'Undetermined',
  787. 'urd' => 'Urdu',
  788. 'vie' => 'Vietnamese',
  789. 'wel' => 'Welsh',
  790. );
  791. return $languages[strtolower($lang_abbr)];
  792. }