PMID.inc 27 KB

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