You are here

function apachesolr_multilingual_apachesolr_update_index in Apache Solr Multilingual 6.2

Same name and namespace in other branches
  1. 6 apachesolr_multilingual.module \apachesolr_multilingual_apachesolr_update_index()

Implements hook_apachesolr_update_index().

Parameters

$document:

$node:

$namespace:

Return value

mixed

File

./apachesolr_multilingual.module, line 52
Multilingual search using Apache Solr.

Code

function apachesolr_multilingual_apachesolr_update_index(&$document, $node, $namespace) {
  static $reentrance = FALSE;
  if (!$reentrance) {
    $reentrance = TRUE;
    $language = $node->language;
    if (!$language) {

      // Language neutral
      $language = variable_get('apachesolr_multilingual_map_language_neutral', '');
      if ($language) {
        $document->language = $node->language = $language;
      }
    }
    if ($language) {
      $fields = $document
        ->getFieldNames();
      if (in_array('vid', $fields) && is_array($document->vid)) {

        // vid is vocabulary id in this case
        $term_languages = array(
          $language,
        );
        if (module_exists('i18ntaxonomy') && variable_get('apachesolr_multilingual_index_term_translations', 0)) {
          $term_languages = variable_get('apachesolr_multilingual_languages', array());
        }
        foreach ($term_languages as $term_language) {
          if ($term_language) {
            $vids = array_unique($document->vid);
            $terms = array();
            foreach ($vids as $vid) {
              if (module_exists('i18ntaxonomy')) {
                $tmp = '';
                if (in_array('im_vid_' . $vid, $fields)) {
                  foreach ($document->{'im_vid_' . $vid} as $tid) {
                    $term = i18nstrings("taxonomy:term:{$tid}:name", '', $term_language);
                    if (!empty($term)) {
                      $tmp .= $term . ' ';
                    }
                  }
                }
                if (!empty($tmp)) {
                  $terms[] = $tmp;
                }
              }
              else {

                // module i18ntaxonomy isn't installed
                // in this case we simply copy the terms
                if (in_array('ts_vid_' . $vid . '_names', $fields)) {
                  $terms[] = $document->{'ts_vid_' . $vid . '_names'};
                }

                // REVIEW see http://drupal.org/node/783924
              }
            }
            if ($terms) {
              $document->{'taxonomy_names_' . $term_language} = $terms;
            }
          }
        }
      }

      // use language specific stemming and so on ..
      if (in_array('title', $fields)) {
        $document->{'title_' . $language} = $document->title;
      }
      if (in_array('body', $fields)) {
        $document->{'body_' . $language} = $document->body;
      }
      if (in_array('tags_h1', $fields)) {
        $document->{'tags_h1_' . $language} = $document->tags_h1;
      }
      if (in_array('tags_h2_h3', $fields)) {
        $document->{'tags_h2_h3_' . $language} = $document->tags_h2_h3;
      }
      if (in_array('tags_h4_h5_h6', $fields)) {
        $document->{'tags_h4_h5_h6_' . $language} = $document->tags_h4_h5_h6;
      }
      if (in_array('tags_a', $fields)) {
        $document->{'tags_a_' . $language} = $document->tags_a;
      }
      if (in_array('tags_inline', $fields)) {
        $document->{'tags_inline_' . $language} = $document->tags_inline;
      }
      foreach ($fields as $field_name) {
        if ((strpos($field_name, 'ts_') === 0 || strpos($field_name, 'tm_') === 0) && !empty($document->{$field_name})) {

          // deal with fields like cck using dynamic ts_* fields
          // search for existing language identifier at second position
          $tmp = explode('_', $field_name);
          if ($language != $tmp[1]) {
            $document->{$tmp[0] . '_' . $language . drupal_substr($field_name, 2)} = $document->{$field_name};
          }
        }
      }
      if (variable_get('apachesolr_multilingual_index_translations', 0) && $node->tnid) {
        $translations = translation_node_get_translations($node->tnid);
        foreach ($translations as $translation_language => $translation) {
          if ($translation->nid != $node->nid) {
            if ($translation_node = node_load($translation->nid)) {
              if ($translation_document = apachesolr_node_to_document($translation_node, $namespace)) {
                if (in_array('title', $fields)) {
                  $document->{'title_' . $translation_language} = $translation_document->title;
                }
                if (in_array('body', $fields)) {
                  $document->{'body_' . $translation_language} = $translation_document->body;
                }
                if (in_array('tags_h1', $fields)) {
                  $document->{'tags_h1_' . $translation_language} = $translation_document->tags_h1;
                }
                if (in_array('tags_h2_h3', $fields)) {
                  $document->{'tags_h2_h3_' . $translation_language} = $translation_document->tags_h2_h3;
                }
                if (in_array('tags_h4_h5_h6', $fields)) {
                  $document->{'tags_h4_h5_h6_' . $translation_language} = $translation_document->tags_h4_h5_h6;
                }
                if (in_array('tags_a', $fields)) {
                  $document->{'tags_a_' . $translation_language} = $translation_document->tags_a;
                }
                if (in_array('tags_inline', $fields)) {
                  $document->{'tags_inline_' . $translation_language} = $translation_document->tags_inline;
                }
                foreach ($fields as $field_name) {
                  if ((strpos($field_name, 'ts_') === 0 || strpos($field_name, 'tm_') === 0) && !empty($translation_document->{$field_name})) {

                    // deal with fields like cck using dynamic ts_* fields
                    // search for existing language identifier at second position
                    $tmp = explode('_', $field_name);
                    if ($translation_language != $tmp[1]) {
                      $document->{$tmp[0] . '_' . $translation_language . drupal_substr($field_name, 2)} = $translation_document->{$field_name};
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    $reentrance = FALSE;
  }
}