--- search_files_attachments.module 2009-09-15 13:45:51.000000000 -0300 +++ search_files_attachmentsNew.module 2010-01-12 10:26:23.000000000 -0400 @@ -6,6 +6,44 @@ * 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 (is_object($fid)) { + $fid = get_object_vars($fid); + } + 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 (is_object($fid)) { + $fid = get_object_vars($fid); + } + + if ($fid['remove'] == 1) { + search_wipe($fid['fid'], 'search_files_att'); + } + } + } + break; + + } + +} + + /** * Implementation of hook_menu(). */ @@ -77,7 +115,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 +153,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,7 +166,7 @@ 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); @@ -146,7 +180,7 @@ function search_files_attachments_search '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 +197,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++;