tripal_pub.AGL.inc 32 KB

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