'value',
'#value' => $server['server_id'],
);
return confirm_form(
$form,
t('Are you sure you want to delete server %name?', array('%name' => $server['name'])),
'admin/config/search/apachesolr',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
function apachesolr_server_delete_form_submit($form, &$form_state) {
if (apachesolr_server_delete($form_state['values']['server_id'])) {
drupal_set_message(t('The server was deleted'));
}
$form_state['redirect'] = 'admin/config/search/apachesolr';
}
/**
* Form builder for adding/editing a Solr server used as a menu callback.
*/
function apachesolr_server_edit_form($form, &$form_state, $server = NULL) {
if (empty($server)) {
$server = array('server_id' => '', 'name' => '', 'scheme' => NULL, 'host' => '', 'port' => '', 'path' => '', 'service_class' => '');
}
$form['original_server_id'] = array(
'#type' => 'value',
'#value' => $server['server_id'],
);
$form['host'] = array(
'#type' => 'textfield',
'#title' => t('Solr host name'),
'#default_value' => $server['host'],
'#description' => t('Host name of your Solr server, e.g. localhost
or example.com
.'),
'#required' => TRUE,
);
$form['port'] = array(
'#type' => 'textfield',
'#title' => t('Solr port'),
'#size' => 5,
'#maxlength' => 5,
'#default_value' => $server['port'],
'#description' => t('Port on which the Solr server listens. The Jetty example server is 8983, while Tomcat is 8080 by default.'),
'#required' => TRUE,
);
$form['path'] = array(
'#type' => 'textfield',
'#title' => t('Solr path'),
'#default_value' => $server['path'],
'#description' => t('Path that identifies the Solr request handler to be used.'),
'#required' => TRUE,
);
$is_default = $server['server_id'] == variable_get('apachesolr_default_server', 'solr');
$form['make_default'] = array(
'#type' => 'checkbox',
'#title' => t('Make this Solr server the default'),
'#default_value' => $is_default,
'#disabled' => $is_default,
'#description' => $is_default ? t('Select this option for a different server to make this one non-default.') : '',
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#default_value' => $server['name'],
'#description' => t('Short description of your Solr server.'),
'#required' => TRUE,
);
$form['server_id'] = array(
'#type' => 'machine_name',
'#title' => t('Server id'),
'#machine_name' => array(
'exists' => 'apachesolr_server_load',
),
'#default_value' => $server['server_id'],
'#description' => t('Unique, machine-readable identifier Solr server.'),
'#required' => TRUE,
);
$form['service_class'] = array(
'#type' => 'value',
'#value' => '',
);
$form['save'] = array(
'#type' => 'submit',
'#validate' =>array('apachesolr_server_save_validate'),
'#submit' =>array('apachesolr_server_save_submit'),
'#value' => t('Save'),
);
$form['test'] = array(
'#type' => 'submit',
'#validate' =>array('apachesolr_server_save_validate'),
'#submit' =>array('apachesolr_server_test_submit'),
'#value' => t('Test connection'),
);
if (!empty($server['server_id']) && !$is_default) {
$form['delete'] = array(
'#type' => 'submit',
'#submit' =>array('apachesolr_server_delete_submit'),
'#value' => t('Delete'),
);
}
return $form;
}
function apachesolr_server_delete_submit($form, &$form_state) {
$form_state['redirect'] = 'admin/config/search/apachesolr/server/' . $form_state['values']['original_server_id'] . '/delete';
}
function apachesolr_server_test_submit($form, &$form_state) {
extract($form_state['values']);
$ping = apachesolr_server_status($host, $port, $path, $service_class);
if ($ping) {
drupal_set_message(t('Your site has contacted the Apache Solr server.'));
}
else {
drupal_set_message(t('Your site was unable to contact the Apache Solr server.'), 'error');
}
$form_state['rebuild'] = TRUE;
}
function apachesolr_server_save_validate($form, &$form_state) {
// Normalize the path to have just a leading slash.
$form_state['values']['path'] = '/' . trim($form_state['values']['path'], '/');
if (isset($form['port']) && isset($form_state['values']['port'])) {
$port = $form_state['values']['port'];
// TODO: Port range should be 0-65535, but 0 crashes apachesolr
if (!ctype_digit($port) || $port < 1 || $port > 65535) {
form_set_error('port', t('The port has to be an integer between 1 and 65535.'));
}
}
}
function apachesolr_server_save_submit($form, &$form_state) {
apachesolr_server_save($form_state['values'], $form_state['values']['original_server_id']);
if (!empty($form_state['values']['make_default'])) {
variable_set('apachesolr_default_server', $form_state['values']['server_id']);
}
cache_clear_all('apachesolr:servers', 'cache_apachesolr');
$form_state['redirect'] = 'admin/config/search/apachesolr';
}
/**
* Form builder for general settings used as a menu callback.
*/
function apachesolr_settings($form, &$form_state) {
$form = array();
$collapse_host = TRUE;
// Perform a check to ensure the server is there
if (empty($_POST)) {
module_load_install('apachesolr');
foreach (apachesolr_requirements('runtime') as $requirement) {
$status = $requirement['severity'] == REQUIREMENT_ERROR ? 'error' : 'status';
if ($status == 'error') {
drupal_set_message($requirement['title'] . ': ' . $requirement['value'], $status);
$collapse_host = FALSE;
}
}
}
// Add a link to add more mlt blocks.
$form['mlt_link'] = array(
'#type' => 'item',
'#description' => format_plural(count(apachesolr_mlt_list_blocks()), 'You currently have 1 block.', 'You currenly have @count blocks.'),
);
$form['apachesolr_host_settings'] = array(
'#title' => t('Solr host settings'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => $collapse_host,
);
$id = variable_get('apachesolr_default_server', 'solr');
$servers = apachesolr_load_all_servers();
$rows = array();
$headers = array(
t('Default'),
array('data' => t('Operations'), 'colspan' => 2),
t('Name'),
t('Host'),
t('Port'),
t('Path'),
);
foreach ($servers as $server_id => $data) {
$is_default = $server_id == variable_get('apachesolr_default_server', 'solr');
if ($is_default) {
$type = 'selected';
$delete_link = '';
}
else {
$type = 'none';
$delete_link = array('class' => 'operation', 'data' => l(t('delete'), 'admin/config/search/apachesolr/server/' . $data['server_id'] .'/delete'));
}
$rows[] = array('data' =>
array(
// Cells
array('class' => 'icon'),
array('class' => 'operation', 'data' => l(t('edit'), 'admin/config/search/apachesolr/server/' . $data['server_id'] .'/edit')),
$delete_link,
check_plain($data['name']),
check_plain($data['host']),
check_plain($data['port']),
check_plain($data['path']),
),
// Attributes for tr
'class' => array(drupal_html_class('apachesolr-' . $type)),
);
}
$actions[] = array(
'#theme' => 'menu_local_action',
'#link' => array('title' => t('Add new server'), 'href' => 'admin/config/search/apachesolr/server/add', 'options' => array()),
);
$form['apachesolr_host_settings']['actions'] = array(
'#markup' => '
' . t('Using schema.xml version: @schema_version', $stats_summary);
if ($stats_summary['@core_name']) {
$solr_msg .= '
' . t('Solr core name: @core_name', $stats_summary);
}
$delay_msg = '
' . t('The server has a @autocommit_time delay before updates are processed.', $stats_summary) . "
' . t('Number of pending deletions: @deletes_total', $stats_summary) . "
\n"; } catch (Exception $e) { watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); } $status_header .= $solr_msg . $delay_msg; $pending_msg = $stats_summary['@pending_docs'] ? t('(@pending_docs sent but not yet processed)', $stats_summary) : ''; $status_header .= '' . t('Number of documents in index: @num !pending', array('@num' => $data->index->numDocs, '!pending' => $pending_msg)) . "
\n"; $status_header .= $delete_msg; } $status_header .= '' . l(t('View more details on the search index contents'), 'admin/reports/apachesolr') . "
\n"; if (variable_get('apachesolr_read_only', APACHESOLR_READ_WRITE) == APACHESOLR_READ_WRITE) { $output['apachesolr_index_action_status_header'] = array('#markup' => $status_header); // Display the Delete Index form. $output['apachesolr_index_action_form'] = drupal_get_form('apachesolr_index_action_form'); } else { drupal_set_message(t('The index is in read-only mode. Options for deleting and re-indexing are therefore not available. The index mode can be changed on the !settings_page', array('!settings_page' => l(t('settings page'), 'admin/config/search/apachesolr'))), 'warning'); } return $output; } function apachesolr_index_report($server_id = NULL) { if (empty($server_id)) { $server_id = apachesolr_default_server(); } try { $solr = apachesolr_get_solr($server_id); // TODO: possibly clear this every page view if we are running multi-site. if (apachesolr_index_get_last_updated()) { $solr->clearCache(); } $data = $solr->getLuke(); } catch (Exception $e) { watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); drupal_set_message(nl2br(check_plain($e->getMessage())), "warning"); $data->fields = array(); } $output = '' . t('Number of documents in index: @num !pending', array('@num' => $data->index->numDocs, '!pending' => '')) . "
\n"; $limit = variable_get('apachesolr_luke_limit', 20000); if (isset($data->index->numDocs) && $data->index->numDocs > $limit) { $output .= '' . t('You have more than @limit documents, so term frequencies are being omitted for performance reasons.', array('@limit' => $limit)) . "
\n"; $not_found = t('Omitted'); } elseif (isset($data->index->numDocs)) { $not_found = t('Not indexed'); try { $solr = apachesolr_get_solr($server_id); // Note: we use 2 since 1 fails on Ubuntu Hardy. $data = $solr->getLuke(2); $output .= '' . t('Number of terms in index: @num', array('@num' => $data->index->numTerms)) . "
\n"; } catch (Exception $e) { watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); $data->fields = array(); } } $fields = (array)$data->fields; if ($fields) { $output .= '' . t('Number of fields in index: @num', array('@num' => count($fields))) . "
\n"; $rows = array(); foreach ($fields as $name => $field) { // TODO: try to map the name to something more meaningful. $rows[$name] = array($name, $field->type, isset($field->distinct) ? $field->distinct : $not_found); } ksort($rows); // Display the table of Field names, Index Types, and term counts. $output .= theme('table', array(t('Field name'), t('Index type'), t('Distinct terms')), $rows); } else { $output .= '' . t('No data on indexed fields.') . "
\n"; } return $output; } /** * Indicates what order the specified facets should be listed in. This function is used in a usort * invocation. * @param $a * The first facet. * @param $b * The second facet. * @return * A signed integer that indicates which of the specified facets should come first. */ function apachesolr_sort_facets($a, $b) { return strcasecmp($a['info'], $b['info']); } /** * This is the submit handler for the active facets form. * * The form values for each module are array filtereed to remove non-enabled items and * stored in the variable table with the name 'apachesolr_enabled_facets'. * * @see apachesolr_enabled_facets_form() */ function apachesolr_enabled_facets_form_submit($form, &$form_state) { $enabled = array(); foreach ($form_state['values']['apachesolr_enabled_facets'] as $module => $facets) { $enabled[$module] = array_filter($facets); } apachesolr_save_enabled_facets($form['#server_id'], $enabled); // This cache being stale can prevent new facet filters from working. apachesolr_clear_cache($form['#server_id']); $msg = t('The Apache Solr filters settings were changed. To arrange the blocks for your enabled filters, visit the blocks administration page.', array('@url' => url('admin/structure/block'))); drupal_set_message($msg, 'warning'); } /** * Creates the form that allows the user to select which facets will be enabled. * * Only enabled facets are sent to solr. Fewer enabled facets can reduce the * load on the search server. Blocks are only offered for enabled facets, so * this also reduces the clutter on the blocks admin page. */ function apachesolr_enabled_facets_form($form, &$form_state, $server_id = NULL) { if (empty($server_id)) { $server_id = apachesolr_default_server(); } $facets = array(); $module_facets = array(); $module_list = array(); foreach (module_implements('apachesolr_facets') as $module) { $return = module_invoke($module, 'apachesolr_facets'); if (!empty($return)) { $module_facets[$module] = $return; uasort($module_facets[$module], 'apachesolr_sort_facets'); $module_list[$module] = $module; } } $enabled_facets = apachesolr_get_enabled_facets($server_id); $form = array(); $form['#server_id'] = $server_id; $form['apachesolr_enabled_facets']['help'] = array( '#type' => 'item', '#markup' => t('You can use this screen to select which search filter blocks should be created by enabling the corresponding filters on this page. For performance reasons, you should only enable filters that you intend to have available to users on the search page. After selecting which filter blocks to create, you will be sent to the blocks page where you can choose which of those blocks should be enabled when your users search by placing each block in a region.'), ); if ($module_list) { $result = db_select('system') ->fields('system', array('name', 'info')) ->condition('name', $module_list, 'IN') ->condition('type', 'module') ->execute(); foreach ($result as $item) { $module_list[$item->name] = unserialize($item->info); } } foreach ($module_facets as $module => $facets) { $form['apachesolr_enabled_facets'][$module] = array( '#type' => 'fieldset', '#title' => check_plain($module_list[$module]['name']), '#collapsible' => TRUE, '#collapsed' => FALSE, ); // We must use module + delta as the keys since that combination is // guaranteed to be unique. A single module could, for example, have // two different blocks that expose different faceting on the same // field in the index. foreach ($facets as $delta => $data) { $form['apachesolr_enabled_facets'][$module][$delta] = array( '#type' => 'checkbox', '#title' => $data['info'], '#return_value' => $data['facet_field'], '#default_value' => isset($enabled_facets[$module][$delta]) ? $data['facet_field'] : 0, ); } } $has_facets = (bool)$module_facets; $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#access' => $has_facets, ); $form['no-facets-message'] = array( '#markup' => t('No filters are available from your currently enabled modules'), '#access' => !$has_facets, ); $form['#tree'] = TRUE; return $form; } /** * Create a form for deleting the contents of the Solr index. */ function apachesolr_index_action_form() { $form = array(); $form['action'] = array( '#type' => 'fieldset', '#title' => t('Index Actions'), ); // Jump through some forms hoops to get a description under each radio button. $actions = array( 'remaining' => array( 'title' => t('Index queued content'), 'description' => t('Any content that is queued for indexing will be submitted to Solr immediately. Depending on amount of content on the site, it may take a long time to complete, and may place an increased load on your server.'), ), 'reindex' => array( 'title' => t('Queue content for reindexing'), 'description' => t('All content on the site will be queued for indexing. The documents currently in the Solr index will remain searchable. The content will be gradually resubmitted to Solr during cron runs.'), ), 'delete' => array( 'title' => t('Delete the index'), 'description' => t('All documents in the Solr index will be deleted. This is rarely necessary unless your index is corrupt or you have installed a new schema.xml. After doing this your content will need to be resubmitted for indexing.'), ), ); foreach ($actions as $key => $action) { // Generate the parents as the autogenerator does, so we will have a // unique id for each radio button. $form['action'][$key] = array( '#type' => 'radio', '#title' => $action['title'], '#default_value' => 'remaining', '#return_value' => $key, '#parents' => array('action'), '#description' => $action['description'], '#id' => drupal_clean_css_identifier('edit-'. implode('-', array('action', $key))), ); } $form['submit'] = array( '#type' => 'submit', '#value' => t('Begin'), '#submit' => array('apachesolr_index_action_form_submit'), ); return $form; } /** * Submit function for the index action form. */ function apachesolr_index_action_form_submit($form, &$form_state) { switch ($form_state['values']['action']) { case 'remaining': apachesolr_batch_index_remaining(); break; case 'reindex': $form_state['redirect'] = 'admin/config/search/apachesolr/index/confirm/clear'; break; case 'delete': $form_state['redirect'] = 'admin/config/search/apachesolr/index/confirm/delete'; break; } } /** * Confirmation form for "Re-index all content" button * @see apachesolr_clear_index_submit() */ function apachesolr_clear_index_confirm() { $form = array(); return confirm_form($form, t('Are you sure you want to queue content for reindexing?'), 'admin/config/search/apachesolr/index', t('All content on the site will be queued for indexing. The documents currently in the Solr index will remain searchable. The content will be gradually resubmitted to Solr during cron runs.'), t('Reindex'), t('Cancel')); } /** * Submit function for the "Re-index all content" confirmation form. * * @see apachesolr_clear_index_confirm() */ function apachesolr_clear_index_confirm_submit($form, &$form_state) { $form_state['redirect'] = 'admin/config/search/apachesolr/index'; apachesolr_rebuild_index_table(); drupal_set_message(t('All the content on your site is queued for indexing. You can wait for it to be indexed during cron runs, or you can manually reindex it.'), 'warning'); } /** * Confirmation form for "Delete the index" button * @see apachesolr_delete_index_submit() */ function apachesolr_delete_index_confirm() { $form = array(); return confirm_form($form, t('Are you sure you want to delete the search index?'), 'admin/config/search/apachesolr/index', t("All documents in the Solr index will be deleted. This is rarely necessary unless your index is corrupt or you have installed a new schema.xml. After doing this your content will need to be queued for indexing."), t('Delete'), t('Cancel')); } /** * Submit function for the "Delete the index" confirmation form. * * @see apachesolr_delete_index_confirm() */ function apachesolr_delete_index_confirm_submit($form, &$form_state) { $form_state['redirect'] = 'admin/config/search/apachesolr/index'; try { apachesolr_delete_index(); drupal_set_message(t('The Apache Solr content index has been erased. All the content on your site is queued for indexing. You can wait for it to be indexed during cron runs, or you can manually reindex it.'), 'warning'); } catch (Exception $e) { watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); } } /** * Utility function to delete the index and reset all index counters. * * @param $type * a single content type to be deleted from the index. * * @throws Exception */ function apachesolr_delete_index($type = NULL) { // Instantiate a new Solr object. $solr = apachesolr_get_solr(); if ($type) { $query = 'type:' . $type; } else { $query = '*:*'; } // Allow other modules to modify the delete query. // For example, use the site hash so that you only delete this site's // content: $query = 'hash:' . apachesolr_site_hash() drupal_alter('apachesolr_delete_index', $query); $solr->deleteByQuery($query); $solr->commit(); // Rebuild our node-tracking table. apachesolr_rebuild_index_table(); apachesolr_index_set_last_updated(REQUEST_TIME); } /** * MoreLikeThis administration and utility functions. */ function apachesolr_mlt_add_block_form() { $form = apachesolr_mlt_block_form(); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => '5', ); return $form; } function apachesolr_mlt_add_block_form_submit($form, &$form_state) { apachesolr_mlt_save_block($form_state['values']); drupal_set_message('New content recommendation block created. Drag it into a region to enable it'); $form_state['redirect'] = 'admin/build/block'; } /** * Form to edit moreLikeThis block settings. * * @param int $delta If editing, the id of the block to edit. * * @return array The form used for editing. * TODO: * Add term boost settings. * Enable the user to specify a query, rather then forcing suggestions based * on the node id. * */ function apachesolr_mlt_block_form($delta = NULL) { if (isset($delta)) { $block = apachesolr_mlt_load_block($delta); if (!$block) { return array(); } } else { $block = apachesolr_mlt_block_defaults(); } $form['name'] = array( '#type' => 'textfield', '#title' => t('Block name'), '#description' => t('The block name displayed to site users.'), '#required' => TRUE, '#default_value' => $block['name'], '#weight' => '-2', ); $form['num_results'] = array( '#type' => 'select', '#title' => t('Maximum number of related items to display'), '#default_value' => $block['num_results'], '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)), '#weight' => -1, ); $form['mlt_fl'] = array( '#type' => 'checkboxes', '#title' => t('Fields for finding related content'), '#description' => t('Choose the fields to be used in calculating similarity. The default combination of %taxonomy_names and %title will provide relevant results for typical sites.', array("%taxonomy_names" => apachesolr_field_name_map("taxonomy_names"), "%title" => apachesolr_field_name_map("title"))), '#options' => apachesolr_mlt_get_fields(), '#required' => TRUE, '#default_value' => $block['mlt_fl'], ); $form['advanced'] = array( '#type' => 'fieldset', '#title' => t('Advanced configuration'), '#weight' => '1', '#collapsible' => TRUE, '#collapsed' => TRUE, ); $options = drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7)); $form['advanced']['mlt_mintf'] = array( '#type' => 'select', '#title' => t('Minimum term frequency'), '#description' => t('A word must appear this many times in any given document before the document is considered relevant for comparison.'), '#default_value' => $block['mlt_mintf'], '#options' => $options, ); $form['advanced']['mlt_mindf'] = array( '#type' => 'select', '#title' => t('Minimum document frequency'), '#description' => t('A word must occur in at least this many documents before it will be used for similarity comparison.'), '#default_value' => $block['mlt_mindf'], '#options' => $options, ); $form['advanced']['mlt_minwl'] = array( '#type' => 'select', '#title' => t('Minimum word length'), '#description' => 'You can use this to eliminate short words such as "the" and "it" from similarity comparisons. Words must be at least this number of characters or they will be ignored.', '#default_value' => $block['mlt_minwl'], '#options' => $options, ); $form['advanced']['mlt_maxwl'] = array( '#type' => 'select', '#title' => t('Maximum word length'), '#description' => t('You can use this to eliminate very long words from similarity comparisons. Words of more than this number of characters will be ignored.'), '#default_value' => $block['mlt_maxwl'], '#options' => drupal_map_assoc(array(8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)), ); $form['advanced']['mlt_maxqt'] = array( '#type' => 'select', '#title' => t('Maximum number of query terms'), '#description' => t('The maximum number of query terms that will be included in any query. Lower numbers will result in fewer recommendations but will get results faster. If a content recommendation is not returning any recommendations, you can either check more "Comparison fields" checkboxes or increase the maximum number of query terms here.'), '#options' => drupal_map_assoc(array(3, 5, 7, 10, 12, 15, 20, 25, 30, 35, 40)), '#default_value' => $block['mlt_maxqt'], ); $form['restrictions'] = array( '#type' => 'fieldset', '#title' => t('Filters'), '#weight' => '1', '#collapsible' => TRUE, '#collapsed' => TRUE, ); $type_options = array(); foreach (node_type_get_types() as $key => $type) { $type_options[$key] = $type->name; } $form['restrictions']['mlt_type_filters'] = array( '#type' => 'checkboxes', '#title' => t('Content Types'), '#default_value' => is_array($block['mlt_type_filters']) ? $block['mlt_type_filters'] : array_keys($type_options), '#options' => $type_options, '#description' => t('Select the content types that similarity suggestions should be restricted to. Multiple types are joined with an OR query, so selecting more types results in more recommendations.'), '#weight' => '-2', ); $form['restrictions']['mlt_custom_filters'] = array( '#type' => 'textfield', '#title' => t('Additional Query'), '#description' => t('An additional query, in Lucene syntax, which will further filter the similarity suggestions. Ex. \'title:strategy\' will filter related content further to only those with strategy in the title. ' . 'Likewise \'body:HDTV^3\' will boost the term HDTV by a factor of 3. Here are some more examples:' . '