tripal_pub.api.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. Valid values
  15. * include: 'pubmed'.
  16. * @param search_array
  17. * An associate array containing the search criteria. The following key
  18. * are expected
  19. * 'num_criteria': Specifies the number of criteria present in the search array
  20. * 'days': The number of days to include in the search starting from today
  21. * 'criteria': An associate array containing the search critiera. There should
  22. * be no less than 'num_criteria' elements in this array.
  23. *
  24. * The following keys are expected in the 'criteria' array
  25. * 'search_terms': A list of terms to search on, separated by spaces.
  26. * 'scope': The fields to search in the remote database. Valid values
  27. * include: 'title', 'abstract', 'author' and 'any'
  28. * 'operation': The logical operation to use for this criteria. Valid
  29. * values include: 'AND', 'OR' and 'NOT'.
  30. * @param $num_to_retrieve
  31. * The number of records to retrieve. In cases with large numbers of
  32. * records to retrieve, the remote database may limit the size of each
  33. * retrieval.
  34. * @param $pager_id
  35. * Optional. This function uses the 'tripal_pager_callback' function
  36. * to page a set of results. This is helpful when generating results to
  37. * be view online. The pager works identical to the pager_query function
  38. * of drupal. Simply provide a unique integer value for this argument. Each
  39. * form on a single page should have a unique $pager_id.
  40. * @param $page
  41. * Optional. If this function is called on the command-line where the
  42. * page for the pager cannot be set using the $_GET variable, use this
  43. * argument to specify the page to retrieve.
  44. *
  45. * @return
  46. * Returns an array of pubs where each element is
  47. * an associative array where the keys are Tripal Pub CV terms.
  48. *
  49. * @ingroup tripal_pub_api
  50. */
  51. function tripal_pub_get_remote_search_results($remote_db, $search_array,
  52. $num_to_retrieve, $pager_id = 0, $page = 0) {
  53. // construct the callback function using the remote database name
  54. $callback = 'tripal_pub_remote_search_' . strtolower($remote_db);
  55. // manually set the $_GET['page'] parameter to trick the pager
  56. // into giving us the requested page
  57. if (is_numeric($page) and $page > 0) {
  58. $_GET['page'] = $page;
  59. }
  60. // now call the callback function to get the rsults
  61. $pubs = array();
  62. if (function_exists($callback)) {
  63. $pubs = call_user_func($callback, $search_array, $num_to_retrieve, $pager_id);
  64. }
  65. return $pubs;
  66. }
  67. /*
  68. * @ingroup tripal_pub_api
  69. */
  70. function tripal_pub_import_publications() {
  71. $num_to_retrieve = 10;
  72. $pager_id = 0;
  73. $page = 1;
  74. $num_pubs = 0;
  75. // get all of the loaders
  76. $sql = "SELECT * FROM {tripal_pub_import} WHERE disabled = 0";
  77. $results = db_query($sql);
  78. while ($import = db_fetch_object($results)) {
  79. $criteria = unserialize($import->criteria);
  80. $remote_db = $criteria['remote_db'];
  81. print_r($search_array);
  82. do {
  83. // retrieve the pubs for this page
  84. $pubs = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $pager_id, $page);
  85. // now add the publications
  86. foreach ($pubs as $pub) {
  87. $p = tripal_pub_add_publication($pub);
  88. $pub_id = $p->pub_id;
  89. // check to see if the pub_dbxref record already exist
  90. $values = array(
  91. 'dbxref_id' => array(
  92. 'accession' => $pub['pub_accession'],
  93. 'db_id' => array(
  94. 'name' => $pub['pub_database'],
  95. ),
  96. ),
  97. 'pub_id' => $pub_id,
  98. );
  99. $options = array('statement_name' => 'sel_pubdbxref_db');
  100. $results = tripal_core_chado_select('pub_dbxref', array('*'), $values, $options);
  101. // if the pub_dbxref record doesn't exist then we need to add the associate
  102. if(count($results) == 0) {
  103. // make sure our database already exists
  104. $db = tripal_db_add_db($pub['pub_database']);
  105. // get the database cross-reference
  106. $values = array(
  107. 'accession' => $pub['pub_accession'],
  108. 'db_id' => $db->db_id,
  109. );
  110. $options = array('statement_name' => 'sel_dbxref_acdb');
  111. $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $options);
  112. // if the accession doesn't exist then add it
  113. if(count($results) == 0){
  114. $dbxref = tripal_db_add_dbxref($db->db_id, $pub['pub_accession']);
  115. }
  116. else {
  117. $dbxref = $results[0];
  118. }
  119. }
  120. else {
  121. $pub_dbxref = $results[0];
  122. }
  123. print $num_pubs . ". " . $pub['pub_database'] . ' ' . $pub['pub_accession'] . "\n";
  124. $num_pubs++;
  125. }
  126. $page++;
  127. }
  128. while (count($pubs) > 0);
  129. }
  130. }
  131. /*
  132. *
  133. */
  134. function tripal_pub_add_publication($pub_details) {
  135. // check to see if the publication already exists
  136. $pub_id = 0;
  137. $values = array(
  138. 'title' => $pub['title'],
  139. 'pyear' => $pub['pyear'],
  140. );
  141. $options = array('statement_name' => 'pub_tipy');
  142. $results = tripal_core_chado_select('pub', array('*'), $values, $options);
  143. // if the publication exists then return the record
  144. if(count($results) == 1) {
  145. return $results[0];
  146. }
  147. if(count($results) > 1) {
  148. watchdog('tripal_pub', "The publication with the same title is present multiple times. Cannot ".
  149. "determine which to use. Title: %title", array('%title' => $pub_details['title']), WATCHDOG_ERROR);
  150. return FALSE;
  151. }
  152. // if the publication does not exist then create it.
  153. $values = array(
  154. 'title' => $pub_details['title'],
  155. 'volume' => $pub_details['volume'],
  156. 'series_name' => $pub_details['journal_name'],
  157. 'issue' => $pub_details['issue'],
  158. 'pyear' => $pub_details['year'],
  159. 'pages' => $pub_details['pages'],
  160. 'uniquename' => $pub_details['citation'],
  161. 'type_id' => array(
  162. 'name' => 'XXXX',
  163. 'cv_id' => array(
  164. 'name' => 'tripal_pub',
  165. ),
  166. ),
  167. );
  168. $options = array('statment_name' => 'ins_pub_tivoseispypaunty');
  169. $pub = tripal_core_chado_insert('pub', $values, $options);
  170. if ($pub) {
  171. watchdog('tripal_pub', "Cannot insert the publication with title: %title", array('%title' => $pub_details['title']), WATCHDOG_ERROR);
  172. return FALSE;
  173. }
  174. // now add in any other items that remain as properties of the publication
  175. foreach ($pub_details as $key => $value) {
  176. }
  177. return $pub;
  178. }