--- search_files_attachments.module	2009-12-14 16:21:21.000000000 -0400
+++ search_files_attachmentsNew.module	2009-12-14 15:37:48.000000000 -0400
@@ -6,6 +6,39 @@
  * Used to index files in attachments
  */
 
+
+/**
+ * Implementation of hook_nodeapi().
+ */
+function search_files_attachments_nodeapi(&$node, $op) {
+  switch ($op) {
+    case 'delete':
+      // Remove any files from the search index
+      if (!empty($node->files)) {
+        foreach ($node->files as $fid) {
+          if (function_exists('search_wipe')) {
+            search_wipe($fid->fid, 'search_files_att');
+          }
+        }
+      }
+      break;
+
+    case 'presave':
+      // Remove files from the search index if needed.
+      if (!empty($node->files)) {
+        foreach ($node->files as $fid) {
+          if (function_exists('search_wipe') && $fid['remove'] == 1) {
+            search_wipe($fid['fid'], 'search_files_att');
+          }
+        }
+      }
+      break;
+
+  }
+
+}
+
+
 /**
  * Implementation of hook_menu().
  */
@@ -77,7 +110,7 @@ function search_files_attachments_search
 
     case 'search':
       // Build matching conditions
-      list($join1, $where1) = _db_rewrite_sql('', 'f', 'files');
+      list($join1, $where1) = _db_rewrite_sql();
 
       $arguments1 = array();
       $conditions1 = 'f.status = 1';
@@ -115,12 +148,8 @@ 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);
- 
-
-      // Do search.
-      $find = do_search($keys, 'search_files_att');
-
+      $find = do_search($keys, 'search_files_att', 'INNER JOIN {files} f ON f.fid = i.sid INNER JOIN {upload} u ON u.fid = f.fid INNER JOIN {node} n ON n.vid = u.vid '. $join1, $conditions1 . (empty($where1) ? '' : ' AND '. $where1), $arguments1, $select2, $join2, $arguments2);
+      
       // Load results.
       $results = array();
       foreach ($find as $item) {
@@ -132,21 +161,21 @@ function search_files_attachments_search
                                   USING(fid)
                                   INNER JOIN {search_dataset} AS d
                                   ON f.fid = d.sid
-                                  WHERE fid = %d
+                                  WHERE fid = %d AND d.type = 'search_files_att'
                                 ", $item->sid));
         $node = node_load($file->nid);
         $realpath = realpath($file->filepath);
         if (is_file($realpath)) {
           $fileinfo = (object)pathinfo($realpath);
           $results[] = array(
-            'link' => url($file->filepath),
+            'link' => file_create_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)),
+            'extra' => array(t('Attached to ') . node_get_types('name', $node) . ' ' . l(t($node->title), 'node/' . $node->nid)),
             'snippet' => search_excerpt($keys, $file->data),
           );
         }
@@ -163,27 +192,16 @@ function search_files_attachments_update
   $limit = (int)variable_get('search_cron_limit', 100);
 
   /* select known files related to search_files_att and marked for reindexing UNION
-   *        known files not related to search_files_att
+   *        or any new files not yet indexed
    */
   $result = db_query_range("
-              SELECT f.fid, f.filepath, d.reindex
-              FROM {files} AS f
-              LEFT JOIN {search_dataset} AS d
-              ON d.sid = f.fid
-              WHERE (
-                d.type = 'search_files_att' AND
-                d.reindex <> 0
-              )
-              UNION DISTINCT
-              SELECT f.fid, f.filepath, NULL
-              FROM {files} AS f
-              WHERE fid NOT IN (
-                SELECT sid
-                FROM {search_dataset} AS d
-                WHERE d.type = 'search_files_att'
-              )
-              ORDER BY reindex ASC, fid
-            ", 0, $limit);
+    SELECT f.fid, f.filepath, d.reindex
+    FROM {node} n, {upload} u, {files} f
+    LEFT JOIN {search_dataset} d
+    ON d.type = 'search_files_att' AND d.sid = f.fid
+    WHERE (d.sid IS NULL OR d.reindex <> 0) AND n.vid = u.vid AND u.fid = f.fid
+    ORDER BY d.reindex ASC, f.fid ASC", 0, $limit);
+
   $found = $count = 0;
   while ($file = db_fetch_object($result)) {
     $found++;
