tripal_pub.api.inc 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. <?php
  2. /**
  3. * @file
  4. * The Tripal Pub API
  5. *
  6. * @defgroup tripal_pub_api Publication Module API
  7. * @ingroup tripal_api
  8. */
  9. /*
  10. * Retrieves a list of publications as an associated array where
  11. * keys correspond directly with Tripal Pub CV terms.
  12. *
  13. * @param remote_db
  14. * The name of the remote publication database to query. These names should
  15. * match the name of the databases in the Chado 'db' table. Currently
  16. * supported databass include
  17. * 'PMID': PubMed
  18. *
  19. * @param search_array
  20. * An associate array containing the search criteria. The following key
  21. * are expected
  22. * 'remote_db': Specifies the name of the remote publication database
  23. * 'num_criteria': Specifies the number of criteria present in the search array
  24. * 'days': The number of days to include in the search starting from today
  25. * 'criteria': An associate array containing the search critiera. There should
  26. * be no less than 'num_criteria' elements in this array.
  27. *
  28. * The following keys are expected in the 'criteria' array
  29. * 'search_terms': A list of terms to search on, separated by spaces.
  30. * 'scope': The fields to search in the remote database. Valid values
  31. * include: 'title', 'abstract', 'author' and 'any'
  32. * 'operation': The logical operation to use for this criteria. Valid
  33. * values include: 'AND', 'OR' and 'NOT'.
  34. * @param $num_to_retrieve
  35. * The number of records to retrieve. In cases with large numbers of
  36. * records to retrieve, the remote database may limit the size of each
  37. * retrieval.
  38. * @param $page
  39. * Optional. If this function is called where the
  40. * page for the pager cannot be set using the $_GET variable, use this
  41. * argument to specify the page to retrieve.
  42. *
  43. * @return
  44. * Returns an array of pubs where each element is
  45. * an associative array where the keys are Tripal Pub CV terms.
  46. *
  47. * @ingroup tripal_pub_api
  48. */
  49. function tripal_pub_get_remote_search_results($remote_db, $search_array, $num_to_retrieve, $page = 0) {
  50. // now call the callback function to get the results
  51. $callback = "tripal_pub_remote_search_$remote_db";
  52. $pubs = array(
  53. 'total_records' => 0,
  54. 'search_str' => '',
  55. 'pubs' => array(),
  56. );
  57. if (function_exists($callback)) {
  58. $pubs = call_user_func($callback, $search_array, $num_to_retrieve, $page);
  59. }
  60. return $pubs;
  61. }
  62. /*
  63. * @ingroup tripal_pub_api
  64. */
  65. function tripal_pub_get_raw_data($dbxref) {
  66. if(preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
  67. $remote_db = $matches[1];
  68. $accession = $matches[2];
  69. // check that the database is supported
  70. $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
  71. if(!in_array($remote_db, $supported_dbs)) {
  72. return "Unsupported database: $dbxref";
  73. }
  74. $search = array(
  75. 'num_criteria' => 1,
  76. 'remote_db' => $remote_db,
  77. 'criteria' => array(
  78. '1' => array(
  79. 'search_terms' => "$remote_db:$accession",
  80. 'scope' => 'id',
  81. 'operation' => '',
  82. 'is_phrase' => 0,
  83. ),
  84. ),
  85. );
  86. $pubs = tripal_pub_get_remote_search_results($remote_db, $search, 1, 0);
  87. return '<textarea cols=80 rows=20>' . $pubs[0]['raw'] . '</textarea>';
  88. }
  89. return 'Invalid DB xref';
  90. }
  91. /*
  92. * @ingroup tripal_pub_api
  93. */
  94. function tripal_pub_update_publications($do_contact = FALSE, $dbxref = NULL, $db = NULL) {
  95. // get a persistent connection
  96. $connection = tripal_db_persistent_chado();
  97. if (!$connection) {
  98. print "A persistant connection was not obtained. Loading will be slow\n";
  99. }
  100. // if we cannot get a connection then let the user know the loading will be slow
  101. tripal_db_start_transaction();
  102. if ($connection) {
  103. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  104. "If the load fails or is terminated prematurely then the entire set of \n" .
  105. "insertions/updates is rolled back and will not be found in the database\n\n";
  106. }
  107. // get a list of all publications by their Dbxrefs that have supported databases
  108. $sql = "
  109. SELECT DB.name as db_name, DBX.accession
  110. FROM pub P
  111. INNER JOIN pub_dbxref PDBX ON P.pub_id = PDBX.pub_id
  112. INNER JOIN dbxref DBX ON DBX.dbxref_id = PDBX.dbxref_id
  113. INNER JOIN db DB ON DB.db_id = DBX.db_id
  114. ";
  115. $args = array();
  116. if ($dbxref and preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
  117. $dbname = $matches[1];
  118. $accession = $matches[2];
  119. $sql .= "WHERE DBX.accession = :accession and DB.name = :dbname ";
  120. $args[':accession'] = $accession;
  121. $args[':dbname'] = $dbname;
  122. }
  123. elseif ($db) {
  124. $sql .= " WHERE DB.name = :dbname ";
  125. $args[':dbname'] = $db;
  126. }
  127. $sql .= "ORDER BY DB.name, P.pub_id";
  128. $results = chado_query($sql, $args);
  129. $num_to_retrieve = 100;
  130. $i = 0; // count the number of IDs. When we hit $num_to_retrieve we'll do the query
  131. $curr_db = ''; // keeps track of the current current database
  132. $ids = array(); // the list of IDs for the database
  133. $search = array(); // the search array passed to the search function
  134. // iterate through the pub IDs
  135. while ($pub = $results->fetchObject()) {
  136. $accession = $pub->accession;
  137. $remote_db = $pub->db_name;
  138. // here we need to only update publications for databases we support
  139. $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
  140. if(!in_array($remote_db, $supported_dbs)) {
  141. continue;
  142. }
  143. $search = array(
  144. 'num_criteria' => 1,
  145. 'remote_db' => $remote_db,
  146. 'criteria' => array(
  147. '1' => array(
  148. 'search_terms' => "$remote_db:$accession",
  149. 'scope' => 'id',
  150. 'operation' => '',
  151. 'is_phrase' => 0,
  152. ),
  153. ),
  154. );
  155. $pubs = tripal_pub_get_remote_search_results($remote_db, $search, 1, 0);
  156. tripal_pub_add_publications($pubs, $do_contact, TRUE);
  157. $i++;
  158. }
  159. // transaction is complete
  160. tripal_db_commit_transaction();
  161. print "Transaction Complete\n";
  162. // sync the newly added publications with Drupal
  163. print "Syncing publications with Drupal...\n";
  164. tripal_pub_sync_pubs();
  165. // if the caller wants to create contacts then we should sync them
  166. if ($do_contact) {
  167. print "Syncing contacts with Drupal...\n";
  168. tripal_contact_sync_contacts();
  169. }
  170. print "Done.\n";
  171. }
  172. /*
  173. * @ingroup tripal_pub_api
  174. */
  175. function tripal_pub_import_publications($report_email = FALSE, $do_update = FALSE) {
  176. $num_to_retrieve = 100;
  177. $page = 0;
  178. // start the transaction
  179. $transaction = db_transaction();
  180. try {
  181. // get all of the loaders
  182. $args = array();
  183. $sql = "SELECT * FROM {tripal_pub_import} WHERE disabled = 0 ";
  184. $results = db_query($sql, $args);
  185. $do_contact = FALSE;
  186. $reports = array();
  187. foreach ($results as $import) {
  188. $page = 0;
  189. print "Importing: " . $import->name . "\n";
  190. // keep track if any of the importers want to create contacts from authors
  191. if ($import->do_contact == 1) {
  192. $do_contact = TRUE;
  193. }
  194. $criteria = unserialize($import->criteria);
  195. $remote_db = $criteria['remote_db'];
  196. do {
  197. // retrieve the pubs for this page. We'll retreive 100 at a time
  198. $results = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $page);
  199. $pubs = $results['pubs'];
  200. $reports[$import->name] = tripal_pub_add_publications($pubs, $import->do_contact, $do_update);
  201. $page++;
  202. }
  203. // continue looping until we have a $pubs array that does not have
  204. // our requested numer of records. This means we've hit the end
  205. while (count($pubs) == $num_to_retrieve);
  206. }
  207. }
  208. catch (Exception $e) {
  209. print "\n"; // make sure we start errors on new line
  210. watchdog_exception('T_pub_import', $e);
  211. $transaction->rollback();
  212. print "FAILED: Rolling back database changes...\n";
  213. return;
  214. }
  215. print "Transaction Complete\n";
  216. // sync the newly added publications with Drupal. If the user
  217. // requested a report then we don't want to print any syncing information
  218. // so pass 'FALSE' to the sync call
  219. print "Syncing publications with Drupal...\n";
  220. tripal_pub_sync_pubs();
  221. // iterate through each of the reports and generate a final report with HTML links
  222. $HTML_report = '';
  223. if ($report_email) {
  224. $HTML_report .= "<html>";
  225. global $base_url;
  226. foreach ($reports as $importer => $report) {
  227. $total = count($report['inserted']);
  228. $HTML_report .= "<b>$total new publications from importer: $importer</b><br><ol>\n";
  229. foreach ($report['inserted'] as $pub) {
  230. $item = $pub['Title'];
  231. if (array_key_exists('pub_id', $pub)) {
  232. $item = l($pub['Title'], "$base_url/pub/" . $pub['pub_id']);
  233. }
  234. $HTML_report .= "<li>$item</li>\n";
  235. }
  236. $HTML_report .= "</ol>\n";
  237. }
  238. $HTML_report .= "</html>";
  239. $site_email = variable_get('site_mail', '');
  240. $params = array(
  241. 'message' => $HTML_report
  242. );
  243. drupal_mail('tripal_pub', 'import_report', $report_email, language_default(), $params, $site_email, TRUE);
  244. }
  245. // if any of the importers wanted to create contacts from the authors then sync them
  246. if($do_contact) {
  247. print "Syncing contacts with Drupal...\n";
  248. tripal_contact_sync_contacts();
  249. }
  250. print "Done.\n";
  251. }
  252. /*
  253. * @ingroup tripal_pub_api
  254. */
  255. function tripal_pub_import_by_dbxref($pub_dbxref, $do_contact = FALSE, $do_update) {
  256. $num_to_retrieve = 1;
  257. $pager_id = 0;
  258. $page = 0;
  259. $num_pubs = 0;
  260. // get a persistent connection
  261. $connection = tripal_db_persistent_chado();
  262. if (!$connection) {
  263. print "A persistant connection was not obtained. Loading will be slow\n";
  264. }
  265. // if we cannot get a connection then let the user know the loading will be slow
  266. tripal_db_start_transaction();
  267. if ($connection) {
  268. print "\nNOTE: Loading of the publication is performed using a database transaction. \n" .
  269. "If the load fails or is terminated prematurely then the entire set of \n" .
  270. "insertions/updates is rolled back and will not be found in the database\n\n";
  271. }
  272. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  273. $dbname = $matches[1];
  274. $accession = $matches[2];
  275. $criteria = array(
  276. 'num_criteria' => 1,
  277. 'remote_db' => $dbname,
  278. 'criteria' => array(
  279. '1' => array(
  280. 'search_terms' => "$dbname:$accession",
  281. 'scope' => 'id',
  282. 'operation' => '',
  283. 'is_phrase' => 0,
  284. ),
  285. ),
  286. );
  287. $remote_db = $criteria['remote_db'];
  288. $pubs = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $page);
  289. $pub_id = tripal_pub_add_publications($pubs, $do_contact, $do_update);
  290. }
  291. // transaction is complete
  292. tripal_db_commit_transaction();
  293. print "Transaction Complete\n";
  294. // sync the newly added publications with Drupal
  295. print "Syncing publications with Drupal...\n";
  296. tripal_pub_sync_pubs();
  297. // if any of the importers wanted to create contacts from the authors then sync them
  298. if($do_contact) {
  299. print "Syncing contacts with Drupal...\n";
  300. tripal_contact_sync_contacts();
  301. }
  302. print "Done.\n";
  303. }
  304. /*
  305. *
  306. */
  307. function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE) {
  308. $report = array();
  309. $report['error'] = 0;
  310. $report['inserted'] = array();
  311. $report['skipped'] = array();
  312. $total_pubs = count($pubs);
  313. // iterate through the publications and add each one
  314. $i = 1;
  315. foreach ($pubs as $pub) {
  316. $memory = number_format(memory_get_usage()) . " bytes";
  317. print "Processing $i of $total_pubs. Memory usage: $memory.\r";
  318. // add the publication to Chado
  319. $action = '';
  320. $pub_id = tripal_pub_add_publication($pub, $action, $do_contact, $update);
  321. if ($pub_id){
  322. // add the publication cross reference (e.g. to PubMed)
  323. if ($pub_id and $pub['Publication Dbxref']) {
  324. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, $pub['Publication Dbxref']);
  325. }
  326. $pub['pub_id'] = $pub_id;
  327. }
  328. switch ($action) {
  329. case 'error':
  330. $report['error']++;
  331. break;
  332. case 'inserted':
  333. $report['inserted'][] = $pub;
  334. break;
  335. case 'updated':
  336. $report['updated'][] = $pub;
  337. break;
  338. case 'skipped':
  339. $report['skipped'][] = $pub;
  340. break;
  341. }
  342. $i++;
  343. }
  344. print "\n";
  345. return $report;
  346. }
  347. /*
  348. *
  349. */
  350. function tripal_pub_add_pub_dbxref($pub_id, $pub_dbxref) {
  351. // break apart the dbxref
  352. $dbname = '';
  353. $accession = '';
  354. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  355. $dbname = $matches[1];
  356. $accession = $matches[2];
  357. }
  358. else {
  359. return FALSE;
  360. }
  361. // check to see if the pub_dbxref record already exist
  362. $values = array(
  363. 'dbxref_id' => array(
  364. 'accession' => $accession,
  365. 'db_id' => array(
  366. 'name' => $dbname,
  367. ),
  368. ),
  369. 'pub_id' => $pub_id,
  370. );
  371. $options = array('statement_name' => 'sel_pubdbxref_dbpu');
  372. $results = tripal_core_chado_select('pub_dbxref', array('*'), $values, $options);
  373. // if the pub_dbxref record exist then we don't need to re-add it.
  374. if(count($results) > 0) {
  375. return $results[0];
  376. }
  377. // make sure our database already exists
  378. $db = tripal_db_add_db($dbname);
  379. // get the database cross-reference
  380. $dbxvalues = array(
  381. 'accession' => $accession,
  382. 'db_id' => $db->db_id,
  383. );
  384. $dbxoptions = array('statement_name' => 'sel_dbxref_acdb');
  385. $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $dbxvalues, $dbxoptions);
  386. // if the accession doesn't exist then add it
  387. if(count($results) == 0){
  388. $dbxref = tripal_db_add_dbxref($db->db_id, $accession);
  389. }
  390. else {
  391. $dbxref = $results[0];
  392. }
  393. // now add the record
  394. $options = array('statement_name' => 'ins_pubdbxref_dbpu');
  395. $results = tripal_core_chado_insert('pub_dbxref', $values, $options);
  396. if (!$results) {
  397. watchdog('tripal_pub', "Cannot add publication dbxref: %db:%accession.",
  398. array('%db' => $dbname, '%accession' => $accession). WATCHDOG_ERROR);
  399. return FALSE;
  400. }
  401. return $results;
  402. }
  403. /**
  404. * Returns the list of publications that are assigned the database
  405. * cross-reference provided
  406. *
  407. * @param $pub_dbxref
  408. * The database cross reference accession. It should be in the form
  409. * DB:ACCESSION, where DB is the database name and ACCESSION is the
  410. * unique publication identifier (e.g. PMID:4382934)
  411. *
  412. * @return
  413. * Returns an array of all the publications that have the provided
  414. * cross reference. If no publications match, then an empty array
  415. * is returned.
  416. *
  417. * @ingroup tripal_pub_api
  418. *
  419. */
  420. function tripal_pub_get_pubs_by_dbxref($pub_dbxref) {
  421. $return = array();
  422. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  423. $dbname = $matches[1];
  424. $accession = $matches[2];
  425. $values = array(
  426. 'dbxref_id' => array (
  427. 'accession' => $accession,
  428. 'db_id' => array(
  429. 'name' => $dbname
  430. ),
  431. ),
  432. );
  433. $options = array('statement_name' => 'sel_pubdbxref_db');
  434. $results = tripal_core_chado_select('pub_dbxref', array('pub_id'), $values, $options);
  435. foreach ($results as $index => $pub) {
  436. $return[] = $pub->pub_id;
  437. }
  438. }
  439. return $return;
  440. }
  441. /**
  442. * Returns the list of publications that match a given title, type and year
  443. *
  444. * @param title
  445. * The title of the publication to look for
  446. * @param type
  447. * Optional. The publication type. The value of this field should come from
  448. * the Tripal Pub vocabulary. This should be the type name (e.g. cvterm.name)
  449. * @param year
  450. * Optional. The year the publication was published.
  451. * @param series_name
  452. * Optional. The name of the series (e.g. Journal name)
  453. *
  454. * @return
  455. * Returns an array of all the publications that have the provided
  456. * cross reference. If no publications match, then an empty array
  457. * is returned.
  458. *
  459. * @ingroup tripal_pub_api
  460. *
  461. */
  462. function tripal_pub_get_pubs_by_title_type_pyear_series($title, $type = NULL, $pyear = NULL, $series_name = NULL) {
  463. $return = array();
  464. // build the values array for the query.
  465. $values = array(
  466. 'title' => $title,
  467. );
  468. $stmnt_suffix = 'ti';
  469. if ($type) {
  470. $values['type_id'] = array(
  471. 'name' => $type,
  472. 'cv_id' => array(
  473. 'name' => 'tripal_pub'
  474. )
  475. );
  476. $stmnt_suffix .= 'ty';
  477. }
  478. if ($pyear) {
  479. $values['pyear'] = $pyear;
  480. $stmnt_suffix .= 'py';
  481. }
  482. if ($series_name) {
  483. $values['series_name'] = strtolower($series_name);
  484. $stmnt_suffix .= 'se';
  485. }
  486. $options = array(
  487. 'statement_name' => 'sel_pub_' . $stmnt_suffix,
  488. 'case_insensitive_columns' => array('title', 'series_name'),
  489. );
  490. $results = tripal_core_chado_select('pub', array('pub_id'), $values, $options);
  491. // iterate through any matches and pull out the pub_id
  492. foreach ($results as $index => $pub) {
  493. $return[] = $pub->pub_id;
  494. }
  495. return $return;
  496. }
  497. /**
  498. * Returns the list of publications that match a given title, type and year
  499. *
  500. * @param title
  501. * The title of the publication to look for
  502. * @param type
  503. * Optional. The publication type. The value of this field should come from
  504. * the Tripal Pub vocabulary. This should be the type name (e.g. cvterm.name)
  505. * @param year
  506. * Optional. The year the publication was published.
  507. * @param series_name
  508. * Optional. The name of the series (e.g. Journal name)
  509. *
  510. * @return
  511. * Returns an array of all the publications that have the provided
  512. * cross reference. If no publications match, then an empty array
  513. * is returned.
  514. *
  515. * @ingroup tripal_pub_api
  516. *
  517. */
  518. function tripal_pub_get_pub_by_uniquename($name) {
  519. $return = array();
  520. // build the values array for the query.
  521. $values = array(
  522. 'uniquename' => $name,
  523. );
  524. $options = array(
  525. 'statement_name' => 'sel_pub_un',
  526. 'case_insensitive_columns' => array('uniquename'),
  527. );
  528. $results = tripal_core_chado_select('pub', array('pub_id'), $values, $options);
  529. // iterate through any matches and pull out the pub_id
  530. foreach ($results as $index => $pub) {
  531. $return[] = $pub->pub_id;
  532. }
  533. return $return;
  534. }
  535. /**
  536. * Adds a new publication to the Chado, along with all properties and
  537. * database cross-references. If the publication does not already exist
  538. * in Chado then it is added. If it does exist nothing is done. If
  539. * the $update parameter is TRUE then the publication is updated if it exists.
  540. *
  541. * @param $pub_details
  542. * An associative array containing all of the details about the publication.
  543. * @param $action
  544. * This variable will get set to a text value indicating the action that was
  545. * performed. The values include 'skipped', 'inserted', 'updated' or 'error'.
  546. * @param $do_contact
  547. * Optional. Set to TRUE if a contact entry should be added to the Chado contact table
  548. * for authors of the publication.
  549. * @param $update_if_exists
  550. * Optional. If the publication already exists then this function will return
  551. * without adding a new publication. However, set this value to TRUE to force
  552. * the function to pudate the publication using the $pub_details that are provided.
  553. *
  554. * @return
  555. * If the publication already exists, is inserted or updated then the publication
  556. * ID is returned, otherwise FALSE is returned. If the publication already exists
  557. * and $update_if_exists is not TRUE then the $action variable is set to 'skipped'.
  558. * If the publication already exists and $update_if_exists is TRUE and if the update
  559. * was successful then $action is set to 'updated'. Otherwise on successful insert
  560. * the $action variable is set to 'inserted'. If the function failes then the
  561. * $action variable is set to 'error'
  562. *
  563. */
  564. function tripal_pub_add_publication($pub_details, &$action, $do_contact = FALSE, $update_if_exists = FALSE) {
  565. $pub_id = 0;
  566. // first try to find the publication using the accession number. It will have
  567. // one if the pub has already been loaded for the publication database
  568. if ($pub_details['Publication Dbxref']) {
  569. $results = tripal_pub_get_pubs_by_dbxref($pub_details['Publication Dbxref']);
  570. if(count($results) == 1) {
  571. $pub_id = $results[0];
  572. if ($pub_id and !$update_if_exists) {
  573. watchdog('tripal_pub', "A publication with this Dbxref already exists... Skipping: %dbxref",
  574. array('%dbxref' => $pub_details['Publication Dbxref']), WATCHDOG_WARNING);
  575. $action = 'skipped';
  576. return $pub_id;
  577. }
  578. }
  579. elseif (count($results) > 1) {
  580. watchdog('tripal_pub', "There are two publications with this accession: %db:%accession. Cannot determine which to update.",
  581. array('%db' => $dbname, '%accession' => $accession), WATCHDOG_ERROR);
  582. $action = 'error';
  583. return FALSE;
  584. }
  585. }
  586. // if we couldn't find a publication by the accession (which means it doesn't
  587. // yet exist or it has been added using a different publication database) then
  588. // try to find it using the title and publication year.
  589. if (!$pub_id and $pub_details['Title']) {
  590. $results = tripal_pub_get_pubs_by_title_type_pyear_series($pub_details['Title'], NULL, $pub_details['Year']);
  591. if (count($results) == 1) {
  592. $pub_id = $results[0];
  593. if ($pub_id and !$update_if_exists) {
  594. watchdog('tripal_pub', "The publication with the same title, type and year already exists. Skipping. ".
  595. " Title: '%title'. Type: '%type'. Year: '%year'",
  596. array('%title' => $pub_details['Title'], '%type' => $pub_details['Publication Type'], '%year' => $pub_details['Year']), WATCHDOG_WARNING);
  597. $action = 'skipped';
  598. return $pub_id;
  599. }
  600. }
  601. elseif (count($results) > 1) {
  602. watchdog('tripal_pub', "The publication with the same title, type and year is present multiple times. Cannot ".
  603. "determine which to use. Title: '%title'. Type: '%type'. Year: '%year'",
  604. array('%title' => $pub_details['Title'], '%type' => $pub_details['Publication Type'], '%year' => $pub_details['Year']), WATCHDOG_ERROR);
  605. $action = 'error';
  606. return FALSE;
  607. }
  608. }
  609. // get the publication type (use the first publication type, any others will get stored as properties)
  610. if (is_array($pub_details['Publication Type'])) {
  611. $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'][0], NULL, 'tripal_pub');
  612. }
  613. elseif ($pub_details['Publication Type']) {
  614. $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'], NULL, 'tripal_pub');
  615. }
  616. else {
  617. watchdog('tripal_pub', "The Publication Type is a required property but is missing", array(), WATCHDOG_ERROR);
  618. $action = 'error';
  619. return FALSE;
  620. }
  621. if (!$pub_type) {
  622. watchdog('tripal_pub', "Cannot find publication type: '%type'",
  623. array('%type' => $pub_details['Publication Type'][0]), WATCHDOG_ERROR);
  624. $action = 'error';
  625. return FALSE;
  626. }
  627. // build the values array for inserting or updating
  628. $values = array(
  629. 'title' => $pub_details['Title'],
  630. 'volume' => $pub_details['Volume'],
  631. 'series_name' => substr($pub_details['Journal Name'], 0, 255),
  632. 'issue' => $pub_details['Issue'],
  633. 'pyear' => $pub_details['Year'],
  634. 'pages' => $pub_details['Pages'],
  635. 'uniquename' => $pub_details['Citation'],
  636. 'type_id' => $pub_type->cvterm_id,
  637. );
  638. // if there is no pub_id then we need to do an insert.
  639. if (!$pub_id) {
  640. $options = array('statement_name' => 'ins_pub_tivoseispypaunty');
  641. $pub = tripal_core_chado_insert('pub', $values, $options);
  642. if (!$pub) {
  643. watchdog('tripal_pub', "Cannot insert the publication with title: %title",
  644. array('%title' => $pub_details['Title']), WATCHDOG_ERROR);
  645. $action = 'error';
  646. return FALSE;
  647. }
  648. $pub_id = $pub['pub_id'];
  649. $action = 'inserted';
  650. }
  651. // if there is a pub_id and we've been told to update, then do the update
  652. if ($pub_id and $update_if_exists) {
  653. $match = array('pub_id' => $pub_id);
  654. $options = array('statement_name' => 'up_pub_tivoseispypaunty');
  655. $success = tripal_core_chado_update('pub', $match, $values, $options);
  656. if (!$success) {
  657. watchdog('tripal_pub', "Cannot update the publication with title: %title",
  658. array('%title' => $pub_details['Title']), WATCHDOG_ERROR);
  659. $action = 'error';
  660. return FALSE;
  661. }
  662. $action = 'updated';
  663. }
  664. // before we add any new properties we need to remove those that are there if this
  665. // is an update. The only thing we don't want to remove are the 'Publication Dbxref'
  666. if ($update_if_exists) {
  667. $sql = "
  668. DELETE FROM {pubprop}
  669. WHERE
  670. pub_id = :pub_id AND
  671. NOT type_id in (
  672. SELECT cvterm_id
  673. FROM {cvterm}
  674. WHERE name = 'Publication Dbxref'
  675. )
  676. ";
  677. chado_query($sql, array(':pub_id' => $pub_id));
  678. }
  679. // iterate through the properties and add them
  680. foreach ($pub_details as $key => $value) {
  681. // the pub_details may have the raw search data (e.g. in XML from PubMed. We'll irgnore this for now
  682. if($key == 'raw') {
  683. continue;
  684. }
  685. // get the cvterm by name or synonym
  686. $cvterm = tripal_cv_get_cvterm_by_name($key, NULL, 'tripal_pub');
  687. if (!$cvterm) {
  688. $cvterm = tripal_cv_get_cvterm_by_synonym($key, NULL, 'tripal_pub');
  689. }
  690. if (!$cvterm) {
  691. watchdog('tripal_pub', "Cannot find term: '%prop'. Skipping.", array('%prop' => $key), WATCHDOG_ERROR);
  692. continue;
  693. }
  694. // skip details that won't be stored as properties
  695. if ($key == 'Author List') {
  696. tripal_pub_add_authors($pub_id, $value, $do_contact);
  697. continue;
  698. }
  699. if ($key == 'Title' or $key == 'Volume' or $key == 'Journal Name' or $key == 'Issue' or
  700. $key == 'Year' or $key == 'Pages') {
  701. continue;
  702. }
  703. $success = 0;
  704. if (is_array($value)) {
  705. foreach ($value as $subkey => $subvalue) {
  706. // if the key is an integer then this array is a simple list and
  707. // we will insert using the primary key. Otheriwse, use the new key
  708. if(is_int($subkey)) {
  709. $success = tripal_core_insert_property('pub', $pub_id, $key, 'tripal_pub', $subvalue, FALSE);
  710. }
  711. else {
  712. $success = tripal_core_insert_property('pub', $pub_id, $subkey, 'tripal_pub', $subvalue, FALSE);
  713. }
  714. }
  715. }
  716. else {
  717. $success = tripal_core_insert_property('pub', $pub_id, $key, 'tripal_pub', $value, TRUE);
  718. }
  719. if (!$success) {
  720. watchdog('tripal_pub', "Cannot add property '%prop' to publication. Skipping.",
  721. array('%prop' => $key), WATCHDOG_ERROR);
  722. continue;
  723. }
  724. }
  725. return $pub_id;
  726. }
  727. /*
  728. *
  729. */
  730. function tripal_pub_add_authors($pub_id, $authors, $do_contact) {
  731. $rank = 0;
  732. // first remove any of the existing pubauthor entires
  733. $sql = "DELETE FROM {pubauthor} WHERE pub_id = :pub_id";
  734. chado_query($sql, array(':pub_id' => $pub_id));
  735. // iterate through the authors and add them to the pubauthors and contact
  736. // tables of chado, then link them through the custom pubauthors_contact table
  737. foreach ($authors as $author) {
  738. // skip invalid author entires
  739. if ($author['valid'] == 'N') {
  740. continue;
  741. }
  742. // remove the 'valid' property as we don't have a CV term for it
  743. unset($author['valid']);
  744. // construct the contact.name field using the author information
  745. $name = '';
  746. $type = 'Person';
  747. if ($author['Given Name']) {
  748. $name .= $author['Given Name'];
  749. }
  750. if ($author['Surname']) {
  751. $name .= ' ' . $author['Surname'];
  752. }
  753. if ($author['Suffix']) {
  754. $name .= ' ' . $author['Suffix'];
  755. }
  756. if ($author['Collective']) {
  757. $name = $author['Collective'];
  758. $type = 'Collective';
  759. }
  760. $name = trim($name);
  761. // add an entry to the pubauthors table
  762. $values = array(
  763. 'pub_id' => $pub_id,
  764. 'rank' => $rank,
  765. 'surname' => $author['Surname'] ? substr($author['Surname'], 0, 100) : substr($author['Collective'], 0, 100),
  766. 'givennames' => $author['Given Name'],
  767. 'suffix' => $author['Suffix'],
  768. );
  769. $options = array('statement_name' => 'ins_pubauthor_idrasugisu');
  770. $pubauthor = tripal_core_chado_insert('pubauthor', $values, $options);
  771. // if the user wants us to create a contact for each author then do it.
  772. if ($do_contact) {
  773. // Add the contact
  774. $contact = tripal_contact_add_contact($name, '', $type, $author);
  775. // if we have succesfully added the contact and the pubauthor entries then we want to
  776. // link them together
  777. if ($contact and $pubauthor) {
  778. // link the pubauthor entry to the contact
  779. $values = array(
  780. 'pubauthor_id' => $pubauthor['pubauthor_id'],
  781. 'contact_id' => $contact['contact_id'],
  782. );
  783. $options = array('statement_name' => 'ins_pubauthorcontact_puco');
  784. $pubauthor_contact = tripal_core_chado_insert('pubauthor_contact', $values, $options);
  785. if (!$pubauthor_contact) {
  786. watchdog('tripal_pub', "Cannot link pub authro and contact.", array(), WATCHDOG_ERROR);
  787. }
  788. }
  789. }
  790. $rank++;
  791. }
  792. }
  793. /**
  794. * Retrieve properties of a given type for a given pub
  795. *
  796. * @param $pub_id
  797. * The pub_id of the properties you would like to retrieve
  798. * @param $property
  799. * The cvterm name of the properties to retrieve
  800. *
  801. * @return
  802. * An pub chado variable with the specified properties expanded
  803. *
  804. * @ingroup tripal_pub_api
  805. */
  806. function tripal_pub_get_property($pub_id, $property) {
  807. return tripal_core_get_property('pub', $pub_id, $property, 'tripal_pub');
  808. }
  809. /**
  810. * Insert a given property
  811. *
  812. * @param $pub_id
  813. * The pub_id of the property to insert
  814. * @param $property
  815. * The cvterm name of the property to insert
  816. * @param $value
  817. * The value of the property to insert
  818. * @param $update_if_present
  819. * A boolean indicated whether to update the record if it's already present
  820. *
  821. * @return
  822. * True of success, False otherwise
  823. *
  824. * @ingroup tripal_pub_api
  825. */
  826. function tripal_pub_insert_property($pub_id, $property, $value, $update_if_present = 0) {
  827. return tripal_core_insert_property('pub', $pub_id, $property, 'tripal_pub', $value, $update_if_present);
  828. }
  829. /**
  830. * Update a given property
  831. *
  832. * @param $pub_id
  833. * The pub_id of the property to update
  834. * @param $property
  835. * The cvterm name of the property to update
  836. * @param $value
  837. * The value of the property to update
  838. * @param $insert_if_missing
  839. * A boolean indicated whether to insert the record if it's absent
  840. *
  841. * Note: The property will be identified using the unique combination of the $pub_id and $property
  842. * and then it will be updated with the supplied value
  843. *
  844. * @return
  845. * True of success, False otherwise
  846. *
  847. * @ingroup tripal_pub_api
  848. */
  849. function tripal_pub_update_property($pub_id, $property, $value, $insert_if_missing = 0) {
  850. return tripal_core_update_property('pub', $pub_id, $property, 'tripal_pub', $value, $insert_if_missing);
  851. }
  852. /**
  853. * Delete a given property
  854. *
  855. * @param $pub_id
  856. * The pub_id of the property to delete
  857. * @param $property
  858. * The cvterm name of the property to delete
  859. *
  860. * Note: The property will be identified using the unique combination of the $pub_id and $property
  861. * and then it will be deleted
  862. *
  863. * @return
  864. * True of success, False otherwise
  865. *
  866. * @ingroup tripal_pub_api
  867. */
  868. function tripal_pub_delete_property($pub_id, $property) {
  869. return tripal_core_delete_property('pub', $pub_id, $property, 'tripal_pub');
  870. }
  871. /**
  872. * This function generates an array suitable for use with the
  873. * tripal_pub_create_citation function for any publication
  874. * already stored in the Chado tables.
  875. *
  876. * @param $pub_id
  877. * The publication ID
  878. * @param $skip_existing
  879. * Set to TRUE to skip publications that already have a citation
  880. * in the pubprop table. Set to FALSE to generate a citation
  881. * regardless if the citation already exists.
  882. *
  883. * @return
  884. * An array suitable for the trpial_pub_create_citation function. On
  885. * failure returns FALSE.
  886. */
  887. function tripal_pub_get_publication_array($pub_id, $skip_existing = TRUE) {
  888. $options = array('return_array' => 1);
  889. // ---------------------------------
  890. // get the publication
  891. // ---------------------------------
  892. $values = array('pub_id' => $pub_id);
  893. $pub = tripal_core_generate_chado_var('pub', $values);
  894. // expand the title
  895. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  896. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  897. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
  898. $pub_array = array();
  899. if (trim($pub->title)) {
  900. $pub_array['Title'] = $pub->title;
  901. }
  902. if (trim($pub->volumetitle)) {
  903. $pub_array['Volume Title'] = $pub->volumetitle;
  904. }
  905. if (trim($pub->volume)) {
  906. $pub_array['Volume'] = $pub->volume;
  907. }
  908. if (trim($pub->series_name)) {
  909. $pub_array['Series Name'] = $pub->series_name;
  910. }
  911. if (trim($pub->issue)) {
  912. $pub_array['Issue'] = $pub->issue;
  913. }
  914. if (trim($pub->pyear)) {
  915. $pub_array['Year'] = $pub->pyear;
  916. }
  917. if (trim($pub->pages)) {
  918. $pub_array['Pages'] = $pub->pages;
  919. }
  920. if (trim($pub->miniref)) {
  921. $pub_array['Mini Ref'] = $pub->miniref;
  922. }
  923. if (trim($pub->uniquename)) {
  924. $pub_array['Uniquename'] = $pub->uniquename;
  925. }
  926. $pub_array['Publication Type'][] = $pub->type_id->name;
  927. // ---------------------------------
  928. // get the citation
  929. // ---------------------------------
  930. $values = array(
  931. 'pub_id' => $pub->pub_id,
  932. 'type_id' => array(
  933. 'name' => 'Citation',
  934. ),
  935. );
  936. $citation = tripal_core_generate_chado_var('pubprop', $values);
  937. if ($citation) {
  938. $citation = tripal_core_expand_chado_vars($citation, 'field', 'pubprop.value', $options);
  939. if (count($citation) > 1) {
  940. watchdog('tripal_pub', "Publication has multiple citations already: %pub_id",
  941. array('%pub_id' => $pubid), WATCHDOG_ERROR);
  942. return FALSE;
  943. }
  944. elseif (count($citation) == 1 and $skip_existing == TRUE) {
  945. // skip this publication, it already has a citation
  946. return FALSE;
  947. }
  948. }
  949. // ---------------------------------
  950. // get the publication types
  951. // ---------------------------------
  952. $values = array(
  953. 'pub_id' => $pub->pub_id,
  954. 'type_id' => array(
  955. 'name' => 'Publication Type',
  956. ),
  957. );
  958. $ptypes = tripal_core_generate_chado_var('pubprop', $values, $options);
  959. if ($ptypes) {
  960. $ptypes = tripal_core_expand_chado_vars($ptypes, 'field', 'pubprop.value', $options);
  961. foreach ($ptypes as $ptype) {
  962. $pub_array['Publication Type'][] = $ptype->value;
  963. }
  964. }
  965. // ---------------------------------
  966. // get the authors list
  967. // ---------------------------------
  968. $values = array(
  969. 'pub_id' => $pub->pub_id,
  970. 'type_id' => array(
  971. 'name' => 'Authors',
  972. ),
  973. );
  974. $authors = tripal_core_generate_chado_var('pubprop', $values);
  975. $authors = tripal_core_expand_chado_vars($authors, 'field', 'pubprop.value', $options);
  976. if (count($authors) > 1) {
  977. watchdog('tripal_pub', "Publication has multiple author lists. It should have only one list: %pub_id",
  978. array('%pub_id' => $pubid), WATCHDOG_ERROR);
  979. return FALSE;
  980. }
  981. else if (trim($authors->value)) {
  982. $pub_array['Authors'] = $authors->value;
  983. }
  984. // if there is no 'Author's property then try to retreive authors from the pubauthor table
  985. else {
  986. $sql = "
  987. SELECT string_agg(surname || ' ' || givennames, ', ')
  988. FROM {pubauthor}
  989. WHERE pub_id = :pub_id
  990. GROUP BY pub_id
  991. ";
  992. $au = chado_query($sql, array(':pub_id' => $pub_id))->fetchField();
  993. if ($au) {
  994. $pub_array['Authors'] = $au;
  995. }
  996. }
  997. //Get other props
  998. $props = array(
  999. 'Journal Abbreviation',
  1000. 'Elocation',
  1001. 'Media Code',
  1002. 'Conference Name',
  1003. 'Keywords',
  1004. 'Series Name',
  1005. 'pISSN',
  1006. 'Publication Date',
  1007. 'Journal Code',
  1008. 'Journal Alias',
  1009. 'Journal Country',
  1010. 'Published Location',
  1011. 'Publication Model',
  1012. 'Language Abbr',
  1013. 'Alias',
  1014. 'Publication Dbxref',
  1015. 'Copyright',
  1016. 'Abstract',
  1017. 'Notes',
  1018. 'Citation',
  1019. 'Language',
  1020. 'URL',
  1021. 'eISSN',
  1022. 'DOI',
  1023. 'ISSN',
  1024. 'Publication Code',
  1025. 'Comments',
  1026. 'Publisher',
  1027. 'Media Alias',
  1028. 'Original Title');
  1029. foreach ($props AS $prop) {
  1030. $sql =
  1031. "SELECT value FROM {pubprop}
  1032. WHERE type_id =
  1033. (SELECT cvterm_id
  1034. FROM {cvterm}
  1035. WHERE name = :cvtname AND cv_id =
  1036. (SELECT cv_id
  1037. FROM {cv}
  1038. WHERE name = 'tripal_pub'
  1039. )
  1040. )
  1041. AND pub_id = :pub_id
  1042. ";
  1043. $val = trim(chado_query($sql, array(':cvtname' => $prop, ':pub_id' => $pub->pub_id))->fetchField());
  1044. if ($val) {
  1045. $pub_array[$prop] =$val;
  1046. }
  1047. }
  1048. return $pub_array;
  1049. }
  1050. /**
  1051. * This function generates citations for publications. It requires
  1052. * an array structure with keys being the terms in the Tripal
  1053. * publication ontology. This function is intended to be used
  1054. * for any function that needs to generate a citation.
  1055. *
  1056. * @param $pub
  1057. * An array structure containing publication details where the keys
  1058. * are the publication ontology term names and values are the
  1059. * corresponding details. The pub array can contain the following
  1060. * keys with corresponding values:
  1061. * - Publication Type: an array of publication types. a publication can have more than one type
  1062. * - Authors: a string containing all of the authors of a publication
  1063. * - Journal Name: a string containing the journal name
  1064. * - Journal Abbreviation: a string containing the journal name abbreviation
  1065. * - Series Name: a string containing the series (e.g. conference proceedings) name
  1066. * - Series Abbreviation: a string containing the series name abbreviation
  1067. * - Volume: the serives volume number
  1068. * - Issue: the series issue number
  1069. * - Pages: the page numbers for the publication
  1070. * - Publication Date: A date in the format "Year Month Day"
  1071. *
  1072. * @return
  1073. * A text string containing the citation
  1074. */
  1075. function tripal_pub_create_citation($pub) {
  1076. $citation = '';
  1077. $pub_type = '';
  1078. // An article may have more than one publication type. For example,
  1079. // a publication type can be 'Journal Article' but also a 'Clinical Trial'.
  1080. // Therefore, we need to select the type that makes most sense for
  1081. // construction of the citation. Here we'll iterate through them all
  1082. // and select the one that matches best.
  1083. if(is_array($pub['Publication Type'])) {
  1084. foreach ($pub['Publication Type'] as $ptype) {
  1085. if ($ptype == 'Journal Article' ) {
  1086. $pub_type = $ptype;
  1087. break;
  1088. }
  1089. else if ($ptype == 'Conference Proceedings'){
  1090. $pub_type = $ptype;
  1091. break;
  1092. }
  1093. else if ($ptype == 'Book') {
  1094. $pub_type = $ptype;
  1095. break;
  1096. }
  1097. else if ($ptype == 'Letter') {
  1098. $pub_type = $ptype;
  1099. break;
  1100. }
  1101. else if ($ptype == 'Book Chapter') {
  1102. $pub_type = $ptype;
  1103. break;
  1104. }
  1105. else if ($ptype == "Research Support, Non-U.S. Gov't") {
  1106. $pub_type = $ptype;
  1107. // we don't break because if the article is also a Journal Article
  1108. // we prefer that type
  1109. }
  1110. }
  1111. if (!$pub_type) {
  1112. watchdog('tripal_pub', "Cannot generate citation for publication type: %types",
  1113. array('%types' => print_r($pub['Publication Type'], TRUE)), WATCHDOG_ERROR);
  1114. return FALSE;
  1115. }
  1116. }
  1117. else {
  1118. $pub_type = $pub['Publication Type'];
  1119. }
  1120. //----------------------
  1121. // Journal Article
  1122. //----------------------
  1123. if ($pub_type == 'Journal Article') {
  1124. $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
  1125. if (array_key_exists('Journal Name', $pub)) {
  1126. $citation .= $pub['Journal Name'] . '. ';
  1127. }
  1128. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  1129. $citation .= $pub['Journal Abbreviation'] . '. ';
  1130. }
  1131. elseif (array_key_exists('Series Name', $pub)) {
  1132. $citation .= $pub['Series Name'] . '. ';
  1133. }
  1134. elseif (array_key_exists('Series Abbreviation', $pub)) {
  1135. $citation .= $pub['Series Abbreviation'] . '. ';
  1136. }
  1137. if (array_key_exists('Publication Date', $pub)) {
  1138. $citation .= $pub['Publication Date'];
  1139. }
  1140. elseif (array_key_exists('Year', $pub)) {
  1141. $citation .= $pub['Year'];
  1142. }
  1143. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  1144. $citation .= '; ';
  1145. }
  1146. if (array_key_exists('Volume', $pub)) {
  1147. $citation .= $pub['Volume'];
  1148. }
  1149. if (array_key_exists('Issue', $pub)) {
  1150. $citation .= '(' . $pub['Issue'] . ')';
  1151. }
  1152. if (array_key_exists('Pages', $pub)) {
  1153. if (array_key_exists('Volume', $pub)) {
  1154. $citation .= ':';
  1155. }
  1156. $citation .= $pub['Pages'];
  1157. }
  1158. $citation .= '.';
  1159. }
  1160. //----------------------
  1161. // Research Support, Non-U.S. Gov't
  1162. //----------------------
  1163. elseif ($pub_type == "Research Support, Non-U.S. Gov't") {
  1164. $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
  1165. if (array_key_exists('Journal Name', $pub)) {
  1166. $citation .= $pub['Journal Name'] . '. ';
  1167. }
  1168. if (array_key_exists('Publication Date', $pub)) {
  1169. $citation .= $pub['Publication Date'];
  1170. }
  1171. elseif (array_key_exists('Year', $pub)) {
  1172. $citation .= $pub['Year'];
  1173. }
  1174. $citation .= '.';
  1175. }
  1176. //----------------------
  1177. // Letter
  1178. //----------------------
  1179. elseif ($pub_type == 'Letter') {
  1180. $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
  1181. if (array_key_exists('Journal Name', $pub)) {
  1182. $citation .= $pub['Journal Name'] . '. ';
  1183. }
  1184. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  1185. $citation .= $pub['Journal Abbreviation'] . '. ';
  1186. }
  1187. elseif (array_key_exists('Series Name', $pub)) {
  1188. $citation .= $pub['Series Name'] . '. ';
  1189. }
  1190. elseif (array_key_exists('Series Abbreviation', $pub)) {
  1191. $citation .= $pub['Series Abbreviation'] . '. ';
  1192. }
  1193. if (array_key_exists('Publication Date', $pub)) {
  1194. $citation .= $pub['Publication Date'];
  1195. }
  1196. elseif (array_key_exists('Year', $pub)) {
  1197. $citation .= $pub['Year'];
  1198. }
  1199. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  1200. $citation .= '; ';
  1201. }
  1202. if (array_key_exists('Volume', $pub)) {
  1203. $citation .= $pub['Volume'];
  1204. }
  1205. if (array_key_exists('Issue', $pub)) {
  1206. $citation .= '(' . $pub['Issue'] . ')';
  1207. }
  1208. if (array_key_exists('Pages', $pub)) {
  1209. if (array_key_exists('Volume', $pub)) {
  1210. $citation .= ':';
  1211. }
  1212. $citation .= $pub['Pages'];
  1213. }
  1214. $citation .= '.';
  1215. }
  1216. //----------------------
  1217. // Book
  1218. //----------------------
  1219. elseif ($pub_type == 'Book') {
  1220. }
  1221. //----------------------
  1222. // Book Chapter
  1223. //----------------------
  1224. elseif ($pub_type == 'Book Chapter') {
  1225. }
  1226. //----------------------
  1227. // Conference Proceedings
  1228. //----------------------
  1229. elseif ($pub_type == 'Conference Proceedings') {
  1230. $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
  1231. if (array_key_exists('Conference Name', $pub)) {
  1232. $citation .= $pub['Conference Name'] . '. ';
  1233. }
  1234. elseif (array_key_exists('Series Name', $pub)) {
  1235. $citation .= $pub['Series Name'] . '. ';
  1236. }
  1237. elseif (array_key_exists('Series Abbreviation', $pub)) {
  1238. $citation .= $pub['Series Abbreviation'] . '. ';
  1239. }
  1240. if (array_key_exists('Publication Date', $pub)) {
  1241. $citation .= $pub['Publication Date'];
  1242. }
  1243. elseif (array_key_exists('Year', $pub)) {
  1244. $citation .= $pub['Year'];
  1245. }
  1246. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  1247. $citation .= '; ';
  1248. }
  1249. if (array_key_exists('Volume', $pub)) {
  1250. $citation .= $pub['Volume'];
  1251. }
  1252. if (array_key_exists('Issue', $pub)) {
  1253. $citation .= '(' . $pub['Issue'] . ')';
  1254. }
  1255. if (array_key_exists('Pages', $pub)) {
  1256. if (array_key_exists('Volume', $pub)) {
  1257. $citation .= ':';
  1258. }
  1259. $citation .= $pub['Pages'];
  1260. }
  1261. $citation .= '.';
  1262. }
  1263. return $citation;
  1264. }