function search_files_attachments_search in Search Files 6.2
Implementation of hook_search().
File
- ./
search_files_attachments.module, line 87 - Used to index files in attachments
Code
function search_files_attachments_search($op = 'search', $keys = NULL) {
switch ($op) {
case 'name':
if (user_access('view search_files results') && !variable_get('search_files_attachments_tab_disabled', FALSE)) {
return variable_get('search_files_attachments_tab_label', t('Attachments'));
}
break;
case 'reset':
db_query("UPDATE {search_dataset} SET reindex = %d WHERE type = 'search_files_att'", time());
return;
case 'status':
$total = db_result(db_query("\n SELECT count(*) FROM {files} f\n JOIN {search_files_helpers} h\n ON SUBSTRING_INDEX(f.filename,'.',-1) = h.extension\n WHERE status = 1\n "));
$remaining = db_result(db_query("\n SELECT count(*)\n FROM {files} AS f\n JOIN {search_files_helpers} h\n ON SUBSTRING_INDEX(f.filename,'.',-1) = h.extension\n LEFT JOIN {search_dataset} AS d\n ON d.sid = f.fid\n WHERE (\n d.type = 'search_files_att'\n AND f.status = 1\n AND (d.sid IS NULL OR d.reindex <> 0)\n )\n "));
return array(
'remaining' => $remaining,
'total' => $total,
);
case 'search':
// Build matching conditions
list($join1, $where1) = _db_rewrite_sql();
$arguments1 = array();
$conditions1 = 'f.status = 1';
$ranking = array();
$arguments2 = array();
$join2 = '';
$total = 0;
// base rankings off node rank settings
if ($weight = (int) variable_get('node_rank_relevance', 5)) {
// Average relevance values hover around 0.15
$ranking[] = '%d * i.relevance';
$arguments2[] = $weight;
$total += $weight;
}
if ($weight = (int) variable_get('node_rank_recent', 5)) {
// Exponential decay with half-life of 6 months, starting at last indexed node
$ranking[] = '(%d * POW(2, (f.timestamp) - %d) * 6.43e-8)';
$arguments2[] = $weight;
$arguments2[] = (int) variable_get('node_cron_last', 0);
$total += $weight;
}
// When all search factors are disabled (ie they have a weight of zero),
// the default score is based only on keyword relevance and there is no need to
// adjust the score of each item.
if ($total == 0) {
$select2 = 'i.relevance AS score';
$total = 1;
}
else {
$select2 = implode(' + ', $ranking) . ' AS score';
}
// Do search.
$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();
$search_queries = array();
$upload_module = module_exists('upload');
$filefield_module = module_exists('filefield');
$file_fields = array();
if ($upload_module) {
$search_queries[] = "SELECT f.*, d.data, u.nid\n FROM {files} AS f\n JOIN {upload} AS u\n USING(fid)\n INNER JOIN {search_dataset} AS d\n ON f.fid = d.sid\n WHERE fid = %d AND d.type = 'search_files_att'";
}
if ($filefield_module) {
$fields = content_fields();
foreach ($fields as $field) {
if ($field['type'] == 'filefield') {
$field_info = content_database_info($field);
$search_queries[] = "SELECT f.*, d.data, u.nid\n FROM {files} AS f\n JOIN {" . $field_info['table'] . "} AS u\n ON u." . $field_info['columns']['fid']['column'] . " = f.fid\n INNER JOIN {search_dataset} AS d\n ON f.fid = d.sid\n WHERE fid = %d";
}
}
}
foreach ($find as $item) {
// Build the node body.
foreach ($search_queries as $search_query) {
$file = db_fetch_object(db_query($search_query, $item->sid));
$node = node_load($file->nid);
$realpath = realpath($file->filepath);
if (is_file($realpath)) {
$fileinfo = (object) pathinfo($realpath);
$results[] = array(
'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(
t('Attached to ') . node_get_types('name', $node) . ' ' . l(t($node->title), 'node/' . $node->nid),
),
'snippet' => search_excerpt($keys, $file->data),
);
}
}
}
return $results;
}
}