tripal_chado.pub_importer_AGL.inc 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. <?php
  2. /**
  3. * @file
  4. * This file provides support for importing and parsing of results from the
  5. * USDA National Agricultural Library (AGL) database. The functions here are used by
  6. * both the publication importer setup form and the publication importer.
  7. *
  8. * The USDA AGL database uses a YAZ protocol for querying and retrieving records.
  9. *
  10. */
  11. /**
  12. * A hook for altering the publication importer form. It Changes the
  13. * 'Days' element to 'Year' and removes the 'Journal Name' filter.
  14. *
  15. * @param $form
  16. * The Drupal form array
  17. * @param $form_state
  18. * The form state array
  19. * @param $num_criteria
  20. * The number of criteria the user currently has added to the form
  21. *
  22. * @return
  23. * The form (drupal form api)
  24. *
  25. * @ingroup tripal_pub
  26. */
  27. function tripal_pub_remote_alter_form_AGL($form, $form_state, $num_criteria = 1) {
  28. // So far we haven't been able to get AGL to filter results to only
  29. // include pubs by the XX number days in the past. So, we will
  30. // change the 'days' element to be the year to query
  31. $form['themed_element']['days']['#title'] = t('Year');
  32. $form['themed_element']['days']['#description'] = t('Please enter a year to limit records by the year they were published, created or modified in the database.');
  33. // The Journal Name filter doesn't seem to work, so remove it
  34. for($i = 1; $i <= $num_criteria; $i++) {
  35. unset($form['themed_element']['criteria'][$i]["scope-$i"]['#options']['journal']);
  36. }
  37. return $form;
  38. }
  39. /**
  40. * A hook for providing additional validation of importer setup form.
  41. *
  42. * @param $form
  43. * The Drupal form array
  44. * @param $form_state
  45. * The form state array
  46. *
  47. * @return
  48. * The form (drupal form api)
  49. *
  50. * @ingroup tripal_pub
  51. */
  52. function tripal_pub_remote_validate_form_AGL($form, $form_state) {
  53. $days = trim($form_state['values']["days"]);
  54. $num_criteria = $form_state['values']['num_criteria'];
  55. if ($days and !preg_match('/^\d\d\d\d$/', $days)) {
  56. form_set_error("days", "Please enter a four digit year.");
  57. }
  58. $num_ids = 0;
  59. for ($i = 1; $i <= $num_criteria; $i++) {
  60. $search_terms = trim($form_state['values']["search_terms-$i"]);
  61. $scope = $form_state['values']["scope-$i"];
  62. if ($scope == 'id' and !preg_match('/^AGL:\d+$/', $search_terms)) {
  63. form_set_error("search_terms-$i", "The AGL accession be a numeric value, prefixed with 'AGL:' (e.g. AGL:3890740).");
  64. }
  65. if ($scope == 'id') {
  66. $num_ids++;
  67. }
  68. if($num_ids > 1) {
  69. form_set_error("search_terms-$i", "Unfortuantely, the AGL importer can only support a single accession at a time. Please remove the others.");
  70. }
  71. }
  72. return $form;
  73. }
  74. /**
  75. * A hook for performing the search on the AGL database.
  76. *
  77. * @param $search_array
  78. * An array containing the serach criteria for the serach
  79. * @param $num_to_retrieve
  80. * Indicates the maximum number of publications to retrieve from the remote
  81. * database
  82. * @param $page
  83. * Indicates the page to retrieve. This corresponds to a paged table, where
  84. * each page has $num_to_retrieve publications.
  85. *
  86. * @return
  87. * An array of publications.
  88. *
  89. * @ingroup tripal_pub
  90. */
  91. function tripal_pub_remote_search_AGL($search_array, $num_to_retrieve, $page) {
  92. // get some values from the serach array
  93. $num_criteria = $search_array['num_criteria'];
  94. $days = array_key_exists('days', $search_array) ? $search_array['days'] : '';
  95. // set some defaults
  96. $search_array['limit'] = $num_to_retrieve;
  97. // To build the CCL search string we want to have a single entry for 'author', 'title', 'abstract'
  98. // or 'id', and also the corresponding 'not for each of those.
  99. // But the search form allows the user to have multiple rows of the same type. So, we will build the
  100. // search string separately for each category and it's negative category (if NOT is selected as the op)
  101. // and at the end we will put them together into a single search string. We need to keep
  102. // track of the first entry of any category because it will not have an op (e.g. 'or' or 'and') but the
  103. // operation will be pushed out to separate the categories. The op for any second or third instance of
  104. // the same category will be included within the search string for the catgory.
  105. $ccl = '';
  106. $title = '';
  107. $author = '';
  108. $abstract = '';
  109. $id = '';
  110. $any = '';
  111. $negate_title = '';
  112. $negate_author = '';
  113. $negate_abstract = '';
  114. $negate_id = '';
  115. $negate_any = '';
  116. $order = array();
  117. $first_abstract = 1;
  118. $first_author = 1;
  119. $first_title = 1;
  120. $first_id = 1;
  121. $first_any = 1;
  122. $first_negate_abstract = 1;
  123. $first_negate_author = 1;
  124. $first_negate_title = 1;
  125. $first_negate_id = 1;
  126. $first_negate_any = 1;
  127. for ($i = 1; $i <= $num_criteria; $i++) {
  128. $search_terms = trim($search_array['criteria'][$i]['search_terms']);
  129. $scope = $search_array['criteria'][$i]['scope'];
  130. $is_phrase = $search_array['criteria'][$i]['is_phrase'];
  131. $op = $search_array['criteria'][$i]['operation'];
  132. if ($op) {
  133. $op = strtolower($op);
  134. }
  135. $search_terms = trim($search_terms);
  136. // if this is not a phrase then make sure the AND and OR are lower-case
  137. if (!$is_phrase) {
  138. $search_terms = preg_replace('/ OR /', ' or ', $search_terms);
  139. $search_terms = preg_replace('/ AND /', ' and ', $search_terms);
  140. }
  141. // else make sure the search terms are surrounded by quotes
  142. else {
  143. $search_terms = "\"$search_terms\"";
  144. }
  145. // if this is a 'not' operation then we want to change it to an
  146. // and
  147. $negate = '';
  148. if ($op == 'not') {
  149. $scope = "negate_$scope";
  150. $op = 'or';
  151. }
  152. $order[] = array('scope' => $scope, 'op' => $op);
  153. // build each category
  154. if ($scope == 'title') {
  155. if ($first_title) {
  156. $title .= "($search_terms) ";
  157. $first_title = 0;
  158. }
  159. else {
  160. $title .= "$op ($search_terms) ";
  161. }
  162. }
  163. if ($scope == 'negate_title') {
  164. if ($first_negate_title) {
  165. $negate_title .= "($search_terms) ";
  166. $first_negate_title = 0;
  167. }
  168. else {
  169. $negate_title .= "$op ($search_terms) ";
  170. }
  171. }
  172. elseif ($scope == 'author') {
  173. if ($first_author) {
  174. $author .= "($search_terms) ";
  175. $first_author = 0;
  176. }
  177. else {
  178. $author .= "$op ($search_terms) ";
  179. }
  180. }
  181. elseif ($scope == 'negate_author') {
  182. if ($first_negate_author) {
  183. $negate_author .= "($search_terms) ";
  184. $first_negate_author = 0;
  185. }
  186. else {
  187. $negate_author .= "$op ($search_terms) ";
  188. }
  189. }
  190. elseif ($scope == 'abstract') {
  191. if ($first_abstract) {
  192. $abstract .= "($search_terms) ";
  193. $first_abstract = 0;
  194. }
  195. else {
  196. $abstract .= "$op ($search_terms) ";
  197. }
  198. }
  199. elseif ($scope == 'negate_abstract') {
  200. if ($first_negate_abstract) {
  201. $negate_abstract .= "($search_terms) ";
  202. $first_negate_abstract = 0;
  203. }
  204. else {
  205. $negate_abstract .= "$op ($search_terms) ";
  206. }
  207. }
  208. elseif ($scope == 'journal') {
  209. if ($first_journal) {
  210. $journal .= "($search_terms) ";
  211. $first_jounral = 0;
  212. }
  213. else {
  214. $journal .= "$op ($search_terms) ";
  215. }
  216. }
  217. elseif ($scope == 'negate_journal') {
  218. if ($first_negate_journal) {
  219. $negate_journal .= "($search_terms) ";
  220. $first_negate_journal = 0;
  221. }
  222. else {
  223. $negate_journal .= "$op ($search_terms) ";
  224. }
  225. }
  226. elseif ($scope == 'id') {
  227. if ($first_id) {
  228. $id .= "(" . preg_replace('/AGL:([^\s]*)/', '$1', $search_terms) . ") ";
  229. $first_id = 0;
  230. }
  231. else {
  232. $id .= "$op (" . preg_replace('/AGL:([^\s]*)/', '$1', $search_terms) . ") ";
  233. }
  234. }
  235. elseif ($scope == 'negate_id') {
  236. if ($first_negate_id) {
  237. $negate_id .= "(" . preg_replace('/AGL:([^\s]*)/', '$1', $search_terms) . ") ";
  238. $first_negate_id = 0;
  239. }
  240. else {
  241. $negate_id .= "$op (" . preg_replace('/AGL:([^\s]*)/', '$1', $search_terms) . ") ";
  242. }
  243. }
  244. elseif ($scope == 'any'){
  245. if ($first_any) {
  246. $any .= "($search_terms) ";
  247. $first_any = 0;
  248. }
  249. else {
  250. $any .= "$op ($search_terms) ";
  251. }
  252. }
  253. elseif ($scope == 'negate_any'){
  254. if ($first_negate_any) {
  255. $negate_any .= "($search_terms) ";
  256. $first_any = 0;
  257. }
  258. else {
  259. $negate_any .= "$op ($search_terms) ";
  260. }
  261. }
  262. }
  263. // now build the CCL string in order
  264. $abstract_done = 0;
  265. $author_done = 0;
  266. $journal_done = 0;
  267. $title_done = 0;
  268. $id_done = 0;
  269. $any_done = 0;
  270. $negate_abstract_done = 0;
  271. $negate_journal_done = 0;
  272. $negate_author_done = 0;
  273. $negate_title_done = 0;
  274. $negate_id_done = 0;
  275. $negate_any_done = 0;
  276. for ($i = 0; $i < count($order) ; $i++) {
  277. if ($order[$i]['scope'] == 'abstract' and !$abstract_done) {
  278. $op = $order[$i]['op'];
  279. $ccl .= "$op abstract=($abstract) ";
  280. $abstract_done = 1;
  281. }
  282. if ($order[$i]['scope'] == 'negate_abstract' and !$negate_abstract_done) {
  283. $ccl .= "not abstract=($negate_abstract) ";
  284. $negate_abstract_done = 1;
  285. }
  286. if ($order[$i]['scope'] == 'author' and !$author_done) {
  287. $op = $order[$i]['op'];
  288. $ccl .= "$op author=($author) ";
  289. $author_done = 1;
  290. }
  291. if ($order[$i]['scope'] == 'negate_author' and !$negate_author_done) {
  292. $ccl .= "not author=($negate_author) ";
  293. $negate_author_done = 1;
  294. }
  295. if ($order[$i]['scope'] == 'journal' and !$journal_done) {
  296. $op = $order[$i]['op'];
  297. $ccl .= "$op journal=($journal) ";
  298. $journal_done = 1;
  299. }
  300. if ($order[$i]['scope'] == 'negate_journal' and !$negate_journal_done) {
  301. $ccl .= "not author=($negate_journal) ";
  302. $negate_journal_done = 1;
  303. }
  304. if ($order[$i]['scope'] == 'id' and !$id_done) {
  305. $op = $order[$i]['op'];
  306. $ccl .= "$op id=($id) ";
  307. $id_done = 1;
  308. }
  309. if ($order[$i]['scope'] == 'negate_id' and !$negate_id_done) {
  310. $ccl .= "not id=($negate_id) ";
  311. $negate_id_done = 1;
  312. }
  313. if ($order[$i]['scope'] == 'title' and !$title_done) {
  314. $op = $order[$i]['op'];
  315. $ccl .= "$op title=($title) ";
  316. $title_done = 1;
  317. }
  318. if ($order[$i]['scope'] == 'negate_title' and !$negate_title_done) {
  319. $ccl .= "not title=($negate_title) ";
  320. $negate_title_done = 1;
  321. }
  322. if ($order[$i]['scope'] == 'any' and !$any_done) {
  323. $op = $order[$i]['op'];
  324. $ccl .= "$op ($any) ";
  325. $any_done = 1;
  326. }
  327. if ($order[$i]['scope'] == 'negate_any' and !$negate_any_done) {
  328. $ccl .= "not ($negate_any) ";
  329. $negate_any_done = 1;
  330. }
  331. }
  332. // for AGL the 'days' form element was converted to represent the year
  333. if ($days) {
  334. $ccl .= "and year=($days)";
  335. }
  336. // remove any preceeding 'and' or 'or'
  337. $ccl = preg_replace('/^\s*(and|or)/', '', $ccl);
  338. // yaz_connect() prepares for a connection to a Z39.50 server. This function is non-blocking
  339. // and does not attempt to establish a connection - it merely prepares a connect to be
  340. // performed later when yaz_wait() is called.
  341. //$yazc = yaz_connect('agricola.nal.usda.gov:7090/voyager'); // NAL Catalog
  342. $yazc = yaz_connect('agricola.nal.usda.gov:7190/voyager'); // NAL Article Citation Database
  343. // use the USMARC record type. But OPAC is also supported by Agricola
  344. yaz_syntax($yazc, "usmarc");
  345. // the search query is built using CCL, we need to first
  346. // configure it so it can map the attributes to defined identifiers
  347. // The attribute set used by AGL can be found at the bottom of this page:
  348. // http://agricola.nal.usda.gov/help/z3950.html
  349. //
  350. // More in depth details: http://www.loc.gov/z3950/agency/bib1.html
  351. //
  352. // CCL Syntax: http://www.indexdata.com/yaz/doc/tools.html#CCL
  353. //
  354. $fields = array(
  355. "title" => "u=4",
  356. "author" => "u=1003",
  357. "abstract" => "u=62",
  358. "id" => "u=12",
  359. "year" => "u=30 r=o",
  360. "journal" => "u=1033"
  361. );
  362. yaz_ccl_conf($yazc, $fields);
  363. if (!yaz_ccl_parse($yazc, $ccl, $cclresult)) {
  364. drupal_set_message('Error parsing search string: ' . $cclresult["errorstring"], "error");
  365. watchdog('tpub_import', 'Error: %errstr', array('%errstr' => $cclresult["errorstring"]), WATCHDOG_ERROR);
  366. return array(
  367. 'total_records' => 0,
  368. 'search_str' => '',
  369. 'pubs' => array(),
  370. );
  371. }
  372. $search_str = $cclresult["rpn"];
  373. // get the total number of records
  374. $total_records = tripal_pub_AGL_count($yazc, $search_str);
  375. // get the pubs in the specified rang
  376. $start = $page * $num_to_retrieve;
  377. $results = tripal_pub_AGL_range($yazc, $search_str, $start, $num_to_retrieve, $total_records);
  378. // close the connection
  379. yaz_close($yazc);
  380. return $results;
  381. }
  382. /**
  383. * Retreives a range of publications from AGL
  384. *
  385. * @param $yazc
  386. * The YAZC connection object.
  387. * @param $search_str
  388. * The search string to use for searching.
  389. * @param $start
  390. * The start of the range
  391. * @param $num_to_retrieve
  392. * The number of publications to retrieve
  393. * @param $total_records
  394. * The total number of records in the dataset. This value should have
  395. * been retrieved by tripal_pub_AGL_count() function.
  396. *
  397. * @return
  398. * An array containing the total_records in the dataaset, the search string
  399. * and an array of the publications that were retreived.
  400. *
  401. * @ingroup tripal_pub
  402. */
  403. function tripal_pub_AGL_range($yazc, $search_str, $start, $num_to_retrieve, $total_records) {
  404. yaz_range($yazc, 1, $total_records);
  405. if (!yaz_present($yazc)) {
  406. $error_no = yaz_errno($yazc);
  407. $error_msg = yaz_error($yazc);
  408. $additional = yaz_addinfo($yazc);
  409. if ($additional != $error_msg) {
  410. $error_msg .= " $additional";
  411. }
  412. drupal_set_message("ERROR waiting on search at AGL: ($error_no) $error_msg", "error");
  413. watchdog('tpub_import', "ERROR waiting on search at AGL: (%error_no) %error_msg",
  414. array('%error_no' => $error_no, '%error_msg' => $error_msg), WATCHDOG_ERROR);
  415. return array(
  416. 'total_records' => $total_records,
  417. 'search_str' => $search_str,
  418. 'pubs' => array(),
  419. );
  420. }
  421. if ($start + $num_to_retrieve > $total_records) {
  422. $num_to_retrieve = $total_records - $start;
  423. }
  424. $pubs = array();
  425. for($i = $start; $i < $start + $num_to_retrieve; $i++) {
  426. // retrieve the XML results
  427. $pub_xml = yaz_record($yazc, $i + 1, 'xml; charset=marc-8,utf-8');
  428. // parse the pub XML
  429. $pub = tripal_pub_AGL_parse_pubxml($pub_xml);
  430. $pubs[] = $pub;
  431. }
  432. return array(
  433. 'total_records' => $total_records,
  434. 'search_str' => $search_str,
  435. 'pubs' => $pubs,
  436. );
  437. }
  438. /**
  439. * Retreives the total number of publications that match the search string.
  440. *
  441. * @param $yazc
  442. * The YAZC connection object.
  443. * @param $search_str
  444. * The search string to use for searching.
  445. *
  446. * @return
  447. * a count of the total number of publications that match the search string
  448. *
  449. * @ingroup tripal_pub
  450. */
  451. function tripal_pub_AGL_count($yazc, $search_str) {
  452. //yaz_sort($yazc, "1=31 id"); // sort by publication date descending
  453. if (!yaz_search($yazc, "rpn", $search_str)){
  454. $error_no = yaz_errno($yazc);
  455. $error_msg = yaz_error($yazc);
  456. $additional = yaz_addinfo($yazc);
  457. if ($additional != $error_msg) {
  458. $error_msg .= " $additional";
  459. }
  460. drupal_set_message("ERROR preparing search at AGL: ($error_no) $error_msg", "error");
  461. watchdog('tpub_import', "ERROR preparing search at AGL: (%error_no) %error_msg",
  462. array('%error_no' => $error_no, '%error_msg' => $error_msg), WATCHDOG_ERROR);
  463. return 0;
  464. }
  465. if (!yaz_wait()) {
  466. $error_no = yaz_errno($yazc);
  467. $error_msg = yaz_error($yazc);
  468. $additional = yaz_addinfo($yazc);
  469. if ($additional != $error_msg) {
  470. $error_msg .= " $additional";
  471. }
  472. drupal_set_message("ERROR waiting on search at AGL: ($error_no) $error_msg", "error");
  473. watchdog('tpub_import', "ERROR waiting on search at AGL: (%error_no) %error_msg",
  474. array('%error_no' => $error_no, '%error_msg' => $error_msg), WATCHDOG_ERROR);
  475. return 0;
  476. }
  477. // get the total number of results from the serach
  478. $count = yaz_hits($yazc);
  479. return $count;
  480. }
  481. /**
  482. * Parse publication XML for a single publication
  483. *
  484. * Description of XML format:
  485. * http://www.loc.gov/marc/bibliographic/bdsummary.html
  486. *
  487. * @param $pub_xml
  488. * A string containing the XML for a single publications
  489. *
  490. * @return
  491. * An array containing the details of the publication
  492. *
  493. * @ingroup tripal_pub
  494. */
  495. function tripal_pub_AGL_parse_pubxml($pub_xml) {
  496. $pub = array();
  497. // we will set the default publication type as a journal article. The NAL
  498. // dataset doesn't specify an article type so we'll have to glean the type
  499. // from other information (e.g. series name has 'Proceedings' in it)
  500. $pub['Publication Type'][0] = 'Journal Article';
  501. if (!$pub_xml) {
  502. return $pub;
  503. }
  504. // read the XML and iterate through it.
  505. $xml = new XMLReader();
  506. $xml->xml(trim($pub_xml));
  507. while ($xml->read()) {
  508. $element = $xml->name;
  509. if ($xml->nodeType == XMLReader::ELEMENT and $element == 'controlfield') {
  510. $tag = $xml->getAttribute('tag');
  511. $xml->read();
  512. $value = $xml->value;
  513. switch ($tag) {
  514. case '001': // control number
  515. $pub['Publication Accession'] = $value;
  516. break;
  517. case '003': // control number identifier
  518. break;
  519. case '005': // datea nd time of latest transaction
  520. break;
  521. case '006': // fixed-length data elemetns
  522. break;
  523. case '007': // physical description fixed field
  524. break;
  525. case '008': // fixed length data elements
  526. $month = array(
  527. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar',
  528. '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  529. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep',
  530. '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  531. );
  532. $date0 = substr($value, 0, 6); // date entered on file
  533. $date1 = substr($value, 7, 4); // year of publication
  534. $date2 = substr($value, 11, 4); // month of publication
  535. $place = substr($value, 15, 3);
  536. $lang = substr($value, 35, 3);
  537. if (preg_match('/\d\d\d\d/', $date1)) {
  538. $pub['Year'] = $date1;
  539. $pub['Publication Date'] = $date1;
  540. }
  541. if (preg_match('/\d\d/', $date2)) {
  542. $pub['Publication Date'] = $date1 . " " . $month[substr($date2, 0, 2)] . " " . substr($date2, 3, 2);
  543. }
  544. if (!preg_match('/\s+/', $place)) {
  545. $pub['Published Location'] = $place;
  546. }
  547. if (!preg_match('/\s+/', $lang)) {
  548. $pub['Language Abbr'] = $lang;
  549. }
  550. break;
  551. default: // unhandled tag
  552. break;
  553. }
  554. }
  555. elseif ($xml->nodeType == XMLReader::ELEMENT and $element == 'datafield') {
  556. $tag = $xml->getAttribute('tag');
  557. $ind1 = $xml->getAttribute('ind1');
  558. $ind2 = $xml->getAttribute('ind2');
  559. switch ($tag) {
  560. case '16': // National Bibliographic Agency Control Number
  561. break;
  562. case '35': // System Control Number
  563. $author = array();
  564. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  565. foreach ($codes as $code => $value) {
  566. switch ($code) {
  567. case 'a': // System control number
  568. $pub['Publication Accession'] = $value;
  569. break;
  570. }
  571. }
  572. case '40': // Cataloging Source (NR)
  573. $author = array();
  574. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  575. foreach ($codes as $code => $value) {
  576. switch ($code) {
  577. case 'a': // original cataolging agency
  578. $pub['Publication Database'] = $value;
  579. break;
  580. }
  581. }
  582. break;
  583. case '72': // Subject Category Code
  584. break;
  585. case '100': // main entry-personal name
  586. $author = tripal_pub_remote_search_AGL_get_author($xml, $ind1);
  587. $pub['Author List'][] = $author;
  588. break;
  589. case '110': // main entry-corporate nmae
  590. $author = array();
  591. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  592. foreach ($codes as $code => $value) {
  593. switch ($code) {
  594. case 'a': // Corporate name or jurisdiction name as entry elemen
  595. $author['Collective'] = $value;
  596. break;
  597. case 'b': // Subordinate unit
  598. $author['Collective'] .= ' ' . $value;
  599. break;
  600. }
  601. }
  602. $pub['Author List'][] = $author;
  603. break;
  604. case '111': // main entry-meeting name
  605. break;
  606. case '130': // main entry-uniform title
  607. break;
  608. case '210': // abbreviated title
  609. break;
  610. case '222': // key title
  611. break;
  612. case '240': // uniform title
  613. break;
  614. case '242': // translation of title by cataloging agency
  615. break;
  616. case '243': // collective uniform title
  617. break;
  618. case '245': // title statement
  619. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  620. foreach ($codes as $code => $value) {
  621. switch ($code) {
  622. case 'a':
  623. $pub['Title'] = trim(preg_replace('/\.$/', '', $value));
  624. break;
  625. case 'b':
  626. $pub['Title'] .= ' ' . $value;
  627. break;
  628. case 'h':
  629. $pub['Publication Model'] = $value;
  630. break;
  631. }
  632. }
  633. break;
  634. case '246': // varying form of title
  635. break;
  636. case '247': // former title
  637. break;
  638. case '250': // edition statement
  639. break;
  640. case '254': // musicla presentation statement
  641. break;
  642. case '255': // cartographic mathematical data
  643. break;
  644. case '256': // computer file characteristics
  645. break;
  646. case '257': // country of producing entity
  647. break;
  648. case '258': // philatelic issue data
  649. break;
  650. case '260': // publication, distribution ,etc (imprint)
  651. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  652. foreach ($codes as $code => $value) {
  653. switch ($code) {
  654. case 'a':
  655. $pub['Published Location'] = $value;
  656. break;
  657. case 'b':
  658. $pub['Publisher'] = $value;
  659. break;
  660. case 'c':
  661. $pub['Publication Date'] = $value;
  662. break;
  663. }
  664. }
  665. break;
  666. case '263': // projected publication date
  667. break;
  668. case '264': // production, publication, distribution, manufacture and copyright notice
  669. break;
  670. case '270': // Address
  671. break;
  672. case '300': // Address
  673. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  674. foreach ($codes as $code => $value) {
  675. switch ($code) {
  676. case 'a':
  677. $pages = $value;
  678. $pages = preg_replace('/^p\. /', '', $pages);
  679. $pages = preg_replace('/\.$/', '' , $pages);
  680. if(preg_match('/p$/', $pages)) {
  681. // skip this, it's the number of pages not the page numbers
  682. }
  683. else {
  684. $pub['Pages'] = $pages;
  685. }
  686. break;
  687. }
  688. }
  689. break;
  690. case '500': // series statements
  691. $pub['Notes'] = $value;
  692. break;
  693. case '504': // Bibliography, Etc. Note
  694. break;
  695. case '520': // Summary, etc
  696. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  697. foreach ($codes as $code => $value) {
  698. switch ($code) {
  699. case 'a':
  700. $pub['Abstract'] = $value;
  701. break;
  702. }
  703. }
  704. break;
  705. case '650': // Subject Added Entry-Topical Term
  706. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  707. foreach ($codes as $code => $value) {
  708. switch ($code) {
  709. case 'a':
  710. $pub['Keywords'][] = $value;
  711. break;
  712. }
  713. }
  714. break;
  715. case '653': // Index Term-Uncontrolled
  716. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  717. foreach ($codes as $code => $value) {
  718. switch ($code) {
  719. case 'a':
  720. $pub['Keywords'][] = $value;
  721. break;
  722. }
  723. }
  724. break;
  725. case '700': // Added Entry-Personal Name
  726. $author = tripal_pub_remote_search_AGL_get_author($xml, $ind1);
  727. $pub['Author List'][] = $author;
  728. break;
  729. case '710': // Added Entry-Corporate Name
  730. $author = array();
  731. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  732. foreach ($codes as $code => $value) {
  733. switch ($code) {
  734. case 'a': // Corporate name or jurisdiction name as entry elemen
  735. $author['Collective'] = $value;
  736. break;
  737. case 'b': // Subordinate unit
  738. $author['Collective'] .= ' ' . $value;
  739. break;
  740. }
  741. }
  742. $pub['Author List'][] = $author;
  743. break;
  744. case '773': // host item entry
  745. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  746. foreach ($codes as $code => $value) {
  747. switch ($code) {
  748. case 'a':
  749. if (preg_match('/Proceedings/i', $value)) {
  750. $pub['Series Name'] = preg_replace('/\.$/', '', $value);
  751. $pub['Publication Type'][0] = 'Conference Proceedings';
  752. }
  753. else {
  754. $pub['Journal Name'] = preg_replace('/\.$/', '', $value);
  755. }
  756. break;
  757. case 't':
  758. if (preg_match('/Proceedings/i', $value)) {
  759. $pub['Series Name'] = preg_replace('/\.$/', '', $value);
  760. $pub['Publication Type'][0] = 'Conference Proceedings';
  761. }
  762. $pub['Journal Name'] = preg_replace('/\.$/', '', $value);
  763. break;
  764. case 'g':
  765. $matches = array();
  766. if (preg_match('/^(\d\d\d\d)/', $value, $matches)) {
  767. $pub['Publication Date'] = $matches[1];
  768. }
  769. elseif (preg_match('/(.*?)(\.|\s+)\s*(\d+),\s(\d\d\d\d)/', $value, $matches)) {
  770. $year = $matches[4];
  771. $month = $matches[1];
  772. $day = $matches[3];
  773. $pub['Publication Date'] = "$year $month $day";
  774. }
  775. elseif (preg_match('/\((.*?)(\.|\s+)(\d\d\d\d)\)/', $value, $matches)) {
  776. $year = $matches[3];
  777. $month = $matches[1];
  778. $pub['Publication Date'] = "$year $month";
  779. }
  780. elseif (preg_match('/^(.*?) (\d\d\d\d)/', $value, $matches)) {
  781. $year = $matches[2];
  782. $month = $matches[1];
  783. $pub['Publication Date'] = "$year $month";
  784. }
  785. if (preg_match('/v\. (.*?)(,|\s+)/', $value, $matches)) {
  786. $pub['Volume'] = $matches[1];
  787. }
  788. if (preg_match('/v\. (.*?)(,|\s+)\((.*?)\)/', $value, $matches)) {
  789. $pub['Volume'] = $matches[1];
  790. $pub['Issue'] = $matches[3];
  791. }
  792. if (preg_match('/no\. (.*?)(\s|$)/', $value, $matches)) {
  793. $pub['Issue'] = $matches[1];
  794. }
  795. break;
  796. case 'p':
  797. $pub['Journal Abbreviation'] = $value;
  798. break;
  799. case 'z':
  800. $pub['ISBN'] = $value;
  801. break;
  802. }
  803. }
  804. break;
  805. case '852': // Location (Where is the publication held)
  806. break;
  807. case '856': // Electronic Location and Access
  808. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  809. foreach ($codes as $code => $value) {
  810. switch ($code) {
  811. case 'u':
  812. $pub['URL'] = $value;
  813. break;
  814. }
  815. }
  816. break;
  817. default:
  818. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  819. $unhandled[$tag][] = $codes;
  820. break;
  821. }
  822. }
  823. }
  824. // build the Dbxref
  825. if ($pub['Publication Database'] != 'AGL') {
  826. }
  827. if ($pub['Publication Accession'] and $pub['Publication Database']) {
  828. $pub['Publication Dbxref'] = $pub['Publication Database'] . ":" . $pub['Publication Accession'];
  829. unset($pub['Publication Accession']);
  830. unset($pub['Publication Database']);
  831. }
  832. // build the full authors list
  833. if (is_array($pub['Author List'])) {
  834. $authors = '';
  835. foreach ($pub['Author List'] as $author) {
  836. if (array_key_exists('valid', $author) and $author['valid'] == 'N') {
  837. // skip non-valid entries. A non-valid entry should have
  838. // a corresponding corrected entry so we can saftely skip it.
  839. continue;
  840. }
  841. if (array_key_exists('Collective', $author)) {
  842. $authors .= $author['Collective'] . ', ';
  843. }
  844. else {
  845. if (array_key_exists('Surname', $author)) {
  846. $authors .= $author['Surname'];
  847. if(array_key_exists('First Initials', $author)) {
  848. $authors .= ' ' . $author['First Initials'];
  849. }
  850. $authors .= ', ';
  851. }
  852. }
  853. }
  854. $authors = substr($authors, 0, -2);
  855. $pub['Authors'] = $authors;
  856. }
  857. else {
  858. $pub['Authors'] = $pub['Author List'];
  859. }
  860. // for Title, Abstract, Authors, convert the html entity and remove special unicode chars that are not meant for display
  861. $pub['Title'] = preg_replace( '/[\p{So}]/u', '', mb_convert_encoding($pub['Title'], 'UTF-8', 'HTML-ENTITIES'));
  862. if (key_exists('Abstract', $pub)) {
  863. $pub['Abstract'] =preg_replace( '/[\p{So}]/u', '',mb_convert_encoding($pub['Abstract'], 'UTF-8', 'HTML-ENTITIES'));
  864. }
  865. $newauths = array();
  866. foreach ($pub['Author List'] AS $auth) {
  867. foreach($auth AS $k => $v) {
  868. $auth[$k] = preg_replace( '/[\p{So}]/u', '',mb_convert_encoding($v, 'UTF-8', 'HTML-ENTITIES'));
  869. }
  870. array_push($newauths, $auth);
  871. }
  872. $pub['Author List'] = $newauths;
  873. // build the citation
  874. $pub['Citation'] = tripal_pub_create_citation($pub);
  875. $pub['raw'] = $pub_xml;
  876. return $pub;
  877. }
  878. /**
  879. * Used for parsing of the XML results to get a set of subfields
  880. *
  881. * @param $xml
  882. * The XMl object to read
  883. * @return
  884. * An array of codes and their values
  885. *
  886. * @ingroup tripal_pub
  887. */
  888. function tripal_pub_remote_search_AGL_get_subfield($xml) {
  889. $codes = array();
  890. while ($xml->read()) {
  891. $sub_element = $xml->name;
  892. // when we've reached the end of the datafield element then break out of the while loop
  893. if ($xml->nodeType == XMLReader::END_ELEMENT and $sub_element == 'datafield') {
  894. return $codes;
  895. }
  896. // if inside the subfield element then get the code
  897. if ($xml->nodeType == XMLReader::ELEMENT and $sub_element == 'subfield') {
  898. $code = $xml->getAttribute('code');
  899. $xml->read();
  900. $value = $xml->value;
  901. $codes[$code] = $value;
  902. }
  903. }
  904. return $codes;
  905. }
  906. /**
  907. * Used for parsing of the XML results to get details about an author
  908. *
  909. * @param $xml
  910. * The XML object to read
  911. * @param $ind1
  912. * Indicates how an author record is stored; 0 means given name is first
  913. * 1 means surname is first, 3 means a family name is given
  914. *
  915. * @return
  916. *
  917. *
  918. * @ingroup tripal_pub
  919. */
  920. function tripal_pub_remote_search_AGL_get_author($xml, $ind1) {
  921. $author = array();
  922. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  923. foreach ($codes as $code => $value) {
  924. switch ($code) {
  925. case 'a':
  926. // remove any trailing commas
  927. $value = preg_replace('/,$/', '', $value);
  928. if ($ind1 == 0) { // Given Name is first
  929. $author['Given Name'] = $names[0];
  930. }
  931. if ($ind1 == 1) { // Surname is first
  932. // split the parts of the name using a comma
  933. $names = explode(',', $value);
  934. $author['Surname'] = $names[0];
  935. $author['Given Name'] = '';
  936. unset($names[0]);
  937. foreach($names as $index => $name) {
  938. $author['Given Name'] .= $name . ' ';
  939. }
  940. $first_names = explode(' ', $author['Given Name']);
  941. $author['First Initials'] = '';
  942. foreach ($first_names as $index => $name) {
  943. $author['First Initials'] .= substr($name, 0, 1);
  944. }
  945. }
  946. if ($ind1 == 3) { // A family name
  947. }
  948. break;
  949. }
  950. }
  951. return $author;
  952. }