AGL.inc 29 KB

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