tripal_pub.api.inc 42 KB

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