commit 271c6f5628e16ad44aeaeee7808d9e771b3a5e65 Author: James Gilliland Date: Sat Feb 26 12:21:37 2011 -0600 Working copy of content types patch This patch provides the ability to link results to their node rather than providing individual results. This is done on a per content type basis. http://drupal.org/node/561862#comment-3091628 diff --git apachesolr_attachments.admin.inc apachesolr_attachments.admin.inc index ebcdb87..ba45508 100644 --- apachesolr_attachments.admin.inc +++ apachesolr_attachments.admin.inc @@ -16,7 +16,7 @@ function apachesolr_attachments_admin_page() { /** * Displays the Attachment Settings Form. -*/ + */ function apachesolr_attachments_settings() { $default = implode(' ', apachesolr_attachments_default_excluded()); $form['apachesolr_attachment_excluded_extensions'] = array( @@ -83,6 +83,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, 'seperate'), + '#options' => array( + 'seperate' => t('Attachments as separate entities'), + 'parent' => t('Attachments as part of parent node'), + 'none' => 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('
', '', $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_index() { */ function apachesolr_attachments_add_documents(&$documents, $nid, $namespace = 'apachesolr_attachments') { $node = node_load($nid, NULL, TRUE); - if (!empty($node->nid)) { + $index_mode = apachesolr_attachments_get_index_mode($node); + if (!empty($node->nid) && $index_mode == 'seperate') { $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) { @@ -304,6 +350,43 @@ function apachesolr_attachments_add_documents(&$documents, $nid, $namespace = 'a } /** + * 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) { @@ -386,6 +469,9 @@ function apachesolr_attachments_get_cck_file_fields() { * @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. @@ -492,4 +578,3 @@ function apachesolr_attachments_extract_using_solr($filepath) { $response = $solr->makeServletRequest(EXTRACTING_SERVLET, $params, 'POST', $headers, $data); return array($response->extracted, $response->extracted_metadata); } - diff --git apachesolr_attachments.install apachesolr_attachments.install index 5271c0d..bd089c3 100644 --- apachesolr_attachments.install +++ apachesolr_attachments.install @@ -5,7 +5,7 @@ */ function apachesolr_attachments_install() { drupal_install_schema('apachesolr_attachments'); -} +} /** * Implementation of hook_uninstall(). @@ -19,6 +19,7 @@ function apachesolr_attachments_uninstall() { variable_del('apachesolr_attachment_excluded_mime'); apachesolr_clear_last_index('apachesolr_attachments'); drupal_uninstall_schema('apachesolr_attachments'); + db_query("DELETE FROM {variable} WHERE name LIKE 'apachesolr_attachments_content_type_indexing_%%'"); } /** @@ -36,7 +37,7 @@ function apachesolr_attachments_requirements($phase) { exec($java .' -version > '. $temp .' 2>&1'); $stderror = file_get_contents($temp); $found = preg_match('/Runtime Environment/', $stderror); - + if (!$found) { $requirements['apachesolr_attachments_java'] = array( 'title' => $t('Java executable not found'), diff --git apachesolr_attachments.module apachesolr_attachments.module index 587db53..76410c5 100644 --- apachesolr_attachments.module +++ apachesolr_attachments.module @@ -20,6 +20,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', @@ -89,6 +103,19 @@ function apachesolr_attachments_search($op = 'search', $keys = NULL) { } /** + * 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) { @@ -137,10 +164,38 @@ function apachesolr_attachments_nodeapi($node, $op) { // 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 = apachesolr_attachments_get_index_mode($node); + if ($index_mode == 'parent') { + + module_load_include('inc', 'apachesolr_attachments', 'apachesolr_attachments.admin'); + + $text = ''; + foreach (apachesolr_attachments_update_indexable_files($node) as $file) { + $text .= "\n " . apachesolr_attachments_get_attachment_text($file); + } + + return $text; + } + break; } } /** + * Get the indexing mode for the attachments of a given node. + * + * @param $node + * A node object. + * @return + * Index mode + */ +function apachesolr_attachments_get_index_mode($node) { + $index_mode = variable_get('apachesolr_attachments_content_type_indexing_' . $node->type, 'seperate'); + drupal_alter('apachesolr_attachments_index_mode', $index_mode, $node); + return $index_mode; +} + +/** * Implementation of hook_cron(). * * Delete all removed attachments from the Solr store. @@ -219,4 +274,3 @@ function apachesolr_attachments_remove_attachments_from_index($nid) { watchdog('ApacheSolrAttach', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); } } -