tripal_pub.AGL.inc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. // the XML is encoded twice, one to convert UTF-8 characters to
  429. // unicode HTML entities and second with URL encoding. We must
  430. // reverse the encoding
  431. //$pub_xml = urldecode($pub_xml);
  432. //$pub_xml = mb_convert_encoding($pub_xml, 'UTF-8', 'HTML-ENTITIES');
  433. // parse the pub XML
  434. $pub = tripal_pub_AGL_parse_pubxml($pub_xml);
  435. $pubs[] = $pub;
  436. }
  437. return array(
  438. 'total_records' => $total_records,
  439. 'search_str' => $search_str,
  440. 'pubs' => $pubs,
  441. );
  442. }
  443. /**
  444. * Retreives the total number of publications that match the search string.
  445. *
  446. * @param $yazc
  447. * The YAZC connection object.
  448. * @param $search_str
  449. * The search string to use for searching.
  450. *
  451. * @return
  452. * a count of the total number of publications that match the search string
  453. *
  454. * @ingroup tripal_pub
  455. */
  456. function tripal_pub_AGL_count($yazc, $search_str) {
  457. //yaz_sort($yazc, "1=31 id"); // sort by publication date descending
  458. if (!yaz_search($yazc, "rpn", $search_str)){
  459. $error_no = yaz_errno($yazc);
  460. $error_msg = yaz_error($yazc);
  461. $additional = yaz_addinfo($yazc);
  462. if ($additional != $error_msg) {
  463. $error_msg .= " $additional";
  464. }
  465. drupal_set_message("ERROR preparing search at AGL: ($error_no) $error_msg", "error");
  466. watchdog('tpub_import', "ERROR preparing search at AGL: (%error_no) %error_msg",
  467. array('%error_no' => $error_no, '%error_msg' => $error_msg), WATCHDOG_ERROR);
  468. return 0;
  469. }
  470. if (!yaz_wait()) {
  471. $error_no = yaz_errno($yazc);
  472. $error_msg = yaz_error($yazc);
  473. $additional = yaz_addinfo($yazc);
  474. if ($additional != $error_msg) {
  475. $error_msg .= " $additional";
  476. }
  477. drupal_set_message("ERROR waiting on search at AGL: ($error_no) $error_msg", "error");
  478. watchdog('tpub_import', "ERROR waiting on search at AGL: (%error_no) %error_msg",
  479. array('%error_no' => $error_no, '%error_msg' => $error_msg), WATCHDOG_ERROR);
  480. return 0;
  481. }
  482. // get the total number of results from the serach
  483. $count = yaz_hits($yazc);
  484. return $count;
  485. }
  486. /**
  487. * Parse publication XML for a single publication
  488. *
  489. * Description of XML format:
  490. * http://www.loc.gov/marc/bibliographic/bdsummary.html
  491. *
  492. * @param $pub_xml
  493. * A string containing the XML for a single publications
  494. *
  495. * @return
  496. * An array containing the details of the publication
  497. *
  498. * @ingroup tripal_pub
  499. */
  500. function tripal_pub_AGL_parse_pubxml($pub_xml) {
  501. $pub = array();
  502. // we will set the default publication type as a journal article. The NAL
  503. // dataset doesn't specify an article type so we'll have to glean the type
  504. // from other information (e.g. series name has 'Proceedings' in it)
  505. $pub['Publication Type'][0] = 'Journal Article';
  506. if (!$pub_xml) {
  507. return $pub;
  508. }
  509. // read the XML and iterate through it.
  510. $xml = new XMLReader();
  511. $xml->xml(trim($pub_xml));
  512. while ($xml->read()) {
  513. $element = $xml->name;
  514. if ($xml->nodeType == XMLReader::ELEMENT and $element == 'controlfield') {
  515. $tag = $xml->getAttribute('tag');
  516. $xml->read();
  517. $value = $xml->value;
  518. switch ($tag) {
  519. case '001': // control number
  520. $pub['Publication Accession'] = $value;
  521. break;
  522. case '003': // control number identifier
  523. break;
  524. case '005': // datea nd time of latest transaction
  525. break;
  526. case '006': // fixed-length data elemetns
  527. break;
  528. case '007': // physical description fixed field
  529. break;
  530. case '008': // fixed length data elements
  531. $month = array(
  532. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar',
  533. '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  534. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep',
  535. '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  536. );
  537. $date0 = substr($value, 0, 6); // date entered on file
  538. $date1 = substr($value, 7, 4); // year of publication
  539. $date2 = substr($value, 11, 4); // month of publication
  540. $place = substr($value, 15, 3);
  541. $lang = substr($value, 35, 3);
  542. if (preg_match('/\d\d\d\d/', $date1)) {
  543. $pub['Year'] = $date1;
  544. $pub['Publication Date'] = $date1;
  545. }
  546. if (preg_match('/\d\d/', $date2)) {
  547. $pub['Publication Date'] = $date1 . " " . $month[substr($date2, 0, 2)] . " " . substr($date2, 3, 2);
  548. }
  549. if (!preg_match('/\s+/', $place)) {
  550. $pub['Published Location'] = $place;
  551. }
  552. if (!preg_match('/\s+/', $lang)) {
  553. $pub['Language Abbr'] = $lang;
  554. }
  555. break;
  556. default: // unhandled tag
  557. break;
  558. }
  559. }
  560. elseif ($xml->nodeType == XMLReader::ELEMENT and $element == 'datafield') {
  561. $tag = $xml->getAttribute('tag');
  562. $ind1 = $xml->getAttribute('ind1');
  563. $ind2 = $xml->getAttribute('ind2');
  564. switch ($tag) {
  565. case '16': // National Bibliographic Agency Control Number
  566. break;
  567. case '35': // System Control Number
  568. $author = array();
  569. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  570. foreach ($codes as $code => $value) {
  571. switch ($code) {
  572. case 'a': // System control number
  573. $pub['Publication Accession'] = $value;
  574. break;
  575. }
  576. }
  577. case '40': // Cataloging Source (NR)
  578. $author = array();
  579. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  580. foreach ($codes as $code => $value) {
  581. switch ($code) {
  582. case 'a': // original cataolging agency
  583. $pub['Publication Database'] = $value;
  584. break;
  585. }
  586. }
  587. break;
  588. case '72': // Subject Category Code
  589. break;
  590. case '100': // main entry-personal name
  591. $author = tripal_pub_remote_search_AGL_get_author($xml, $ind1);
  592. $pub['Author List'][] = $author;
  593. break;
  594. case '110': // main entry-corporate nmae
  595. $author = array();
  596. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  597. foreach ($codes as $code => $value) {
  598. switch ($code) {
  599. case 'a': // Corporate name or jurisdiction name as entry elemen
  600. $author['Collective'] = $value;
  601. break;
  602. case 'b': // Subordinate unit
  603. $author['Collective'] .= ' ' . $value;
  604. break;
  605. }
  606. }
  607. $pub['Author List'][] = $author;
  608. break;
  609. case '111': // main entry-meeting name
  610. break;
  611. case '130': // main entry-uniform title
  612. break;
  613. case '210': // abbreviated title
  614. break;
  615. case '222': // key title
  616. break;
  617. case '240': // uniform title
  618. break;
  619. case '242': // translation of title by cataloging agency
  620. break;
  621. case '243': // collective uniform title
  622. break;
  623. case '245': // title statement
  624. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  625. foreach ($codes as $code => $value) {
  626. switch ($code) {
  627. case 'a':
  628. $pub['Title'] = trim(preg_replace('/\.$/', '', $value));
  629. break;
  630. case 'b':
  631. $pub['Title'] .= ' ' . $value;
  632. break;
  633. case 'h':
  634. $pub['Publication Model'] = $value;
  635. break;
  636. }
  637. }
  638. break;
  639. case '246': // varying form of title
  640. break;
  641. case '247': // former title
  642. break;
  643. case '250': // edition statement
  644. break;
  645. case '254': // musicla presentation statement
  646. break;
  647. case '255': // cartographic mathematical data
  648. break;
  649. case '256': // computer file characteristics
  650. break;
  651. case '257': // country of producing entity
  652. break;
  653. case '258': // philatelic issue data
  654. break;
  655. case '260': // publication, distribution ,etc (imprint)
  656. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  657. foreach ($codes as $code => $value) {
  658. switch ($code) {
  659. case 'a':
  660. $pub['Published Location'] = $value;
  661. break;
  662. case 'b':
  663. $pub['Publisher'] = $value;
  664. break;
  665. case 'c':
  666. $pub['Publication Date'] = $value;
  667. break;
  668. }
  669. }
  670. break;
  671. case '263': // projected publication date
  672. break;
  673. case '264': // production, publication, distribution, manufacture and copyright notice
  674. break;
  675. case '270': // Address
  676. break;
  677. case '300': // Address
  678. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  679. foreach ($codes as $code => $value) {
  680. switch ($code) {
  681. case 'a':
  682. $pages = $value;
  683. $pages = preg_replace('/^p\. /', '', $pages);
  684. $pages = preg_replace('/\.$/', '' , $pages);
  685. if(preg_match('/p$/', $pages)) {
  686. // skip this, it's the number of pages not the page numbers
  687. }
  688. else {
  689. $pub['Pages'] = $pages;
  690. }
  691. break;
  692. }
  693. }
  694. break;
  695. case '500': // series statements
  696. $pub['Notes'] = $value;
  697. break;
  698. case '504': // Bibliography, Etc. Note
  699. break;
  700. case '520': // Summary, etc
  701. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  702. foreach ($codes as $code => $value) {
  703. switch ($code) {
  704. case 'a':
  705. $pub['Abstract'] = $value;
  706. break;
  707. }
  708. }
  709. break;
  710. case '650': // Subject Added Entry-Topical Term
  711. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  712. foreach ($codes as $code => $value) {
  713. switch ($code) {
  714. case 'a':
  715. $pub['Keywords'][] = $value;
  716. break;
  717. }
  718. }
  719. break;
  720. case '653': // Index Term-Uncontrolled
  721. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  722. foreach ($codes as $code => $value) {
  723. switch ($code) {
  724. case 'a':
  725. $pub['Keywords'][] = $value;
  726. break;
  727. }
  728. }
  729. break;
  730. case '700': // Added Entry-Personal Name
  731. $author = tripal_pub_remote_search_AGL_get_author($xml, $ind1);
  732. $pub['Author List'][] = $author;
  733. break;
  734. case '710': // Added Entry-Corporate Name
  735. $author = array();
  736. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  737. foreach ($codes as $code => $value) {
  738. switch ($code) {
  739. case 'a': // Corporate name or jurisdiction name as entry elemen
  740. $author['Collective'] = $value;
  741. break;
  742. case 'b': // Subordinate unit
  743. $author['Collective'] .= ' ' . $value;
  744. break;
  745. }
  746. }
  747. $pub['Author List'][] = $author;
  748. break;
  749. case '773': // host item entry
  750. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  751. foreach ($codes as $code => $value) {
  752. switch ($code) {
  753. case 'a':
  754. if (preg_match('/Proceedings/i', $value)) {
  755. $pub['Series Name'] = preg_replace('/\.$/', '', $value);
  756. $pub['Publication Type'][0] = 'Conference Proceedings';
  757. }
  758. else {
  759. $pub['Journal Name'] = preg_replace('/\.$/', '', $value);
  760. }
  761. break;
  762. case 't':
  763. if (preg_match('/Proceedings/i', $value)) {
  764. $pub['Series Name'] = preg_replace('/\.$/', '', $value);
  765. $pub['Publication Type'][0] = 'Conference Proceedings';
  766. }
  767. $pub['Journal Name'] = preg_replace('/\.$/', '', $value);
  768. break;
  769. case 'g':
  770. $matches = array();
  771. if (preg_match('/^(\d\d\d\d)/', $value, $matches)) {
  772. $pub['Publication Date'] = $matches[1];
  773. }
  774. elseif (preg_match('/(.*?)(\.|\s+)\s*(\d+),\s(\d\d\d\d)/', $value, $matches)) {
  775. $year = $matches[4];
  776. $month = $matches[1];
  777. $day = $matches[3];
  778. $pub['Publication Date'] = "$year $month $day";
  779. }
  780. elseif (preg_match('/\((.*?)(\.|\s+)(\d\d\d\d)\)/', $value, $matches)) {
  781. $year = $matches[3];
  782. $month = $matches[1];
  783. $pub['Publication Date'] = "$year $month";
  784. }
  785. elseif (preg_match('/^(.*?) (\d\d\d\d)/', $value, $matches)) {
  786. $year = $matches[2];
  787. $month = $matches[1];
  788. $pub['Publication Date'] = "$year $month";
  789. }
  790. if (preg_match('/v\. (.*?)(,|\s+)/', $value, $matches)) {
  791. $pub['Volume'] = $matches[1];
  792. }
  793. if (preg_match('/v\. (.*?)(,|\s+)\((.*?)\)/', $value, $matches)) {
  794. $pub['Volume'] = $matches[1];
  795. $pub['Issue'] = $matches[3];
  796. }
  797. if (preg_match('/no\. (.*?)(\s|$)/', $value, $matches)) {
  798. $pub['Issue'] = $matches[1];
  799. }
  800. break;
  801. case 'p':
  802. $pub['Journal Abbreviation'] = $value;
  803. break;
  804. case 'z':
  805. $pub['ISBN'] = $value;
  806. break;
  807. }
  808. }
  809. break;
  810. case '852': // Location (Where is the publication held)
  811. break;
  812. case '856': // Electronic Location and Access
  813. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  814. foreach ($codes as $code => $value) {
  815. switch ($code) {
  816. case 'u':
  817. $pub['URL'] = $value;
  818. break;
  819. }
  820. }
  821. break;
  822. default:
  823. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  824. $unhandled[$tag][] = $codes;
  825. break;
  826. }
  827. }
  828. }
  829. //dpm($unhandled);
  830. // build the Dbxref
  831. if ($pub['Publication Database'] != 'AGL') {
  832. }
  833. if ($pub['Publication Accession'] and $pub['Publication Database']) {
  834. $pub['Publication Dbxref'] = $pub['Publication Database'] . ":" . $pub['Publication Accession'];
  835. unset($pub['Publication Accession']);
  836. unset($pub['Publication Database']);
  837. }
  838. // build the full authors list
  839. if (is_array($pub['Author List'])) {
  840. $authors = '';
  841. foreach ($pub['Author List'] as $author) {
  842. if (array_key_exists('valid', $author) and $author['valid'] == 'N') {
  843. // skip non-valid entries. A non-valid entry should have
  844. // a corresponding corrected entry so we can saftely skip it.
  845. continue;
  846. }
  847. if (array_key_exists('Collective', $author)) {
  848. $authors .= $author['Collective'] . ', ';
  849. }
  850. else {
  851. if (array_key_exists('Surname', $author)) {
  852. $authors .= $author['Surname'];
  853. if(array_key_exists('First Initials', $author)) {
  854. $authors .= ' ' . $author['First Initials'];
  855. }
  856. $authors .= ', ';
  857. }
  858. }
  859. }
  860. $authors = substr($authors, 0, -2);
  861. $pub['Authors'] = $authors;
  862. }
  863. else {
  864. $pub['Authors'] = $pub['Author List'];
  865. }
  866. // build the citation
  867. $pub['Citation'] = tripal_pub_create_citation($pub);
  868. $pub['raw'] = $pub_xml;
  869. return $pub;
  870. }
  871. /**
  872. * Used for parsing of the XML results to get a set of subfields
  873. *
  874. * @param $xml
  875. * The XMl object to read
  876. * @return
  877. * An array of codes and their values
  878. *
  879. * @ingroup tripal_pub
  880. */
  881. function tripal_pub_remote_search_AGL_get_subfield($xml) {
  882. $codes = array();
  883. while ($xml->read()) {
  884. $sub_element = $xml->name;
  885. // when we've reached the end of the datafield element then break out of the while loop
  886. if ($xml->nodeType == XMLReader::END_ELEMENT and $sub_element == 'datafield') {
  887. return $codes;
  888. }
  889. // if inside the subfield element then get the code
  890. if ($xml->nodeType == XMLReader::ELEMENT and $sub_element == 'subfield') {
  891. $code = $xml->getAttribute('code');
  892. $xml->read();
  893. $value = $xml->value;
  894. $codes[$code] = $value;
  895. }
  896. }
  897. return $codes;
  898. }
  899. /**
  900. * Used for parsing of the XML results to get details about an author
  901. *
  902. * @param $xml
  903. * The XML object to read
  904. * @param $ind1
  905. * Indicates how an author record is stored; 0 means given name is first
  906. * 1 means surname is first, 3 means a family name is given
  907. *
  908. * @return
  909. *
  910. *
  911. * @ingroup tripal_pub
  912. */
  913. function tripal_pub_remote_search_AGL_get_author($xml, $ind1) {
  914. $author = array();
  915. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  916. foreach ($codes as $code => $value) {
  917. switch ($code) {
  918. case 'a':
  919. // remove any trailing commas
  920. $value = preg_replace('/,$/', '', $value);
  921. if ($ind1 == 0) { // Given Name is first
  922. $author['Given Name'] = $names[0];
  923. }
  924. if ($ind1 == 1) { // Surname is first
  925. // split the parts of the name using a comma
  926. $names = explode(',', $value);
  927. $author['Surname'] = $names[0];
  928. $author['Given Name'] = '';
  929. unset($names[0]);
  930. foreach($names as $index => $name) {
  931. $author['Given Name'] .= $name . ' ';
  932. }
  933. $first_names = explode(' ', $author['Given Name']);
  934. $author['First Initials'] = '';
  935. foreach ($first_names as $index => $name) {
  936. $author['First Initials'] .= substr($name, 0, 1);
  937. }
  938. }
  939. if ($ind1 == 3) { // A family name
  940. }
  941. break;
  942. }
  943. }
  944. return $author;
  945. }