You are here

function apachesolr_node_to_document in Apache Solr Search 6

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

Given a node ID, return a document representing that node.

1 call to apachesolr_node_to_document()
apachesolr_add_node_document in ./apachesolr.index.inc
Add a document to the $documents array based on a node ID.

File

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

Code

function apachesolr_node_to_document($nid, $namespace) {

  // Set reset = TRUE to avoid static caching of all nodes that get indexed.
  $node = node_load($nid, NULL, TRUE);
  if (empty($node)) {
    return FALSE;
  }
  $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.
    $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;
        }
      }
    }
    $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);
    $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();
    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['callback'];
        if ($cck_info['callback'] && function_exists($function)) {
          $field = $function($node, $key);
        }
        else {
          $field = $node->{$key};
        }
        $index_key = apachesolr_index_key($cck_info);
        foreach ($field as $value) {

          // Don't index NULLs or empty strings
          // We can use 'value' rather than 'safe' since we strip tags and later check_plain().
          if (isset($value['value']) && strlen($value['value'])) {
            if ($cck_info['multiple']) {
              $document
                ->setMultiValue($index_key, apachesolr_clean_text($value['value']));
            }
            else {
              $document->{$index_key} = apachesolr_clean_text($value['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;
}