You are here

function apachesolr_multilingual_schema_generator_form_do_validate in Apache Solr Multilingual 6

Same name and namespace in other branches
  1. 6.2 apachesolr_multilingual.schema_generator.inc \apachesolr_multilingual_schema_generator_form_do_validate()

Implements hook_form_validate.

_state

Parameters

$form:

Return value

unknown_type

2 calls to apachesolr_multilingual_schema_generator_form_do_validate()
apachesolr_multilingual_schema_generator_form_multilingual_validate in ./apachesolr_multilingual.schema_generator.inc
apachesolr_multilingual_schema_generator_form_unique_language_validate in ./apachesolr_multilingual.schema_generator.inc

File

./apachesolr_multilingual.schema_generator.inc, line 180
Schema generator for multilingual search

Code

function apachesolr_multilingual_schema_generator_form_do_validate(&$form, &$form_state, $multilingual) {
  $selected_languages = variable_get('apachesolr_multilingual_languages', array());
  $options = array();
  foreach ($selected_languages as $lg) {
    if ($lg) {
      $options[] = $lg;
    }
  }
  $language = $options[0];

  // schema.xml template file
  $schema_source_path = dirname(__FILE__) . '/resources/';
  $schema_source_file = $schema_source_path . 'schema.xml';
  $schema_str_complete = file_get_contents($schema_source_file);

  // solrconfig.xml template file
  $solrconfig_source_path = dirname(__FILE__) . '/resources/';
  $solrconfig_source_file = $solrconfig_source_path . 'solrconfig.xml';
  $solrconfig_str_complete = file_get_contents($solrconfig_source_file);
  if (variable_get('apachesolr_multilingual_multicore', 1)) {
    $solrconfig_str_complete = preg_replace("@<dataDir>.*?</dataDir>@", "<!-- disabled for multicore setup \n  \$0 -->", $solrconfig_str_complete);
  }
  $solrconfig_str_complete = preg_replace("@<maxTime>[0-9]+</maxTime>@", '<maxTime>' . variable_get('apachesolr_multilingual_autoCommit_maxTime', 120000) . '</maxTime>', $solrconfig_str_complete);
  ob_clean();
  ob_start();
  if (!$multilingual) {
    $dedicated_stopwords_for_spellchecker = TRUE;
    if ($stemmer = apachesolr_multilingual_get_stemmer($language)) {

      // adjust stemmer
      $schema_str_complete = str_replace('language="English', 'language="' . $stemmer, $schema_str_complete);
      if ('English' == $stemmer) {
        $dedicated_stopwords_for_spellchecker = FALSE;
      }
    }
    else {

      // remove stemmer
      $schema_str_complete = preg_replace("/<filter class=\"solr.SnowballPorterFilterFactory\".*\$/m", '', $schema_str_complete);
    }
    if ($dedicated_stopwords_for_spellchecker) {
      $schema_str_complete = preg_replace("/<\\!-- stopwords_spell -->.*\$/m", '<filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords_spell.txt"/>', $schema_str_complete);
    }
  }
  else {

    // REVIEW
    // unfortunately solr acceopts only one stop word list to preprocess input for spellchecker.
    // in multilingual setup it's better to have no stopwords than combine all languages
    $schema_str_complete = preg_replace("/<!-- stopwords_spell -->.*\$/m", '<!-- no stopwords -->', $schema_str_complete);

    // read the block fieldType_text_LANGUAGE.xml
    $schema_source_file = $schema_source_path . 'fieldType_text_LANGUAGE.xml';
    $field_type_text_language = file_get_contents($schema_source_file);

    // read the block fieldType_text_no_stemming_LANGUAGE.xml
    $schema_source_file = $schema_source_path . 'fieldType_text_no_stemming_LANGUAGE.xml';
    $field_type_text_no_stemming_language = file_get_contents($schema_source_file);

    // read the block fieldType_textSpell_LANGUAGE.xml
    $schema_source_file = $schema_source_path . 'fieldType_textSpell_LANGUAGE.xml';
    $field_type_text_spell_language = file_get_contents($schema_source_file);

    // read the block fieldType_textSpell_multilingual.xml
    $schema_source_file = $schema_source_path . 'fieldType_textSpell_multilingual.xml';
    $field_type_text_spell_multilingual = file_get_contents($schema_source_file);
    $schema_str_complete = str_replace('<!-- fieldType_textSpell_multilingual_StopFilter -->', $field_type_text_spell_multilingual, $schema_str_complete);

    // read the block lst_spellchecker_LANGUAGE.xml
    $solrconfig_source_file = $solrconfig_source_path . 'lst_spellchecker_LANGUAGE.xml';
    $lst_spellchecker_language = file_get_contents($solrconfig_source_file);
    $blocks = array();
    $blocks['<!-- fieldType_textSpell_LANGUAGE -->'] = $field_type_text_spell_language;
    $blocks['<!-- lst_spellchecker_LANGUAGE -->'] = $lst_spellchecker_language;
    $blocks['<!-- field_title_LANGUAGE -->'] = '<field name="title[LANGUAGE_ID]" type="text[LANGUAGE_ID]" indexed="true" stored="true" termVectors="true" omitNorms="true"/>';
    $blocks['<!-- field_body_LANGUAGE -->'] = '<field name="body[LANGUAGE_ID]" type="text[LANGUAGE_ID]" indexed="true" stored="true" termVectors="true"/>';
    $blocks['<!-- field_spell_LANGUAGE -->'] = '<field name="spell[LANGUAGE_ID]" type="textSpell[LANGUAGE_ID]" indexed="true" stored="true" multiValued="true"/>';
    $blocks['<!-- copyField_title_LANGUAGE_spell_LANGUAGE -->'] = '<copyField source="title[LANGUAGE_ID]" dest="spell[LANGUAGE_ID]"/>';
    $blocks['<!-- copyField_body_LANGUAGE_spell_LANGUAGE -->'] = '<copyField source="body[LANGUAGE_ID]" dest="spell[LANGUAGE_ID]"/>';
    $blocks['<!-- field_taxonomy_names_LANGUAGE -->'] = '<field name="taxonomy_names[LANGUAGE_ID]" type="text[LANGUAGE_ID]" indexed="true" stored="false" termVectors="true" multiValued="true" omitNorms="true"/>';
    $blocks['<!-- field_tags_h1_LANGUAGE -->'] = '<field name="tags_h1[LANGUAGE_ID]" type="text[LANGUAGE_ID]" indexed="true" stored="false" omitNorms="true"/>';
    $blocks['<!-- field_tags_h2_h3_LANGUAGE -->'] = '<field name="tags_h2_h3[LANGUAGE_ID]" type="text[LANGUAGE_ID]" indexed="true" stored="false" omitNorms="true"/>';
    $blocks['<!-- field_tags_h4_h5_h6_LANGUAGE -->'] = '<field name="tags_h4_h5_h6[LANGUAGE_ID]" type="text[LANGUAGE_ID]" indexed="true" stored="false" omitNorms="true"/>';
    $blocks['<!-- field_tags_a_LANGUAGE -->'] = '<field name="tags_a[LANGUAGE_ID]" type="text[LANGUAGE_ID]" indexed="true" stored="false" omitNorms="true"/>';
    $blocks['<!-- field_tags_inline_LANGUAGE -->'] = '<field name="tags_inline[LANGUAGE_ID]" type="text[LANGUAGE_ID]" indexed="true" stored="false" omitNorms="true"/>';
    $blocks['<!-- fieldType_textSpell_multilingual_StopFilter -->'] = '<filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords_spell[LANGUAGE_ID].txt"/>';
    $blocks['<!-- dynamicField_ts_LANGUAGE_* -->'] = '<dynamicField name="ts[LANGUAGE_ID]_*" type="text[LANGUAGE_ID]" indexed="true" stored="true" multiValued="false" termVectors="true"/>';

    // solrconfig.xml gettablefiles
    $apachesolr_multilingual_filetypes = variable_get('apachesolr_multilingual_filetypes', array(
      'stopwords.txt' => 'stopwords.txt',
      'synonyms.txt' => 'synonyms.txt',
      'protwords.txt' => 'protwords.txt',
      'compoundwords.txt' => 'compoundwords.txt',
    ));
    $apachesolr_multilingual_filetypes['stopwords_spell.txt'] = 'stopwords_spell.txt';
    $gettable_files = array();
    foreach ($apachesolr_multilingual_filetypes as $gettable_file) {
      $gettable_files[] = str_replace('.txt', '[LANGUAGE_ID].txt', $gettable_file);
    }
    $tse = array(
      '[LANGUAGE_NAME]',
      '[LANGUAGE_ID]',
    );

    // work through the lang.
    foreach ($options as $language) {

      // set the vars to replace in the blocks
      $LANGUAGE_NAME = apachesolr_multilingual_get_stemmer($language);
      $LANGUAGE_ID = '_' . $language;
      $trp = array(
        $LANGUAGE_NAME,
        $LANGUAGE_ID,
      );
      if ($LANGUAGE_NAME) {
        $blocks['<!-- fieldType_text_LANGUAGE -->'] = $field_type_text_language;
      }
      else {
        $blocks['<!-- fieldType_text_LANGUAGE -->'] = $field_type_text_no_stemming_language;
      }

      // schema.xml replace the vars in the blocks
      // solrconfig.xml replace the vars in the blocks
      foreach ($blocks as $search => $replace) {
        $schema_str_complete = str_replace($search, $search . "\n" . apachesolr_multilingual_replace_language_vars($language, str_replace($tse, $trp, $replace)), $schema_str_complete);
        $solrconfig_str_complete = str_replace($search, $search . "\n" . apachesolr_multilingual_replace_language_vars($language, str_replace($tse, $trp, $replace)), $solrconfig_str_complete);
      }
      $search = '<!-- gettableFiles_LANGUAGE  -->';
      foreach ($gettable_files as $s => $replace) {
        $solrconfig_str_complete = str_replace($search, $search . "\n" . apachesolr_multilingual_replace_language_vars($language, str_replace($tse, $trp, $replace)), $solrconfig_str_complete);
      }
    }

    // set $language id to a value that doesn't exist. That will cause apachesolr_multilingual_replace_language_vars() to use default values.
    $language = 'default';
  }
  $schema_str_complete = apachesolr_multilingual_replace_language_vars($language, $schema_str_complete);
  $solrconfig_str_complete = apachesolr_multilingual_replace_language_vars($language, $solrconfig_str_complete);
  ob_end_clean();
  $form['schema_str_complete']['#type'] = 'value';
  $form['schema_str_complete']['#value'] = $schema_str_complete;
  $form['solrconfig_str_complete']['#type'] = 'value';
  $form['solrconfig_str_complete']['#value'] = $solrconfig_str_complete;
  if ($form_state['clicked_button']['#name'] === 'b1' || $form_state['clicked_button']['#name'] === 'b2') {
    drupal_set_header('Content-Type: text/xml; charset=utf-8');
    drupal_set_header('Content-Disposition: attachment; filename=schema.xml');
    print $schema_str_complete;
    exit;
  }
  if ($form_state['clicked_button']['#name'] === 'sc1' || $form_state['clicked_button']['#name'] === 'sc2') {
    drupal_set_header('Content-Type: text/xml; charset=utf-8');
    drupal_set_header('Content-Disposition: attachment; filename=solrconfig.xml');
    print $solrconfig_str_complete;
    exit;
  }
}