You are here

function apachesolr_attachments_node_solr_document in Apache Solr Attachments 7

Builds the file-specific information for a Solr document.

Parameters

ApacheSolrDocument $document: The Solr document we are building up.

stdClass $entity: The entity we are indexing.

string $entity_type: The type of entity we're dealing with.

1 string reference to 'apachesolr_attachments_node_solr_document'
apachesolr_attachments_apachesolr_entity_info_alter in ./apachesolr_attachments.module
@file Indexer for the userhook_apachesolr_entity_info_alter entities for the Apachesolr module.

File

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

Code

function apachesolr_attachments_node_solr_document(ApacheSolrDocument &$document, $parent_entity, $env_id) {
  module_load_include('inc', 'apachesolr_attachments', 'apachesolr_attachments.index');
  list($parent_entity_id, $parent_entity_vid, $parent_entity_bundle) = entity_extract_ids('node', $parent_entity);

  // proceed with combining attachment content to entity document only if the
  // parent entity is flagged for indexing attachments with parent entity
  if (variable_get('apachesolr_attachments_entity_bundle_indexing_' . $parent_entity_bundle, 'seperate') == 'parent') {
    $file_field_names = array();
    $fields = field_info_field_by_ids();
    if (is_array($fields)) {
      foreach ($fields as $field_id => $field_info) {
        if ($field_info['type'] == 'file') {
          foreach ($field_info['bundles'] as $entity_type => $bundles) {
            if (in_array($parent_entity_bundle, $bundles)) {
              $file_field_names[$field_info['field_name']] = $field_info['field_name'];
            }
          }
        }
      }
    }
    foreach ($file_field_names as $file_field) {
      if (isset($parent_entity->{$file_field})) {
        $parent_entity_file_fields = $parent_entity->{$file_field};

        //@todo deal with different languages properly
        foreach ($parent_entity_file_fields as $language => $files) {
          foreach ($files as $file) {
            $file = (object) $file;

            // perform some basic validation that the file is ok to extract text from
            $status = $file->status == 1 ? 1 : 0;

            // Check if the mimetype is allowed
            $status = $status & apachesolr_attachments_is_file($file);
            $status = $status & apachesolr_attachments_allowed_mime($file->filemime);
            if ($status) {
              $text = apachesolr_attachments_get_attachment_text($file);
              if ($text) {

                // append extracted text to index content field
                $document->content .= apachesolr_clean_text($file->filename) . ' ' . $text;
              }
            }
          }
        }
      }
    }
  }
  return array();

  // all alterations are made to $document passed in by reference
}