From 2e5c2fe216ba5178fc48e5aa89468a4e4aa19798 Mon Sep 17 00:00:00 2001
From: Florent Torregrosa <florent.torregrosa@gmail.com>
Date: Sat, 21 Nov 2015 19:27:11 +0100
Subject: [PATCH] Issue #2570233 by Grimreaper, izus: Support for indexing file
 fields on comments.

---
 contrib/search_api_attachments_comment/README.txt  |   4 +
 .../callback_attachments_comment_settings.inc      | 115 +++++++++++++++++++++
 .../search_api_attachments_comment.info            |   8 ++
 .../search_api_attachments_comment.module          |  41 ++++++++
 4 files changed, 168 insertions(+)
 create mode 100644 contrib/search_api_attachments_comment/README.txt
 create mode 100644 contrib/search_api_attachments_comment/includes/callback_attachments_comment_settings.inc
 create mode 100644 contrib/search_api_attachments_comment/search_api_attachments_comment.info
 create mode 100644 contrib/search_api_attachments_comment/search_api_attachments_comment.module

diff --git a/contrib/search_api_attachments_comment/README.txt b/contrib/search_api_attachments_comment/README.txt
new file mode 100644
index 0000000..d511d11
--- /dev/null
+++ b/contrib/search_api_attachments_comment/README.txt
@@ -0,0 +1,4 @@
+Search API Attachments Comment
+
+This contrib module extends the work of search_api_attachments by indexing
+the files that belong to the comments of the nodes of the index.
diff --git a/contrib/search_api_attachments_comment/includes/callback_attachments_comment_settings.inc b/contrib/search_api_attachments_comment/includes/callback_attachments_comment_settings.inc
new file mode 100644
index 0000000..4baaa0d
--- /dev/null
+++ b/contrib/search_api_attachments_comment/includes/callback_attachments_comment_settings.inc
@@ -0,0 +1,115 @@
+<?php
+
+/**
+ * @file
+ * Search API data alteration callback.
+ */
+
+class SearchApiAttachmentsCommentAlterSettings extends SearchApiAttachmentsAlterSettings {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function alterItems(array &$items) {
+    // Ensure we are on a node index.
+    if ($this->index->item_type != 'node') {
+      return;
+    }
+
+    // Extension restriction.
+    $exclude = array();
+    foreach (explode(' ', $this->options['excluded_extensions']) as $ext) {
+      $exclude[$ext] = file_get_mimetype('dummy.' . $ext);
+    }
+
+    // File size restriction.
+    $max_file_size = parse_size($this->options['max_file_size']);
+
+    // Get the file fields.
+    $file_fields = $this->getFileFields();
+
+    foreach ($items as $id => &$item) {
+      // Load the comments.
+      $comments = comment_load_multiple(array(), array('nid' => $id));
+
+      // Loop in the comments.
+      foreach ($comments as $comment) {
+        $comment_wrapper = entity_metadata_wrapper('comment', $comment->cid);
+        foreach ($file_fields as $file_field) {
+          if (isset($comment->{$file_field['field_name']})) {
+            $files = $comment_wrapper->{$file_field['field_name']}->value();
+            // Manage case of single value fields by reproducing the structure
+            // of multiple values fields.
+            if ($file_field['cardinality'] == 1) {
+              $files = array($files);
+            }
+            // Index the files content.
+            if (!empty($files)) {
+              // Limit to the max number of value per field.
+              if (isset($this->options['number_indexed']) && $this->options['number_indexed'] != '0' && count($files) > $this->options['number_indexed']) {
+                $files = array_slice($files, 0, $this->options['number_indexed']);
+              }
+              foreach ($files as $file) {
+                // Private file restriction.
+                if (!$this->is_temporary($file) && !($this->options['excluded_private'] && $this->is_private($file))) {
+                  // Extension restriction.
+                  if (!in_array($file['filemime'], $exclude)) {
+                    // File size restriction.
+                    $file_size_errors = file_validate_size((object) $file, $max_file_size);
+                    if (empty($file_size_errors)) {
+                      $attachments = 'attachments_' . $file_field['field_name'];
+                      if (isset($item->{$attachments})) {
+                        $item->{$attachments} .= ' ' . $this->getFileContent($file);
+                      }
+                      else {
+                        $item->{$attachments} = $this->getFileContent($file);
+                      }
+                    }
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function propertyInfo() {
+    $ret = array();
+    if ($this->index->item_type == 'file') {
+      $ret['attachments_content'] = array(
+        'label' => 'File content',
+        'description' => 'File content',
+        'type' => 'text',
+      );
+    }
+    else {
+      $fields = $this->getFileFields();
+      foreach ($fields as $name => $field) {
+        $ret['attachments_' . $name] = array(
+          'label' => 'Attachment content: ' . $name,
+          'description' => $name,
+          'type' => 'text',
+        );
+      }
+    }
+    return $ret;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getFileFields() {
+    $ret = array();
+    foreach (field_info_fields() as $name => $field) {
+      if ($field['type'] == 'file') {
+        $ret[$name] = $field;
+      }
+    }
+    return $ret;
+  }
+}
diff --git a/contrib/search_api_attachments_comment/search_api_attachments_comment.info b/contrib/search_api_attachments_comment/search_api_attachments_comment.info
new file mode 100644
index 0000000..22479fb
--- /dev/null
+++ b/contrib/search_api_attachments_comment/search_api_attachments_comment.info
@@ -0,0 +1,8 @@
+name = Search API Attachments Comment
+description = "Extends the attachments indexing to file fields in the comments."
+dependencies[] = comment
+dependencies[] = search_api_attachments
+core = 7.x
+package = Search
+
+files[] = includes/callback_attachments_comment_settings.inc
diff --git a/contrib/search_api_attachments_comment/search_api_attachments_comment.module b/contrib/search_api_attachments_comment/search_api_attachments_comment.module
new file mode 100644
index 0000000..5d7b773
--- /dev/null
+++ b/contrib/search_api_attachments_comment/search_api_attachments_comment.module
@@ -0,0 +1,41 @@
+<?php
+/**
+ * @file
+ * Drupal hooks
+ */
+
+/**
+ * Implements hook_search_api_alter_callback_info().
+ */
+function search_api_attachments_comment_search_api_alter_callback_info() {
+  $callbacks['search_api_attachments_comment_alter_settings'] = array(
+    'name' => t('File attachments Comment'),
+    'description' => t('Extract the content of attached files of the comments and index it.'),
+    'class' => 'SearchApiAttachmentsCommentAlterSettings',
+  );
+  return $callbacks;
+}
+
+/**
+ * Implements hook_comment_insert().
+ */
+function search_api_attachments_comment_comment_insert($comment) {
+  // Mark the node of the comment to be reindexed.
+  search_api_track_item_change('node', array($comment->nid));
+}
+
+/**
+ * Implements hook_comment_update().
+ */
+function search_api_attachments_comment_comment_update($comment) {
+  // Mark the node of the comment to be reindexed.
+  search_api_track_item_change('node', array($comment->nid));
+}
+
+/**
+ * Implements hook_comment_delete().
+ */
+function search_api_attachments_comment_comment_delete($comment) {
+  // Mark the node of the comment to be reindexed.
+  search_api_track_item_change('node', array($comment->nid));
+}
-- 
2.1.4

