silverstripe-fulltextsearch/src/Solr/Solr.php

162 lines
6.6 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\Injector\Injector;
use SilverStripe\Core\Manifest\Module;
use SilverStripe\Core\Manifest\ModuleLoader;
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;
use SilverStripe\FullTextSearch\Solr\Services\SolrService;
use SilverStripe\FullTextSearch\Solr\Services\SolrService_Core;
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', 'webdav' or 'post'
2015-11-21 07:19:20 +01:00
*
* When mode == SolrConfigStore_File or 'file' (indexes should be written on a local filesystem)
2015-11-21 07:19:20 +01:00
* 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_Post or 'post' (indexes should stored on a remote Solr server via post)
* This mode will require custom software on the remote solr server which handles receiving the post and
* passing on that information to solr. It is up to the user of this mode to write such software.
* path (default: /solrindex) - The suburl on the solr host that is set up to accept index configurations
* port (default: none) - The port on the remote server which is set up to receive the post information
*
* When mode == SolrConfigStore_WebDAV or 'webdav' (indexes should stored on a remote Solr server via webdav)
2015-11-21 07:19:20 +01:00
* 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'];
/** @var Module $module */
$module = ModuleLoader::getModule('silverstripe/fulltextsearch');
$modulePath = $module->getPath();
2015-11-21 07:19:20 +01:00
if (version_compare($version, '4', '>=')) {
$versionDefaults = [
'service' => Solr4Service::class,
'extraspath' => $modulePath . '/conf/solr/4/extras/',
'templatespath' => $modulePath . '/conf/solr/4/templates/',
];
2015-11-21 07:19:20 +01:00
} else {
$versionDefaults = [
'service' => Solr3Service::class,
'extraspath' => $modulePath . '/conf/solr/3/extras/',
'templatespath' => $modulePath . '/conf/solr/3/templates/',
];
2015-11-21 07:19:20 +01:00
}
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 */
2015-11-21 07:19:20 +01:00
protected static $service_core_singletons = array();
/**
* Get a SolrService
*
* @param string $core Optional name of index class
* @return SolrService|SolrService_Core
2015-11-21 07:19:20 +01:00
*/
public static function service($core = null)
{
$options = self::solr_options();
if (!self::$service_singleton) {
self::$service_singleton = Injector::inst()->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];
}
return self::$service_singleton;
2015-11-21 07:19:20 +01:00
}
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
}
2017-04-26 13:19:25 +02:00
}