? .svn ? solr_multi_instance.diff ? SolrPhpClient/.svn ? SolrPhpClient/Apache/.svn ? SolrPhpClient/Apache/Solr/.svn ? SolrPhpClient/Apache/Solr/Service/.svn ? SolrPhpClient/phpdocs/.svn ? SolrPhpClient/phpdocs/Apache/.svn ? SolrPhpClient/phpdocs/Apache/Solr/.svn ? SolrPhpClient/phpdocs/media/.svn ? contrib/.svn ? contrib/apachesolr_image/.svn ? tests/.svn Index: apachesolr.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr.module,v retrieving revision 1.1.2.12 diff -u -r1.1.2.12 apachesolr.module --- apachesolr.module 20 Apr 2008 14:14:45 -0000 1.1.2.12 +++ apachesolr.module 13 Jun 2008 08:17:09 -0000 @@ -6,55 +6,274 @@ $items[] = array( 'path' => 'admin/settings/apachesolr', 'title' => t('Apache Solr search'), - 'description' => t('Administer Apache Solr.'), + 'description' => t('Administer Apache Solr.'), + 'callback' => 'drupal_get_form', + 'callback arguments' => 'apachesolr_settings', + 'access' => user_access('administer site configuration'), + ); + + $items[] = array( + 'path' => 'admin/settings/apachesolr/0' , + 'title' => t('Apache Solr search'), + 'description' => t('Administer Apache Solr'), + 'type' => MENU_DEFAULT_LOCAL_TASK, 'callback' => 'drupal_get_form', 'callback arguments' => 'apachesolr_settings', 'access' => user_access('administer site configuration'), ); + + $items[] = array( + 'path' => 'admin/settings/apachesolr/add', + 'title' => t('Add new configuration'), + 'description' => t('Administer Apache Solr'), + 'type' => MENU_LOCAL_TASK, + 'weight' => 19, + 'callback' => 'drupal_get_form', + 'callback arguments' => array('apachesolr_settings','add'), + 'access' => user_access('administer site configuration'), + ); + + $items[] = array( + 'path' => 'admin/settings/apachesolr/delete', + 'title' => t('Delete Solr Instance'), + 'description' => t('Administer Apache Solr'), + 'type' => MENU_CALLBACK, + 'weight' => 19, + 'callback' => 'drupal_get_form', + 'callback arguments' => array('apachesolr_delete_confirm'), + 'access' => user_access('administer site configuration'), + ); + $items[] = array( 'path' => 'admin/settings/apachesolr/index', 'title' => t('Apache Solr search index'), 'callback' => 'apachesolr_index_page', + 'type' => MENU_LOCAL_TASK, + 'weight' => 20, 'access' => user_access('administer site configuration'), ); + + } else { + $all_instances = variable_get('apachesolr_instances',array()); + $weight = 1; + foreach ($all_instances as $key => $instance) { + $weight++; + $items[] = array( + 'path' => 'admin/settings/apachesolr/' . $key, + 'type' => MENU_LOCAL_TASK, + 'title' => t($all_instances[$key]['name']), + 'description' => t('Administer Apache Solr.'), + 'callback' => 'drupal_get_form', + 'weight' => $weight, + 'callback arguments' => array('apachesolr_settings',$key), + 'access' => user_access('administer site configuration'), + ); + } } return $items; } -function apachesolr_settings() { +function apachesolr_settings($instance_key = 0) { $form = array(); - - $form['apachesolr_host'] = array( + $form['#tree'] = true; + + $form['instance_key'] = array ( + '#type' => 'hidden', + '#value' => (string)$instance_key + ); + + $all_instances = variable_get('apachesolr_instances',array()); + $defaults = $all_instances[$instance_key]; + + if (!$defaults) { + $defaults = array(); + } + + $conf_set['apachesolr_host'] = array( '#type' => 'textfield', '#title' => t('Solr host name'), - '#default_value' => variable_get('apachesolr_host', 'localhost'), + '#default_value' => 'localhost', '#description' => t('Host name of your Solr server, e.g. localhost or example.com.'), ); - $form['apachesolr_port'] = array( + $conf_set['apachesolr_port'] = array( '#type' => 'textfield', '#title' => t('Solr port'), - '#default_value' => variable_get('apachesolr_port', '8983'), + '#default_value' => '8983', '#description' => t('Port on which the Solr server listens. Tomcat is 8080 by default.'), ); - $form['apachesolr_path'] = array( + $conf_set['apachesolr_path'] = array( '#type' => 'textfield', '#title' => t('Solr path'), - '#default_value' => variable_get('apachesolr_path', '/solr'), + '#default_value' => '/solr', '#description' => t('Path that identifies the Solr request handler to be used. Leave this as /solr for now.'), ); - return system_settings_form($form); + + $form['name'] = array ( + '#type' => 'textfield', + '#title' => "Instance Name", + '#required' => true, + '#weight' => -7, + ); + + if (!$instance_key) { + $form['name']['#type'] = 'value'; + $form['name']['#value'] = 'Default'; + } + + $form['same_server'] = array ( + '#type' => 'checkbox', + '#title' => t('Use the same server for indexing and searching?'), + '#weight' => -4, + '#default_value' => true, + '#attributes' => array('onChange' => 'console.log($(this).val());ifs = $(".index_fieldset"); if ($(this).is(":checked")) { ifs.hide(500) } else { ifs.show(500) }'), + '#description' => t('Uncheck this box if you want to use a different server for indexing than for searching (most users can ignore this)'), + ); + + $form['search'] = array('#type' => 'fieldset', '#collapsible' => true) + $conf_set; + + $form['search']['#title'] = "Search Server"; + $form['search']['#weight'] = -5; + + if (count($defaults) != 0 && array_diff($defaults['search'],$defaults['index'])) { + $form['same_server']['#default_value'] = false; + $form['index']['#attributes']['style'] = 'display:block'; + } else { + $hider = << 'fieldset', '#collapsible' => true) + $conf_set; + $form['index']['#title'] = "Indexing Server"; + $form['index']['#attributes'] = array('class' => 'index_fieldset'); + $form['index']['#weight'] = -3; + + foreach ($defaults as $type => $default_settings) { + //Type is search or index + if (is_array($default_settings)) { + foreach ($default_settings as $field_name => $setting) { + if ($form[$type]) { + $form[$type][$field_name]['#default_value'] = $setting; + } + } + } else { + $form[$type]['#default_value'] = $default_settings; + } + } + + //If the index is any + + + $form = system_settings_form($form); + $form['buttons']['reset']['#value'] = t('Delete this instance'); + + return $form; +} + +function apachesolr_settings_submit($form_id,$form_values) { + + + if ($form_values['op'] == $form_values['buttons']['reset']) { + drupal_goto('admin/settings/apachesolr/delete/' . $form_values['instance_key']); + } + + if ($form_values['same_server'] == true) { + $form_values['index'] = $form_values['search']; + } + + unset($form_values['op']); + unset($form_values['buttons']); + unset($form_values['form_token']); + unset($form_values['form_id']); + unset($form_values['same_server']); + + $instance_key = $form_values['instance_key']; + unset($form_values['instance_key']); + + $apachesolr_conf = variable_get('apachesolr_instances',array()); + + if ($instance_key == 'add') { + $instance_key = count($apachesolr_conf) ? count($apachesolr_conf) : 0; + //we are adding a new one, or saving for the firs ttime + } + + $apachesolr_conf[$instance_key] = $form_values; + variable_set('apachesolr_instances',$apachesolr_conf); + drupal_goto('admin/settings/apachesolr/' . $instance_key); +} + +/** + * Menu callback -- ask for confirmation of instance deletion + */ +function apachesolr_delete_confirm($instance_key) { + $instances = variable_get('apachesolr_instances',array()); + $form['instance_key'] = array('#type' => 'value', '#value' => $instance_key); + if (!isset($instances[$instance_key])) { + drupal_set_message('This Instance does not exist','error'); + return; + } + return confirm_form($form, + t('Are you sure you want to delete %title?', array('%title' => $instances[$instance_key]['name'])), + isset($_GET['destination']) ? $_GET['destination'] : 'admin/settings/apachesolr', + t('This action cannot be undone.'), + t('Delete'), t('Cancel')); +} + + +/** + * Wrapper around variable get to make getting variables for different instances easier + * + * @param string $variable_name : can also user . syntax to traverse an array + * @param mixed default : For compatibility + * @param int $instance_key : Optional, if client is using multiple instances + */ +function apachesolr_variable_get($variable_name, $default, $instance_key = 0) { + $instances = variable_get('apachesolr_instances',array()); + $vars = $instances[$instance_key]; + $_parts = explode('.',$variable_name); + if (count($_parts) > 1) { + $target = $vars; + foreach ($_parts as $part) { + if (is_array($target)) { + $target = $target[$part]; + } + } + return $target; + } else { + return $vars[$variable_name]; + } +} + +/** + * Execute node deletion + */ +function apachesolr_delete_confirm_submit($form_id, $form_values) { + if ($form_values['confirm']) { + $instances = variable_get('apachesolr_instances',array()); + unset($instances[$form_values['instance_key']]); + variable_set('apachesolr_instances',$instances); + } + + return 'admin/settings/apachesolr'; } function apachesolr_requirements($phase) { // Ensure translations don't break at install time $t = get_t(); if ($phase == 'runtime') { - $host = variable_get('apachesolr_host', 'localhost'); - $port = variable_get('apachesolr_port', 8983); - $path = variable_get('apachesolr_path', '/solr'); + $host = apachesolr_variable_get('search.apachesolr_host', 'localhost'); + $port = apachesolr_variable_get('search.apachesolr_port', 8983); + $path = apachesolr_variable_get('search.apachesolr_path', '/solr'); $ping = FALSE; try { - $solr =& apachesolr_get_solr($host, $port, $path); + $solr =& apachesolr_get_solr_defaults(); $ping = $solr->ping(); // If there is no $solr object, there is no server available, so don't continue. if (!$ping) { @@ -82,7 +301,7 @@ function apachesolr_index_page() { //TODO: Should we be generating the dynamic blocks based on what is indexed? // Or should we be using things like node_field.type to get this list? - $response = drupal_http_request(apachesolr_base_url(). "/admin/luke?numTerms=0"); + $response = drupal_http_request(apachesolr_base_url('search'). "/admin/luke?numTerms=0"); if ($response->code == '200') { $xml_response = simplexml_load_string($response->data); $fields = $xml_response->xpath("//lst[@name='fields']/lst"); @@ -153,9 +372,10 @@ static function update_index($namespace) { $solr = FALSE; - try { + try { // Get the $solr object - $solr =& apachesolr_get_solr(variable_get('apachesolr_host', 'localhost'), variable_get('apachesolr_port', 8983), variable_get('apachesolr_path', '/solr')); + $solr =& apachesolr_get_solr_defaults('index'); + dpr($solr); // If there is no $solr object, there is no server available, so don't continue. if (!$solr->ping()) { throw new Exception(t('No Solr instance available during indexing')); @@ -281,7 +501,7 @@ switch ($op) { case 'delete': try { - $solr =& apachesolr_get_solr(variable_get('apachesolr_host', 'localhost'), variable_get('apachesolr_port', 8983), variable_get('apachesolr_path', '/solr')); + $solr =& apachesolr_get_solr_defaults('index'); $solr->deleteById($node->nid); $solr->commit(); } @@ -574,7 +794,7 @@ */ function &apachesolr_get_solr($host = 'localhost', $port = 8983, $path = '/solr') { static $solr_cache; - + if (empty($solr_cache[$host][$port][$path])) { $include_path = get_include_path(); set_include_path ('./'. drupal_get_path('module', 'apachesolr') .'/SolrPhpClient/'); @@ -585,6 +805,10 @@ return $solr_cache[$host][$port][$path]; } +function apachesolr_get_solr_defaults($type = 'search',$instance = 0) { + return apachesolr_get_solr(apachesolr_variable_get($type . '.apachesolr_host', 'localhost'), apachesolr_variable_get($type . '.apachesolr_port', 8983), apachesolr_variable_get($type . '.apachesolr_path', '/solr')); +} + /** * It is important to hold on to the Solr response object for the duration of the * page request so that we can use it for things like building facet blocks. @@ -620,8 +844,15 @@ return $_queries[$keys]; } -function apachesolr_base_url() { - return "http://". variable_get('apachesolr_host', 'localhost'). ':'. variable_get('apachesolr_port', '8983'). variable_get('apachesolr_path', '/solr'); +function apachesolr_base_url($type = 'search') { + switch ($type) { + case 'index': + return "http://". apachesolr_variable_get('index.apachesolr_host', 'localhost'). ':'. apachesolr_variable_get('index.apachesolr_port', '8983'). apachesolr_variable_get('index.apachesolr_path', '/solr'); + break; + case 'search': + return "http://". apachesolr_variable_get('search.apachesolr_host', 'localhost'). ':'. apachesolr_variable_get('search.apachesolr_port', '8983'). apachesolr_variable_get('search.apachesolr_path', '/solr'); + break; + } } /** @@ -749,4 +980,4 @@ function theme_apachesolr_breadcrumb_tid($tid) { $term = taxonomy_get_term($tid); return $term->name; -} +} \ No newline at end of file Index: apachesolr_search.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr_search.module,v retrieving revision 1.1.2.6 diff -u -r1.1.2.6 apachesolr_search.module --- apachesolr_search.module 20 Apr 2008 14:14:45 -0000 1.1.2.6 +++ apachesolr_search.module 13 Jun 2008 08:17:09 -0000 @@ -26,7 +26,7 @@ case 'search': global $pager_total; // This is the object that does the communication with the solr server. - $solr =& apachesolr_get_solr(variable_get('apachesolr_host', 'localhost'), variable_get('apachesolr_port', 8983), variable_get('apachesolr_path', '/solr')); + $solr =& apachesolr_get_solr_defaults('search'); // This is the object that knows about the query coming from the user. $query =& apachesolr_drupal_query($keys);