You are here

function apachesolr_attachments_add_documents in Apache Solr Attachments 6

Same name and namespace in other branches
  1. 6.2 apachesolr_attachments.admin.inc \apachesolr_attachments_add_documents()

Callback for apachesolr_index_nodes().

Adds a document for each indexable file attachment for the given node ID.

1 string reference to 'apachesolr_attachments_add_documents'
apachesolr_attachments_update_index in ./apachesolr_attachments.module
Hook is called by search.module to add things to the search index. In our case we will search content types and add any CCK type that is a file type that we know how to parse and any uploaded file attachments.

File

./apachesolr_attachments.admin.inc, line 230
Provides a file attachment search implementation for use with the Apache Solr module

Code

function apachesolr_attachments_add_documents(&$documents, $nid, $namespace = 'apachesolr_attachments') {
  $node = node_load($nid, NULL, TRUE);
  if (!empty($node->nid)) {
    $hash = apachesolr_site_hash();

    // Let any module exclude this node from the index.
    $build_document = TRUE;
    foreach (module_implements('apachesolr_node_exclude') as $module) {
      $exclude = module_invoke($module, 'apachesolr_node_exclude', $node, $namespace);
      if (!empty($exclude)) {
        $build_document = FALSE;
      }
    }
    if ($build_document) {

      // Since there is no notification for an attachment being unassociated with a
      // node (but that action will trigger it to be indexed again), we check for
      // fids that were added before but no longer present on this node.
      $fids = array();
      $result = db_query("SELECT fid FROM {apachesolr_attachments_files} WHERE nid = %d", $node->nid);
      while ($row = db_fetch_array($result)) {
        $fids[$row['fid']] = $row['fid'];
      }
      $files = apachesolr_attachments_get_indexable_files($node);

      // Find deleted files.
      $missing_fids = array_diff_key($fids, $files);
      if ($missing_fids) {
        db_query("UPDATE {apachesolr_attachments_files} SET removed = 1 WHERE fid IN (" . db_placeholders($missing_fids) . ")", $missing_fids);
      }
      $new_files = array_diff_key($files, $fids);

      // Add new files.
      foreach ($new_files as $file) {
        db_query("INSERT INTO {apachesolr_attachments_files} (fid, nid, removed, sha1) VALUES (%d, %d, 0, '')", $file->fid, $node->nid);
      }
      foreach ($files as $file) {
        $text = apachesolr_attachments_get_attachment_text($file);
        if ($text) {
          $document = new Apache_Solr_Document();

          // A single file might be attached to multiple nodes.
          $document->id = apachesolr_document_id($file->fid . '-' . $node->nid, 'file');
          $document->url = file_create_url($file->filepath);
          $document->path = $file->filepath;
          $document->hash = $hash;
          $document->entity = 'file';
          $document->site = url(NULL, array(
            'absolute' => TRUE,
          ));
          $document->nid = $node->nid;
          $document->title = $file->filename;
          $document->created = apachesolr_date_iso($file->timestamp);
          $document->changed = $document->created;
          $document->status = $node->status;
          $document->sticky = $node->sticky;
          $document->promote = $node->promote;
          $document->uid = $node->uid;
          $document->name = $node->name;
          if (empty($node->language)) {

            // 'und' is the language-neutral code in Drupal 7.
            $document->language = 'und';
          }
          else {
            $document->language = $node->language;
          }
          $document->body = $file->filename . ' ' . apachesolr_clean_text($file->description) . ' ' . $text;
          $document->ss_filemime = $file->filemime;
          $document->ss_file_node_title = apachesolr_clean_text($node->title);
          $document->ss_file_node_url = url('node/' . $node->nid, array(
            'absolute' => TRUE,
          ));
          apachesolr_add_taxonomy_to_document($document, $node);

          // Let modules add to the document.
          foreach (module_implements('apachesolr_update_index') as $module) {
            $function = $module . '_apachesolr_update_index';
            $function($document, $node, $namespace);
          }
          drupal_alter('apachesolr_attachment_index', $document, $node, $file, $namespace);
          $documents[] = $document;
        }
        else {

          // Shortened project name because the watchdog limits type to 16 characters.
          watchdog('ApacheSolrAttach', 'Could not extract any indexable text from %filepath', array(
            '%filepath' => $file->filepath,
          ), WATCHDOG_WARNING);
        }
      }
    }
  }
}