From 891f6ae125ca8b57f90ce5af9a03646111ac0472 Mon Sep 17 00:00:00 2001 From: Andrew Aitken-Fincham Date: Tue, 13 Apr 2021 15:38:07 +1200 Subject: [PATCH] add SolrConfigStore_POST for ss3 --- code/solr/Solr.php | 11 +- code/solr/SolrConfigStore.php | 102 ------------------ .../solr/configstore/SolrConfigStore_File.php | 50 +++++++++ .../solr/configstore/SolrConfigStore_POST.php | 51 +++++++++ .../configstore/SolrConfigStore_WebDAV.php | 53 +++++++++ 5 files changed, 164 insertions(+), 103 deletions(-) create mode 100644 code/solr/configstore/SolrConfigStore_File.php create mode 100644 code/solr/configstore/SolrConfigStore_POST.php create mode 100644 code/solr/configstore/SolrConfigStore_WebDAV.php diff --git a/code/solr/Solr.php b/code/solr/Solr.php index ec8e12b..6254e4d 100644 --- a/code/solr/Solr.php +++ b/code/solr/Solr.php @@ -27,7 +27,7 @@ class Solr * * indexstore => an array with * - * mode - a classname which implements SolrConfigStore, or 'file' or 'webdav' + * mode - a classname which implements SolrConfigStore, or 'file', 'webdav' or 'post' * * 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. @@ -38,6 +38,13 @@ class Solr * 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 * remotepath - 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 + * */ protected static $solr_options = array(); @@ -298,6 +305,8 @@ class Solr_Configure extends Solr_BuildTask return new SolrConfigStore_File($indexstore); } elseif ($mode == 'webdav') { return new SolrConfigStore_WebDAV($indexstore); + } elseif ($mode == 'post') { + return new SolrConfigStore_POST($indexstore); } elseif (ClassInfo::exists($mode) && ClassInfo::classImplements($mode, 'SolrConfigStore')) { return new $mode($indexstore); } else { diff --git a/code/solr/SolrConfigStore.php b/code/solr/SolrConfigStore.php index 0f16439..5163661 100644 --- a/code/solr/SolrConfigStore.php +++ b/code/solr/SolrConfigStore.php @@ -30,105 +30,3 @@ interface SolrConfigStore */ public function instanceDir($index); } - -/** - * Class SolrConfigStore_File - * - * A ConfigStore that uploads files to a Solr instance on a locally accessible filesystem - * by just using file copies - */ -class SolrConfigStore_File implements SolrConfigStore -{ - public function __construct($config) - { - $this->local = $config['path']; - $this->remote = isset($config['remotepath']) ? $config['remotepath'] : $config['path']; - } - - public function getTargetDir($index) - { - $targetDir = "{$this->local}/{$index}/conf"; - - if (!is_dir($targetDir)) { - $worked = @mkdir($targetDir, 0770, true); - - if (!$worked) { - throw new RuntimeException( - sprintf('Failed creating target directory %s, please check permissions', $targetDir) - ); - } - } - - return $targetDir; - } - - public function uploadFile($index, $file) - { - $targetDir = $this->getTargetDir($index); - copy($file, $targetDir.'/'.basename($file)); - } - - public function uploadString($index, $filename, $string) - { - $targetDir = $this->getTargetDir($index); - file_put_contents("$targetDir/$filename", $string); - } - - public function instanceDir($index) - { - return $this->remote.'/'.$index; - } -} - -/** - * Class SolrConfigStore_WebDAV - * - * A ConfigStore that uploads files to a Solr instance via a WebDAV server - */ -class SolrConfigStore_WebDAV implements SolrConfigStore -{ - public function __construct($config) - { - $options = Solr::solr_options(); - - $this->url = implode('', array( - 'http://', - isset($config['auth']) ? $config['auth'].'@' : '', - $options['host'].':'.(isset($config['port']) ? $config['port'] : $options['port']), - $config['path'] - )); - $this->remote = $config['remotepath']; - } - - public function getTargetDir($index) - { - $indexdir = "{$this->url}/$index"; - if (!WebDAV::exists($indexdir)) { - WebDAV::mkdir($indexdir); - } - - $targetDir = "{$this->url}/$index/conf"; - if (!WebDAV::exists($targetDir)) { - WebDAV::mkdir($targetDir); - } - - return $targetDir; - } - - public function uploadFile($index, $file) - { - $targetDir = $this->getTargetDir($index); - WebDAV::upload_from_file($file, $targetDir.'/'.basename($file)); - } - - public function uploadString($index, $filename, $string) - { - $targetDir = $this->getTargetDir($index); - WebDAV::upload_from_string($string, "$targetDir/$filename"); - } - - public function instanceDir($index) - { - return $this->remote ? "{$this->remote}/$index" : $index; - } -} diff --git a/code/solr/configstore/SolrConfigStore_File.php b/code/solr/configstore/SolrConfigStore_File.php new file mode 100644 index 0000000..5029f86 --- /dev/null +++ b/code/solr/configstore/SolrConfigStore_File.php @@ -0,0 +1,50 @@ +local = $config['path']; + $this->remote = isset($config['remotepath']) ? $config['remotepath'] : $config['path']; + } + + public function getTargetDir($index) + { + $targetDir = "{$this->local}/{$index}/conf"; + + if (!is_dir($targetDir)) { + $worked = @mkdir($targetDir, 0770, true); + + if (!$worked) { + throw new RuntimeException( + sprintf('Failed creating target directory %s, please check permissions', $targetDir) + ); + } + } + + return $targetDir; + } + + public function uploadFile($index, $file) + { + $targetDir = $this->getTargetDir($index); + copy($file, $targetDir.'/'.basename($file)); + } + + public function uploadString($index, $filename, $string) + { + $targetDir = $this->getTargetDir($index); + file_put_contents("$targetDir/$filename", $string); + } + + public function instanceDir($index) + { + return $this->remote.'/'.$index; + } +} diff --git a/code/solr/configstore/SolrConfigStore_POST.php b/code/solr/configstore/SolrConfigStore_POST.php new file mode 100644 index 0000000..e8d7787 --- /dev/null +++ b/code/solr/configstore/SolrConfigStore_POST.php @@ -0,0 +1,51 @@ +url = implode('', array( + 'http://', + isset($config['auth']) ? $config['auth'].'@' : '', + $options['host'].':'.(isset($config['port']) ? $config['port'] : $options['port']), + $config['path'] + )); + + if (isset($config['remotepath'])) { + $this->remote = $config['remotepath']; + } + } + + public function uploadFile($index, $file) + { + $this->uploadString($index, basename($file), file_get_contents($file)); + } + + public function uploadString($index, $filename, $string) + { + $targetDir = "{$this->url}/config/$index"; + + file_get_contents($targetDir . '/' . $filename, false, stream_context_create([ + 'http' => [ + 'method' => 'POST', + 'header' => 'Content-type: application/octet-stream', + 'content' => (string) $string + ] + ])); + } + + public function instanceDir($index) + { + return $this->remote ? "{$this->remote}/$index" : $index; + } +} diff --git a/code/solr/configstore/SolrConfigStore_WebDAV.php b/code/solr/configstore/SolrConfigStore_WebDAV.php new file mode 100644 index 0000000..010f5dc --- /dev/null +++ b/code/solr/configstore/SolrConfigStore_WebDAV.php @@ -0,0 +1,53 @@ +url = implode('', array( + 'http://', + isset($config['auth']) ? $config['auth'].'@' : '', + $options['host'].':'.(isset($config['port']) ? $config['port'] : $options['port']), + $config['path'] + )); + $this->remote = $config['remotepath']; + } + + public function getTargetDir($index) + { + $indexdir = "{$this->url}/$index"; + if (!WebDAV::exists($indexdir)) { + WebDAV::mkdir($indexdir); + } + + $targetDir = "{$this->url}/$index/conf"; + if (!WebDAV::exists($targetDir)) { + WebDAV::mkdir($targetDir); + } + + return $targetDir; + } + + public function uploadFile($index, $file) + { + $targetDir = $this->getTargetDir($index); + WebDAV::upload_from_file($file, $targetDir.'/'.basename($file)); + } + + public function uploadString($index, $filename, $string) + { + $targetDir = $this->getTargetDir($index); + WebDAV::upload_from_string($string, "$targetDir/$filename"); + } + + public function instanceDir($index) + { + return $this->remote ? "{$this->remote}/$index" : $index; + } +}