You are here

function search_files_directories_search in Search Files 6.2

Implementation of hook_search().

File

./search_files_directories.module, line 83
Used to index files in directories

Code

function search_files_directories_search($op = 'search', $keywords = NULL) {
  switch ($op) {
    case 'name':

      // do not display tab unless user has access to view results, also allow for disabling the search tab (for use with search_combine)
      if (user_access('view search_files results') && !variable_get('search_files_directories_tab_disabled', FALSE)) {
        return variable_get('search_files_directories_tab_label', t('Directories'));
      }
    case 'reset':
      db_query("UPDATE {search_dataset} SET reindex = %d WHERE type = 'search_files_dir'", time());
      db_query("UPDATE {search_files_directories_files} SET index_attempts = 0");
      break;
    case 'status':
      $return = array();
      $return['total'] = db_result(db_query("SELECT count(*) AS count FROM {search_files_directories_files}"));
      $sql = "\n        SELECT count(*) AS count\n        FROM {search_files_directories_files} AS files\n        LEFT JOIN (\n          SELECT *\n          FROM {search_dataset}\n          WHERE type = 'search_files_dir'\n        ) AS d\n        ON files.id = d.sid\n        WHERE (\n          d.reindex IS NULL OR\n          d.reindex != 0\n        )\n      ";
      $return['remaining'] = db_result(db_query($sql));
      return $return;
      break;
    case 'search':
      if (!user_access('view search_files results')) {
        return NULL;
      }
      $doc_root = getcwd();
      $file_extensions = search_files_get_file_extensions();
      $results = array();
      $find = do_search($keywords, 'search_files_dir');
      date_default_timezone_set('GMT');
      foreach ($find as $item) {
        $sql = "\n          SELECT concat(d.filepath, '/', f.path) AS path, concat(d.uripath, '/', f.path) AS uripath\n          FROM {search_files_directories_files} AS f\n          LEFT JOIN (\n            SELECT id, filepath, uripath\n            FROM {search_files_directories_directories}\n          ) AS d\n          ON f.directory_id = d.id\n          WHERE f.id = %d\n        ";
        $result = db_fetch_object(db_query($sql, $item->sid));
        $search_results = db_query("SELECT * FROM {search_dataset} WHERE (sid = %d AND type = 'search_files_dir')", $item->sid);
        if ($dataset = db_fetch_object($search_results)) {
          $file_name = explode('/', $result->path);
          $file_name = $file_name[count($file_name) - 1];
          $file_extension = explode('.', $file_name);
          $file_extension = $file_extension[count($file_extension) - 1];
          $link = $result->uripath;

          // skip over files which vanished after being indexed - cleanup is left to search_files_update_index()
          if (file_exists($result->path)) {
            $results[] = array(
              'link' => $link,
              'date' => filectime($result->path),
              'type' => $file_extensions[$file_extension] . ' file',
              'title' => $file_name . ' (' . format_size(filesize($result->path)) . ')',
              'snippet' => search_excerpt($keywords, $dataset->data),
            );
          }
        }
        date_default_timezone_set('MST');
      }
      return $results;
      break;
  }
}