PMID.inc 26 KB

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