tripal_search.module 5.7 KB

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