silverstripe-fulltextsearch/code/solr/Solr.php

167 lines
6.5 KiB
PHP
Raw Normal View History

<?php
2017-04-26 13:19:25 +02:00
2017-04-21 02:27:01 +02:00
namespace SilverStripe\FullTextSearch\Solr;
2017-02-17 04:27:38 +01:00
use SilverStripe\Control\Director;
use SilverStripe\Core\Object;
2017-04-26 13:19:25 +02:00
use SilverStripe\FullTextSearch\Search\FullTextSearch;
use SilverStripe\FullTextSearch\Solr\Services\Solr4Service;
use SilverStripe\FullTextSearch\Solr\Services\Solr3Service;
2015-11-21 07:19:20 +01:00
class Solr
{
/**
* Configuration on where to find the solr server and how to get new index configurations into it.
*
* Required fields:
* host (default: localhost) - The host or IP Solr is listening on
* port (default: 8983) - The port Solr is listening on
* path (default: /solr) - The suburl the solr service is available on
*
* Optional fields:
* version (default: 4) - The Solr server version. Currently supports 3 and 4 (you can add a sub-version like 4.5 if
* you like, but currently it has no effect)
* service (default: depends on version, Solr3Service for 3, Solr4Service for 4)
* the class that provides actual communcation to the Solr server
* extraspath (default: <basefolder>/fulltextsearch/conf/solr/{version}/extras/) - Absolute path to
* the folder containing templates which are used for generating the schema and field definitions.
* templates (default: <basefolder>/fulltextsearch/conf/solr/{version}/templates/) - Absolute path to
* the configuration default files, e.g. solrconfig.xml.
*
* indexstore => an array with
*
* mode - a classname which implements SolrConfigStore, or 'file' or 'webdav'
*
* When mode == SolrConfigStore_File or file (indexes should be written on a local filesystem)
* path - The (locally accessible) path to write the index configurations to.
* remotepath (default: the same as indexpath) - The path that the Solr server will read the index configurations from
*
* When mode == SolrConfigStore_WebDAV or webdav (indexes should stored on a remote Solr server via webdav)
* auth (default: none) - A username:password pair string to use to auth against the webdav server
* path (default: /solrindex) - The suburl on the solr host that is set up to accept index configurations via webdav
* port (default: none) - The port for WebDAV if different from the Solr port
2015-11-21 07:19:20 +01:00
* remotepath - The path that the Solr server will read the index configurations from
*/
protected static $solr_options = array();
/** A cache of solr_options with the defaults all merged in */
protected static $merged_solr_options = null;
/**
* Update the configuration for Solr. See $solr_options for a discussion of the accepted array keys
* @param array $options - The options to update
*/
public static function configure_server($options = array())
{
self::$solr_options = array_merge(self::$solr_options, $options);
self::$merged_solr_options = null;
self::$service_singleton = null;
self::$service_core_singletons = array();
}
/**
* Get the configured Solr options with the defaults all merged in
* @return array - The merged options
*/
public static function solr_options()
{
if (self::$merged_solr_options) {
return self::$merged_solr_options;
}
$defaults = array(
'host' => 'localhost',
'port' => 8983,
'path' => '/solr',
'version' => '4'
);
// Build some by-version defaults
$version = isset(self::$solr_options['version']) ? self::$solr_options['version'] : $defaults['version'];
if (version_compare($version, '4', '>=')) {
$versionDefaults = array(
2017-04-26 13:19:25 +02:00
'service' => Solr4Service::class,
2015-11-21 07:19:20 +01:00
'extraspath' => Director::baseFolder().'/fulltextsearch/conf/solr/4/extras/',
'templatespath' => Director::baseFolder().'/fulltextsearch/conf/solr/4/templates/',
);
} else {
$versionDefaults = array(
2017-04-26 13:19:25 +02:00
'service' => Solr3Service::class,
2015-11-21 07:19:20 +01:00
'extraspath' => Director::baseFolder().'/fulltextsearch/conf/solr/3/extras/',
'templatespath' => Director::baseFolder().'/fulltextsearch/conf/solr/3/templates/',
);
}
return (self::$merged_solr_options = array_merge($defaults, $versionDefaults, self::$solr_options));
}
public static function set_service_class($class)
{
user_error('set_service_class is deprecated - pass as part of $options to configure_server', E_USER_WARNING);
self::configure_server(array('service' => $class));
}
/** @var SolrService | null - The instance of SolrService for core management */
protected static $service_singleton = null;
/** @var [SolrService_Core] - The instances of SolrService_Core for each core */
protected static $service_core_singletons = array();
/**
* Get a SolrService
*
* @param string $core Optional name of index class
2015-11-21 07:19:20 +01:00
* @return SolrService_Core
*/
public static function service($core = null)
{
$options = self::solr_options();
if (!self::$service_singleton) {
self::$service_singleton = Object::create(
$options['service'],
$options['host'],
$options['port'],
$options['path']
2015-11-21 07:19:20 +01:00
);
}
if ($core) {
if (!isset(self::$service_core_singletons[$core])) {
self::$service_core_singletons[$core] = self::$service_singleton->serviceForCore(
singleton($core)->getIndexName()
);
}
return self::$service_core_singletons[$core];
} else {
return self::$service_singleton;
}
}
public static function get_indexes()
{
2017-04-26 13:19:25 +02:00
return FullTextSearch::get_indexes(SolrIndex::class);
2015-11-21 07:19:20 +01:00
}
/**
* Include the thirdparty Solr client api library. Done this way to avoid issues where code is called in
* mysite/_config before fulltextsearch/_config has a change to update the include path.
*/
public static function include_client_api()
{
static $included = false;
if (!$included) {
$solr_php_path = __DIR__. '/../..' . '/thirdparty/solr-php-client/';
set_include_path(get_include_path() . PATH_SEPARATOR . $solr_php_path);
require_once($solr_php_path . 'Apache/Solr/Service.php');
require_once($solr_php_path . 'Apache/Solr/Document.php');
2015-11-21 07:19:20 +01:00
$included = true;
}
}
2017-04-26 13:19:25 +02:00
}