? 561862_apachesolr_attachments_attach_to_node.patch
? cleanups.patch
Index: apachesolr_attachments.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr_attachments/apachesolr_attachments.admin.inc,v
retrieving revision 1.4.2.5
diff -u -p -r1.4.2.5 apachesolr_attachments.admin.inc
--- apachesolr_attachments.admin.inc	15 Jun 2010 16:34:39 -0000	1.4.2.5
+++ apachesolr_attachments.admin.inc	15 Jun 2010 18:28:55 -0000
@@ -17,7 +17,7 @@ function apachesolr_attachments_admin_pa
 
 /**
  * Displays the Attachment Settings Form.
-*/
+ */
 function apachesolr_attachments_settings() {
   $default = implode(' ', apachesolr_attachments_default_excluded());
   $form['apachesolr_attachment_excluded_extensions'] = array(
@@ -84,6 +84,73 @@ function apachesolr_attachments_settings
   return $form;
 }
 
+/**
+ * Form callback for content type settings.
+ */
+function apachesolr_attachments_content_type_settings() {
+  list($excluded_types) = _apachesolr_exclude_types('apachesolr_attachments');
+  $types = array_diff_key(node_get_types('names'), $excluded_types);
+
+  foreach ($types as $key => $name) {
+    $form['content_type-settings']['apachesolr_attachments_content_type_indexing_' . $key] = array(
+      '#type' => 'radios',
+      '#title' => t('@type', array('@type' => $name)),
+      '#default_value' => variable_get('apachesolr_attachments_content_type_indexing_' . $key, APACHESOLR_ATTACHMENTS_MODE_SEPARATE_ENTITY),
+      '#options' => array(
+        APACHESOLR_ATTACHMENTS_MODE_SEPARATE_ENTITY => t('Attachments as separate entities'),
+        APACHESOLR_ATTACHMENTS_MODE_ON_PARENT_NODE => t('Attachments as part of parent node'),
+        APACHESOLR_ATTACHMENTS_MODE_DONT_INDEX => t("Don't index attachments"),
+      ),
+    );
+  }
+
+  $form = system_settings_form($form);
+  $form['#theme'] = 'apachesolr_attachments_content_type_settings'; // reset form theme.
+  return $form;
+}
+
+/**
+ * Theme callback for apachesolr_attachments content type settings.
+ *
+ * This groups radio elements into a table that should be more manageble for
+ * larger numbers of content types.
+ *
+ * @group themeable
+ */
+function theme_apachesolr_attachments_content_type_settings($form) {
+
+  $headers = array();
+  $rows = array();
+  foreach (element_children($form['content_type-settings']) as $index) {
+    $element = &$form['content_type-settings'][$index];
+    $row = array();
+
+    if (empty($headers)) {
+      $headers[] = t('Content type');
+      foreach (element_children($element) as $sub_index) {
+        $headers[] = $element[$sub_index]['#title'];
+      }
+    }
+
+    foreach (element_children($element) as $sub_index) {
+      unset($element[$sub_index]['#title']);
+      $row[] = drupal_render($element[$sub_index]);
+    }
+    // Toss actual rendering.
+    $label = drupal_render($element);
+    // Hack out radios raper that's not very useful now.
+    $label = str_replace('<div class="form-radios"></div>', '', $label);
+    array_unshift($row, $label);
+    $rows[] = $row;
+  }
+
+  $output = theme('table', $headers, $rows);
+
+  $output .= drupal_render($form);
+
+  return $output;
+}
+
 function apachesolr_attachments_settings_validate($form, &$form_state) {
   if ($form_state['values']['apachesolr_attachment_extract_using'] == 'tika') {
     $path = realpath($form_state['values']['apachesolr_attachments_tika_path']);
@@ -224,33 +291,12 @@ function apachesolr_attachements_delete_
  */
 function apachesolr_attachments_add_documents(&$documents, $nid, $namespace = 'apachesolr_attachments') {
   $node = node_load($nid, NULL, TRUE);
-  if (!empty($node->nid)) {
+  $index_mode = variable_get('apachesolr_attachments_content_type_indexing_' . $node->type, APACHESOLR_ATTACHMENTS_MODE_SEPARATE_ENTITY);
+  if (!empty($node->nid) && $index_mode == APACHESOLR_ATTACHMENTS_MODE_SEPARATE_ENTITY) {
 
     $hash = apachesolr_site_hash();
 
-    // Since there is no notification for an attachment being unassociated with a
-    // node (but that action will trigger it to be indexed again), we check for
-    // fids that were added before but no longer present on this node.
-
-    $fids = array();
-    $result = db_query("SELECT fid FROM {apachesolr_attachments_files} WHERE nid = %d", $node->nid);
-    while ($row = db_fetch_array($result)) {
-      $fids[$row['fid']] = $row['fid'];
-    }
-
-    $files = apachesolr_attachments_get_indexable_files($node);
-
-    // Find deleted files.
-    $missing_fids = array_diff_key($fids, $files);
-    if ($missing_fids) {
-      db_query("UPDATE {apachesolr_attachments_files} SET removed = 1 WHERE fid IN (". db_placeholders($missing_fids) .")", $missing_fids);
-    }
-    $new_files = array_diff_key($files, $fids);
-    // Add new files.
-    foreach ($new_files as $file) {
-      db_query("INSERT INTO {apachesolr_attachments_files} (fid, nid, removed, sha1) VALUES (%d, %d, 0, '')", $file->fid, $node->nid);
-    }
-    foreach ($files as $file) {
+    foreach (apachesolr_attachments_update_indexable_files($node) as $file) {
       $text = apachesolr_attachments_get_attachment_text($file);
 
       if ($text) {
@@ -296,6 +342,43 @@ function apachesolr_attachments_add_docu
 }
 
 /**
+ * Builds an updated list of indexable files and updates related caches.
+ *
+ * @param $node
+ * A Drupal node object associated with the files.
+ * @return
+ * A list of indexable file objects.
+ */
+function apachesolr_attachments_update_indexable_files($node) {
+
+  // Since there is no notification for an attachment being unassociated with a
+  // node (but that action will trigger it to be indexed again), we check for
+  // fids that were added before but no longer present on this node.
+
+  $fids = array();
+  $result = db_query("SELECT fid FROM {apachesolr_attachments_files} WHERE nid = %d", $node->nid);
+  while ($row = db_fetch_array($result)) {
+    $fids[$row['fid']] = $row['fid'];
+  }
+
+  $files = apachesolr_attachments_get_indexable_files($node);
+
+  // Find deleted files.
+  $missing_fids = array_diff_key($fids, $files);
+  if ($missing_fids) {
+    db_query("UPDATE {apachesolr_attachments_files} SET removed = 1 WHERE fid IN (". db_placeholders($missing_fids) .")", $missing_fids);
+  }
+  $new_files = array_diff_key($files, $fids);
+  // Add new files.
+  foreach ($new_files as $file) {
+    db_query("INSERT INTO {apachesolr_attachments_files} (fid, nid, removed, sha1) VALUES (%d, %d, 0, '')", $file->fid, $node->nid);
+  }
+
+  return $files;
+}
+
+
+/**
  * Return all non-excluded file attachments for a particular node
  */
 function apachesolr_attachments_get_indexable_files($node) {
@@ -378,6 +461,9 @@ function apachesolr_attachments_get_cck_
  * @throws Exception
  */
 function apachesolr_attachments_get_attachment_text($file) {
+  // We need the apachesolr_clean_text function so include this file:
+  module_load_include('inc', 'apachesolr', 'apachesolr.index');
+
   // Any down-side to using realpath()?
   $filepath = realpath($file->filepath);
   // Check that we have a valid filepath.
@@ -478,4 +564,3 @@ function apachesolr_attachments_extract_
   $response = $solr->makeServletRequest(EXTRACTING_SERVLET, $params, 'POST', $headers, $data);
   return array($response->extracted, $response->extracted_metadata);
 }
-
Index: apachesolr_attachments.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr_attachments/apachesolr_attachments.module,v
retrieving revision 1.1.4.2
diff -u -p -r1.1.4.2 apachesolr_attachments.module
--- apachesolr_attachments.module	3 Feb 2010 18:44:02 -0000	1.1.4.2
+++ apachesolr_attachments.module	15 Jun 2010 18:28:55 -0000
@@ -8,6 +8,10 @@
 
 define (EXTRACTING_SERVLET, 'extract/tika');
 
+define('APACHESOLR_ATTACHMENTS_MODE_SEPARATE_ENTITY', 1);
+define('APACHESOLR_ATTACHMENTS_MODE_ON_PARENT_NODE', 2);
+define('APACHESOLR_ATTACHMENTS_MODE_DONT_INDEX', 3);
+
 /**
  * Implementation of hook_menu().
  */
@@ -21,6 +25,20 @@ function apachesolr_attachments_menu() {
     'file' => 'apachesolr_attachments.admin.inc',
     'type' => MENU_LOCAL_TASK,
   );
+  $items['admin/settings/apachesolr/attachments/general'] = array(
+    'title' => 'General',
+    'type' => MENU_DEFAULT_LOCAL_TASK,
+  );
+  $items['admin/settings/apachesolr/attachments/content_type'] = array(
+    'title' => 'Content type',
+    'description' => 'Administer Apache Solr Attachments per content settings.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('apachesolr_attachments_content_type_settings'),
+    'access arguments' => array('administer search'),
+    'file' => 'apachesolr_attachments.admin.inc',
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 1,
+  );
   $items['admin/settings/apachesolr/attachments/confirm/reindex'] = array(
     'title' => 'Reindex all files',
     'page callback' => 'drupal_get_form',
@@ -90,6 +108,19 @@ function apachesolr_attachments_search($
 }
 
 /**
+ * Implementation of hook_theme().
+ */
+function apachesolr_attachments_theme() {
+  return array(
+    // Point this to our admin file for the theme function.
+    'apachesolr_attachments_content_type_settings' => array(
+      'arguments' => array('form' => NULL),
+      'file' => 'apachesolr_attachments.admin.inc',
+    ),
+  );
+}
+
+/**
  * Implementation of hook_apachesolr_types_exclude().
  */
 function apachesolr_attachments_apachesolr_types_exclude($namespace) {
@@ -138,6 +169,20 @@ function apachesolr_attachments_nodeapi(
       // Mark attachments for later re-deletion in case the query fails.
       db_query("UPDATE {apachesolr_attachments_files} SET removed = 1 WHERE nid = %d", $node->nid);
       break;
+    case 'update index':
+      $index_mode = variable_get('apachesolr_attachments_content_type_indexing_' . $node->type, APACHESOLR_ATTACHMENTS_MODE_SEPARATE_ENTITY);
+      if ($index_mode == APACHESOLR_ATTACHMENTS_MODE_ON_PARENT_NODE) {
+
+        module_load_include('inc', 'apachesolr_attachments', 'apachesolr_attachments.admin');
+
+        $rv = '';
+        foreach (apachesolr_attachments_update_indexable_files($node) as $file) {
+          $rv .= "\r\n " . apachesolr_attachments_get_attachment_text($file);
+        }
+
+        return $rv;
+      }
+      break;
   }
 }
 
@@ -218,4 +263,3 @@ function apachesolr_attachments_remove_a
     watchdog('Apache Solr Attachments', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
   }
 }
-
