AGL.inc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. // we will set the default publication type as a journal article. The NAL
  411. // dataset doesn't specify an article type so we'll have to glean the type
  412. // from other information (e.g. series name has 'Proceedings' in it)
  413. $pub['Publication Type'][0] = 'Journal Article';
  414. if (!$pub_xml) {
  415. return $pub;
  416. }
  417. // read the XML and iterate through it.
  418. $xml = new XMLReader();
  419. $xml->xml(trim($pub_xml));
  420. while ($xml->read()) {
  421. $element = $xml->name;
  422. if ($xml->nodeType == XMLReader::ELEMENT and $element == 'controlfield') {
  423. $tag = $xml->getAttribute('tag');
  424. $xml->read();
  425. $value = $xml->value;
  426. switch ($tag) {
  427. case '001': // control number
  428. $pub['Publication Accession'] = $value;
  429. break;
  430. case '003': // control number identifier
  431. break;
  432. case '005': // datea nd time of latest transaction
  433. break;
  434. case '006': // fixed-length data elemetns
  435. break;
  436. case '007': // physical description fixed field
  437. break;
  438. case '008': // fixed length data elements
  439. $month = array(
  440. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar',
  441. '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  442. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep',
  443. '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  444. );
  445. $date0 = substr($value, 0, 6); // date entered on file
  446. $date1 = substr($value, 7, 4); // year of publication
  447. $date2 = substr($value, 11, 4); // month of publication
  448. $place = substr($value, 15, 3);
  449. $lang = substr($value, 35, 3);
  450. if (preg_match('/\d\d\d\d/', $date1)) {
  451. $pub['Year'] = $date1;
  452. $pub['Publication Date'] = $date1;
  453. }
  454. if (preg_match('/\d\d/', $date2)) {
  455. $pub['Publication Date'] = $date1 . " " . $month[substr($date2, 0, 2)] . " " . substr($date2, 3, 2);
  456. }
  457. if (!preg_match('/\s+/', $place)) {
  458. $pub['Published Location'] = $place;
  459. }
  460. if (!preg_match('/\s+/', $lang)) {
  461. $pub['Language Abbr'] = $lang;
  462. }
  463. break;
  464. default: // unhandled tag
  465. break;
  466. }
  467. }
  468. elseif ($xml->nodeType == XMLReader::ELEMENT and $element == 'datafield') {
  469. $tag = $xml->getAttribute('tag');
  470. $ind1 = $xml->getAttribute('ind1');
  471. $ind2 = $xml->getAttribute('ind2');
  472. switch ($tag) {
  473. case '16': // National Bibliographic Agency Control Number
  474. break;
  475. case '35': // System Control Number
  476. $author = array();
  477. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  478. foreach ($codes as $code => $value) {
  479. switch ($code) {
  480. case 'a': // System control number
  481. $pub['Publication Accession'] = $value;
  482. break;
  483. }
  484. }
  485. case '40': // Cataloging Source (NR)
  486. $author = array();
  487. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  488. foreach ($codes as $code => $value) {
  489. switch ($code) {
  490. case 'a': // original cataolging agency
  491. $pub['Publication Database'] = $value;
  492. break;
  493. }
  494. }
  495. break;
  496. case '72': // Subject Category Code
  497. break;
  498. case '100': // main entry-personal name
  499. $author = tripal_pub_remote_search_AGL_get_author($xml, $ind1);
  500. $pub['Author List'][] = $author;
  501. break;
  502. case '110': // main entry-corporate nmae
  503. $author = array();
  504. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  505. foreach ($codes as $code => $value) {
  506. switch ($code) {
  507. case 'a': // Corporate name or jurisdiction name as entry elemen
  508. $author['Collective'] = $value;
  509. break;
  510. case 'b': // Subordinate unit
  511. $author['Collective'] .= ' ' . $value;
  512. break;
  513. }
  514. }
  515. $pub['Author List'][] = $author;
  516. break;
  517. case '111': // main entry-meeting name
  518. break;
  519. case '130': // main entry-uniform title
  520. break;
  521. case '210': // abbreviated title
  522. break;
  523. case '222': // key title
  524. break;
  525. case '240': // uniform title
  526. break;
  527. case '242': // translation of title by cataloging agency
  528. break;
  529. case '243': // collective uniform title
  530. break;
  531. case '245': // title statement
  532. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  533. foreach ($codes as $code => $value) {
  534. switch ($code) {
  535. case 'a':
  536. $pub['Title'] = trim(preg_replace('/\.$/', '', $value));
  537. break;
  538. case 'b':
  539. $pub['Title'] .= ' ' . $value;
  540. break;
  541. case 'h':
  542. $pub['Publication Model'] = $value;
  543. break;
  544. }
  545. }
  546. break;
  547. case '246': // varying form of title
  548. break;
  549. case '247': // former title
  550. break;
  551. case '250': // edition statement
  552. break;
  553. case '254': // musicla presentation statement
  554. break;
  555. case '255': // cartographic mathematical data
  556. break;
  557. case '256': // computer file characteristics
  558. break;
  559. case '257': // country of producing entity
  560. break;
  561. case '258': // philatelic issue data
  562. break;
  563. case '260': // publication, distribution ,etc (imprint)
  564. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  565. foreach ($codes as $code => $value) {
  566. switch ($code) {
  567. case 'a':
  568. $pub['Published Location'] = $value;
  569. break;
  570. case 'b':
  571. $pub['Publisher'] = $value;
  572. break;
  573. case 'c':
  574. $pub['Publication Date'] = $value;
  575. break;
  576. }
  577. }
  578. break;
  579. case '263': // projected publication date
  580. break;
  581. case '264': // production, publication, distribution, manufacture and copyright notice
  582. break;
  583. case '270': // Address
  584. break;
  585. case '300': // Address
  586. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  587. foreach ($codes as $code => $value) {
  588. switch ($code) {
  589. case 'a':
  590. $pages = $value;
  591. $pages = preg_replace('/^p\. /', '', $pages);
  592. $pages = preg_replace('/\.$/', '' , $pages);
  593. if(preg_match('/p$/', $pages)) {
  594. // skip this, it's the number of pages not the page numbers
  595. }
  596. else {
  597. $pub['Pages'] = $pages;
  598. }
  599. break;
  600. }
  601. }
  602. break;
  603. case '500': // series statements
  604. $pub['Notes'] = $value;
  605. break;
  606. case '504': // Bibliography, Etc. Note
  607. break;
  608. case '520': // Summary, etc
  609. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  610. foreach ($codes as $code => $value) {
  611. switch ($code) {
  612. case 'a':
  613. $pub['Abstract'] = $value;
  614. break;
  615. }
  616. }
  617. break;
  618. case '650': // Subject Added Entry-Topical Term
  619. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  620. foreach ($codes as $code => $value) {
  621. switch ($code) {
  622. case 'a':
  623. $pub['Keywords'][] = $value;
  624. break;
  625. }
  626. }
  627. break;
  628. case '653': // Index Term-Uncontrolled
  629. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  630. foreach ($codes as $code => $value) {
  631. switch ($code) {
  632. case 'a':
  633. $pub['Keywords'][] = $value;
  634. break;
  635. }
  636. }
  637. break;
  638. case '700': // Added Entry-Personal Name
  639. $author = tripal_pub_remote_search_AGL_get_author($xml, $ind1);
  640. $pub['Author List'][] = $author;
  641. break;
  642. case '710': // Added Entry-Corporate Name
  643. $author = array();
  644. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  645. foreach ($codes as $code => $value) {
  646. switch ($code) {
  647. case 'a': // Corporate name or jurisdiction name as entry elemen
  648. $author['Collective'] = $value;
  649. break;
  650. case 'b': // Subordinate unit
  651. $author['Collective'] .= ' ' . $value;
  652. break;
  653. }
  654. }
  655. $pub['Author List'][] = $author;
  656. break;
  657. case '773': // host item entry
  658. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  659. foreach ($codes as $code => $value) {
  660. switch ($code) {
  661. case 'a':
  662. if (preg_match('/Proceedings/i', $value)) {
  663. $pub['Series Name'] = preg_replace('/\.$/', '', $value);
  664. $pub['Publication Type'][0] = 'Conference Proceedings';
  665. }
  666. else {
  667. $pub['Journal Name'] = preg_replace('/\.$/', '', $value);
  668. }
  669. break;
  670. case 't':
  671. if (preg_match('/Proceedings/i', $value)) {
  672. $pub['Series Name'] = preg_replace('/\.$/', '', $value);
  673. $pub['Publication Type'][0] = 'Conference Proceedings';
  674. }
  675. $pub['Journal Name'] = preg_replace('/\.$/', '', $value);
  676. break;
  677. case 'g':
  678. $matches = array();
  679. if (preg_match('/^(\d\d\d\d)/', $value, $matches)) {
  680. $pub['Publication Date'] = $matches[1];
  681. }
  682. elseif (preg_match('/(.*?)(\.|\s+)\s*(\d+),\s(\d\d\d\d)/', $value, $matches)) {
  683. $year = $matches[4];
  684. $month = $matches[1];
  685. $day = $matches[3];
  686. $pub['Publication Date'] = "$year $month $day";
  687. }
  688. elseif (preg_match('/\((.*?)(\.|\s+)(\d\d\d\d)\)/', $value, $matches)) {
  689. $year = $matches[3];
  690. $month = $matches[1];
  691. $pub['Publication Date'] = "$year $month";
  692. }
  693. elseif (preg_match('/^(.*?) (\d\d\d\d)/', $value, $matches)) {
  694. $year = $matches[2];
  695. $month = $matches[1];
  696. $pub['Publication Date'] = "$year $month";
  697. }
  698. if (preg_match('/v\. (.*?)(,|\s+)/', $value, $matches)) {
  699. $pub['Volume'] = $matches[1];
  700. }
  701. if (preg_match('/v\. (.*?)(,|\s+)\((.*?)\)/', $value, $matches)) {
  702. $pub['Volume'] = $matches[1];
  703. $pub['Issue'] = $matches[3];
  704. }
  705. if (preg_match('/no\. (.*?)(\s|$)/', $value, $matches)) {
  706. $pub['Issue'] = $matches[1];
  707. }
  708. break;
  709. case 'p':
  710. $pub['Journal Abbreviation'] = $value;
  711. break;
  712. case 'z':
  713. $pub['ISBN'] = $value;
  714. break;
  715. }
  716. }
  717. break;
  718. case '852': // Location (Where is the publication held)
  719. break;
  720. case '856': // Electronic Location and Access
  721. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  722. foreach ($codes as $code => $value) {
  723. switch ($code) {
  724. case 'u':
  725. $pub['URL'] = $value;
  726. break;
  727. }
  728. }
  729. break;
  730. default:
  731. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  732. $unhandled[$tag][] = $codes;
  733. break;
  734. }
  735. }
  736. }
  737. //dpm($unhandled);
  738. // build the Dbxref
  739. if ($pub['Publication Database'] != 'AGL') {
  740. }
  741. if ($pub['Publication Accession'] and $pub['Publication Database']) {
  742. $pub['Publication Dbxref'] = $pub['Publication Database'] . ":" . $pub['Publication Accession'];
  743. unset($pub['Publication Accession']);
  744. unset($pub['Publication Database']);
  745. }
  746. // build the full authors list
  747. if (is_array($pub['Author List'])) {
  748. foreach ($pub['Author List'] as $author) {
  749. if ($author['valid'] == 'N') {
  750. // skip non-valid entries. A non-valid entry should have
  751. // a corresponding corrected entry so we can saftely skip it.
  752. continue;
  753. }
  754. if ($author['Collective']) {
  755. $authors .= $author['Collective'] . ', ';
  756. }
  757. else {
  758. $authors .= $author['Surname'] . ' ' . $author['First Initials'] . ', ';
  759. }
  760. }
  761. $authors = substr($authors, 0, -2);
  762. $pub['Authors'] = $authors;
  763. }
  764. else {
  765. $pub['Authors'] = $pub['Author List'];
  766. }
  767. // build the citation
  768. $pub['Citation'] = tripal_pub_create_citation($pub);
  769. $pub['raw'] = $pub_xml;
  770. return $pub;
  771. }
  772. /*
  773. *
  774. *
  775. */
  776. function tripal_pub_remote_search_AGL_get_subfield($xml) {
  777. $codes = array();
  778. while ($xml->read()) {
  779. $sub_element = $xml->name;
  780. // when we've reached the end of the datafield element then break out of the while loop
  781. if ($xml->nodeType == XMLReader::END_ELEMENT and $sub_element == 'datafield') {
  782. return $codes;
  783. }
  784. // if inside the subfield element then get the code
  785. if ($xml->nodeType == XMLReader::ELEMENT and $sub_element == 'subfield') {
  786. $code = $xml->getAttribute('code');
  787. $xml->read();
  788. $value = $xml->value;
  789. $codes[$code] = $value;
  790. }
  791. }
  792. return $codes;
  793. }
  794. /*
  795. *
  796. *
  797. */
  798. function tripal_pub_remote_search_AGL_get_author($xml, $ind1) {
  799. $author = array();
  800. $codes = tripal_pub_remote_search_AGL_get_subfield($xml);
  801. foreach ($codes as $code => $value) {
  802. switch ($code) {
  803. case 'a':
  804. // remove any trailing commas
  805. $value = preg_replace('/,$/', '', $value);
  806. if ($ind1 == 0) { // Given Name is first
  807. $author['Given Name'] = $names[0];
  808. }
  809. if ($ind1 == 1) { // Surname is first
  810. // split the parts of the name using a comma
  811. $names = explode(',', $value);
  812. $author['Surname'] = $names[0];
  813. $author['Given Name'] = '';
  814. unset($names[0]);
  815. foreach($names as $index => $name) {
  816. $author['Given Name'] .= $name . ' ';
  817. }
  818. $first_names = explode(' ', $author['Given Name']);
  819. $author['First Initials'] = '';
  820. foreach ($first_names as $index => $name) {
  821. $author['First Initials'] .= substr($name, 0, 1);
  822. }
  823. }
  824. if ($ind1 == 3) { // A family name
  825. }
  826. break;
  827. }
  828. }
  829. return $author;
  830. }