You are here

search_files_attachments.module in Search Files 7.2

Same filename and directory in other branches
  1. 6.2 search_files_attachments.module

Used to index files in attachments

File

search_files_attachments.module
View source
<?php

/**
 * @file
 * Used to index files in attachments
 */

/**
 * Implements hook_menu().
 */
function search_files_attachments_menu() {
  $items = array();
  $items['admin/config/search/search_files/attachments'] = array(
    'title' => '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/config/search/search_files/attachments/dashboard'] = array(
    'title' => 'Dashboard',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  );
  $items['admin/config/search/search_files/attachments/update_index'] = array(
    'title' => 'Update index',
    'description' => 'Manually runs hook update_index',
    'page callback' => 'search_files_attachments_update_index',
    'page arguments' => array(
      'back' => 'admin/config/search/search_files/attachments',
    ),
    'access arguments' => array(
      'administer search_files configuration',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/search/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;
}

/**
 * Implements hook_search_access().
 */
function search_files_attachments_search_access() {
  return user_access('view search_files results');
}

/**
 * Implements hook_search_info().
 */
function search_files_attachments_search_info() {
  return array(
    'title' => variable_get('search_files_attachments_tab_label', t('Attachments')),
    'path' => 'attachments',
    'conditions_callback' => 'search_files_attachments_conditions_callback',
  );
}

/**
 * Implements hook_search_admin().
 */
function search_files_attachments_search_admin() {
}

/**
 * Implements hook_search_execute().
 *
 * Build a file list whose content matches $keys
 */
function search_files_attachments_search_execute($keys = NULL, $conditions = NULL) {
  if (!user_access('view search_files results')) {
    return array();
  }

  // Select attached files id from content with attached fields.
  $field_id_list = _search_files_attachments_create_field_id_list();

  // Only continue if attached files found.
  if (count($field_id_list) == 0) {
    return array();
  }

  // Search $keys expression in the selected files
  $query = db_select('search_index', 'i', array(
    'target' => 'slave',
  ))
    ->extend('SearchQuery')
    ->extend('PagerDefault');
  $query
    ->join('search_dataset', 'd', 'd.sid = i.sid');
  $query
    ->join('file_managed', 'm', 'm.fid = d.sid');
  $query
    ->fields('i', array(
    'score',
  ))
    ->fields('d', array(
    'data',
  ))
    ->fields('m', array(
    'filename',
    'uri',
  ))
    ->condition('i.sid', $field_id_list, 'IN')
    ->condition('i.type', 'search_files_att');
  $query
    ->searchExpression($keys, 'search_files_att');

  // 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) {
    $results[] = array(
      'link' => file_create_url($item->uri),
      'title' => $item->filename,
      'score' => $item->score,
      'snippet' => search_excerpt($keys, $item->data),
    );
  }
  return $results;
}

/**
 * Implementation of hook_search_reset().
 */
function search_files_attachments_search_reset() {
  db_update('search_dataset')
    ->fields(array(
    'reindex' => REQUEST_TIME,
  ))
    ->condition('type', 'search_files_att')
    ->execute();
}

/**
 * 'conditions_callback': Name of a callback function that is invoked by
 * search_view() to get an array of additional search conditions to pass to
 * search_data(). For example, a search module may get additional keywords,
 * filters, or modifiers for the search from the query string.
 *
 * @see sample_search_conditions_callback()
 */
function search_files_attachments_conditions_callback() {

  //dpm(__function__, 'insert code here');
}

/**
 * Create a field id list from all field tables.
 *
 * These id are used in {search_dataset} and {search_index} tables.
 */
function _search_files_attachments_create_field_id_list($nid = NULL) {
  $query = db_select('file_usage', 'f')
    ->distinct()
    ->fields('f', array(
    'fid',
  ))
    ->condition('module', 'file')
    ->condition('type', 'node')
    ->condition('count', 0, '>');
  if (isset($nid)) {
    $query
      ->condition('id', $nid);
  }
  return $query
    ->execute()
    ->fetchCol();
}

/**
 * Implements hook_update_index().
 *
 * @param $goto
 *   (optional) drupal path - if this is called from the menu system we need to
 *   go back to $goto. If this is called from cron, cron has to go on.
 */
function search_files_attachments_update_index($goto = FALSE) {
  variable_set('search_files_attachments_last_index', REQUEST_TIME);
  $public_file_dir = variable_get('file_public_path', 'sites/default/files');
  $limit = (int) variable_get('search_cron_limit', 100);

  // Select attached files related to search_files_att and marked to re-index.
  // UNION known files not yet related to 'search_files_att'.
  $field_id_list = _search_files_attachments_create_field_id_list();
  if (count($field_id_list) > 0) {
    $query_file = "\n      SELECT f.fid, f.uri AS filepath, f.filename, d.reindex FROM {file_managed} f\n      LEFT JOIN {search_dataset} d ON d.sid = f.fid\n      WHERE (f.fid in (:fid)\n      AND d.type = 'search_files_att'\n      AND (d.reindex != 0 OR d.reindex IS NULL))\n      UNION DISTINCT\n      SELECT f.fid, f.uri AS filepath, f.filename , NULL FROM {file_managed} f\n      WHERE f.fid NOT IN (\n        SELECT sid FROM {search_dataset} d\n        WHERE d.type = 'search_files_att'\n      )\n      ORDER BY reindex ASC, fid";
    $query_result = db_query_range($query_file, 0, $limit, array(
      ':fid' => $field_id_list,
    ));
    $found = $count = 0;
    foreach ($query_result as $file) {
      $found++;
      if (search_files_attachments_index_file($file)) {
        $count++;
      }
    }
  }

  // if we were called manually from dashboard, return to where we come from
  if ($goto) {
    search_files_update_totals('search_files_att');
    drupal_goto($goto);
  }
}

/**
 * Indexing file contents.
 */
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.
 */
function search_files_attachments_get_file_contents($path) {
  $helpers = search_files_get_helpers();
  $wrapper = file_stream_wrapper_get_instance_by_uri($path);
  $realpath = $wrapper
    ->realpath();
  $pathinfo = (object) pathinfo($realpath);
  if (isset($helpers[$pathinfo->extension]) && is_file($realpath)) {
    $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) {
  $form['search_files_attachments_tab_label'] = array(
    '#title' => t('Search Label'),
    '#type' => 'textfield',
    '#description' => t('What do you want the Search tab to be labeled?'),
    '#default_value' => variable_get('search_files_attachments_tab_label', t('Attachments')),
  );
  return system_settings_form($form);
}

/**
 * Generates the search_files_directories dashboard page.
 */
function search_files_attachments_dashboard() {
  $lastindex = variable_get('search_files_attachments_last_index', 0);
  $items = array();
  if ($lastindex == 0) {
    $items[] = t('Last Index = %index', array(
      '%index' => t('never'),
    ));
  }
  else {
    $items[] = t('Last Index = %index', array(
      '%index' => format_date($lastindex, $type = 'medium'),
    ));
  }
  $count = db_query("SELECT COUNT(*) FROM {search_dataset} WHERE type = 'search_files_att'")
    ->fetchField();
  $items[] = t("Files indexed = %count", array(
    '%count' => $count,
  ));
  $count = db_query("SELECT COUNT(*) FROM {search_dataset} WHERE (type = 'search_files_att' AND reindex > 0)")
    ->fetchField();
  $items[] = t("Files indexed and scheduled for reindexing = %count", array(
    '%count' => $count,
  ));
  $items[] = l(t('Update index'), 'admin/config/search/search_files/attachments/update_index');
  return array(
    '#theme' => 'item_list',
    '#items' => $items,
  );
}

/*
 * Implements hook_node_delete()
 */
function search_files_attachments_node_delete($node) {
  $attach_file_list = _search_files_attachments_create_field_id_list($node->nid);
  db_delete('search_dataset')
    ->condition('sid', $attach_file_list, 'IN')
    ->execute();
}

Functions

Namesort descending Description
search_files_attachments_conditions_callback 'conditions_callback': Name of a callback function that is invoked by search_view() to get an array of additional search conditions to pass to search_data(). For example, a search module may get additional keywords, filters, or modifiers for…
search_files_attachments_dashboard Generates the search_files_directories dashboard page.
search_files_attachments_get_file_contents Get the file contents using the helpers.
search_files_attachments_index_file Indexing file contents.
search_files_attachments_menu Implements hook_menu().
search_files_attachments_node_delete
search_files_attachments_search_access Implements hook_search_access().
search_files_attachments_search_admin Implements hook_search_admin().
search_files_attachments_search_execute Implements hook_search_execute().
search_files_attachments_search_info Implements hook_search_info().
search_files_attachments_search_reset Implementation of hook_search_reset().
search_files_attachments_settings_form generate the settings form for the search_files module using the system_settings_form() function
search_files_attachments_update_index Implements hook_update_index().
_search_files_attachments_create_field_id_list Create a field id list from all field tables.