silverstripe-fulltextsearch/src/Solr/Services/SolrService.php

96 lines
3.0 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\FullTextSearch\Solr\Services;
use Apache_Solr_Response;
use SilverStripe\Core\Config\Config;
/**
* The API for accessing the primary Solr installation, which includes both SolrService_Core,
* plus extra methods for interrogating, creating, reloading and getting SolrService_Core instances
* for Solr cores.
*/
2015-11-21 07:19:20 +01:00
class SolrService extends SolrService_Core
{
private static $core_class = SolrService_Core::class;
2015-11-21 07:19:20 +01:00
/**
* Handle encoding the GET parameters and making the HTTP call to execute a core command
*
* @param string $command
* @param string $core
* @param array $params
* @return Apache_Solr_Response
2015-11-21 07:19:20 +01:00
*/
protected function coreCommand($command, $core, $params = array())
2015-11-21 07:19:20 +01:00
{
2022-04-13 01:24:03 +02:00
$command = strtoupper($command ?? '');
2015-11-21 07:19:20 +01:00
$params = array_merge($params, array('action' => $command, 'wt' => 'json'));
$params[$command === 'CREATE' ? 'name' : 'core'] = $core;
2015-11-21 07:19:20 +01:00
return $this->_sendRawGet($this->_constructUrl('admin/cores', $params));
}
2015-11-21 07:19:20 +01:00
/**
* Is the passed core active?
*
* @param string $core The name of the core (an encoded class name)
* @return boolean True if that core exists & is active
2015-11-21 07:19:20 +01:00
*/
public function coreIsActive($core)
{
// Request the status of the full core name
2015-11-21 07:19:20 +01:00
$result = $this->coreCommand('STATUS', $core);
return isset($result->status->$core->uptime);
2015-11-21 07:19:20 +01:00
}
2015-11-21 07:19:20 +01:00
/**
* Create a new core
*
* @param string $core The name of the core
* @param string $instancedir The base path of the core on the server
* @param string $config The filename of solrconfig.xml on the server. Default is $instancedir/solrconfig.xml
* @param string $schema The filename of schema.xml on the server. Default is $instancedir/schema.xml
* @param string $datadir The path to store data for this core on the server. Default depends on solrconfig.xml
2015-11-21 07:19:20 +01:00
* @return Apache_Solr_Response
*/
public function coreCreate($core, $instancedir, $config = null, $schema = null, $datadir = null)
2015-11-21 07:19:20 +01:00
{
$args = array('instanceDir' => $instancedir);
if ($config) {
$args['config'] = $config;
}
if ($schema) {
$args['schema'] = $schema;
}
if ($datadir) {
$args['dataDir'] = $datadir;
}
2015-11-21 07:19:20 +01:00
return $this->coreCommand('CREATE', $core, $args);
}
2015-11-21 07:19:20 +01:00
/**
* Reload a core
*
* @param string $core The name of the core
2015-11-21 07:19:20 +01:00
* @return Apache_Solr_Response
*/
public function coreReload($core)
{
return $this->coreCommand('RELOAD', $core);
}
2015-11-21 07:19:20 +01:00
/**
* Create a new Solr4Service_Core instance for the passed core
*
* @param string $core The name of the core
* @return Solr4Service_Core
2015-11-21 07:19:20 +01:00
*/
public function serviceForCore($core)
{
$klass = Config::inst()->get(get_called_class(), 'core_class');
return new $klass($this->_host, $this->_port, $this->_path . $core, $this->_httpTransport);
2015-11-21 07:19:20 +01:00
}
}