You are here

apachesolr_multilingual_confgen.module in Apache Solr Multilingual 6.3

Multilingual search using Apache Solr.

@author Markus Kalkbrenner (mkalkbrenner) | bio.logis GmbH

File

apachesolr_multilingual_confgen/apachesolr_multilingual_confgen.module
View source
<?php

/**
 * @file
 * Multilingual search using Apache Solr.
 *
 * @see apachesolr.module
 *
 * @author Markus Kalkbrenner (mkalkbrenner) | bio.logis GmbH
 *   @see http://drupal.org/user/124705
 */

/**
 * Implements hook_init().
 */
function apachesolr_multilingual_confgen_init() {
  global $conf;
  $variables = array_merge(variable_list_group('apachesolr_confgen_textfiles'), variable_list_group('apachesolr_multilingual_confgen'));
  foreach ($variables as $var_name => $var_info) {
    if (!empty($var_info['solr_multilingual'])) {
      $conf['i18n_variables'][] = $var_name;
    }
  }
}

/**
 * Implements hook_menu().
 */
function apachesolr_multilingual_confgen_menu() {
  $items['admin/settings/apachesolr/confgen/multilingual'] = array(
    'title' => 'Multilingual Settings',
    'weight' => 0,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'apachesolr_multilingual_confgen_admin_form',
    ),
    'file' => 'apachesolr_multilingual_confgen.admin.inc',
    'access arguments' => array(
      'administer search',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implements hook_menu_alter().
 */
function apachesolr_multilingual_confgen_menu_alter(&$items) {
  $items['admin/settings/apachesolr/confgen/textfiles']['title'] = 'Multilingual Text Files';
}
function apachesolr_multilingual_confgen_variable_get($name, $language_id) {
  $value = i18n_variable_get($name, $language_id);
  if (!is_null($value)) {
    return apachesolr_confgen_format_variable($name, $value);
  }
  else {
    $languages = language_list();
    $options = array(
      'language' => $languages[$language_id],
    );
    return apachesolr_confgen_variable_get($name, $options);
  }
}

/**
 * Implements hook_apachesolr_confgen_schema_alter().
 */
function apachesolr_multilingual_confgen_apachesolr_confgen_schema_alter($qp, $solr_version) {
  module_load_include('generator.inc', 'apachesolr_multilingual_confgen');
  apachesolr_multilingual_confgen_modify_schema($qp, $solr_version);
}

/**
 * Implements hook_apachesolr_confgen_solrconfig_alter().
 */
function apachesolr_multilingual_confgen_apachesolr_confgen_solrconfig_alter($qp, $solr_version) {
  module_load_include('generator.inc', 'apachesolr_multilingual_confgen');
  apachesolr_multilingual_confgen_modify_solrconfig($qp, $solr_version);
}

/**
 * Helper function that returns the name of a stemmer
 * if available
 *
 * @param string
 *   the language id
 *
 * @return string
 *   the name of the stemmer
 */
function apachesolr_multilingual_confgen_get_stemmer($language_id = NULL, $solr_version = NULL) {
  $available_stemmers = array(
    'da' => 'Danish',
    'nl' => 'Dutch',
    'en' => 'English',
    'fi' => 'Finnish',
    'fr' => 'French',
    'de' => 'German',
    'it' => 'Italian',
    'nn' => 'Norwegian',
    'nb' => 'Norwegian',
    'pt-br' => 'Portuguese',
    'pt-pt' => 'Portuguese',
    'ro' => 'Romanian',
    'ru' => 'Russian',
    'es' => 'Spanish',
    'sv' => 'Swedish',
    'tr' => 'Turkish',
  );
  switch ($solr_version) {
    case '1.4.x':
      break;
    case '3.5.x':
    case '4.5.x':
    default:

      // add newer stemmers
      $available_stemmers += array(
        'ar' => 'Arabic',
        'eu' => 'Basque',
        'bg' => 'Bulgarian',
        'ca' => 'Catalan',
        'cs' => 'Czech',
        'en-min' => 'English (Minimal)',
        'en-new' => 'English (New)',
        'fr-lt' => 'French (Light)',
        'fr-min' => 'French (Minimal)',
        'de2' => 'German2',
        'de-lt' => 'German (Light)',
        'de-min' => 'German (Minimal)',
        'ga' => 'Irish',
        'gl' => 'Galician',
        'el' => 'Greek',
        'hi' => 'Hindi',
        'hu' => 'Hungarian',
        'hu-lt' => 'Hungarian (Light)',
        'id' => 'Indonesian',
        'id-lt' => 'Indonesian (Light)',
        'it-lt' => 'Italian (Light)',
        'lv' => 'Latvian',
        'nn-lt' => 'Norwegian (Light)',
        'nn-min' => 'Norwegian (Minimal)',
        'pl' => 'Polish',
        'pt-pt-lt' => 'Portuguese (Light)',
        'pt-pt-min' => 'Portuguese (Minimal)',
        'pt-pt-ag' => 'Portuguese (Aggressive)',
        'ru-lt' => 'Russian (Light)',
        'es-lt' => 'Spanish (Light)',
        'sv' => 'Swedish (Light)',
      );
      break;
  }
  if (is_null($language_id)) {
    return $available_stemmers;
  }
  elseif (!is_string($language_id)) {

    // Some language detection functions return FALSE in some cases.
    // Instead of forcing everyone to not call this function in such a case
    // we handle it gracefully here.
  }
  elseif (array_key_exists($language_id, $available_stemmers)) {
    return $available_stemmers[$language_id];
  }
  else {
    if (strpos($language_id, '-') === 2) {

      // try to find a fallback stemmer
      $language_id_fallback = substr($language_id, 0, 2);
      if (array_key_exists($language_id, $available_stemmers)) {
        return $available_stemmers[$language_id_fallback];
      }
    }
  }
  return '';
}
function apachesolr_multilingual_confgen_get_i18n_variables() {
  module_load_include('variable.inc', 'apachesolr_multilingual_confgen');
  return apachesolr_multilingual_confgen_variable_info();
}
function apachesolr_multilingual_confgen_get_apachesolr_confgen_textfiles_i18n_variables() {
  module_load_include('variable.inc', 'apachesolr_confgen_textfiles');
  $variables = apachesolr_confgen_textfiles_variable_info();
  unset($variables['apachesolr_confgen_textfiles_admin_extra_html']);
  return $variables;
}
function apachesolr_multilingual_confgen_variable_info_alter(&$variables, $options) {

  // set default values for multilingual variables if not already modified
  $path = drupal_get_path('module', 'apachesolr_multilingual_confgen') . '/res/';
  foreach (apachesolr_multilingual_confgen_get_apachesolr_confgen_textfiles_i18n_variables() as $var_name => $var_info) {
    $variables[$var_name]['solr_multilingual'] = TRUE;
    foreach (array_keys(apachesolr_multilingual_language_list()) as $language_id) {
      if (i18n_variable_get($var_name, $language_id) === NULL && file_exists($path . basename($var_info['title'], '.txt') . '_' . $language_id . '.txt')) {
        i18n_variable_set($var_name, file_get_contents($path . basename($var_info['title'], '.txt') . '_' . $language_id . '.txt'), $language_id);
      }
    }
  }
  $available_stemmers = apachesolr_multilingual_confgen_get_stemmer();
  foreach (apachesolr_multilingual_confgen_get_i18n_variables() as $var_name => $var_info) {
    foreach (array_keys(apachesolr_multilingual_language_list()) as $language_id) {
      if (i18n_variable_get($var_name, $language_id) === NULL) {

        // TODO replace that switch by reading defaults from property files
        switch ($var_name) {
          case 'apachesolr_multilingual_stemmer_language':
            if (array_key_exists($language_id, $available_stemmers)) {
              i18n_variable_set($var_name, $available_stemmers[$language_id], $language_id);
            }
            break;
        }
      }
    }
  }
}

/**
 * Implements hook_apachesolr_confgen_zip_file_alter().
 */
function apachesolr_multilingual_confgen_apachesolr_confgen_zip_file_alter(&$files, $solr_version) {
  foreach (apachesolr_multilingual_confgen_get_apachesolr_confgen_textfiles_i18n_variables() as $var_name => $var_info) {
    foreach (apachesolr_multilingual_language_list() as $language_id => $language) {
      $files[basename($var_info['title'], '.txt') . '_' . $language_id . '.txt'] = apachesolr_multilingual_confgen_variable_get($var_name, $language_id);
    }
  }
}

/**
 * Implements hook_apachesolr_confgen_solrcore_properties_alter().
 */
function apachesolr_multilingual_confgen_apachesolr_confgen_solrcore_properties_alter(&$properties, $solr_version) {
  if (strpos($solr_version, '1.') !== 0) {
    foreach (apachesolr_multilingual_confgen_get_apachesolr_confgen_textfiles_i18n_variables() as $var_name => $var_info) {
      foreach (apachesolr_multilingual_language_list() as $language_id => $language) {
        $properties['solr.replication.confFiles'] .= ',' . (basename($var_info['title'], '.txt') . '_' . $language_id . '.txt');
      }
    }
  }
}

/**
 * Implements hook_variable_update().
 *
 * FIXME BACKPORT
 */
function apachesolr_multilingual_confgen_variable_update($name, $value, $old_value, $options) {
  switch ($name) {
    case 'apachesolr_multilingual_stemmer_language':
    case 'apachesolr_multilingual_splitOnCaseChange_spell':
    case 'apachesolr_multilingual_splitOnCaseChange_query':
    case 'apachesolr_multilingual_splitOnCaseChange_index':
    case 'apachesolr_multilingual_splitOnNumerics_spell':
    case 'apachesolr_multilingual_splitOnNumerics_query':
    case 'apachesolr_multilingual_splitOnNumerics_index':
    case 'apachesolr_multilingual_stemEnglishPossessive_spell':
    case 'apachesolr_multilingual_stemEnglishPossessive_query':
    case 'apachesolr_multilingual_stemEnglishPossessive_index':
    case 'apachesolr_multilingual_generateWordParts_spell':
    case 'apachesolr_multilingual_generateWordParts_query':
    case 'apachesolr_multilingual_generateWordParts_index':
    case 'apachesolr_multilingual_generateNumberParts_spell':
    case 'apachesolr_multilingual_generateNumberParts_query':
    case 'apachesolr_multilingual_generateNumberParts_index':
    case 'apachesolr_multilingual_catenateWords_spell':
    case 'apachesolr_multilingual_catenateWords_query':
    case 'apachesolr_multilingual_catenateWords_index':
    case 'apachesolr_multilingual_catenateNumbers_spell':
    case 'apachesolr_multilingual_catenateNumbers_query':
    case 'apachesolr_multilingual_catenateNumbers_index':
    case 'apachesolr_multilingual_catenateAll_spell':
    case 'apachesolr_multilingual_catenateAll_query':
    case 'apachesolr_multilingual_catenateAll_index':
    case 'apachesolr_multilingual_preserveOriginal_spell':
    case 'apachesolr_multilingual_preserveOriginal_query':
    case 'apachesolr_multilingual_preserveOriginal_index':
    case 'apachesolr_multilingual_min_query':
    case 'apachesolr_multilingual_min_index':
    case 'apachesolr_multilingual_max_query':
    case 'apachesolr_multilingual_max_index':
    case 'apachesolr_multilingual_ignoreCase_stopwords_spell':
    case 'apachesolr_multilingual_ignoreCase_stopwords':
    case 'apachesolr_multilingual_ignoreCase_synonyms':
    case 'apachesolr_multilingual_lengthMin_spell':
    case 'apachesolr_multilingual_lengthMax_spell':
    case 'apachesolr_multilingual_minWordSize_spell':
    case 'apachesolr_multilingual_minWordSize':
    case 'apachesolr_multilingual_minSubwordSize_spell':
    case 'apachesolr_multilingual_minSubwordSize':
    case 'apachesolr_multilingual_maxSubwordSize_spell':
    case 'apachesolr_multilingual_maxSubwordSize':
    case 'apachesolr_multilingual_onlyLongestMatch_spell':
    case 'apachesolr_multilingual_onlyLongestMatch':
    case 'apachesolr_multilingual_lowerCase_text_ws':
    case 'apachesolr_multilingual_lowerCase_edge_n2_kw_text':
    case 'apachesolr_multilingual_lowerCase_spell':
    case 'apachesolr_multilingual_lowerCase':
      apachesolr_confgen_message_new_config($name, $value, $old_value, $options);
      break;
  }
}

/**
 * A shorthand for variable_info and variable_form_element
 *
 * @param string $name
 * @return Array The generated form element
 */
function apachesolr_multilingual_confgen_variable_definition_to_form_element($name) {
  module_load_include('form.inc', 'variable');
  $variable = variable_info($name);
  return variable_form_element($variable);
}