? search_files-268195-16.patch
? search_files_attachments_inline.info
? search_files_attachments_inline.module
Index: search_files_attachments.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/search_files/Attic/search_files_attachments.module,v
retrieving revision 1.1.2.26
diff -u -p -r1.1.2.26 search_files_attachments.module
--- search_files_attachments.module	3 Sep 2009 17:47:48 -0000	1.1.2.26
+++ search_files_attachments.module	4 Sep 2009 13:28:09 -0000
@@ -115,17 +115,18 @@ function search_files_attachments_search
       }
 
       // Do search.
-      $find = do_search($keys, 'file', 'INNER JOIN {files} AS f ON f.`fid` = i.`sid` LEFT JOIN upload u USING (`fid`) LEFT JOIN node n USING (`nid`)'. $join1, $conditions1 . (empty($where1) ? '' : ' AND '. $where1), $arguments1, $select2, $join2, $arguments2);
+      // $find = do_search($keys, 'file', 'INNER JOIN {files} AS f ON f.`fid` = i.`sid` LEFT JOIN upload u USING (`fid`) LEFT JOIN node n USING (`nid`)'. $join1, $conditions1 . (empty($where1) ? '' : ' AND '. $where1), $arguments1, $select2, $join2, $arguments2);
  
 
       // Do search.
-      $find = do_search($keys, 'search_files_att');
+      $find = search_files_attachments_do_search($keys, array('search_files_att', 'node'));
 
       // Load results.
       $results = array();
       foreach ($find as $item) {
         // Build the node body.
-        $file = db_fetch_object(db_query("
+        if ($item->type == 'search_files_att') {
+          $file = db_fetch_object(db_query("
                                   SELECT f.*, d.data, u.nid
                                   FROM {files} AS f
                                   JOIN {upload} AS u
@@ -134,20 +135,46 @@ function search_files_attachments_search
                                   ON f.fid = d.sid
                                   WHERE fid = %d
                                 ", $item->sid));
-        $node = node_load($file->nid);
-        $realpath = realpath($file->filepath);
-        if (is_file($realpath)) {
-          $fileinfo = (object)pathinfo($realpath);
+          $node = node_load($file->nid);
+          $realpath = realpath($file->filepath);
+          if (is_file($realpath)) {
+            $fileinfo = (object)pathinfo($realpath);
+            $results[] = array(
+              'link' => url($file->filepath),
+              'title' => $file->filename,
+              'user' => theme('username', $node),
+              'date' => $file->timestamp,
+              'type' => check_plain(search_files_helper_name($fileinfo->extension)),
+              'node' => $node,
+              'score' => $item->score / $total,
+              'extra' => array(l(t('Node'), "node/" . $node->nid)),
+              'snippet' => search_excerpt($keys, $file->data),
+            );
+          }
+        } 
+        else {
+          // The search result relates to a node
+          $node = node_load($item->sid);
+          $node->build_mode = NODE_BUILD_SEARCH_RESULT;
+          $node = node_build_content($node, FALSE, FALSE);
+          $node->body = drupal_render($node->content);
+          
+          // Fetch comments for snippet.
+          $node->body .= module_invoke('comment', 'nodeapi', $node, 'update index');
+          // Fetch terms for snippet.
+          $node->body .= module_invoke('taxonomy', 'nodeapi', $node, 'update index');
+
+          $extra = node_invoke_nodeapi($node, 'search result');
           $results[] = array(
-            'link' => url($file->filepath),
-            'title' => $file->filename,
+            'link' => url('node/'. $item->sid, array('absolute' => TRUE)),
+            'type' => check_plain(node_get_types('name', $node)),
+            'title' => $node->title,
             'user' => theme('username', $node),
-            'date' => $file->timestamp,
-            'type' => check_plain(search_files_helper_name($fileinfo->extension)),
+            'date' => $node->changed,
             'node' => $node,
+            'extra' => $extra,
             'score' => $item->score / $total,
-            'extra' => array(l(t('Node'), "node/" . $node->nid)),
-            'snippet' => search_excerpt($keys, $file->data),
+            'snippet' => search_excerpt($keys, $node->body),
           );
         }
       }
@@ -265,3 +292,50 @@ function search_files_attachments_dashbo
   return $output;
 }
 
+function search_files_attachments_do_search($keywords, $types = array(), $join1 = '', $where1 = '1 = 1', $arguments1 = array(), $columns2 = 'i.relevance AS score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY score DESC') {
+  $query = search_parse_query($keywords);
+
+  if ($query[2] == '') {
+    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))));
+  }
+  if ($query[6]) {
+    if ($query[6] == 'or') {
+      drupal_set_message(t('Search for either of the two terms with uppercase <strong>OR</strong>. For example, <strong>cats OR dogs</strong>.'));
+    }
+  }
+  if ($query === NULL || $query[0] == '' || $query[2] == '') {
+    return array();
+  }
+
+  // Build query for keyword normalization.
+  $conditions = "$where1 AND ($query[2]) AND (i.type = '%s'" . str_repeat(" OR i.type='%s'", count($types) - 1) . ")";
+  $arguments1 = array_merge($arguments1, $query[3], $types);
+  $join = "INNER JOIN {search_total} t ON i.word = t.word $join1";
+  if (!$query[5]) {
+    $conditions .= " AND ($query[0])";
+    $arguments1 = array_merge($arguments1, $query[1]);
+    $join .= " INNER JOIN {search_dataset} d ON i.sid = d.sid AND i.type = d.type";
+  }
+
+  // Calculate maximum keyword relevance, to normalize it.
+  $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";
+  $arguments = array_merge($arguments1, array($query[4]));
+  $normalize = db_result(db_query_range($select, $arguments, 0, 1));
+  if (!$normalize) {
+    return array();
+  }
+  $columns2 = str_replace('i.relevance', '('. (1.0 / $normalize) .' * SUM(i.score * t.count))', $columns2);
+
+  // Build query to retrieve results.
+  $select = "SELECT i.type, i.sid, $columns2 FROM {search_index} i $join $join2 WHERE $conditions GROUP BY i.type, i.sid HAVING COUNT(*) >= %d";
+  $count_select =  "SELECT COUNT(*) FROM ($select) n1";
+  $arguments = array_merge($arguments2, $arguments1, array($query[4]));
+
+  // Do actual search query
+  $result = pager_query("$select $sort_parameters", 10, 0, $count_select, $arguments);
+  $results = array();
+  while ($item = db_fetch_object($result)) {
+    $results[] = $item;
+  }
+  return $results;
+}
