You are here

function search_files_search in Search Files 5

implementation of hook_search()

Parameters

(string) $op:

(string) $keywords:

Return value

(mixed)

File

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

Code

function search_files_search($op = 'search', $keywords = null) {

  // drupal_set_message('FUNCTION = <pre>search_files_search</pre>');
  switch ($op) {
    case 'name':
      return variable_get('search_files_label', 'Server Files');
    case 'reset':

      /*  $sql="
            UPDATE
              {search_dataset}
            SET
              `reindex`='%s'
            WHERE
              `type`='search_files'
          "; */

      //MIGRACION A VERSION 5 @ivazquez:
      $sql = "\n            UPDATE \n              {search_files_reindex}\n            SET\n              reindex='%s'\n           ";
      $result = db_query($sql, time());
      db_query("UPDATE {search_files_files} SET index_attempts='0' WHERE 1=1");
      break;
    case 'status':
      $return = array();
      $sql = "SELECT COUNT(*) as count FROM {search_files_files}";
      $return['total'] = db_result(db_query($sql));

      /*$sql = "
          SELECT
            COUNT(*) AS `count`
          FROM
            {search_files_files}
          LEFT JOIN
          (
            SELECT
              *
            FROM
              {search_dataset}
            WHERE
              `type` = 'search_files'
          ) AS `dataset` ON {search_files_files}.`id` = `dataset`.`sid`
          WHERE
          (
            `dataset`.`reindex` IS NULL OR
            `dataset`.`reindex` != 0
          )
        ";*/
      $sql = "\n          SELECT \n          COUNT(*) AS count\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          )\n          ";
      $return['remaining'] = db_result(db_query($sql));
      return $return;
      break;
    case 'search':

      //drupal_set_message('$keywords = '.$keywords);
      if (!user_access('view search_files results')) {
        return null;
      }
      $doc_root = $_SERVER['DOCUMENT_ROOT'];

      //drupal_set_message($doc_root);
      $file_extensions = search_files_get_file_extensions();
      $results = array();
      $find = do_search($keywords, 'search_files');

      //drupal_set_message('find = <pre>'.var_export($find, true).'</pre>');

      //      date_default_timezone_set('GMT');
      foreach ($find as $item) {

        //drupal_set_message('item = <pre>'. var_export($item, true).'</pre>');
        $sql = "\n          SELECT\n            *\n          FROM\n            {search_files_files}\n          WHERE\n            id='%s'\n        ";
        $result = db_fetch_object(db_query($sql, $item->sid));
        $search_results = db_query("SELECT * FROM {search_dataset} s WHERE sid = %d AND type = '%s'", $item->sid, 'search_files');
        if ($dataset = db_fetch_object($search_results)) {

          //drupal_set_message('result = <pre>'.print_r($result, true).'</pre>');
          $file_name = explode('/', $result->full_path);
          $file_name = $file_name[count($file_name) - 1];
          $file_extension = explode('.', $file_name);
          $file_extension = $file_extension[count($file_extension) - 1];
          $link = str_replace($doc_root, '', $result->full_path);

          // we have had a problem where some links were returned relative to the search page,
          // not the document root, basically there was no beginning forward slash '/' in the
          // path
          if (strpos($link, '/') !== 0) {
            $link = '/' . $link;
          }
          $results[] = array(
            'link' => $link,
            'date' => filectime($result->full_path),
            'type' => $file_extensions[$file_extension] . ' file',
            'title' => $file_name . ' (' . format_size(filesize($result->full_path)) . ')',
            'snippet' => search_excerpt($keywords, $dataset->data),
          );
        }

        //date_default_timezone_set('MST');
      }

      //drupal_set_message('results = <pre>'.var_export($results, true).'</pre>');
      return $results;
      break;
  }
}