AGL.inc 28 KB

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