You are here

function search_file_attachments_update_index in Search File Attachments 7

Implements hook_update_index().

File

./search_file_attachments.module, line 66
Contains the functionality for the Search File Attachments module.

Code

function search_file_attachments_update_index() {
  global $_search_file_attachments_last_change, $_search_file_attachments_last_id;
  require_once dirname(__FILE__) . '/search_file_attachments.inc';
  register_shutdown_function('search_file_attachments_shutdown');
  $_search_file_attachments_last_change = variable_get('search_file_attachments_cron_last_change', 0);
  $_search_file_attachments_last_id = variable_get('search_file_attachments_cron_last_id', 0);
  $index_files = search_file_attachments_get_files($_search_file_attachments_last_change, $_search_file_attachments_last_id);
  $included_extensions = search_file_extensions_included_extensions();
  $included_mimetypes = search_file_attachments_extensions_to_mimetypes($included_extensions);
  foreach ($index_files as $file) {

    // We index all files in order of file ID, going back to reindex old ones
    // only if they were edited since the last time files were indexed. See
    // search_file_attachments_get_files().
    if ($file->fid > $_search_file_attachments_last_id) {
      $_search_file_attachments_last_id = $file->fid;
      $_search_file_attachments_last_change = REQUEST_TIME;
    }
    else {

      // We are reindexing an old file. Use that as the last change, so that
      // any other old files edited after it also get reindexed.
      $_search_file_attachments_last_change = $file->timestamp;
    }
    if (in_array($file->filemime, $included_mimetypes)) {
      $content = t('Filename') . ': ' . $file->filename . ' - ' . t('Content') . ': ';

      // Extract the file content and add it to the drupal search index.
      $extracted_content = search_file_attachments_get_file_content($file);
      $content .= $extracted_content;
      search_index($file->fid, 'file', $content);

      // Cache the extracted file content to use it later for the
      // search results.
      db_merge('search_file_attachments_index')
        ->key(array(
        'fid' => $file->fid,
      ))
        ->fields(array(
        'content' => $extracted_content,
      ))
        ->execute();
    }
    variable_set('search_file_attachments_cron_last_change', $_search_file_attachments_last_change);
    variable_set('search_file_attachments_cron_last_id', $_search_file_attachments_last_id);
  }
}