You are here

function apachesolr_multilingual_apachesolr_update_index in Apache Solr Multilingual 6

Same name and namespace in other branches
  1. 6.2 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) {
      if (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 = '';
                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
                $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 ..
      $document->{'title_' . $language} = $document->title;
      $document->{'body_' . $language} = $document->body;
      $document->{'tags_h1_' . $language} = $document->tags_h1;
      $document->{'tags_h2_h3_' . $language} = $document->tags_h2_h3;
      $document->{'tags_h4_h5_h6_' . $language} = $document->tags_h4_h5_h6;
      $document->{'tags_a_' . $language} = $document->tags_a;
      $document->{'tags_inline_' . $language} = $document->tags_inline;
      $solr = apachesolr_get_solr();
      $fields = $solr
        ->getFields();
      foreach ($fields as $field_name => $field) {
        if (strpos($field_name, 'ts_') === 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->{'ts_' . $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_document = apachesolr_node_to_document($translation->nid, $namespace)) {
              $document->{'title_' . $translation_language} = $translation_document->title;
              $document->{'body_' . $translation_language} = $translation_document->body;
              $document->{'tags_h1_' . $translation_language} = $translation_document->tags_h1;
              $document->{'tags_h2_h3_' . $translation_language} = $translation_document->tags_h2_h3;
              $document->{'tags_h4_h5_h6_' . $translation_language} = $translation_document->tags_h4_h5_h6;
              $document->{'tags_a_' . $translation_language} = $translation_document->tags_a;
              $document->{'tags_inline_' . $translation_language} = $translation_document->tags_inline;
              foreach ($fields as $field_name => $field) {
                if (strpos($field_name, 'ts_') === 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->{'ts_' . $translation_language . drupal_substr($field_name, 2)} = $translation_document->{$field_name};
                  }
                }
              }
            }
          }
        }
      }
    }
    $reentrance = FALSE;
  }
}