View source
<?php
define('APACHESOLR_READ_WRITE', 0);
define('APACHESOLR_READ_ONLY', 1);
function apachesolr_init() {
if (!function_exists('json_decode')) {
set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
require_once 'Zend/Json/Decoder.php';
require_once 'Zend/Json/Encoder.php';
function json_decode($string, $assoc = FALSE) {
if ($assoc) {
$objectDecodeType = Zend_Json::TYPE_ARRAY;
}
else {
$objectDecodeType = Zend_Json::TYPE_OBJECT;
}
return Zend_Json_Decoder::decode($string, $objectDecodeType);
}
function json_encode($data) {
return Zend_Json_Encoder::encode($data, $objectDecodeType);
}
}
}
function apachesolr_menu() {
$items = array();
$items['admin/settings/apachesolr'] = array(
'title' => 'Apache Solr',
'description' => 'Administer Apache Solr.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'apachesolr_settings',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer search',
),
'file' => 'apachesolr.admin.inc',
);
$items['admin/settings/apachesolr/settings'] = array(
'title' => 'Settings',
'weight' => -10,
'access arguments' => array(
'administer search',
),
'file' => 'apachesolr.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/settings/apachesolr/enabled-filters'] = array(
'title' => 'Enabled filters',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'apachesolr_enabled_facets_form',
),
'weight' => -7,
'access arguments' => array(
'administer search',
),
'file' => 'apachesolr.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/settings/apachesolr/index'] = array(
'title' => 'Search index',
'page callback' => 'apachesolr_index_page',
'access arguments' => array(
'administer search',
),
'weight' => -8,
'file' => 'apachesolr.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/settings/apachesolr/index/confirm/clear'] = array(
'title' => 'Confirm the re-indexing of all content',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'apachesolr_clear_index_confirm',
),
'access arguments' => array(
'administer search',
),
'file' => 'apachesolr.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/settings/apachesolr/index/confirm/delete'] = array(
'title' => 'Confirm index deletion',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'apachesolr_delete_index_confirm',
),
'access arguments' => array(
'administer search',
),
'file' => 'apachesolr.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/reports/apachesolr'] = array(
'title' => 'Apache Solr search index',
'page callback' => 'apachesolr_index_report',
'access arguments' => array(
'access site reports',
),
'file' => 'apachesolr.admin.inc',
);
$items['admin/reports/apachesolr/index'] = array(
'title' => 'Search index',
'file' => 'apachesolr.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/reports/apachesolr/conf'] = array(
'title' => 'Configuration files',
'page callback' => 'apachesolr_config_files_overview',
'access arguments' => array(
'access site reports',
),
'file' => 'apachesolr.admin.inc',
'weight' => 5,
'type' => MENU_LOCAL_TASK,
);
$items['admin/reports/apachesolr/conf/%'] = array(
'title' => 'Configuration file',
'page callback' => 'apachesolr_config_file',
'page arguments' => array(
4,
),
'access arguments' => array(
'access site reports',
),
'file' => 'apachesolr.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/settings/apachesolr/mlt/add_block'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'apachesolr_mlt_add_block_form',
),
'access arguments' => array(
'administer search',
),
'file' => 'apachesolr.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/settings/apachesolr/mlt/delete_block/%'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'apachesolr_mlt_delete_block_form',
5,
),
'access arguments' => array(
'administer search',
),
'file' => 'apachesolr.admin.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
function apachesolr_failure($search_name, $querystring) {
$fail_rule = variable_get('apachesolr_failure', 'show_error');
switch ($fail_rule) {
case 'show_error':
drupal_set_message(t('The Apache Solr search engine is not available. Please contact your site administrator.'), 'error');
break;
case 'show_drupal_results':
drupal_set_message(t("%search_name is not available. Your search is being redirected.", array(
'%search_name' => $search_name,
)));
drupal_goto('search/node/' . drupal_urlencode($querystring));
break;
case 'show_no_results':
return;
}
}
function apachesolr_site_hash() {
if (!($hash = variable_get('apachesolr_site_hash', FALSE))) {
global $base_url;
$hash = substr(base_convert(sha1(uniqid($base_url, TRUE)), 16, 36), 0, 6);
variable_set('apachesolr_site_hash', $hash);
}
return $hash;
}
function apachesolr_document_id($id, $entity = 'node') {
return apachesolr_site_hash() . "/{$entity}/" . $id;
}
function apachesolr_user($op, &$edit, &$account) {
switch ($op) {
case 'update':
if (isset($edit['name']) && $account->name != $edit['name']) {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query("UPDATE {apachesolr_search_node} asn INNER JOIN {node} n ON asn.nid = n.nid SET asn.changed = %d WHERE n.uid = %d", time(), $account->uid);
break;
default:
db_query("UPDATE {apachesolr_search_node} SET changed = %d WHERE nid IN (SELECT nid FROM {node} WHERE uid = %d)", time(), $account->uid);
break;
}
}
break;
}
}
function apachesolr_taxonomy($op, $type, $edit) {
if ($type == 'term' && $op == 'update') {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query("UPDATE {apachesolr_search_node} asn INNER JOIN {term_node} tn ON asn.nid = tn.nid SET asn.changed = %d WHERE tn.tid = %d", time(), $edit['tid']);
break;
default:
db_query("UPDATE {apachesolr_search_node} SET changed = %d WHERE nid IN (SELECT nid FROM {term_node} WHERE tid = %d)", time(), $edit['tid']);
break;
}
}
}
function apachesolr_comment($edit, $op) {
$edit = (array) $edit;
switch ($op) {
case 'insert':
case 'update':
case 'delete':
case 'publish':
case 'unpublish':
apachesolr_mark_node($edit['nid']);
break;
}
}
function apachesolr_mark_node($nid) {
db_query("UPDATE {apachesolr_search_node} SET changed = %d WHERE nid = %d", time(), $nid);
}
function apachesolr_node_type($op, $info) {
if ($op == 'delete') {
apachesolr_rebuild_index_table($info->type);
}
elseif (!empty($info->old_type) && $info->old_type != $info->type) {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query("UPDATE {apachesolr_search_node} asn INNER JOIN {node} n ON asn.nid = n.nid SET asn.changed = %d WHERE (n.type = '%s' OR n.type = '%s')", time(), $info->old_type, $info->type);
break;
default:
db_query("UPDATE {apachesolr_search_node} SET changed = %d WHERE nid IN (SELECT nid FROM {node} WHERE type = '%s' OR type = '%s')", time(), $info->old_type, $info->type);
break;
}
}
}
function apachesolr_index_status($namespace) {
list($excluded_types, $args, $join_sql, $exclude_sql) = apachesolr_exclude_types($namespace);
$total = db_result(db_query("SELECT COUNT(asn.nid) FROM {apachesolr_search_node} asn " . $join_sql . "WHERE asn.status = 1 " . $exclude_sql, $excluded_types));
$remaining = db_result(db_query("SELECT COUNT(asn.nid) FROM {apachesolr_search_node} asn " . $join_sql . "WHERE (asn.changed > %d OR (asn.changed = %d AND asn.nid > %d)) AND asn.status = 1 " . $exclude_sql, $args));
return array(
'remaining' => $remaining,
'total' => $total,
);
}
function apachesolr_get_last_index($namespace) {
$stored = variable_get('apachesolr_index_last', array());
return isset($stored[$namespace]) ? $stored[$namespace] : array(
'last_change' => 0,
'last_nid' => 0,
);
}
function apachesolr_clear_last_index($namespace = '') {
if ($namespace) {
$stored = variable_get('apachesolr_index_last', array());
unset($stored[$namespace]);
variable_set('apachesolr_index_last', $stored);
}
else {
variable_del('apachesolr_index_last');
}
}
function apachesolr_rebuild_index_table($type = NULL) {
if (isset($type)) {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query("DELETE FROM {apachesolr_search_node} USING {apachesolr_search_node} asn INNER JOIN {node} n ON asn.nid = n.nid WHERE n.type = '%s'", $type);
break;
default:
db_query("DELETE FROM {apachesolr_search_node} WHERE nid IN (SELECT nid FROM {node} WHERE type = '%s')", $type);
break;
}
db_query("INSERT INTO {apachesolr_search_node} (nid, status, changed)\n SELECT n.nid, n.status, %d AS changed\n FROM {node} n WHERE n.type = '%s'", time(), $type);
}
else {
db_query("DELETE FROM {apachesolr_search_node}");
db_query("INSERT INTO {apachesolr_search_node} (nid, status, changed)\n SELECT n.nid, n.status, GREATEST(n.created, n.changed, COALESCE(c.last_comment_timestamp, 0)) AS changed\n FROM {node} n\n LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid");
$time = time();
db_query("UPDATE {apachesolr_search_node} SET changed = %d WHERE changed > %d", $time, $time);
apachesolr_clear_last_index();
}
cache_clear_all('*', 'cache_apachesolr', TRUE);
}
function apachesolr_exclude_types($namespace) {
extract(apachesolr_get_last_index($namespace));
$excluded_types = module_invoke_all('apachesolr_types_exclude', $namespace);
$args = array(
$last_change,
$last_change,
$last_nid,
);
$join_sql = '';
$exclude_sql = '';
if ($excluded_types) {
$excluded_types = array_unique($excluded_types);
$join_sql = "INNER JOIN {node} n ON n.nid = asn.nid ";
$exclude_sql = "AND n.type NOT IN(" . db_placeholders($excluded_types, 'varchar') . ") ";
$args = array_merge($args, $excluded_types);
}
return array(
$excluded_types,
$args,
$join_sql,
$exclude_sql,
);
}
function apachesolr_get_nodes_to_index($namespace, $limit) {
$rows = array();
if (variable_get('apachesolr_read_only', APACHESOLR_READ_WRITE) == APACHESOLR_READ_ONLY) {
return $rows;
}
list($excluded_types, $args, $join_sql, $exclude_sql) = apachesolr_exclude_types($namespace);
$result = db_query_range("SELECT asn.nid, asn.changed FROM {apachesolr_search_node} asn " . $join_sql . "WHERE (asn.changed > %d OR (asn.changed = %d AND asn.nid > %d)) AND asn.status = 1 " . $exclude_sql . "ORDER BY asn.changed ASC, asn.nid ASC", $args, 0, $limit);
while ($row = db_fetch_object($result)) {
$rows[] = $row;
}
return $rows;
}
function apachesolr_index_nodes($rows, $namespace) {
if (!$rows) {
return FALSE;
}
try {
$solr = apachesolr_get_solr();
if (!$solr
->ping(variable_get('apachesolr_ping_timeout', 4))) {
throw new Exception(t('No Solr instance available during indexing.'));
}
} catch (Exception $e) {
watchdog('Apache Solr', nl2br(check_plain($e
->getMessage())), NULL, WATCHDOG_ERROR);
return FALSE;
}
module_load_include('inc', 'apachesolr', 'apachesolr.index');
$documents = array();
$old_position = apachesolr_get_last_index($namespace);
$position = $old_position;
$callbacks = module_invoke_all('apachesolr_document_handlers', 'node', $namespace);
$callbacks = array_filter($callbacks, 'function_exists');
global $user;
session_save_session(FALSE);
$saved_user = $user;
$user = drupal_anonymous_user();
foreach ($rows as $row) {
try {
if ($node = node_load($row->nid, NULL, TRUE)) {
foreach ($callbacks as $callback) {
$documents[] = $callback($node, $namespace);
}
}
$position['last_change'] = $row->changed;
$position['last_nid'] = $row->nid;
} catch (Exception $e) {
watchdog('Apache Solr', 'Error constructing documents to index: <br /> !message', array(
'!message' => "Node ID: {$row->nid}<br />" . nl2br(strip_tags($e
->getMessage())),
), WATCHDOG_ERROR);
}
}
$user = $saved_user;
session_save_session(TRUE);
$tmp = array();
apachesolr_flatten_documents_array($documents, $tmp);
$documents = $tmp;
if (count($documents)) {
try {
watchdog('Apache Solr', 'Adding @count documents.', array(
'@count' => count($documents),
));
$docs_chunk = array_chunk($documents, 20);
foreach ($docs_chunk as $docs) {
$solr
->addDocuments($docs);
}
apachesolr_index_set_last_updated($position['last_change']);
} catch (Exception $e) {
$nids = array();
if (!empty($docs)) {
foreach ($docs as $doc) {
$nids[] = $doc->nid;
}
}
watchdog('Apache Solr', 'Indexing failed on one of the following nodes: @nids <br /> !message', array(
'@nids' => implode(', ', $nids),
'!message' => nl2br(strip_tags($e
->getMessage())),
), WATCHDOG_ERROR);
return FALSE;
}
}
if ($namespace && $position != $old_position) {
$stored = variable_get('apachesolr_index_last', array());
$stored[$namespace] = $position;
variable_set('apachesolr_index_last', $stored);
}
return $position;
}
function apachesolr_date_iso($date_timestamp) {
return gmdate('Y-m-d\\TH:i:s\\Z', $date_timestamp);
}
function apachesolr_flatten_documents_array($documents, &$tmp) {
foreach ($documents as $index => $item) {
if (is_array($item)) {
apachesolr_flatten_documents_array($item, $tmp);
}
else {
$tmp[] = $item;
}
}
}
function apachesolr_delete_node_from_index($node) {
static $failed = FALSE;
if ($failed) {
return FALSE;
}
try {
$solr = apachesolr_get_solr();
$solr
->deleteById(apachesolr_document_id($node->nid));
apachesolr_index_set_last_updated(time());
return TRUE;
} catch (Exception $e) {
watchdog('Apache Solr', nl2br(check_plain($e
->getMessage())), NULL, WATCHDOG_ERROR);
$failed = TRUE;
return FALSE;
}
}
function apachesolr_index_set_last_updated($updated = 0) {
if ($updated) {
variable_set('apachesolr_index_updated', (int) $updated);
}
else {
variable_del('apachesolr_index_updated');
}
}
function apachesolr_index_get_last_updated() {
return variable_get('apachesolr_index_updated', 0);
}
function apachesolr_cron() {
module_load_include('inc', 'apachesolr', 'apachesolr.index');
apachesolr_cron_check_node_table();
try {
$solr = apachesolr_get_solr();
$result = db_query("SELECT n.nid, n.status FROM {apachesolr_search_node} asn INNER JOIN {node} n ON n.nid = asn.nid WHERE asn.status <> n.status");
while ($node = db_fetch_object($result)) {
apachesolr_nodeapi_update($node, FALSE);
}
$result = db_query("SELECT asn.nid FROM {apachesolr_search_node} asn LEFT JOIN {node} n ON n.nid = asn.nid WHERE n.nid IS NULL");
while ($node = db_fetch_object($result)) {
apachesolr_nodeapi_delete($node, FALSE);
}
$optimize_interval = variable_get('apachesolr_optimize_interval', 60 * 60 * 24);
$last = variable_get('apachesolr_last_optimize', 0);
$time = time();
if ($optimize_interval && $time - $last > $optimize_interval) {
$solr
->optimize(FALSE, FALSE);
variable_set('apachesolr_last_optimize', $time);
apachesolr_index_set_last_updated($time);
}
$updated = apachesolr_index_get_last_updated();
if ($updated) {
$solr
->clearCache();
$solr
->getLuke();
if ($time - $updated >= variable_get('apachesolr_cache_delay', 300)) {
apachesolr_index_set_last_updated(0);
}
}
} catch (Exception $e) {
watchdog('Apache Solr', nl2br(check_plain($e
->getMessage())) . ' in apachesolr_cron', NULL, WATCHDOG_ERROR);
}
}
function apachesolr_flush_caches() {
return array(
'cache_apachesolr',
);
}
function apachesolr_clear_cache() {
try {
$solr = apachesolr_get_solr();
$solr
->clearCache();
} catch (Exception $e) {
watchdog('Apache Solr', nl2br(check_plain($e
->getMessage())), NULL, WATCHDOG_ERROR);
drupal_set_message(nl2br(check_plain($e
->getMessage())), 'warning');
}
}
function apachesolr_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'delete':
apachesolr_nodeapi_delete($node);
break;
case 'insert':
db_query("INSERT INTO {apachesolr_search_node} (nid, status, changed) VALUES (%d, %d, %d)", $node->nid, $node->status, time());
break;
case 'update':
apachesolr_nodeapi_update($node);
break;
}
}
function apachesolr_nodeapi_delete($node, $set_message = TRUE) {
if (apachesolr_delete_node_from_index($node)) {
db_query("DELETE FROM {apachesolr_search_node} WHERE nid = %d", $node->nid);
if ($set_message && user_access('administer search') && variable_get('apachesolr_set_nodeapi_messages', 1)) {
apachesolr_set_stats_message('Deleted content will be removed from the Apache Solr search index in approximately @autocommit_time.');
}
}
}
function apachesolr_nodeapi_update($node, $set_message = TRUE) {
if (!$node->status && db_result(db_query("SELECT status FROM {apachesolr_search_node} WHERE nid = %d", $node->nid))) {
if (apachesolr_delete_node_from_index($node)) {
db_query("UPDATE {apachesolr_search_node} SET changed = %d, status = %d WHERE nid = %d", time(), $node->status, $node->nid);
if ($set_message && user_access('administer search') && variable_get('apachesolr_set_nodeapi_messages', 1)) {
apachesolr_set_stats_message('Unpublished content will be removed from the Apache Solr search index in approximately @autocommit_time.');
}
}
}
else {
db_query("UPDATE {apachesolr_search_node} SET changed = %d, status = %d WHERE nid = %d", time(), $node->status, $node->nid);
}
}
function apachesolr_set_stats_message($text, $type = 'status', $repeat = FALSE) {
try {
$solr = apachesolr_get_solr();
$stats_summary = $solr
->getStatsSummary();
drupal_set_message(t($text, $stats_summary), $type, FALSE);
} catch (Exception $e) {
watchdog('Apache Solr', nl2br(check_plain($e
->getMessage())), NULL, WATCHDOG_ERROR);
}
}
function apachesolr_get_enabled_facets($module = NULL) {
$enabled = variable_get('apachesolr_enabled_facets', array());
if (isset($module)) {
return isset($enabled[$module]) ? $enabled[$module] : array();
}
return $enabled;
}
function apachesolr_save_enabled_facets($enabled) {
variable_set('apachesolr_enabled_facets', $enabled);
return $enabled;
}
function apachesolr_save_module_facets($module, $facets = array()) {
$enabled = variable_get('apachesolr_enabled_facets', array());
if (!empty($facets) && is_array($facets)) {
$enabled[$module] = $facets;
}
else {
unset($enabled[$module]);
}
variable_set('apachesolr_enabled_facets', $enabled);
return $enabled;
}
function apachesolr_block($op = 'list', $delta = 0, $edit = array()) {
static $access;
switch ($op) {
case 'list':
$blocks = apachesolr_mlt_list_blocks();
$blocks['sort'] = array(
'info' => t('Apache Solr Core: Sorting'),
'cache' => BLOCK_CACHE_PER_PAGE,
);
return $blocks;
case 'view':
if ($delta != 'sort' && ($node = menu_get_object()) && (!arg(2) || arg(2) == 'view')) {
$suggestions = array();
if (!isset($access)) {
$access = node_access('view', $node);
}
$block = apachesolr_mlt_load_block($delta);
if ($access && $block) {
$docs = apachesolr_mlt_suggestions($block, apachesolr_document_id($node->nid));
if (!empty($docs)) {
$suggestions['subject'] = check_plain($block['name']);
$suggestions['content'] = theme('apachesolr_mlt_recommendation_block', $docs, $delta);
if (user_access('administer search')) {
$suggestions['content'] .= l(t('Configure this block'), 'admin/build/block/configure/apachesolr/' . $delta, array(
'attributes' => array(
'class' => 'apachesolr-mlt-admin-link',
),
));
}
}
}
return $suggestions;
}
elseif (apachesolr_has_searched() && $delta == 'sort') {
$response = apachesolr_static_response_cache();
if (empty($response) || $response->response->numFound < 2) {
return;
}
$query = apachesolr_current_query();
$sorts = $query
->get_available_sorts();
$solrsort = $query
->get_solrsort();
$sort_links = array();
$path = $query
->get_path();
$new_query = clone $query;
$toggle = array(
'asc' => 'desc',
'desc' => 'asc',
);
foreach ($sorts as $name => $sort) {
$active = $solrsort['#name'] == $name;
if ($name == 'score') {
$direction = '';
$new_direction = 'desc';
}
elseif ($active) {
$direction = $toggle[$solrsort['#direction']];
$new_direction = $toggle[$solrsort['#direction']];
}
else {
$direction = '';
$new_direction = $sort['default'];
}
$new_query
->set_solrsort($name, $new_direction);
$sort_links[$name] = array(
'title' => $sort['title'],
'path' => $path,
'options' => array(
'query' => $new_query
->get_url_queryvalues(),
),
'active' => $active,
'direction' => $direction,
);
if ($name == 'score') {
$sort_links[$name]['options']['query']['solrsort'] = 'score desc';
}
}
drupal_alter('apachesolr_sort_links', $sort_links);
if (!empty($sort_links)) {
foreach ($sort_links as $name => $link) {
$themed_links[$name] = theme('apachesolr_sort_link', $link['title'], $link['path'], $link['options'], $link['active'], $link['direction']);
}
return array(
'subject' => t('Sort by'),
'content' => theme('apachesolr_sort_list', $themed_links),
);
}
}
break;
case 'configure':
if ($delta != 'sort') {
require_once drupal_get_path('module', 'apachesolr') . '/apachesolr.admin.inc';
return apachesolr_mlt_block_form($delta);
}
break;
case 'save':
if ($delta != 'sort') {
require_once drupal_get_path('module', 'apachesolr') . '/apachesolr.admin.inc';
apachesolr_mlt_save_block($edit, $delta);
}
break;
}
}
function apachesolr_block_visibility($query, $module, $delta) {
$type_filters = variable_get('apachesolr_type_filter', array());
if (isset($type_filters[$module][$delta]) && $type_filters[$module][$delta] == TRUE) {
$facet_info = apachesolr_get_facet_definitions();
if (isset($facet_info[$module][$delta]['content_types'])) {
$has_filter = $query
->get_filters($facet_info[$module][$delta]['facet_field']);
$show = count($has_filter);
foreach ($facet_info[$module][$delta]['content_types'] as $content_type) {
if ($query
->has_filter('type', $content_type)) {
$show = TRUE;
}
}
if (!$show) {
return FALSE;
}
}
}
return TRUE;
}
function apachesolr_get_facet_definitions() {
static $definitions;
if (!isset($definitions)) {
$operator_settings = variable_get('apachesolr_operator', array());
foreach (module_implements('apachesolr_facets') as $module) {
$facets = module_invoke($module, 'apachesolr_facets');
if (!empty($facets)) {
foreach ($facets as $delta => $info) {
$definitions[$module][$delta] = $info;
if (isset($definitions[$module][$delta])) {
$definitions[$module][$delta]['operator'] = isset($operator_settings[$module][$delta]) ? $operator_settings[$module][$delta] : 'AND';
}
}
}
}
}
return $definitions;
}
function apachesolr_get_facet_definition_by_field_name($field_name) {
$definitions = apachesolr_get_facet_definitions();
foreach ($definitions as $module => $facets) {
foreach ($facets as $key => $values) {
if (isset($values['facet_field']) && $values['facet_field'] == $field_name) {
return $definitions[$module][$key];
}
}
}
}
function apachesolr_form_node_type_form_alter(&$form, $form_state) {
$form['#submit'][] = 'apachesolr_clear_cache';
}
function apachesolr_form_content_field_overview_form_alter(&$form, $form_state) {
$form['#submit'][] = 'apachesolr_clear_cache';
}
function apachesolr_form_content_field_edit_form_alter(&$form, $form_state) {
$form['#submit'][] = 'apachesolr_clear_cache';
}
function apachesolr_form_block_admin_configure_alter(&$form, $form_state) {
if ($form['module']['#value'] == 'apachesolr' && $form['delta']['#value'] != 'sort') {
$form['block_settings']['title']['#access'] = FALSE;
}
$module = $form['module']['#value'];
$delta = $form['delta']['#value'];
$enabled_facets = apachesolr_get_enabled_facets();
if (!isset($enabled_facets[$module][$delta])) {
return;
}
$facet_info = apachesolr_get_facet_definitions();
if (isset($facet_info[$module][$delta]['content_types'])) {
$type_filter_settings = variable_get('apachesolr_type_filter', array());
$count = count($facet_info[$module][$delta]['content_types']);
$types = format_plural($count, t('type'), t('types'));
$are = format_plural($count, t('is'), t('are'));
$content_types = implode(', ', $facet_info[$module][$delta]['content_types']) . '.';
$form['block_settings']['apachesolr_type_filter'] = array(
'#type' => 'checkbox',
'#title' => t('Show this block only when the type filter is selected for: %content_types', array(
'%content_types' => $content_types,
)),
'#default_value' => isset($type_filter_settings[$module][$delta]) ? $type_filter_settings[$module][$delta] : FALSE,
'#description' => t('This filter is relevant only for specific content types. Check this to display the block only when the type filter has been selected for one of the relevant content types.'),
'#weight' => 11,
);
}
$operator_settings = variable_get('apachesolr_operator', array());
$form['block_settings']['apachesolr_operator'] = array(
'#type' => 'radios',
'#title' => t('Operator to use for facets'),
'#options' => drupal_map_assoc(array(
'AND',
'OR',
)),
'#default_value' => isset($operator_settings[$module][$delta]) ? $operator_settings[$module][$delta] : 'AND',
'#description' => t('AND filters are exclusive. OR filters are inclusive. Selecting more AND filters narrows the result set. Selecting more OR filters widens the result set.'),
'#weight' => 12,
);
$form['#validate'][] = 'apachesolr_block_admin_configure_submit';
}
function apachesolr_block_admin_configure_submit($form, &$form_state) {
if (isset($form_state['values']['apachesolr_type_filter'])) {
$type_filter_settings = variable_get('apachesolr_type_filter', array());
$module = $form_state['values']['module'];
$delta = $form_state['values']['delta'];
unset($type_filter_settings[$module][$delta]);
$type_filter_settings[$module][$delta] = $form_state['values']['apachesolr_type_filter'];
variable_set('apachesolr_type_filter', $type_filter_settings);
}
if (isset($form_state['values']['apachesolr_operator'])) {
$operator_settings = variable_get('apachesolr_operator', array());
$module = $form_state['values']['module'];
$delta = $form_state['values']['delta'];
unset($operator_settings[$module][$delta]);
$operator_settings[$module][$delta] = $form_state['values']['apachesolr_operator'];
variable_set('apachesolr_operator', $operator_settings);
}
}
function apachesolr_facet_block($response, $query, $module, $delta, $facet_field, $filter_by, $facet_callback = FALSE) {
if (!empty($response->facet_counts->facet_fields->{$facet_field})) {
$facet_query_sorts = variable_get('apachesolr_facet_query_sorts', array());
$contains_active = FALSE;
$items = array();
foreach ($response->facet_counts->facet_fields->{$facet_field} as $facet => $count) {
$options = array(
'delta' => $delta,
);
$exclude = FALSE;
if ($facet == '_empty_') {
$exclude = TRUE;
$facet = '[* TO *]';
$options['html'] = TRUE;
}
if ($facet_callback && function_exists($facet_callback)) {
$facet_text = $facet_callback($facet, $options);
}
elseif ($exclude) {
$facet_text = theme('placeholder', t('Missing this field'));
}
else {
$facet_text = $facet;
}
$active = $query
->has_filter($facet_field, $facet);
if ($active) {
$sortpre = '*';
}
elseif (isset($facet_query_sorts[$module][$delta]) && strpos($facet_query_sorts[$module][$delta], 'index key') === 0) {
$sortpre = $facet;
}
elseif (isset($facet_query_sorts[$module][$delta]) && strpos($facet_query_sorts[$module][$delta], 'index') === 0) {
$sortpre = $facet_text;
}
elseif ($exclude) {
$sortpre = '-';
}
else {
$sortpre = 1000000 - $count;
}
$new_query = clone $query;
if ($active) {
$contains_active = TRUE;
$new_query
->remove_filter($facet_field, $facet);
$options['query'] = $new_query
->get_url_queryvalues();
$link = theme('apachesolr_unclick_link', $facet_text, $new_query
->get_path(), $options);
}
else {
$new_query
->add_filter($facet_field, $facet, $exclude);
$options['query'] = $new_query
->get_url_queryvalues();
$link = theme('apachesolr_facet_link', $facet_text, $new_query
->get_path(), $options, $count, FALSE, $response->response->numFound);
}
if ($count || $active) {
$items[$sortpre . '*' . $facet_text] = $link;
}
}
if ($items && ($response->response->numFound > 1 || $contains_active)) {
switch ($facet_query_sorts[$module][$delta]) {
case 'index numeric asc':
ksort($items, SORT_NUMERIC);
break;
case 'index numeric desc':
krsort($items, SORT_NUMERIC);
break;
case 'index desc':
case 'index key desc':
krsort($items, SORT_STRING);
break;
case 'index asc':
case 'index key asc':
default:
ksort($items, SORT_STRING);
break;
}
$initial_limits = variable_get('apachesolr_facet_query_initial_limits', array());
$limit = isset($initial_limits[$module][$delta]) ? $initial_limits[$module][$delta] : variable_get('apachesolr_facet_query_initial_limit_default', 10);
$output = theme('apachesolr_facet_list', $items, $limit, $delta);
return array(
'subject' => $filter_by,
'content' => $output,
);
}
}
return NULL;
}
function apachesolr_date_facet_block($response, $query, $module, $delta, $facet_field, $filter_by, $facet_callback = FALSE) {
$items = array();
$new_query = clone $query;
foreach (array_reverse($new_query
->get_filters($facet_field)) as $filter) {
$options = array();
$new_query
->remove_filter($facet_field, $filter['#value']);
if ($facet_callback && function_exists($facet_callback)) {
$facet_text = $facet_callback($filter['#start'], $options);
}
else {
$facet_text = apachesolr_date_format_iso_by_gap(apachesolr_date_find_query_gap($filter['#start'], $filter['#end']), $filter['#start']);
}
$options['query'] = $new_query
->get_url_queryvalues();
array_unshift($items, theme('apachesolr_unclick_link', $facet_text, $new_query
->get_path(), $options));
}
if (!empty($response->facet_counts->facet_dates->{$facet_field})) {
$field = clone $response->facet_counts->facet_dates->{$facet_field};
$end = $field->end;
unset($field->end);
$gap = $field->gap;
unset($field->gap);
if (isset($field->start)) {
$start = $field->start;
unset($field->start);
}
$range_end = array();
foreach ($field as $facet => $count) {
if (isset($prev_facet)) {
$range_end[$prev_facet] = $facet;
}
$prev_facet = $facet;
}
$range_end[$prev_facet] = $end;
foreach ($field as $facet => $count) {
$options = array();
if ($facet == '_empty_' || $count == 0) {
continue;
}
if ($facet_callback && function_exists($facet_callback)) {
$facet_text = $facet_callback($facet, $options);
}
else {
$facet_text = apachesolr_date_format_iso_by_gap(substr($gap, 2), $facet);
}
$new_query = clone $query;
$new_query
->add_filter($facet_field, '[' . $facet . ' TO ' . $range_end[$facet] . ']');
$options['query'] = $new_query
->get_url_queryvalues();
$items[] = theme('apachesolr_facet_link', $facet_text, $new_query
->get_path(), $options, $count, FALSE, $response->response->numFound);
}
}
if (count($items) > 0) {
$initial_limits = variable_get('apachesolr_facet_query_initial_limits', array());
$limit = isset($initial_limits[$module][$delta]) ? $initial_limits[$module][$delta] : variable_get('apachesolr_facet_query_initial_limit_default', 10);
$output = theme('apachesolr_facet_list', $items, $limit, $delta);
return array(
'subject' => $filter_by,
'content' => $output,
);
}
return NULL;
}
function apachesolr_date_find_query_gap($start_iso, $end_iso) {
$gaps = array(
'SECOND' => 6,
'MINUTE' => 5,
'HOUR' => 4,
'DAY' => 3,
'MONTH' => 2,
'YEAR' => 1,
);
$re = '@(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})@';
if (preg_match($re, $start_iso, $start) && preg_match($re, $end_iso, $end)) {
foreach ($gaps as $gap => $idx) {
if ($start[$idx] != $end[$idx]) {
return $gap;
}
}
}
return 'YEAR';
}
function apachesolr_date_format_iso_by_gap($gap, $iso) {
$unix = strtotime($iso);
if ($unix !== FALSE) {
switch ($gap) {
case 'YEAR':
return format_date($unix, 'custom', 'Y', 0);
case 'MONTH':
return format_date($unix, 'custom', 'F Y', 0);
case 'DAY':
return format_date($unix, 'custom', 'F j, Y', 0);
case 'HOUR':
return format_date($unix, 'custom', 'g A', 0);
case 'MINUTE':
return format_date($unix, 'custom', 'g:i A', 0);
case 'SECOND':
return format_date($unix, 'custom', 'g:i:s A', 0);
}
}
return $iso;
}
function apachesolr_date_format_range($start_iso, $end_iso) {
$gap = apachesolr_date_find_query_gap($start_iso, $end_iso);
return apachesolr_date_format_iso_by_gap($gap, $start_iso);
}
function apachesolr_date_determine_gap($start, $end) {
$start = strtotime($start);
$end = strtotime($end);
if ($end - $start >= 86400 * 365) {
return 'YEAR';
}
if (date('Ym', $start) != date('Ym', $end)) {
return 'MONTH';
}
if ($end - $start > 86400) {
return 'DAY';
}
return 'HOUR';
}
function apachesolr_date_gap_drilldown($gap) {
$drill = array(
'YEAR' => 'MONTH',
'MONTH' => 'DAY',
'DAY' => 'HOUR',
'HOUR' => 'MINUTE',
);
return isset($drill[$gap]) ? $drill[$gap] : NULL;
}
function apachesolr_facetcount_form($module, $delta) {
$initial = variable_get('apachesolr_facet_query_initial_limits', array());
$limits = variable_get('apachesolr_facet_query_limits', array());
$sorts = variable_get('apachesolr_facet_query_sorts', array());
$children = variable_get('apachesolr_facet_show_children', array());
$facet_missing = variable_get('apachesolr_facet_missing', array());
$limit = drupal_map_assoc(array(
50,
40,
30,
20,
15,
10,
5,
3,
));
$form['apachesolr_facet_query_initial_limit'] = array(
'#type' => 'select',
'#title' => t('Initial filter links'),
'#options' => $limit,
'#description' => t('The initial number of filter links to show in this block.'),
'#default_value' => isset($initial[$module][$delta]) ? $initial[$module][$delta] : variable_get('apachesolr_facet_query_initial_limit_default', 10),
);
$limit = drupal_map_assoc(array(
100,
75,
50,
40,
30,
20,
15,
10,
5,
3,
));
$form['apachesolr_facet_query_limit'] = array(
'#type' => 'select',
'#title' => t('Maximum filter links'),
'#options' => $limit,
'#description' => t('The maximum number of filter links to show in this block.'),
'#default_value' => isset($limits[$module][$delta]) ? $limits[$module][$delta] : variable_get('apachesolr_facet_query_limit_default', 20),
);
if ($delta != 'created' && $delta != 'changed') {
$form['apachesolr_facet_query_sort'] = array(
'#type' => 'radios',
'#title' => t('Sort order of facet links'),
'#options' => array(
'count' => t('Count'),
'index asc' => t('Alphabetical, ascending'),
'index desc' => t('Alphabetical, descending'),
'index numeric asc' => t('Numeric, ascending'),
'index numeric desc' => t('Numeric, descending'),
'index key asc' => t('Key sort, ascending'),
'index key desc' => t('Key sort, descending'),
),
'#description' => t('The sort order of facet links in this block. %Count, which is the default, will show facets with the most results first. %Alphabetical will sort alphabetically, and %Numeric numerically, either ascending or descending.', array(
'%Count' => t('Count'),
'%Alphabetical' => t('Alphanumeric'),
'%Numeric' => t('Numeric'),
)),
'#default_value' => isset($sorts[$module][$delta]) ? $sorts[$module][$delta] : 'count',
);
}
$form['apachesolr_facet_show_children'] = array(
'#type' => 'radios',
'#title' => t('Always show child facets'),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#description' => t('Show the child facets even if the parent facet is not selected.'),
'#default_value' => isset($children[$module][$delta]) ? $children[$module][$delta] : 0,
);
$form['apachesolr_facet_missing'] = array(
'#type' => 'radios',
'#title' => t('Include a facet for missing'),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#description' => t('A facet can be generated corresponding to all documents entirely missing this field.'),
'#default_value' => isset($facet_missing[$module][$delta]) ? $facet_missing[$module][$delta] : 0,
);
return $form;
}
function apachesolr_facetcount_save($edit) {
$module = $edit['module'];
$delta = $edit['delta'];
$limits = variable_get('apachesolr_facet_query_limits', array());
$limits[$module][$delta] = (int) $edit['apachesolr_facet_query_limit'];
variable_set('apachesolr_facet_query_limits', $limits);
$sorts = variable_get('apachesolr_facet_query_sorts', array());
$sorts[$module][$delta] = $edit['apachesolr_facet_query_sort'];
variable_set('apachesolr_facet_query_sorts', $sorts);
$initial = variable_get('apachesolr_facet_query_initial_limits', array());
$initial[$module][$delta] = (int) $edit['apachesolr_facet_query_initial_limit'];
variable_set('apachesolr_facet_query_initial_limits', $initial);
$children = variable_get('apachesolr_facet_show_children', array());
$children[$module][$delta] = (int) $edit['apachesolr_facet_show_children'];
variable_set('apachesolr_facet_show_children', $children);
$facet_missing = variable_get('apachesolr_facet_missing', array());
$facet_missing[$module][$delta] = (int) $edit['apachesolr_facet_missing'];
variable_set('apachesolr_facet_missing', $facet_missing);
}
function apachesolr_pager_init($total, $limit = 10, $element = 0) {
global $pager_page_array, $pager_total, $pager_total_items;
$page = isset($_GET['page']) ? $_GET['page'] : '';
$pager_page_array = explode(',', $page);
$pager_total_items[$element] = $total;
$pager_total[$element] = ceil($pager_total_items[$element] / $limit);
$pager_page_array[$element] = max(0, min((int) $pager_page_array[$element], (int) $pager_total[$element] - 1));
return $pager_page_array[$element];
}
function apachesolr_modify_query(&$query, &$params, $caller) {
if (empty($query)) {
return;
}
foreach (module_implements('apachesolr_modify_query') as $module) {
$function_name = $module . '_apachesolr_modify_query';
$function_name($query, $params, $caller);
}
if ($query && ($fq = $query
->get_fq())) {
foreach ($fq as $delta => $values) {
if (is_array($values) || is_object($values)) {
foreach ($values as $value) {
$params['fq'][$delta][] = $value;
}
}
}
}
if ($query) {
$sort = $query
->get_solrsort();
$sortstring = $sort['#name'] . ' ' . $sort['#direction'];
if ($sortstring != 'score desc') {
$params['sort'] = $sortstring;
}
}
}
function apachesolr_has_searched($searched = NULL) {
static $_searched = FALSE;
if (is_bool($searched)) {
$_searched = $searched;
}
return $_searched;
}
function apachesolr_get_solr($host = NULL, $port = NULL, $path = NULL) {
static $solr_cache;
if (empty($host)) {
$host = variable_get('apachesolr_host', 'localhost');
}
if (empty($port)) {
$port = variable_get('apachesolr_port', '8983');
}
if (empty($path)) {
$path = variable_get('apachesolr_path', '/solr');
}
if (empty($solr_cache[$host][$port][$path])) {
list($module, $filepath, $class) = variable_get('apachesolr_service_class', array(
'apachesolr',
'Drupal_Apache_Solr_Service.php',
'Drupal_Apache_Solr_Service',
));
include_once drupal_get_path('module', $module) . '/' . $filepath;
$solr = new $class($host, $port, $path);
$solr
->setCollapseSingleValueArrays(FALSE);
$solr_cache[$host][$port][$path] = $solr;
}
return $solr_cache[$host][$port][$path];
}
function apachesolr_server_status($host = NULL, $port = NULL, $path = NULL) {
if (empty($host)) {
$host = variable_get('apachesolr_host', 'localhost');
}
if (empty($port)) {
$port = variable_get('apachesolr_port', '8983');
}
if (empty($path)) {
$path = variable_get('apachesolr_path', '/solr');
}
$ping = FALSE;
try {
$solr = apachesolr_get_solr($host, $port, $path);
$ping = @$solr
->ping(variable_get('apachesolr_ping_timeout', 4));
} catch (Exception $e) {
watchdog('Apache Solr', check_plain($e
->getMessage()), NULL, WATCHDOG_ERROR);
}
return $ping;
}
function apachesolr_do_query($caller, $current_query, &$params = array(
'rows' => 10,
), $page = 0) {
foreach (module_implements('apachesolr_prepare_query') as $module) {
$function_name = $module . '_apachesolr_prepare_query';
$function_name($current_query, $params, $caller);
}
$query = apachesolr_current_query($current_query, $caller);
apachesolr_modify_query($query, $params, $caller);
$params['start'] = $page * $params['rows'];
if (!$query) {
return array(
NULL,
array(),
);
}
$function = $caller . '_finalize_query';
if (function_exists($function)) {
$function($query, $params);
}
$keys = $query
->get_query_basic();
if ($keys == '' && isset($params['fq'])) {
$qalt = array();
foreach ($params['fq'] as $delta => $value) {
if (!preg_match('/^(?:\\{!|-)/', $value)) {
$qalt[] = '(' . $value . ')';
unset($params['fq'][$delta]);
}
}
if ($qalt) {
$params['q.alt'] = implode(' ', $qalt);
}
}
$solr = apachesolr_get_solr();
$keys = htmlspecialchars($keys, ENT_NOQUOTES, 'UTF-8');
$keys = str_replace("'", ''', $keys);
$response = $solr
->search($keys, $params['start'], $params['rows'], $params);
apachesolr_static_response_cache($response, $caller);
return array(
$query,
$response,
);
}
function apachesolr_static_response_cache($response = NULL, $namespace = 'apachesolr_search') {
static $_response = array();
if (is_object($response)) {
$_response[$namespace] = clone $response;
}
if (!isset($_response[$namespace])) {
$_response[$namespace] = NULL;
}
return $_response[$namespace];
}
function apachesolr_drupal_query($keys = '', $filters = '', $solrsort = '', $base_path = '', $solr = NULL) {
list($module, $class) = variable_get('apachesolr_query_class', array(
'apachesolr',
'Solr_Base_Query',
));
module_load_include('php', $module, $class);
if (empty($solr)) {
$solr = apachesolr_get_solr();
}
try {
$query = new $class($solr, $keys, $filters, $solrsort, $base_path);
} catch (Exception $e) {
watchdog('Apache Solr', nl2br(check_plain($e
->getMessage())), NULL, WATCHDOG_ERROR);
$query = NULL;
}
return $query;
}
function apachesolr_current_query($query = NULL, $namespace = 'apachesolr_search') {
static $saved_query = array();
if (is_object($query)) {
$saved_query[$namespace] = clone $query;
}
return is_object($saved_query[$namespace]) ? clone $saved_query[$namespace] : NULL;
}
function apachesolr_index_key($field) {
switch ($field['index_type']) {
case 'text':
$type_prefix = 't';
break;
case 'string':
$type_prefix = 's';
break;
case 'integer':
$type_prefix = 'i';
break;
case 'sint':
$type_prefix = 'si';
break;
case 'double':
$type_prefix = 'p';
break;
case 'boolean':
$type_prefix = 'b';
break;
case 'date':
$type_prefix = 'd';
break;
case 'float':
$type_prefix = 'f';
break;
case 'tdate':
$type_prefix = 'td';
break;
case 'tint':
$type_prefix = 'ti';
break;
case 'tlong':
$type_prefix = 'tl';
break;
case 'tfloat':
$type_prefix = 'tf';
break;
case 'tdouble':
$type_prefix = 'tp';
break;
default:
$type_prefix = 's';
}
$sm = $field['multiple'] ? 'm_' : 's_';
return $type_prefix . $sm . $field['name'];
}
function apachesolr_field_name_map($field_name) {
static $map;
if (!isset($map)) {
$map = array(
'body' => t('Body text - the full, rendered content'),
'title' => t('Title'),
'teaser' => t('Teaser'),
'name' => t('Author name'),
'path_alias' => t('Path alias'),
'taxonomy_names' => t('All taxonomy term names'),
'tags_h1' => t('Body text inside H1 tags'),
'tags_h2_h3' => t('Body text inside H2 or H3 tags'),
'tags_h4_h5_h6' => t('Body text inside H4, H5, or H6 tags'),
'tags_inline' => t('Body text in inline tags like EM or STRONG'),
'tags_a' => t('Body text inside links (A tags)'),
'tid' => t('Taxonomy term IDs'),
);
if (module_exists('taxonomy')) {
foreach (taxonomy_get_vocabularies() as $vocab) {
$map['ts_vid_' . $vocab->vid . '_names'] = t('Taxonomy term names only from the %name vocabulary', array(
'%name' => $vocab->name,
));
$map['im_vid_' . $vocab->vid] = t('Taxonomy term IDs from the %name vocabulary', array(
'%name' => $vocab->name,
));
}
}
foreach (apachesolr_cck_fields() as $name => $field) {
$map[apachesolr_index_key($field)] = t('CCK @type field %label', array(
'@type' => $field['index_type'],
'%label' => $field['label'],
));
}
drupal_alter('apachesolr_field_name_map', $map);
}
return isset($map[$field_name]) ? $map[$field_name] : $field_name;
}
function apachesolr_content_fieldapi($op, $field) {
switch ($op) {
case 'delete instance':
cache_clear_all('*', 'cache_apachesolr', TRUE);
apachesolr_mark_node_type($field['type_name']);
break;
case 'update instance':
cache_clear_all('*', 'cache_apachesolr', TRUE);
$previous = content_field_instance_read(array(
'field_name' => $field['field_name'],
'type_name' => $field['type_name'],
));
$prev_field = array_pop($previous);
if ($field['display_settings'][NODE_BUILD_SEARCH_INDEX]['exclude'] != $prev_field['display_settings'][NODE_BUILD_SEARCH_INDEX]['exclude']) {
apachesolr_mark_node_type($field['type_name']);
}
elseif ($field['multiple'] != $prev_field['multiple']) {
apachesolr_mark_node_type($field['type_name']);
}
break;
}
}
function apachesolr_mark_node_type($type_name) {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query("UPDATE {apachesolr_search_node} asn INNER JOIN {node} n ON asn.nid = n.nid SET asn.changed = %d WHERE n.type = '%s'", time(), $type_name);
break;
default:
db_query("UPDATE {apachesolr_search_node} SET changed = %d WHERE nid IN (SELECT nid FROM {node} WHERE type = '%s')", time(), $type_name);
break;
}
}
function apachesolr_cck_fields() {
static $fields;
if (!isset($fields)) {
$fields = array();
if (module_exists('content')) {
module_load_include('inc', 'content', 'includes/content.crud');
$cck_field_instances = content_field_instance_read();
$mappings['text'] = array(
'optionwidgets_select' => array(
'display_callback' => 'apachesolr_cck_text_field_callback',
'indexing_callback' => 'apachesolr_cck_text_indexing_callback',
'index_type' => 'string',
'facets' => TRUE,
),
'optionwidgets_buttons' => array(
'display_callback' => 'apachesolr_cck_text_field_callback',
'indexing_callback' => 'apachesolr_cck_text_indexing_callback',
'index_type' => 'string',
'facets' => TRUE,
),
'optionwidgets_onoff' => array(
'display_callback' => 'apachesolr_cck_text_field_callback',
'indexing_callback' => 'apachesolr_cck_text_indexing_callback',
'index_type' => 'string',
'facets' => TRUE,
),
);
$mappings['number_integer'] = array(
'optionwidgets_select' => array(
'display_callback' => 'apachesolr_cck_text_field_callback',
'indexing_callback' => 'apachesolr_cck_integer_indexing_callback',
'index_type' => 'sint',
'facets' => TRUE,
),
'optionwidgets_buttons' => array(
'display_callback' => 'apachesolr_cck_text_field_callback',
'indexing_callback' => 'apachesolr_cck_integer_indexing_callback',
'index_type' => 'sint',
'facets' => TRUE,
),
'optionwidgets_onoff' => array(
'display_callback' => 'apachesolr_cck_text_field_callback',
'indexing_callback' => 'apachesolr_cck_integer_indexing_callback',
'index_type' => 'sint',
'facets' => TRUE,
),
);
$mappings['nodereference'] = array(
'nodereference_buttons' => array(
'display_callback' => 'apachesolr_cck_nodereference_field_callback',
'indexing_callback' => 'apachesolr_cck_nodereference_indexing_callback',
'index_type' => 'integer',
'facets' => TRUE,
),
'nodereference_select' => array(
'display_callback' => 'apachesolr_cck_nodereference_field_callback',
'indexing_callback' => 'apachesolr_cck_nodereference_indexing_callback',
'index_type' => 'integer',
'facets' => TRUE,
),
'nodereference_autocomplete' => array(
'display_callback' => 'apachesolr_cck_nodereference_field_callback',
'indexing_callback' => 'apachesolr_cck_nodereference_indexing_callback',
'index_type' => 'integer',
'facets' => TRUE,
),
);
$mappings['userreference'] = array(
'userreference_buttons' => array(
'display_callback' => 'apachesolr_cck_userreference_field_callback',
'indexing_callback' => 'apachesolr_cck_userreference_indexing_callback',
'index_type' => 'integer',
'facets' => TRUE,
),
'userreference_select' => array(
'display_callback' => 'apachesolr_cck_userreference_field_callback',
'indexing_callback' => 'apachesolr_cck_userreference_indexing_callback',
'index_type' => 'integer',
'facets' => TRUE,
),
'userreference_autocomplete' => array(
'display_callback' => 'apachesolr_cck_userreference_field_callback',
'indexing_callback' => 'apachesolr_cck_userreference_indexing_callback',
'index_type' => 'integer',
'facets' => TRUE,
),
);
drupal_alter('apachesolr_cck_fields', $mappings);
foreach ($cck_field_instances as $instance) {
$field_type = $instance['type'];
$field_name = $instance['field_name'];
$widget_type = $instance['widget']['type'];
if (!isset($mappings[$field_type][$widget_type]) && !isset($mappings['per-field'][$field_name]) && isset($mappings[$field_type]['default'])) {
$widget_type = 'default';
}
if ((isset($mappings[$field_type][$widget_type]) || isset($mappings['per-field'][$field_name])) && empty($instance['display_settings'][NODE_BUILD_SEARCH_INDEX]['exclude'])) {
if (isset($mappings['per-field'][$field_name])) {
$instance['index_type'] = $mappings['per-field'][$field_name]['index_type'];
$instance['indexing_callback'] = $mappings['per-field'][$field_name]['indexing_callback'];
$instance['display_callback'] = $mappings['per-field'][$field_name]['display_callback'];
if (isset($mappings['per-field'][$field_name]['facet_block_callback'])) {
$instance['facet_block_callback'] = $mappings['per-field'][$field_name]['facet_block_callback'];
}
$instance['facets'] = $mappings['per-field'][$field_name]['facets'];
}
else {
$instance['index_type'] = $mappings[$field_type][$widget_type]['index_type'];
$instance['indexing_callback'] = $mappings[$field_type][$widget_type]['indexing_callback'];
$instance['display_callback'] = $mappings[$field_type][$widget_type]['display_callback'];
if (isset($mappings[$field_type][$widget_type]['facet_block_callback'])) {
$instance['facet_block_callback'] = $mappings[$field_type][$widget_type]['facet_block_callback'];
}
$instance['facets'] = $mappings[$field_type][$widget_type]['facets'];
}
$instance['multiple'] = (bool) $instance['multiple'];
$instance['name'] = 'cck_' . $field_name;
if (isset($fields[$field_name]) && is_array($fields[$field_name])) {
$fields[$field_name] = array_merge($fields[$field_name], $instance);
}
else {
$fields[$field_name] = $instance;
}
$fields[$field_name]['content_types'][] = $instance['type_name'];
unset($fields[$field_name]['type_name']);
}
}
}
}
return $fields;
}
function apachesolr_cck_text_field_callback($facet, $options) {
if (function_exists('content_format')) {
return content_format($options['delta'], array(
'value' => $facet,
));
}
else {
return $facet;
}
}
function apachesolr_cck_nodereference_field_callback($facet, $options) {
if (function_exists('content_format')) {
return strip_tags(content_format($options['delta'], array(
'nid' => $facet,
)));
}
else {
return $facet;
}
}
function apachesolr_cck_userreference_field_callback($facet, $options) {
if (function_exists('content_format')) {
return strip_tags(content_format($options['delta'], array(
'uid' => $facet,
)));
}
else {
return $facet;
}
}
function apachesolr_theme() {
return array(
'apachesolr_facet_link' => array(
'arguments' => array(
'facet_text' => NULL,
'path' => NULL,
'options' => NULL,
'count' => NULL,
'active' => FALSE,
'num_found' => NULL,
),
),
'apachesolr_unclick_link' => array(
'arguments' => array(
'facet_text' => NULL,
'path' => NULL,
'options' => NULL,
),
),
'apachesolr_facet_list' => array(
'arguments' => array(
'items' => NULL,
'display_limit' => 0,
'delta' => '',
),
),
'apachesolr_sort_list' => array(
'arguments' => array(
'items' => NULL,
),
),
'apachesolr_sort_link' => array(
'arguments' => array(
'text' => NULL,
'path' => NULL,
'options' => NULL,
'active' => FALSE,
'direction' => '',
),
),
'apachesolr_mlt_recommendation_block' => array(
'arguments' => array(
'docs' => NULL,
'delta' => NULL,
),
),
);
}
function apachesolr_mlt_suggestions($settings, $id) {
try {
$solr = apachesolr_get_solr();
$fields = array(
'mlt_mintf' => 'mlt.mintf',
'mlt_mindf' => 'mlt.mindf',
'mlt_minwl' => 'mlt.minwl',
'mlt_maxwl' => 'mlt.maxwl',
'mlt_maxqt' => 'mlt.maxqt',
'mlt_boost' => 'mlt.boost',
'mlt_qf' => 'mlt.qf',
);
$params = array(
'qt' => 'mlt',
'fl' => 'nid,title,path,url',
'mlt.fl' => implode(',', $settings['mlt_fl']),
);
foreach ($fields as $form_key => $name) {
if (!empty($settings[$form_key])) {
$params[$name] = $settings[$form_key];
}
}
$query = apachesolr_drupal_query('id:' . $id);
$type_filters = array();
if (is_array($settings['mlt_type_filters'])) {
foreach ($settings['mlt_type_filters'] as $type_filter) {
$type_filters[] = "type:{$type_filter}";
}
$params['fq']['mlt'][] = '(' . implode(' OR ', $type_filters) . ') ';
}
if ($custom_filters = $settings['mlt_custom_filters']) {
$params['fq']['mlt'][] = $custom_filters;
}
apachesolr_modify_query($query, $params, 'apachesolr_mlt');
if (empty($query)) {
return;
}
$response = $solr
->search($query
->get_query_basic(), 0, $settings['num_results'], $params);
if ($response->response) {
$docs = (array) end($response->response);
return $docs;
}
} catch (Exception $e) {
watchdog('Apache Solr', nl2br(check_plain($e
->getMessage())), NULL, WATCHDOG_ERROR);
}
}
function apachesolr_form_block_admin_display_form_alter(&$form) {
foreach ($form as $key => $block) {
if (strpos($key, "apachesolr_mlt-") === 0 && $block['module']['#value'] == 'apachesolr') {
$form[$key]['delete'] = array(
'#value' => l(t('delete'), 'admin/settings/apachesolr/mlt/delete_block/' . $block['delta']['#value']),
);
}
}
}
function apachesolr_mlt_list_blocks() {
$blocks = variable_get('apachesolr_mlt_blocks', array());
foreach ($blocks as $delta => $settings) {
$blocks[$delta] += array(
'info' => t('Apache Solr recommendations: !name', array(
'!name' => $settings['name'],
)),
'cache' => BLOCK_CACHE_PER_PAGE,
);
}
return $blocks;
}
function apachesolr_mlt_load_block($delta) {
$blocks = variable_get('apachesolr_mlt_blocks', array());
return isset($blocks[$delta]) ? $blocks[$delta] : FALSE;
}
function theme_apachesolr_mlt_recommendation_block($docs, $delta) {
$links = array();
foreach ($docs as $result) {
$links[] = l($result->title, $result->path, array(
'html' => TRUE,
));
}
return theme('item_list', $links, NULL, 'ul', array(
'class' => 'apachesolr-mlt-block',
));
}
function theme_apachesolr_facet_link($facet_text, $path, $options = array(), $count, $active = FALSE, $num_found = NULL) {
$options['attributes']['class'][] = 'apachesolr-facet';
if ($active) {
$options['attributes']['class'][] = 'active';
}
$options['attributes']['class'] = implode(' ', $options['attributes']['class']);
return apachesolr_l($facet_text . " ({$count})", $path, $options);
}
function apachesolr_l($text, $path, $options = array()) {
$options += array(
'attributes' => array(),
'html' => FALSE,
'query' => array(),
);
unset($options['attributes']['title']);
if (variable_get('clean_url', '0')) {
$path = str_replace('+', '%2B', $path);
}
$query = apachesolr_current_query();
$get = array_diff_key($_GET, array(
'q' => 1,
'page' => 1,
), $options['query'], $query
->get_url_queryvalues());
$options['query'] += $get;
return '<a href="' . check_url(url($path, $options)) . '"' . drupal_attributes($options['attributes']) . '>' . ($options['html'] ? $text : check_plain(html_entity_decode($text))) . '</a>';
}
function apachesolr_js() {
static $settings;
if (is_null($settings)) {
$settings['apachesolr_facetstyle'] = variable_get('apachesolr_facetstyle', 'checkboxes');
$show_more_blocks = array();
$facet_map = array();
foreach (apachesolr_get_facet_definitions() as $module => $definitions) {
foreach ($definitions as $facet => $facet_definition) {
$facet_map[$facet_definition['facet_field']] = $facet;
}
}
$show_more_selector = array();
foreach (apachesolr_get_enabled_facets() as $module => $blocks) {
foreach ($blocks as $block) {
$show_more_selector[] = "#block-{$module}-{$facet_map[$block]}:has(.apachesolr-hidden-facet) .apachesolr-facet-list";
}
}
$settings['apachesolr_show_more_blocks'] = implode(', ', $show_more_selector);
drupal_add_js($settings, 'setting');
drupal_add_js(drupal_get_path('module', 'apachesolr') . '/apachesolr.js');
}
}
function theme_apachesolr_unclick_link($facet_text, $path, $options = array()) {
apachesolr_js();
if (empty($options['html'])) {
$facet_text = check_plain(html_entity_decode($facet_text));
}
else {
unset($options['html']);
}
$options['attributes']['class'] = 'apachesolr-unclick';
return apachesolr_l("(-)", $path, $options) . ' ' . $facet_text;
}
function theme_apachesolr_sort_link($text, $path, $options = array(), $active = FALSE, $direction = '') {
$icon = '';
if ($direction) {
$icon = ' ' . theme('tablesort_indicator', $direction);
}
if ($active) {
if (isset($options['attributes']['class'])) {
$options['attributes']['class'] .= ' active';
}
else {
$options['attributes']['class'] = 'active';
}
}
return $icon . apachesolr_l($text, $path, $options);
}
function theme_apachesolr_facet_list($items, $display_limit = 0, $delta = '') {
apachesolr_js();
$items = array_values($items);
if ($display_limit > 0 && count($items) > $display_limit) {
$hidden_items = array_splice($items, $display_limit);
foreach ($hidden_items as $hidden_item) {
if (!is_array($hidden_item)) {
$hidden_item = array(
'data' => $hidden_item,
);
}
$hidden_item['class'] = isset($hidden_item['class']) ? $hidden_item['class'] . ' apachesolr-hidden-facet' : 'apachesolr-hidden-facet';
$items[] = $hidden_item;
}
}
$admin_link = '';
if (user_access('administer search')) {
$admin_link = l(t('Configure enabled filters'), 'admin/settings/apachesolr/enabled-filters');
}
return theme('item_list', $items, NULL, 'ul', array(
'class' => 'apachesolr-facet-list',
)) . $admin_link;
}
function theme_apachesolr_sort_list($items) {
$items = array_values($items);
return theme('item_list', $items, NULL, 'ul', array(
'class' => 'apachesolr-sort-list',
));
}
interface Drupal_Solr_Query_Interface {
function get_filters($name = NULL);
function has_filter($field, $value);
function add_filter($field, $value, $exclude = FALSE);
function remove_filter($field, $value = NULL);
function get_keys();
function set_keys($keys);
function remove_keys();
function get_path();
function get_url_queryvalues();
function get_query_basic();
function get_available_sorts();
function set_available_sort($field, $sort);
function get_solrsort();
function set_solrsort($field, $direction);
function add_subquery(Drupal_Solr_Query_Interface $query, $fq_operator = 'OR', $q_operator = 'AND');
function remove_subquery(Drupal_Solr_Query_Interface $query);
function remove_subqueries();
}
function apachesolr_tt($name, $string, $langcode = NULL, $update = FALSE) {
if (module_exists('i18nstrings')) {
return tt($name, $string, $langcode, $update);
}
else {
return $string;
}
}