You are here

search_file_attachments.module in Search File Attachments 7

Same filename and directory in other branches
  1. 8 search_file_attachments.module

Contains the functionality for the Search File Attachments module.

File

search_file_attachments.module
View source
<?php

/**
 * @file
 * Contains the functionality for the Search File Attachments module.
 */

/**
 * Implements hook_menu().
 */
function search_file_attachments_menu() {
  $items['admin/config/search/file_attachments'] = array(
    'title' => 'Search File Attachments settings',
    'description' => 'Configure the file search settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'search_file_attachments_settings_form',
    ),
    'access arguments' => array(
      'administer search file attachments',
    ),
    'file' => 'search_file_attachments.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function search_file_attachments_permission() {
  return array(
    'administer search file attachments' => array(
      'title' => t('Administer search file attachments'),
    ),
  );
}

/**
 * Implements hook_search_info().
 */
function search_file_attachments_search_info() {
  return array(
    'title' => t('Files'),
    'path' => 'files',
  );
}

/**
 * Implements hook_search_reset().
 */
function search_file_attachments_search_reset() {

  // Reset the index status variables.
  variable_set('search_file_attachments_cron_last_change', 0);
  variable_set('search_file_attachments_cron_last_id', 0);

  // Empty the file content table, because all content will be extracted
  // again.
  db_truncate('search_file_attachments_index')
    ->execute();

  // Set rebuilt to the file datasets.
  db_update('search_dataset')
    ->fields(array(
    'reindex' => REQUEST_TIME,
  ))
    ->condition('type', 'file')
    ->execute();
}

/**
 * Implements hook_update_index().
 */
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);
  }
}

/**
 * Implements hook_search_status().
 */
function search_file_attachments_search_status() {
  $total = db_query('SELECT COUNT(*) FROM {file_managed} WHERE status = 1')
    ->fetchField();
  $remaining = db_query("SELECT COUNT(*) FROM {file_managed} f\n    LEFT JOIN {search_dataset} d ON d.type = 'file' AND d.sid = f.fid\n    WHERE f.status = 1 AND d.sid IS NULL OR d.reindex <> 0")
    ->fetchField();
  return array(
    'remaining' => $remaining,
    'total' => $total,
  );
}

/**
 * Implements hook_search_execute().
 */
function search_file_attachments_search_execute($keys, $conditions) {

  // Build matching conditions.
  $query = db_select('search_index', 'i', array(
    'target' => 'slave',
  ))
    ->extend('SearchQuery')
    ->extend('PagerDefault');
  $query
    ->fields('f', array(
    'fid',
    'uri',
    'filename',
    'filemime',
    'filesize',
  ));
  $query
    ->addField('sfa', 'content');
  $query
    ->join('file_managed', 'f', 'f.fid = i.sid');
  $query
    ->join('search_file_attachments_index', 'sfa', 'sfa.fid = i.sid');
  $query
    ->searchExpression($keys, 'file');
  $query
    ->setOption('filename', 'f.filename');

  // Only continue if the first pass query matches.
  if (!$query
    ->executeFirstPass()) {
    return array();
  }

  // Load results.
  $find = $query
    ->limit(10)
    ->execute();
  $results = array();
  foreach ($find as $item) {
    $uri = file_create_url($item->uri);
    $results[] = array(
      'title' => check_plain(ucfirst($item->filename)),
      'link' => url($uri, array(), array(
        'absolute' => TRUE,
      )),
      'type' => $item->filemime,
      'extra' => array(
        format_size($item->filesize),
      ),
      'snippet' => search_excerpt($keys, $item->content),
      'language' => LANGUAGE_NONE,
    );
  }
  return $results;
}

/**
 * Implements hook_file_delete().
 */
function search_file_attachments_file_delete($file) {

  // Delete the entry from the search index.
  search_reindex($file->fid, 'file');

  // Delete the file content cache entry.
  db_delete('search_file_attachments_index')
    ->condition('fid', $file->fid)
    ->execute();
}