You are here

apachesolr_confgen.module in Apache Solr Config Generator 6

Same filename and directory in other branches
  1. 8 apachesolr_confgen.module
  2. 7 apachesolr_confgen.module

Generator for Apache Solr Configuration Files.

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

File

apachesolr_confgen.module
View source
<?php

/**
 * @file
 * Generator for Apache Solr Configuration Files.
 *
 * @see apachesolr.module
 *
 * @author Markus Kalkbrenner (mkalkbrenner) | bio.logis GmbH
 *   @see http://drupal.org/user/124705
 */

/**
 * Implements hook_menu().
 */
function apachesolr_confgen_menu() {
  $items['admin/settings/apachesolr/confgen'] = array(
    'title' => 'Config File Generation',
    'weight' => 20,
    'access arguments' => array(
      'administer search',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'apachesolr_confgen_form',
    ),
    'file' => 'apachesolr_confgen.admin.inc',
    'access arguments' => array(
      'administer search',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/settings/apachesolr/confgen/download'] = array(
    'title' => 'Config File Download',
    'weight' => 20,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  return $items;
}

/**
 *
 */
function apachesolr_confgen_solr_versions() {
  return array(
    '1.4.x' => 'solr 1.4.x',
    '3.5.x' => 'solr 3.x (>= 3.5)',
    '4.5.x' => 'solr 4.x (>= 4.5)',
    '5.2.x' => 'solr 5.x (>= 5.2)',
  );
}
function apachesolr_confgen_get_local_path($solr_version = '1.4.x') {
  $local_path = '';
  switch ($solr_version) {
    case '1.4.x':
      $local_path = 'solr-1.4/';
      break;
    case '3.5.x':
      $local_path = 'solr-3.x/';
      break;
    case '4.5.x':
      $local_path = 'solr-4.x/';
      break;
    case '5.2.x':
      $local_path = 'solr-5.x/';
      break;
  }
  return $local_path;
}
function apachesolr_confgen_get_original_qp($type, $solr_version = '1.4.x') {

  // see http://drupal.org/node/1781878
  if (function_exists('querypath_include_code')) {
    querypath_include_code();
  }
  $local_path = apachesolr_confgen_get_local_path($solr_version);

  // TODO error handling
  if ($local_path) {
    return qp(apachesolr_confgen_load_xml_file($local_path . $type . '.xml'), NULL, array(
      'use_parser' => 'xml',
    ));
  }
}
function apachesolr_confgen_clone_qp($qp) {
  return qp($qp
    ->find(':root')
    ->xml(), NULL, array(
    'use_parser' => 'xml',
  ));
}
function apachesolr_confgen_load_config_file($file) {
  $solr_conf_path = drupal_get_path('module', 'apachesolr_confgen') . '/solr-conf/';
  if (!file_exists($solr_conf_path . $file)) {
    $solr_conf_path = drupal_get_path('module', 'apachesolr') . '/solr-conf/';
  }

  // TODO error handling
  return file_get_contents($solr_conf_path . $file);
}

/**
 * Loads raw xml file and resolves xi:includes
 */
function apachesolr_confgen_load_xml_file($file) {
  $xml = apachesolr_confgen_load_config_file($file);
  if (preg_match_all('@<xi:include[^>]+?href="([^"]+?)".*?</xi:include>@s', $xml, $matches)) {
    $dirname = dirname($file);
    foreach ($matches[0] as $key => $xi_include) {
      $xml = str_replace($xi_include, apachesolr_confgen_load_xml_file($dirname . '/' . $matches[1][$key]), $xml);
    }
  }
  return $xml;
}

/**
 * Shows a hint to the user to use the new configurations
 */
function apachesolr_confgen_message_new_config($name, $value, $old_value, $options) {
  static $msg_sent = FALSE;
  if (!$msg_sent) {
    if ($value != $old_value) {
      $info = variable_info($name);
      if (is_null($info)) {

        // ignore for now
        // drupal_set_message(t('Error: Unknown variable @varname.', array('@varname' => $name)), 'error');
      }
      else {
        drupal_set_message(t('Changing "%title" will require you to !link_download a new set of configuration files, update your Solr configuration, restart your Solr server and !link_delete the index.', array(
          '%title' => $info['title'] . (array_key_exists('key', $options) ? ' (' . $options['key'] . ')' : ''),
          '!link_download' => l(t('download'), 'admin/settings/apachesolr/confgen'),
          '!link_delete' => l(t('delete'), 'admin/config/search/apachesolr'),
        )), 'warning');
        $msg_sent = TRUE;
      }
    }
  }
}
function apachesolr_confgen_message_new_config_no_delete($name, $value, $old_value, $options) {
  static $msg_sent = FALSE;
  if (!$msg_sent) {
    if ($value != $old_value) {
      $info = variable_info($name);
      if (is_null($info)) {

        // ignore for now
        // drupal_set_message(t('Error: Unknown variable @varname.', array('@varname' => $name)), 'error');
      }
      else {
        drupal_set_message(t('Changing "%title" will require you to !link_download a new set of configuration files, update your Solr configuration and restart your Solr server.', array(
          '%title' => $info['title'] . (array_key_exists('key', $options) ? ' (' . $options['key'] . ')' : ''),
          '!link_download' => l(t('download'), 'admin/settings/apachesolr/confgen'),
        )), 'warning');
        $msg_sent = TRUE;
      }
    }
  }
}
function apachesolr_confgen_variable_get($name, $options = array()) {
  $value = variable_get_value($name, $options);
  return apachesolr_confgen_format_variable($name, $value);
}
function apachesolr_confgen_format_variable($name, $value) {
  $info = variable_info($name);
  if ('boolean' == $info['type'] && array_key_exists('solr_format', $info)) {
    switch ($info['solr_format']) {
      case 'string':
        return $value ? 'true' : 'false';
      case 'numeric':
        return $value ? '1' : '0';
    }
  }
  return $value;
}
function apachesolr_confgen_get_required_version_string($version, $language_id, $format = 'html') {

  // TODO replace by theme function
  return $format == 'html' ? ' <strong>' . t('Solr %version and newer.', array(
    '%version' => $version,
  ), $language_id) . '</strong>' : ' (' . t('Solr @version and newer', array(
    '@version' => $version,
  ), $language_id) . ')';
}