You are here

function apachesolr_node_to_document in Apache Solr Search 6.2

Same name and namespace in other branches
  1. 5.2 apachesolr.index.inc \apachesolr_node_to_document()
  2. 6 apachesolr.index.inc \apachesolr_node_to_document()

Given a node, return a document representing that node.

1 string reference to 'apachesolr_node_to_document'
apachesolr_search_apachesolr_document_handlers in ./apachesolr_search.module
Implementation of hook_apachesolr_document_handlers().

File

./apachesolr.index.inc, line 21
Functions used when indexing content to Apache Solr.

Code

function apachesolr_node_to_document($node, $namespace) {
  $document = FALSE;

  // 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) {

    // Build the node body.
    $node->build_mode = NODE_BUILD_SEARCH_INDEX;
    $node = node_build_content($node, FALSE, FALSE);
    $node->body = drupal_render($node->content);
    $node->title = apachesolr_clean_text($node->title);

    // Fetch extra data normally not visible, including comments.
    // We do this manually (with module_implements instead of node_invoke_nodeapi)
    // because we want a keyed array to come back. Only in this way can we decide
    // whether to index comments or not.
    $extra = array();
    $excludes = variable_get('apachesolr_exclude_nodeapi_types', array());
    $exclude_nodeapi = isset($excludes[$node->type]) ? $excludes[$node->type] : array();
    foreach (module_implements('nodeapi') as $module) {

      // Invoke nodeapi if this module has not been excluded, for example,
      // exclude 'comment' for a type to skip indexing its comments.
      if (empty($exclude_nodeapi[$module])) {
        $function = $module . '_nodeapi';
        if ($output = $function($node, 'update index', NULL, NULL)) {
          $extra[$module] = $output;
        }
      }
    }
    if (!variable_get('apachesolr_index_comments_with_node', TRUE)) {
      unset($extra['comment']);
    }
    $text = $node->body . "\n\n" . implode(' ', $extra);
    $document = new Apache_Solr_Document();
    $document->id = apachesolr_document_id($node->nid);
    $document->site = url(NULL, array(
      'absolute' => TRUE,
    ));
    $document->hash = apachesolr_site_hash();
    $document->entity = 'node';
    $document->nid = $node->nid;
    $document->uid = $node->uid;
    $document->title = $node->title;
    $document->status = $node->status;
    $document->sticky = $node->sticky;
    $document->promote = $node->promote;
    $document->moderate = $node->moderate;
    $document->tnid = $node->tnid;
    $document->translate = $node->translate;
    if (empty($node->language)) {

      // 'und' is the language-neutral code in Drupal 7.
      $document->language = 'und';
    }
    else {
      $document->language = $node->language;
    }
    $document->body = apachesolr_clean_text($text);
    if (isset($node->teaser)) {
      $document->teaser = apachesolr_clean_text($node->teaser);
    }
    else {
      $document->teaser = truncate_utf8($document->body, 300, TRUE);
    }
    $document->type = $node->type;
    $document->type_name = node_get_types('name', $node);
    $document->created = apachesolr_date_iso($node->created);
    $document->changed = apachesolr_date_iso($node->changed);
    $last_change = isset($node->last_comment_timestamp) && $node->last_comment_timestamp > $node->changed ? $node->last_comment_timestamp : $node->changed;
    $document->last_comment_or_change = apachesolr_date_iso($last_change);
    $document->comment_count = isset($node->comment_count) ? $node->comment_count : 0;
    $document->name = $node->name;
    $path = 'node/' . $node->nid;
    $document->url = url($path, array(
      'absolute' => TRUE,
    ));
    $document->path = $path;

    // Path aliases can have important information about the content.
    // Add them to the index as well.
    if (function_exists('drupal_get_path_alias')) {

      // Add any path alias to the index, looking first for language specific
      // aliases but using language neutral aliases otherwise.
      $language = empty($node->language) ? '' : $node->language;
      $output = drupal_get_path_alias($path, $language);
      if ($output && $output != $path) {
        $document->path_alias = $output;
      }
    }

    // Get CCK fields list
    $cck_fields = apachesolr_cck_fields();

    // NOTE: One CCK field might result in multiple Solr fields.
    foreach ($cck_fields as $key => $cck_info) {
      if (isset($node->{$key})) {

        // Got a CCK field. See if it is to be indexed.
        $function = $cck_info['indexing_callback'];
        $fields = NULL;
        if ($cck_info['indexing_callback'] && function_exists($function)) {

          // NOTE: This function should always return an array.
          $fields = $function($node, $key, $cck_info);
        }

        // NOTE: This is now being done in the mandatory indexing callback.
        // $index_key = apachesolr_index_key($cck_info);
        if (is_array($fields)) {

          // NOTE: If this were done as $key => $value, and the $index_key were
          // done within the loop, could we support indexing one CCK field to
          // multiple Solr fields?
          foreach ($fields as $field) {
            if ($cck_info['multiple']) {
              $document
                ->setMultiValue($field['key'], apachesolr_clean_text($field['value']));
            }
            else {
              $document->{$field['key']} = apachesolr_clean_text($field['value']);
            }
          }
        }
      }
    }

    // Index book module data.
    if (!empty($node->book['bid'])) {

      // Hard-coded - must change if apachesolr_index_key() changes.
      $document->is_book_bid = (int) $node->book['bid'];
    }
    apachesolr_add_tags_to_document($document, $text);
    apachesolr_add_taxonomy_to_document($document, $node);
    if (isset($node->files) && is_array($node->files)) {
      $document->is_upload_count = count($node->files);
    }

    // Let modules add to the document.
    foreach (module_implements('apachesolr_update_index') as $module) {
      $function = $module . '_apachesolr_update_index';
      $function($document, $node, $namespace);
    }
  }
  return $document;
}