tripal_search.module 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. //
  3. // Copyright 2009 Clemson University
  4. //
  5. /*******************************************************************************
  6. * Tripal Search initiation
  7. ******************************************************************************/
  8. function tripal_search_init(){
  9. // Add javascript
  10. drupal_add_js(drupal_get_path('theme', 'tripal').
  11. '/js/tripal_search.js');
  12. }
  13. /*******************************************************************************
  14. * Tripal Search adds advanced search functions to Drupal's Search module
  15. */
  16. function tripal_search_menu() {
  17. $items = array();
  18. foreach (module_list() as $name) {
  19. if (module_hook($name, 'search') && $title = module_invoke($name, 'search', 'name')) {
  20. $items['tripal_search/'. $name] = array(
  21. 'title' => $title,
  22. 'page callback' => 'tripal_search_view',
  23. 'access arguments' => array('search content'),
  24. 'type' => MENU_LOCAL_TASK
  25. );
  26. }
  27. }
  28. return $items;
  29. }
  30. /*******************************************************************************
  31. * Implementation of hook_form_alter
  32. */
  33. function tripal_search_form_alter(&$form, $form_state, $form_id) {
  34. if ($form_id == 'search_form' && arg(2)) {
  35. // for pagination etc.
  36. $get = drupal_query_string_encode($_GET, array('q'));
  37. if(preg_match("/node/",arg(1))){
  38. $form['basic']['inline']['fasta'] = array('#type' => 'markup',
  39. '#value' => "<br><br><a id=\"tripal_search_link\" href=\"".
  40. url('tripal_search/'. arg(1) .'/'. urlencode(search_get_keys()),
  41. array('query' => trim($get)?$get:NULL)).
  42. "\">Download features (multi-FASTA format)</a><br><br>");
  43. }
  44. }
  45. }
  46. /*******************************************************************************
  47. * Menu callback; presents an tripal_search results page.
  48. */
  49. function tripal_search_view() {
  50. $type = arg(1);
  51. $keys = search_get_keys();
  52. // Only perform search if there is non-whitespace search term:
  53. if (!module_hook($type, 'search')) {
  54. return drupal_not_found();
  55. }
  56. if (trim($keys)) {
  57. $results = tripal_do_search($keys,$type); //var_dump($pager_total['0']);
  58. return tripal_search_file($type, $keys, $results);
  59. }
  60. }
  61. /*******************************************************************************
  62. * Return an open search results file.
  63. */
  64. function tripal_search_file($type, $keys, $results) {
  65. drupal_set_header('Content-Type: text');
  66. drupal_set_header('Content-Disposition: attachment; filename="searchresults.fasta"');
  67. foreach ($results as $result) {
  68. // Get feature id from drupal database
  69. $sqld = "SELECT * FROM {chado_feature} CF INNER JOIN {node} N on N.nid = CF.nid WHERE CF.nid = %d";
  70. if($f_objd = db_fetch_object(db_query($sqld, $result->sid))){
  71. // Get sequence from chado database
  72. $previous_db = tripal_db_set_active('chado'); // use chado database
  73. $sqlc = "SELECT * FROM {feature} WHERE feature_id = '%s'";
  74. $f_objc = db_fetch_object(db_query($sqlc,$f_objd->feature_id));
  75. tripal_db_set_active($previous_db); // now use drupal database
  76. print tripal_feature_return_fasta($f_objc, $desc);
  77. }
  78. }
  79. }
  80. /*******************************************************************************
  81. * This code is a duplicate of the do_search function but with the paging
  82. * call replaced by a simle db_query call.
  83. */
  84. function tripal_do_search($keywords, $type, $join1 = '', $where1 = '1 = 1', $arguments1 = array(), $columns2 = 'i.relevance AS score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY score DESC') {
  85. $query = search_parse_query($keywords);
  86. if ($query[2] == '') {
  87. form_set_error('keys', t('You must include at least one positive keyword with @count characters or more.', array('@count' => variable_get('minimum_word_size', 3))));
  88. }
  89. if ($query[6]) {
  90. if ($query[6] == 'or') {
  91. drupal_set_message(t('Search for either of the two terms with uppercase <strong>OR</strong>. For example, <strong>cats OR dogs</strong>.'));
  92. }
  93. }
  94. if ($query === NULL || $query[0] == '' || $query[2] == '') {
  95. return array();
  96. }
  97. // Build query for keyword normalization.
  98. $conditions = "$where1 AND ($query[2]) AND i.type = '%s'";
  99. $arguments1 = array_merge($arguments1, $query[3], array($type));
  100. $join = "INNER JOIN {search_total} t ON i.word = t.word $join1";
  101. if (!$query[5]) {
  102. $conditions .= " AND ($query[0])";
  103. $arguments1 = array_merge($arguments1, $query[1]);
  104. $join .= " INNER JOIN {search_dataset} d ON i.sid = d.sid AND i.type = d.type";
  105. }
  106. // Calculate maximum keyword relevance, to normalize it.
  107. $select = "SELECT SUM(i.score * t.count) AS score FROM {search_index} i $join WHERE $conditions GROUP BY i.type, i.sid HAVING COUNT(*) >= %d ORDER BY score DESC";
  108. $arguments = array_merge($arguments1, array($query[4]));
  109. $normalize = db_result(db_query_range($select, $arguments, 0, 1));
  110. if (!$normalize) {
  111. return array();
  112. }
  113. $columns2 = str_replace('i.relevance', '('. (1.0 / $normalize) .' * SUM(i.score * t.count))', $columns2);
  114. // Build query to retrieve results.
  115. $select = "SELECT i.type, i.sid, $columns2 FROM {search_index} i $join $join2 WHERE $conditions GROUP BY i.type, i.sid HAVING COUNT(*) >= %d";
  116. $count_select = "SELECT COUNT(*) FROM ($select) n1";
  117. $arguments = array_merge($arguments2, $arguments1, array($query[4]));
  118. // Do actual search query
  119. // $result = pager_query("$select $sort_parameters", 10, 0, $count_select, $arguments);
  120. $result = db_query("$select $sort_parameters",$arguments);
  121. $results = array();
  122. while ($item = db_fetch_object($result)) {
  123. $results[] = $item;
  124. }
  125. return $results;
  126. }