You are here

function search_files_update_index in Search Files 5

Implementation of hook_update_index()

lists all the files in the director(y/ies) and puts the files into the "search_files_files" table

then indexes X(configurable) number of these files

1 string reference to 'search_files_update_index'
search_files_menu in ./search_files.module
Implementation of hook_menu()

File

./search_files.module, line 671
Used to index all files in directory(s) on the server

Code

function search_files_update_index() {
  $helpers = search_files_get_helpers();

  // only update the list of files in the directories once per day
  if (variable_get('search_files_last_index', 0) < time() - 86400) {
    variable_set('search_files_last_index', time());
    $result = db_query('SELECT * FROM {search_files_directories}');
    while ($directory = db_fetch_object($result)) {
      search_files_list_directory($directory->directory, $directory->id);
    }
  }
  $index_number = (int) variable_get('search_cron_limit', 100);
  $sql = "\n      SELECT \n        *\n      FROM \n        {search_files_files}\n      LEFT JOIN \n      (\n        SELECT \n          {search_dataset}.sid sid ,type,data,{search_files_reindex}.reindex reindex\n        FROM\n          {search_dataset},{search_files_reindex}\n        WHERE \n          type = 'search_files'\n        AND\n          {search_dataset}.sid = {search_files_reindex}.sid\n      ) AS dataset ON {search_files_files}.id = dataset.sid \n      WHERE\n      (\n        dataset.reindex IS NULL OR\n        dataset.reindex != 0\n      ) AND {search_files_files}.index_attempts <= 5\n      LIMIT %s\n    ";
  $result = db_query($sql, $index_number);
  while ($file = db_fetch_object($result)) {
    $full_path = $file->full_path;
    $file_name = explode('/', $full_path);
    $file_name = $file_name[count($file_name) - 1];
    $file_extension = explode('.', $file_name);
    $file_extension = $file_extension[count($file_extension) - 1];
    if (in_array($file_extension, array_keys($helpers))) {

      // record that we are attempting to index the file in case it hangs
      $increment_sql = "\n        UPDATE\n          {search_files_files}\n        SET\n          index_attempts = index_attempts + 1\n        WHERE\n          id = '%s'\n      ";
      $increment_result = db_query($increment_sql, $file->id);
      if ($file->index_attempts >= 5) {

        // indexind failed too many times, record this to the log and continue
        watchdog('Search Files', t('failed to index %full_path after %attempts attempts', array(
          '%full_path' => $file->full_path,
          '%attempts' => $file->index_attempts,
        )), array(), WATCHDOG_ERROR);
        continue;
      }

      // %file% is a token that is placed in the helper's parameter list to represent the file path to the attachment.
      // We need to put the filename in quotes in case it contains spaces.
      $quoted_file_path = '"' . escapeshellcmd($full_path) . '"';
      $helper_command = preg_replace('/%file%/', $quoted_file_path, $helpers[$file_extension]);
      $file_text = shell_exec($helper_command);
      $file_text = search_files_convert_to_utf8($file_text);
      search_index($file->id, 'search_files', 'file name: ' . $quoted_file_path . ', text: ' . $file_text);

      /* DRUPAL 5 @ivazquez: */
      db_query("UPDATE {search_files_reindex} set reindex = 0 where sid=%d", $file->id);
      if (!db_affected_rows()) {
        db_query("INSERT INTO {search_files_reindex} (sid, reindex) values (%d,'%s') ", $file->id, 0);
      }
    }
    else {
      search_index($file->id, 'search_files', '');

      /* DRUPAL 5 @ivazquez: */
      db_query("UPDATE {search_files_reindex} set reindex = 0 where sid=%d", $file->id);
      if (!db_affected_rows()) {
        db_query("INSERT INTO {search_files_reindex} (sid, reindex) values (%d,'%s') ", $file->id, 0);
      }
    }
  }
}