'Attachments', 'description' => 'Configure Search Files Attachments Module', 'page callback' => 'search_files_attachments_dashboard', 'access arguments' => array('administer search_files configuration'), 'type' => MENU_NORMAL_ITEM, 'weight' => 1, ); $items['admin/settings/search_files/attachments/dashboard'] = array( 'title' => 'Dashboard', 'description' => 'Dashboard for Search Files in Attachments Module', 'page callback' => 'search_files_attachments_dashboard', 'access arguments' => array('administer search_files configuration'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => 0, ); $items['admin/settings/search_files/attachments/update_index'] = array( 'title' => 'Update index', 'description' => 'manually run hook update_index', 'page callback' => 'search_files_attachments_update_index', 'access arguments' => array('administer search_files configuration'), 'type' => MENU_CALLBACK, ); $items['admin/settings/search_files/attachments/settings'] = array( 'title' => 'Settings', 'description' => 'Change settings for Search Files Attachments Module', 'page callback' => 'drupal_get_form', 'page arguments' => array('search_files_attachments_settings_form'), 'access arguments' => array('administer search_files configuration'), 'type' => MENU_LOCAL_TASK, 'weight' => 1, ); return $items; } /** * Implementation of hook_search(). */ 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(" SELECT count(*) FROM {files} f JOIN {search_files_helpers} h ON SUBSTRING_INDEX(f.filename,'.',-1) = h.extension WHERE status = 1 ")); $remaining = db_result(db_query(" SELECT count(*) FROM {files} AS f JOIN {search_files_helpers} h ON SUBSTRING_INDEX(f.filename,'.',-1) = h.extension LEFT JOIN {search_dataset} AS d ON d.sid = f.fid WHERE ( d.type = 'search_files_att' AND f.status = 1 AND (d.sid IS NULL OR d.reindex <> 0) ) ")); return array('remaining' => $remaining, 'total' => $total); case 'search': // Build matching conditions list($join1, $where1) = _db_rewrite_sql('', 'f', 'files'); $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, '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'); // Load results. $results = array(); foreach ($find as $item) { // Build the node body. $file = db_fetch_object(db_query(" SELECT f.*, d.data, u.nid FROM {files} AS f JOIN {upload} AS u USING(fid) INNER JOIN {search_dataset} AS d 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); $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), ); } } return $results; } } /** * Implementation of hook_update_index(). */ function search_files_attachments_update_index() { variable_set('search_files_attachments_last_index', time()); $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 */ $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 JOIN {search_files_helpers} h ON SUBSTRING_INDEX(f.filename,'.',-1) = h.extension WHERE fid NOT IN ( SELECT sid FROM {search_dataset} AS d WHERE d.type = 'search_files_att' ) ORDER BY reindex ASC, fid ", 0, $limit); $found = $count = 0; while ($file = db_fetch_object($result)) { $found++; if (search_files_attachments_index_file($file)) $count++; } // if we were called manually from dashboard, return to where we come from if (preg_match('/search_files\/(attachments|directories)$/', $_SERVER['HTTP_REFERER'])) { search_files_update_totals('search_files_att'); drupal_goto($_SERVER['HTTP_REFERER']); } } /** * get files to index */ function search_files_attachments_index_file($file) { $contents = search_files_attachments_get_file_contents($file->filepath); if ($contents) $contents = search_files_convert_to_utf8("{$file->filename} ". $contents); search_index($file->fid, 'search_files_att', $contents); return $contents; } /** * get the file contents using the helpers configured in the search_files_module */ function search_files_attachments_get_file_contents($path) { $helpers = search_files_get_helpers(); $realpath = realpath($path); $pathinfo = (object)pathinfo($realpath); $file_exists = is_file($realpath); $helper_exists = array_key_exists($pathinfo->extension, $helpers); if ($helper_exists && $file_exists) { $cmd = preg_replace('/%file%/', escapeshellarg($realpath), $helpers[$pathinfo->extension]); $contents = shell_exec($cmd); return $contents; } return FALSE; } /** * generate the settings form for the search_files module using the * system_settings_form() function */ function search_files_attachments_settings_form() { $form = array(); $form['search_files_attachments_tab_label'] = array( '#title' => 'Search Label', '#type' => 'textfield', '#description' => 'What do you want the Search tab to be labeled?', '#default_value' => variable_get('search_files_attachments_tab_label', t('Attachments')), ); $form['search_files_attachments_tab_disabled'] = array( '#title' => t('Disable search attachments tab'), '#type' => 'checkbox', '#default_value' => variable_get('search_files_attachments_tab_disabled', FALSE), '#return_value' => 1, ); return system_settings_form($form); } /** * generate the search_files_directories dashboard page */ function search_files_attachments_dashboard() { $output = ''; $lastindex = variable_get('search_files_attachments_last_index', 0); if ($lastindex == 0) { $output .= sprintf("%s = %s
\n", t('Last Index'), t('never')); } else { $output .= sprintf("%s = %s
\n", t('Last Index'), format_date($lastindex, $type = 'custom', $format = 'Y-m-d H:i:s', $timezone = NULL, $langcode = NULL)); } $sql = "SELECT count(*) FROM {search_dataset} WHERE type = 'search_files_att'"; $result = db_query($sql); $result = db_result($result); $output .= sprintf("Files indexed = %s
\n", $result); $sql = "SELECT count(*) FROM {search_dataset} WHERE (type = 'search_files_att' AND reindex > 0)"; $result = db_query($sql); $result = db_result($result); $output .= sprintf("Files indexed and scheduled for reindexing = %s
\n", $result); $output .= l(t('Update index'), 'admin/settings/search_files/attachments/update_index'); $output .= "
\n"; return $output; }