You are here

apachesolr_multilingual_confgen.module in Apache Solr Multilingual 7

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_menu().
 */
function apachesolr_multilingual_confgen_menu() {
  $items['admin/config/search/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/config/search/apachesolr/confgen/textfiles']['title'] = 'Multilingual Text Files';
}

/**
 * Implements hook_variable_realm_info().
 */
function apachesolr_multilingual_confgen_variable_realm_info() {
  $realm['language_solr_version'] = array(
    'title' => t('Language + Solr Version'),
    'weight' => 200,
    'controller class' => 'VariableRealmUnionController',
    'union' => array(
      'language',
      'solr_version',
    ),
    'select' => FALSE,
    'variable name' => t('multilingual and solr version dependent'),
    'variable class' => 'multilingual-solr-version-dependent',
    'form settings' => TRUE,
    'form switcher' => TRUE,
  );
  return $realm;
}
function apachesolr_multilingual_confgen_variable_get($name, $language_id, $solr_version = '1.4.x') {
  $value = variable_realm_get('language_solr_version', $language_id . ':' . $solr_version, $name, NULL);
  if (NULL === $value) {
    $value = variable_realm_get('language', $language_id, $name, variable_get_default($name));
  }
  return apachesolr_confgen_format_variable($name, $value);
}

/**
 * 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) {
  static $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',
  );
  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.
    return '';
  }
  elseif (array_key_exists($language_id, $available_stemmers)) {
    return $available_stemmers[$language_id];
  }
  else {
    return '';
  }
}
function apachesolr_multilingual_confgen_get_i18n_variables() {
  module_load_include('inc', 'variable');
  return variable_list_group('apachesolr_multilingual_confgen');
}
function apachesolr_multilingual_confgen_get_apachesolr_confgen_textfiles_i18n_variables() {
  if (!module_exists('apachesolr_confgen_textfiles')) {
    return array();
  }
  module_load_include('inc', 'variable');
  $variables = variable_list_group('apachesolr_confgen_textfiles');
  unset($variables['apachesolr_confgen_textfiles_admin_extra_html']);
  unset($variables['apachesolr_confgen_textfiles_solrcore_properties']);
  return $variables;
}

/**
 * Implements hook_modules_enabled().
 */
function apachesolr_multilingual_confgen_modules_enabled($modules) {
  if (in_array('apachesolr_confgen_textfiles', $modules)) {
    apachesolr_multiligual_confgen_register_multilingual_variables();
  }
}

/**
 * Implements hook_modules_disabled().
 */
function apachesolr_multilingual_confgen_modules_disabled($modules) {
  if (in_array('apachesolr_confgen_textfiles', $modules)) {
    apachesolr_multiligual_confgen_unregister_multilingual_variables(apachesolr_multilingual_confgen_get_apachesolr_confgen_textfiles_i18n_variables());
  }
}

/**
 * Implements hook_multilingual_settings_changed().
 */
function apachesolr_multilingual_confgen_multilingual_settings_changed() {
  apachesolr_multiligual_confgen_register_multilingual_variables();
  drupal_set_message(t('Multilingual settings have been changed. Maybe you have to !link_download a new set of configuration files, to update your solr configuration and to restart your solr server.', array(
    '!link_download' => l(t('download'), 'admin/config/search/apachesolr/confgen'),
  )), 'warning');
}
function apachesolr_multiligual_confgen_register_multilingual_variables() {
  variable_cache_clear();
  $apachesolr_confgen_textfiles_i18n_variables = apachesolr_multilingual_confgen_get_apachesolr_confgen_textfiles_i18n_variables();

  // register variables as multilingual variables
  $new_variables = array_keys(array_merge($apachesolr_confgen_textfiles_i18n_variables, apachesolr_multilingual_confgen_get_i18n_variables()));
  $controller = variable_realm_controller('language');
  $old_variables = $controller
    ->getEnabledVariables();
  $variables = array_flip(array_merge(array_flip($old_variables), array_flip($new_variables)));
  $controller
    ->setRealmVariable('list', $variables);

  // set default values for multilingual variables if not already modified
  $path = drupal_get_path('module', 'apachesolr_multilingual_confgen') . '/res/';
  foreach ($apachesolr_confgen_textfiles_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 && 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;
        }
      }
    }
  }
}
function apachesolr_multiligual_confgen_unregister_multilingual_variables($old_variables) {
  variable_cache_clear();
  $controller = variable_realm_controller('language');
  $new_variables = $controller
    ->getEnabledVariables();
  $variables = array_diff($new_variables, array_keys($old_variables));
  $controller
    ->setRealmVariable('list', $variables);
}

/**
 * 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_realm_variable_list_alter().
 *
 * Prevents removal of apachesolr_multilingual variables
 * from the list of multilingual variables.
 */
function apachesolr_multilingual_confgen_variable_realm_variable_list_alter(&$variables, $realm_name) {
  if ('language' == $realm_name) {
    $new_variables = array_merge($variables, array_keys(apachesolr_multilingual_confgen_get_i18n_variables()), array_keys(apachesolr_multilingual_confgen_get_apachesolr_confgen_textfiles_i18n_variables()));
    $diff = array_diff($new_variables, $variables);
    if (!empty($diff)) {
      foreach ($diff as $name) {
        $info = variable_get_info($name);
        drupal_set_message(t('Variable %title has been declared as multilingual or solr version dependent by Apache Solr Multilingual and cannot be removed from the list of multilingual variables.', array(
          '%title' => $info['title'],
        )), 'error');
      }
      $variables = $new_variables;
    }
  }
}

/**
 * Implements hook_variable_update().
 */
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_get_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_get_info($name);
  return variable_form_element($variable);
}

Functions

Namesort descending Description
apachesolr_multiligual_confgen_register_multilingual_variables
apachesolr_multiligual_confgen_unregister_multilingual_variables
apachesolr_multilingual_confgen_apachesolr_confgen_schema_alter Implements hook_apachesolr_confgen_schema_alter().
apachesolr_multilingual_confgen_apachesolr_confgen_solrconfig_alter Implements hook_apachesolr_confgen_solrconfig_alter().
apachesolr_multilingual_confgen_apachesolr_confgen_solrcore_properties_alter Implements hook_apachesolr_confgen_solrcore_properties_alter().
apachesolr_multilingual_confgen_apachesolr_confgen_zip_file_alter Implements hook_apachesolr_confgen_zip_file_alter().
apachesolr_multilingual_confgen_get_apachesolr_confgen_textfiles_i18n_variables
apachesolr_multilingual_confgen_get_i18n_variables
apachesolr_multilingual_confgen_get_stemmer Helper function that returns the name of a stemmer if available
apachesolr_multilingual_confgen_menu Implements hook_menu().
apachesolr_multilingual_confgen_menu_alter Implements hook_menu_alter().
apachesolr_multilingual_confgen_modules_disabled Implements hook_modules_disabled().
apachesolr_multilingual_confgen_modules_enabled Implements hook_modules_enabled().
apachesolr_multilingual_confgen_multilingual_settings_changed Implements hook_multilingual_settings_changed().
apachesolr_multilingual_confgen_variable_definition_to_form_element A shorthand for variable_get_info and variable_form_element
apachesolr_multilingual_confgen_variable_get
apachesolr_multilingual_confgen_variable_realm_info Implements hook_variable_realm_info().
apachesolr_multilingual_confgen_variable_realm_variable_list_alter Implements hook_variable_realm_variable_list_alter().
apachesolr_multilingual_confgen_variable_update Implements hook_variable_update().