You are here

function apachesolr_commentsearch_node_to_comments in Apache Solr Search 6.2

1 string reference to 'apachesolr_commentsearch_node_to_comments'
apachesolr_commentsearch_apachesolr_document_handlers in contrib/apachesolr_commentsearch/apachesolr_commentsearch.module
Implementation of hook_apachesolr_document_handlers().

File

contrib/apachesolr_commentsearch/apachesolr_commentsearch.module, line 23

Code

function apachesolr_commentsearch_node_to_comments($node, $namespace) {
  $documents = array();
  $result = db_query("SELECT c.*, u.name AS registered_name FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid = %d", $node->nid);
  while ($comment = db_fetch_object($result)) {
    $text = check_markup($comment->comment, $comment->format, FALSE);
    $document = new Apache_Solr_Document();

    // Comments have their status integers backwards compared to nodes.
    // Here we use the logic that the status of the comment is dependent both on the node and
    // the comment. If the node is published, we look to see if the comment is published, too.
    // If either the node or the comment are not published, the status of the comment
    // will get published as 0.
    $document->status = $node->status ? $comment->status == COMMENT_PUBLISHED ? 1 : 0 : 0;
    if ($document->status == 0) {

      // don't index unpublished comments.
      continue;
    }
    $document->id = apachesolr_document_id($comment->cid, 'comment');
    $document->is_cid = $comment->cid;
    $document->ss_thread = $comment->thread;
    $document->site = url(NULL, array(
      'absolute' => TRUE,
    ));
    $document->hash = apachesolr_site_hash();
    $document->entity = 'comment';

    // Since the nid of this comment is set, when the node gets deleted,
    // the comment will also get removed from the index. See apachesolr_delete_node_from_index()
    $document->nid = $comment->nid;
    $document->uid = $comment->uid;
    $title = empty($comment->subject) ? $node->title : $comment->subject;
    $document->title = apachesolr_clean_text($title);
    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 = 'comment';
    $document->type_name = 'Comment';

    // A comment's timestamp is the time that it was created or last updated,
    // so we must use it for both the "created" and "changed" fields.
    $timestamp = apachesolr_date_iso($comment->timestamp);
    $document->created = $timestamp;
    $document->changed = $timestamp;
    $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->name = $comment->name;
    $path = "node/{$node->nid}";
    $document->url = url($path, array(
      'absolute' => TRUE,
      'fragment' => "comment-{$comment->cid}",
    ));
    $document->path = $path;
    $documents[] = $document;
  }
  return $documents;
}