View source
<?php
class SearchApiSolrConnection implements SearchApiSolrConnectionInterface {
const NAMED_LIST_FORMAT = 'map';
const PING_SERVLET = 'admin/ping';
const UPDATE_SERVLET = 'update';
const SEARCH_SERVLET = 'select';
const LUKE_SERVLET = 'admin/luke';
const SYSTEM_SERVLET = 'admin/system';
const STATS_SERVLET = 'admin/stats.jsp';
const STATS_SERVLET_4 = 'admin/mbeans?wt=xml&stats=true';
const FILE_SERVLET = 'admin/file';
protected $options;
protected $base_url;
protected $update_url;
protected $http_auth;
protected $stream_context;
protected $luke = array();
protected $stats;
protected $system_info;
protected $soft_commit = TRUE;
public function __construct(array $options) {
$options += array(
'scheme' => 'http',
'host' => 'localhost',
'port' => 8983,
'path' => 'solr',
'http_user' => NULL,
'http_pass' => NULL,
);
$this->options = $options;
$path = '/' . trim($options['path'], '/') . '/';
$this->base_url = $options['scheme'] . '://' . $options['host'] . ':' . $options['port'] . $path;
if (strlen($options['http_user']) && strlen($options['http_pass'])) {
$this->http_auth = 'Basic ' . base64_encode($options['http_user'] . ':' . $options['http_pass']);
}
}
public function ping($timeout = 2) {
$start = microtime(TRUE);
if ($timeout <= 0.0) {
$timeout = -1;
}
$pingUrl = $this
->constructUrl(self::PING_SERVLET);
$options = array(
'method' => 'HEAD',
'timeout' => $timeout,
);
$response = $this
->makeHttpRequest($pingUrl, $options);
if ($response->code == 200) {
return microtime(TRUE) - $start + 1.0E-6;
}
else {
return FALSE;
}
}
public function setSoftCommit($soft_commit) {
$this->soft_commit = (bool) $soft_commit;
}
public function getSoftCommit() {
return $this->soft_commit;
}
public function setStreamContext($stream_context) {
$this->stream_context = $stream_context;
}
public function getStreamContext() {
return $this->stream_context;
}
protected function getCacheId($suffix = '') {
if (!empty($this->options['server'])) {
$cid = $this->options['server'];
return $suffix ? "{$cid}:{$suffix}" : $cid;
}
}
protected function setSystemInfo() {
$cid = $this
->getCacheId(__FUNCTION__);
if ($cid) {
$cache = cache_get($cid, 'cache_search_api_solr');
if ($cache) {
$this->system_info = json_decode($cache->data);
}
}
if (empty($this->system_info)) {
$url = $this
->constructUrl(self::SYSTEM_SERVLET, array(
'wt' => 'json',
));
$response = $this
->sendRawGet($url);
$this->system_info = json_decode($response->data);
if ($cid) {
cache_set($cid, $response->data, 'cache_search_api_solr');
}
}
}
public function getSystemInfo() {
if (!isset($this->system_info)) {
$this
->setSystemInfo();
}
return $this->system_info;
}
protected function setLuke($num_terms = 0) {
if (empty($this->luke[$num_terms])) {
$cid = $this
->getCacheId(__FUNCTION__ . ":{$num_terms}");
if ($cid) {
$cache = cache_get($cid, 'cache_search_api_solr');
if (isset($cache->data)) {
$this->luke = $cache->data;
}
}
if (empty($this->luke[$num_terms])) {
$params = array(
'numTerms' => "{$num_terms}",
'wt' => 'json',
'json.nl' => self::NAMED_LIST_FORMAT,
);
$url = $this
->constructUrl(self::LUKE_SERVLET, $params);
$this->luke[$num_terms] = $this
->sendRawGet($url);
if ($cid) {
cache_set($cid, $this->luke, 'cache_search_api_solr');
}
}
}
}
public function getFields($num_terms = 0) {
$fields = array();
$luke_data = $this
->getLuke($num_terms);
if (isset($luke_data->fields)) {
foreach ($luke_data->fields as $name => $info) {
$fields[$name] = new SearchApiSolrField($info);
}
}
return $fields;
}
public function getLuke($num_terms = 0) {
if (!isset($this->luke[$num_terms])) {
$this
->setLuke($num_terms);
}
return $this->luke[$num_terms];
}
public function getSolrVersion() {
if (!empty($this->options['solr_version'])) {
return $this->options['solr_version'];
}
$system_info = $this
->getSystemInfo();
if (isset($system_info->lucene->{'solr-spec-version'})) {
return (int) $system_info->lucene->{'solr-spec-version'};
}
return 0;
}
protected function setStats() {
$data = $this
->getLuke();
$solr_version = $this
->getSolrVersion();
if (empty($this->stats) && isset($data->index->numDocs)) {
$cid = $this
->getCacheId(__FUNCTION__);
if ($cid) {
$cache = cache_get($cid, 'cache_search_api_solr');
if (isset($cache->data)) {
$this->stats = simplexml_load_string($cache->data);
}
}
if (empty($this->stats)) {
if ($solr_version >= 4) {
$url = $this
->constructUrl(self::STATS_SERVLET_4);
}
else {
$url = $this
->constructUrl(self::STATS_SERVLET);
}
$response = $this
->sendRawGet($url);
$this->stats = simplexml_load_string($response->data);
if ($cid) {
cache_set($cid, $response->data, 'cache_search_api_solr');
}
}
}
}
public function getStats() {
if (!isset($this->stats)) {
$this
->setStats();
}
return $this->stats;
}
public function getStatsSummary() {
$stats = $this
->getStats();
$solr_version = $this
->getSolrVersion();
$summary = array(
'@pending_docs' => '',
'@autocommit_time_seconds' => '',
'@autocommit_time' => '',
'@deletes_by_id' => '',
'@deletes_by_query' => '',
'@deletes_total' => '',
'@schema_version' => '',
'@core_name' => '',
'@index_size' => '',
);
if (!empty($stats)) {
if ($solr_version <= 3) {
$docs_pending_xpath = $stats
->xpath('//stat[@name="docsPending"]');
$summary['@pending_docs'] = (int) trim(current($docs_pending_xpath));
$max_time_xpath = $stats
->xpath('//stat[@name="autocommit maxTime"]');
$max_time = (int) trim(current($max_time_xpath));
$summary['@autocommit_time_seconds'] = $max_time / 1000;
$summary['@autocommit_time'] = format_interval($max_time / 1000);
$deletes_id_xpath = $stats
->xpath('//stat[@name="deletesById"]');
$summary['@deletes_by_id'] = (int) trim(current($deletes_id_xpath));
$deletes_query_xpath = $stats
->xpath('//stat[@name="deletesByQuery"]');
$summary['@deletes_by_query'] = (int) trim(current($deletes_query_xpath));
$summary['@deletes_total'] = $summary['@deletes_by_id'] + $summary['@deletes_by_query'];
$schema = $stats
->xpath('/solr/schema[1]');
$summary['@schema_version'] = trim($schema[0]);
$core = $stats
->xpath('/solr/core[1]');
$summary['@core_name'] = trim($core[0]);
$size_xpath = $stats
->xpath('//stat[@name="indexSize"]');
$summary['@index_size'] = trim(current($size_xpath));
}
else {
$system_info = $this
->getSystemInfo();
$docs_pending_xpath = $stats
->xpath('//lst["stats"]/long[@name="docsPending"]');
$summary['@pending_docs'] = (int) trim(current($docs_pending_xpath));
$max_time_xpath = $stats
->xpath('//lst["stats"]/str[@name="autocommit maxTime"]');
$max_time = (int) trim(current($max_time_xpath));
$summary['@autocommit_time_seconds'] = $max_time / 1000;
$summary['@autocommit_time'] = format_interval($max_time / 1000);
$deletes_id_xpath = $stats
->xpath('//lst["stats"]/long[@name="deletesById"]');
$summary['@deletes_by_id'] = (int) trim(current($deletes_id_xpath));
$deletes_query_xpath = $stats
->xpath('//lst["stats"]/long[@name="deletesByQuery"]');
$summary['@deletes_by_query'] = (int) trim(current($deletes_query_xpath));
$summary['@deletes_total'] = $summary['@deletes_by_id'] + $summary['@deletes_by_query'];
$schema = $system_info->core->schema;
$summary['@schema_version'] = $schema;
$core = $stats
->xpath('//lst["core"]/str[@name="coreName"]');
$summary['@core_name'] = trim(current($core));
$size_xpath = $stats
->xpath('//lst["core"]/str[@name="indexSize"]');
$summary['@index_size'] = trim(current($size_xpath));
}
}
return $summary;
}
public function clearCache() {
if ($cid = $this
->getCacheId()) {
cache_clear_all($cid, 'cache_search_api_solr', TRUE);
}
$this->luke = array();
$this->stats = NULL;
$this->system_info = NULL;
}
protected function checkResponse($response) {
$code = (int) $response->code;
if ($code != 200) {
if ($code >= 400 && $code != 403 && $code != 404) {
$response->status_message .= $response->data;
}
throw new SearchApiException('"' . $code . '" Status: ' . $response->status_message);
}
return $response;
}
public function makeServletRequest($servlet, array $params = array(), array $options = array()) {
$params += array(
'wt' => 'json',
'json.nl' => self::NAMED_LIST_FORMAT,
);
$url = $this
->constructUrl($servlet, $params);
$response = $this
->makeHttpRequest($url, $options);
return $this
->checkResponse($response);
}
protected function sendRawGet($url, array $options = array()) {
$options['method'] = 'GET';
$response = $this
->makeHttpRequest($url, $options);
return $this
->checkResponse($response);
}
protected function sendRawPost($url, array $options = array()) {
$options['method'] = 'POST';
if (empty($options['headers']['Content-Type'])) {
$options['headers']['Content-Type'] = 'text/xml; charset=UTF-8';
}
$response = $this
->makeHttpRequest($url, $options);
return $this
->checkResponse($response);
}
protected function makeHttpRequest($url, array $options = array()) {
if (empty($options['method']) || $options['method'] == 'GET' || $options['method'] == 'HEAD') {
$options['data'] = NULL;
}
if ($this->http_auth) {
$options['headers']['Authorization'] = $this->http_auth;
}
if ($this->stream_context) {
$options['context'] = $this->stream_context;
}
$result = drupal_http_request($url, $options);
$result->status_message = isset($result->status_message) ? $result->status_message : '';
if (!isset($result->code) || $result->code < 0) {
$result->code = 0;
$result->status_message = 'Request failed';
$result->protocol = 'HTTP/1.0';
}
if (isset($result->error)) {
$result->status_message .= ': ' . check_plain($result->error);
}
if (!isset($result->data)) {
$result->data = '';
$result->response = NULL;
}
else {
$response = json_decode($result->data);
if (is_object($response)) {
foreach ($response as $key => $value) {
$result->{$key} = $value;
}
}
}
return $result;
}
public static function escape($value, $version = 0) {
$replacements = array();
$specials = array(
'+',
'-',
'&&',
'||',
'!',
'(',
')',
'{',
'}',
'[',
']',
'^',
'"',
'~',
'*',
'?',
':',
"\\",
'AND',
'OR',
'NOT',
);
if ($version >= 4) {
$specials[] = '/';
}
foreach ($specials as $special) {
$replacements[$special] = "\\{$special}";
}
return strtr($value, $replacements);
}
public static function escapePhrase($value) {
$replacements['"'] = '\\"';
$replacements["\\"] = "\\\\";
return strtr($value, $replacements);
}
public static function phrase($value) {
return '"' . self::escapePhrase($value) . '"';
}
public static function escapeFieldName($value) {
$value = str_replace(':', '\\:', $value);
return $value;
}
protected function constructUrl($servlet, array $params = array(), $added_query_string = NULL) {
$query_string = $this
->httpBuildQuery($params);
if ($query_string) {
$query_string = '?' . $query_string;
if ($added_query_string) {
$query_string = $query_string . '&' . $added_query_string;
}
}
elseif ($added_query_string) {
$query_string = '?' . $added_query_string;
}
return $this->base_url . $servlet . $query_string;
}
public function getBaseUrl() {
return $this->base_url;
}
public function setBaseUrl($url) {
$this->base_url = $url;
$this->update_url = NULL;
}
public function update($rawPost, $timeout = 3600) {
if (empty($this->update_url)) {
$this->update_url = $this
->constructUrl(self::UPDATE_SERVLET, array(
'wt' => 'json',
));
}
$options['data'] = $rawPost;
if ($timeout) {
$options['timeout'] = $timeout;
}
return $this
->sendRawPost($this->update_url, $options);
}
public function addDocuments(array $documents, $overwrite = NULL, $commitWithin = NULL) {
$attr = '';
if (isset($overwrite)) {
$attr .= ' overwrite="' . ($overwrite ? 'true"' : 'false"');
}
if (isset($commitWithin)) {
$attr .= ' commitWithin="' . (int) $commitWithin . '"';
}
$rawPost = "<add{$attr}>";
foreach ($documents as $document) {
if (is_object($document) && $document instanceof SearchApiSolrDocument) {
$rawPost .= $document
->toXml();
}
}
$rawPost .= '</add>';
return $this
->update($rawPost);
}
public function commit($waitSearcher = TRUE, $timeout = 3600) {
return $this
->optimizeOrCommit('commit', $waitSearcher, $timeout);
}
public function deleteById($id, $timeout = 3600) {
return $this
->deleteByMultipleIds(array(
$id,
), $timeout);
}
public function deleteByMultipleIds(array $ids, $timeout = 3600) {
$rawPost = '<delete>';
foreach ($ids as $id) {
$rawPost .= '<id>' . htmlspecialchars($id, ENT_NOQUOTES, 'UTF-8') . '</id>';
}
$rawPost .= '</delete>';
return $this
->update($rawPost, $timeout);
}
public function deleteByQuery($rawQuery, $timeout = 3600) {
$rawPost = '<delete><query>' . htmlspecialchars($rawQuery, ENT_NOQUOTES, 'UTF-8') . '</query></delete>';
return $this
->update($rawPost, $timeout);
}
public function optimize($waitSearcher = TRUE, $timeout = 3600) {
return $this
->optimizeOrCommit('optimize', $waitSearcher, $timeout);
}
protected function optimizeOrCommit($type, $waitSearcher = TRUE, $timeout = 3600) {
$waitSearcher = $waitSearcher ? '' : ' waitSearcher="false"';
if ($this
->getSolrVersion() <= 3) {
$rawPost = "<{$type}{$waitSearcher} />";
}
else {
$softCommit = $this->soft_commit ? ' softCommit="true"' : '';
$rawPost = "<{$type}{$waitSearcher}{$softCommit} />";
}
$response = $this
->update($rawPost, $timeout);
$this
->clearCache();
return $response;
}
protected function httpBuildQuery(array $query, $parent = '') {
$params = array();
foreach ($query as $key => $value) {
$key = $parent ? $parent : rawurlencode($key);
if (is_array($value)) {
$value = $this
->httpBuildQuery($value, $key);
if ($value) {
$params[] = $value;
}
}
elseif (!isset($value)) {
$params[] = $key;
}
else {
$params[] = $key . '=' . rawurlencode($value);
}
}
return implode('&', $params);
}
public function search($query = NULL, array $params = array(), $method = 'GET') {
$params['wt'] = 'json';
$params += array(
'json.nl' => self::NAMED_LIST_FORMAT,
);
if (isset($query)) {
$params['q'] = $query;
}
if (!isset($params['q']) || !strlen($params['q'])) {
unset($params['q'], $params['qf']);
}
$queryString = $this
->httpBuildQuery($params);
if (!empty($this->options['log_query'])) {
watchdog('search_api_solr', 'Query: @query', array(
'@query' => $queryString,
), WATCHDOG_DEBUG);
}
if ($method == 'GET' || $method == 'AUTO') {
$searchUrl = $this
->constructUrl(self::SEARCH_SERVLET, array(), $queryString);
if ($method == 'GET' || strlen($searchUrl) <= variable_get('search_api_solr_http_get_max_length', 4000)) {
$response = $this
->sendRawGet($searchUrl);
if (!empty($this->options['log_response'])) {
$this
->logResponse($response);
}
return $response;
}
}
$searchUrl = $this
->constructUrl(self::SEARCH_SERVLET);
$options['data'] = $queryString;
$options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
$response = $this
->sendRawPost($searchUrl, $options);
if (!empty($this->options['log_response'])) {
$this
->logResponse($response);
}
return $response;
}
protected function logResponse($response) {
$data = $response->code . ' ' . $response->status_message;
if (!empty($response->response)) {
$data .= "\n" . print_r($response->response, TRUE);
}
watchdog('search_api_solr', 'Response: <div style="white-space: pre-wrap;">@response</div>', array(
'@response' => $data,
), WATCHDOG_DEBUG);
if (!empty($response->facet_counts)) {
watchdog('search_api_solr', 'Facets: <div style="white-space: pre-wrap;">@facets</div>', array(
'@facets' => print_r($response->facet_counts, TRUE),
), WATCHDOG_DEBUG);
}
}
}