tripal_pub.pub_citation.inc

Functions to manage citations

File

tripal_pub/includes/tripal_pub.pub_citation.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Functions to manage citations
  5. */
  6. /**
  7. * The admin form for submitting job to create citations
  8. *
  9. * @param $form_state
  10. *
  11. * @ingroup tripal_pub
  12. */
  13. function tripal_pub_citation_form($form, &$form_state) {
  14. $form['instructions'] = array(
  15. '#markup' => '<p>Use this form to unify publication citations. Citations are created automtically when
  16. importing publications but citations are set by the user when publications are added manually.
  17. Or publications added to the Chado database by tools other than the Tripal Publication Importer may
  18. not have citations set. If you are certain that all necessary information for all publications is present (e.g.
  19. authors, volume, issue, page numbers, etc.) but citations are not consistent, then you can
  20. choose to update all citations for all publications using the form below. Alternatively, you
  21. can update citations only for publication that do not already have one.</p>'
  22. );
  23. $form['options'] = array(
  24. '#type' => 'radios',
  25. '#options' => array(
  26. 'all' => 'Create citation for all publications. Replace the existing citation if it exists.',
  27. 'new' => 'Create citation for publication only if it does not already have one.'),
  28. '#default_value' => 'all'
  29. );
  30. $form['submit'] = array(
  31. '#type' => 'submit',
  32. '#value' => t('Submit')
  33. );
  34. return $form;
  35. }
  36. /**
  37. * Submit form. Create Tripal job for citations
  38. *
  39. * @param $form_state
  40. *
  41. * @ingroup tripal_pub
  42. */
  43. function tripal_pub_citation_form_submit(&$form_state) {
  44. $options [0] = $form_state['options']['#value'];
  45. tripal_add_job("Create citations ($options[0])", 'tripal_pub', 'tripal_pub_create_citations', $options, $user->uid);
  46. }
  47. /**
  48. * Launch the Tripal job to generate citations. Called by tripal jobs
  49. *
  50. * @param $options
  51. * Options pertaining to what publications to generate citations for.
  52. * One of the following must be present:
  53. * - all: Create and replace citation for all pubs
  54. * - new: Create citation for pubs that don't already have one
  55. *
  56. * @ingroup tripal_pub
  57. */
  58. function tripal_pub_create_citations($options) {
  59. $skip_existing = TRUE;
  60. $sql = "
  61. SELECT cvterm_id
  62. FROM {cvterm}
  63. WHERE
  64. name = 'Citation' AND
  65. cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal_pub')
  66. ";
  67. $citation_type_id = chado_query($sql)->fetchField();
  68. // Create and replace citation for all pubs
  69. if ($options == 'all') {
  70. $sql = "SELECT pub_id FROM {pub} P WHERE pub_id <> 1";
  71. $skip_existing = FALSE;
  72. }
  73. // Create citation for pubs that don't already have one
  74. else if ($options == 'new') {
  75. $sql = "
  76. SELECT pub_id
  77. FROM {pub} P
  78. WHERE
  79. (SELECT value
  80. FROM {pubprop} PB
  81. WHERE type_id = :type_id AND P.pub_id = PB.pub_id AND rank = 0) IS NULL
  82. AND pub_id <> 1
  83. ";
  84. $skip_existing = TRUE;
  85. }
  86. $result = chado_query($sql, array(':type_id' => $citation_type_id));
  87. $counter_updated = 0;
  88. $counter_generated = 0;
  89. while ($pub = $result->fetchObject()) {
  90. $pub_arr = tripal_pub_get_publication_array($pub->pub_id, $skip_existing);
  91. if ($pub_arr) {
  92. $citation = tripal_pub_create_citation($pub_arr);
  93. print $citation . "\n\n";
  94. // Replace if citation exists. This condition is never TRUE if $skip_existing is TRUE
  95. if ($pub_arr['Citation']) {
  96. $sql = "
  97. UPDATE {pubprop} SET value = :value
  98. WHERE pub_id = :pub_id AND type_id = :type_id AND rank = :rank
  99. ";
  100. chado_query($sql, array(':value' => $citation, ':pub_id' => $pub->pub_id,
  101. ':type_id' => $citation_type_id, ':rank' => 0));
  102. $counter_updated ++;
  103. // Generate a new citation
  104. } else {
  105. $sql = "
  106. INSERT INTO {pubprop} (pub_id, type_id, value, rank)
  107. VALUES (:pub_id, :type_id, :value, :rank)
  108. ";
  109. chado_query($sql, array(':pub_id' => $pub->pub_id, ':type_id' => $citation_type_id,
  110. ':value' => $citation, ':rank' => 0));
  111. $counter_generated ++;
  112. }
  113. }
  114. }
  115. print "$counter_generated citations generated. $counter_updated citations updated.\n";
  116. }
  117. /**
  118. * This function generates citations for publications. It requires
  119. * an array structure with keys being the terms in the Tripal
  120. * publication ontology. This function is intended to be used
  121. * for any function that needs to generate a citation.
  122. *
  123. * @param $pub
  124. * An array structure containing publication details where the keys
  125. * are the publication ontology term names and values are the
  126. * corresponding details. The pub array can contain the following
  127. * keys with corresponding values:
  128. * - Publication Type: an array of publication types. a publication can have more than one type
  129. * - Authors: a string containing all of the authors of a publication
  130. * - Journal Name: a string containing the journal name
  131. * - Journal Abbreviation: a string containing the journal name abbreviation
  132. * - Series Name: a string containing the series (e.g. conference proceedings) name
  133. * - Series Abbreviation: a string containing the series name abbreviation
  134. * - Volume: the serives volume number
  135. * - Issue: the series issue number
  136. * - Pages: the page numbers for the publication
  137. * - Publication Date: A date in the format "Year Month Day"
  138. *
  139. * @return
  140. * A text string containing the citation
  141. *
  142. * @ingroup tripal_pub
  143. */
  144. function tripal_pub_create_citation($pub) {
  145. $citation = '';
  146. $pub_type = '';
  147. // An article may have more than one publication type. For example,
  148. // a publication type can be 'Journal Article' but also a 'Clinical Trial'.
  149. // Therefore, we need to select the type that makes most sense for
  150. // construction of the citation. Here we'll iterate through them all
  151. // and select the one that matches best.
  152. if (is_array($pub['Publication Type'])) {
  153. foreach ($pub['Publication Type'] as $ptype) {
  154. if ($ptype == 'Journal Article' ) {
  155. $pub_type = $ptype;
  156. break;
  157. }
  158. else if ($ptype == 'Conference Proceedings'){
  159. $pub_type = $ptype;
  160. break;
  161. }
  162. else if ($ptype == 'Review') {
  163. $pub_type = $ptype;
  164. break;
  165. }
  166. else if ($ptype == 'Book') {
  167. $pub_type = $ptype;
  168. break;
  169. }
  170. else if ($ptype == 'Letter') {
  171. $pub_type = $ptype;
  172. break;
  173. }
  174. else if ($ptype == 'Book Chapter') {
  175. $pub_type = $ptype;
  176. break;
  177. }
  178. else if ($ptype == "Research Support, Non-U.S. Gov't") {
  179. $pub_type = $ptype;
  180. // we don't break because if the article is also a Journal Article
  181. // we prefer that type
  182. }
  183. }
  184. // If we don't have a recognized publication type, then just use the
  185. // first one in the list.
  186. if (!$pub_type) {
  187. $pub_type = $pub['Publication Type'][0];
  188. }
  189. }
  190. else {
  191. $pub_type = $pub['Publication Type'];
  192. }
  193. //----------------------
  194. // Journal Article
  195. //----------------------
  196. if ($pub_type == 'Journal Article') {
  197. if (array_key_exists('Authors', $pub)) {
  198. $citation = $pub['Authors'] . '. ';
  199. }
  200. $citation .= $pub['Title'] . '. ';
  201. if (array_key_exists('Journal Name', $pub)) {
  202. $citation .= $pub['Journal Name'] . '. ';
  203. }
  204. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  205. $citation .= $pub['Journal Abbreviation'] . '. ';
  206. }
  207. elseif (array_key_exists('Series Name', $pub)) {
  208. $citation .= $pub['Series Name'] . '. ';
  209. }
  210. elseif (array_key_exists('Series Abbreviation', $pub)) {
  211. $citation .= $pub['Series Abbreviation'] . '. ';
  212. }
  213. if (array_key_exists('Publication Date', $pub)) {
  214. $citation .= $pub['Publication Date'];
  215. }
  216. elseif (array_key_exists('Year', $pub)) {
  217. $citation .= $pub['Year'];
  218. }
  219. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  220. $citation .= '; ';
  221. }
  222. if (array_key_exists('Volume', $pub)) {
  223. $citation .= $pub['Volume'];
  224. }
  225. if (array_key_exists('Issue', $pub)) {
  226. $citation .= '(' . $pub['Issue'] . ')';
  227. }
  228. if (array_key_exists('Pages', $pub)) {
  229. if (array_key_exists('Volume', $pub)) {
  230. $citation .= ':';
  231. }
  232. $citation .= $pub['Pages'];
  233. }
  234. $citation .= '.';
  235. }
  236. //----------------------
  237. // Review
  238. //----------------------
  239. elseif ($pub_type == 'Review') {
  240. if (array_key_exists('Authors', $pub)) {
  241. $citation = $pub['Authors'] . '. ';
  242. }
  243. $citation .= $pub['Title'] . '. ';
  244. if (array_key_exists('Journal Name', $pub)) {
  245. $citation .= $pub['Journal Name'] . '. ';
  246. }
  247. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  248. $citation .= $pub['Journal Abbreviation'] . '. ';
  249. }
  250. elseif (array_key_exists('Series Name', $pub)) {
  251. $citation .= $pub['Series Name'] . '. ';
  252. }
  253. elseif (array_key_exists('Series Abbreviation', $pub)) {
  254. $citation .= $pub['Series Abbreviation'] . '. ';
  255. }
  256. if (array_key_exists('Publication Date', $pub)) {
  257. $citation .= $pub['Publication Date'];
  258. }
  259. elseif (array_key_exists('Year', $pub)) {
  260. $citation .= $pub['Year'];
  261. }
  262. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  263. $citation .= '; ';
  264. }
  265. if (array_key_exists('Volume', $pub)) {
  266. $citation .= $pub['Volume'];
  267. }
  268. if (array_key_exists('Issue', $pub)) {
  269. $citation .= '(' . $pub['Issue'] . ')';
  270. }
  271. if (array_key_exists('Pages', $pub)) {
  272. if (array_key_exists('Volume', $pub)) {
  273. $citation .= ':';
  274. }
  275. $citation .= $pub['Pages'];
  276. }
  277. $citation .= '.';
  278. }
  279. //----------------------
  280. // Research Support, Non-U.S. Gov't
  281. //----------------------
  282. elseif ($pub_type == "Research Support, Non-U.S. Gov't") {
  283. if (array_key_exists('Authors', $pub)) {
  284. $citation = $pub['Authors'] . '. ';
  285. }
  286. $citation .= $pub['Title'] . '. ';
  287. if (array_key_exists('Journal Name', $pub)) {
  288. $citation .= $pub['Journal Name'] . '. ';
  289. }
  290. if (array_key_exists('Publication Date', $pub)) {
  291. $citation .= $pub['Publication Date'];
  292. }
  293. elseif (array_key_exists('Year', $pub)) {
  294. $citation .= $pub['Year'];
  295. }
  296. $citation .= '.';
  297. }
  298. //----------------------
  299. // Letter
  300. //----------------------
  301. elseif ($pub_type == 'Letter') {
  302. if (array_key_exists('Authors', $pub)) {
  303. $citation = $pub['Authors'] . '. ';
  304. }
  305. $citation .= $pub['Title'] . '. ';
  306. if (array_key_exists('Journal Name', $pub)) {
  307. $citation .= $pub['Journal Name'] . '. ';
  308. }
  309. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  310. $citation .= $pub['Journal Abbreviation'] . '. ';
  311. }
  312. elseif (array_key_exists('Series Name', $pub)) {
  313. $citation .= $pub['Series Name'] . '. ';
  314. }
  315. elseif (array_key_exists('Series Abbreviation', $pub)) {
  316. $citation .= $pub['Series Abbreviation'] . '. ';
  317. }
  318. if (array_key_exists('Publication Date', $pub)) {
  319. $citation .= $pub['Publication Date'];
  320. }
  321. elseif (array_key_exists('Year', $pub)) {
  322. $citation .= $pub['Year'];
  323. }
  324. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  325. $citation .= '; ';
  326. }
  327. if (array_key_exists('Volume', $pub)) {
  328. $citation .= $pub['Volume'];
  329. }
  330. if (array_key_exists('Issue', $pub)) {
  331. $citation .= '(' . $pub['Issue'] . ')';
  332. }
  333. if (array_key_exists('Pages', $pub)) {
  334. if (array_key_exists('Volume', $pub)) {
  335. $citation .= ':';
  336. }
  337. $citation .= $pub['Pages'];
  338. }
  339. $citation .= '.';
  340. }
  341. //-----------------------
  342. // Conference Proceedings
  343. //-----------------------
  344. elseif ($pub_type == 'Conference Proceedings') {
  345. if (array_key_exists('Authors', $pub)) {
  346. $citation = $pub['Authors'] . '. ';
  347. }
  348. $citation .= $pub['Title'] . '. ';
  349. if (array_key_exists('Conference Name', $pub)) {
  350. $citation .= $pub['Conference Name'] . '. ';
  351. }
  352. elseif (array_key_exists('Series Name', $pub)) {
  353. $citation .= $pub['Series Name'] . '. ';
  354. }
  355. elseif (array_key_exists('Series Abbreviation', $pub)) {
  356. $citation .= $pub['Series Abbreviation'] . '. ';
  357. }
  358. if (array_key_exists('Publication Date', $pub)) {
  359. $citation .= $pub['Publication Date'];
  360. }
  361. elseif (array_key_exists('Year', $pub)) {
  362. $citation .= $pub['Year'];
  363. }
  364. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  365. $citation .= '; ';
  366. }
  367. if (array_key_exists('Volume', $pub)) {
  368. $citation .= $pub['Volume'];
  369. }
  370. if (array_key_exists('Issue', $pub)) {
  371. $citation .= '(' . $pub['Issue'] . ')';
  372. }
  373. if (array_key_exists('Pages', $pub)) {
  374. if (array_key_exists('Volume', $pub)) {
  375. $citation .= ':';
  376. }
  377. $citation .= $pub['Pages'];
  378. }
  379. $citation .= '.';
  380. }
  381. //-----------------------
  382. // Default
  383. //-----------------------
  384. else {
  385. if (array_key_exists('Authors', $pub)) {
  386. $citation = $pub['Authors'] . '. ';
  387. }
  388. $citation .= $pub['Title'] . '. ';
  389. if (array_key_exists('Series Name', $pub)) {
  390. $citation .= $pub['Series Name'] . '. ';
  391. }
  392. elseif (array_key_exists('Series Abbreviation', $pub)) {
  393. $citation .= $pub['Series Abbreviation'] . '. ';
  394. }
  395. if (array_key_exists('Publication Date', $pub)) {
  396. $citation .= $pub['Publication Date'];
  397. }
  398. elseif (array_key_exists('Year', $pub)) {
  399. $citation .= $pub['Year'];
  400. }
  401. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  402. $citation .= '; ';
  403. }
  404. if (array_key_exists('Volume', $pub)) {
  405. $citation .= $pub['Volume'];
  406. }
  407. if (array_key_exists('Issue', $pub)) {
  408. $citation .= '(' . $pub['Issue'] . ')';
  409. }
  410. if (array_key_exists('Pages', $pub)) {
  411. if (array_key_exists('Volume', $pub)) {
  412. $citation .= ':';
  413. }
  414. $citation .= $pub['Pages'];
  415. }
  416. $citation .= '.';
  417. }
  418. return $citation;
  419. }