Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Richter committed Aug 31, 2023
0 parents commit 4b3a522
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 0 deletions.
87 changes: 87 additions & 0 deletions Classes/Common/ElasticClientBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Slub\LisztCommon\Common;

/*
* This file is part of the Liszt Catalog Raisonne project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
*/

use Illuminate\Support\Collection;
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class ElasticClientBuilder extends ClientBuilder {

protected string $caFilePath;
protected Collection $extConf;
protected array $hosts;
protected string $password;

public static function getClient(): Client
{
return parent::create()->
initialize()->
autoconfig()->
build();
}

protected function initialize(): ElasticClientBuilder
{
$this->extConf = new Collection(
GeneralUtility::makeInstance(ExtensionConfiguration::class)->
get('liszt_common'));

$this->hosts = [ $this->extConf->get('elasticHostName') ];
$this->setCaFilePath();
$this->setPassword();

return $this;
}

protected function autoconfig (): ElasticClientBuilder {
$this->sethosts($this->hosts);
if ($this->password) {
$this->setBasicAuthentication('elastic', $this->password);
}
if ($this->caFilePath) {
$this->setSSLVerification($this->caFilePath);
}

return $this;
}

private function setCaFilePath(): void
{
if ($this->extConf->get('elasticCaFileName') == '') {
$this->caFilePath = '';
return;
}

$this->caFilePath = $this->extConf->
only('elastcCredentialsFilePath', 'elasticCaFileName')->
implode('/');
}

private function setPassword(): void
{
if ($this->extConf->get('elasticPwdFileName') == '') {
$this->password = '';
return;
}

$passwordFilePath = $this->extConf->
only('elasticCredentialsFilePath', 'elasticPwdFileName')->
implode('/');
$passwordFile = fopen($passwordFilePath, 'r') or
die($passwordFilePath . ' not found. Check your extension\'s configuration');

$this->password = $passwordFile->getContents();
}
}
40 changes: 40 additions & 0 deletions Classes/Controller/ClientEnabledController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace Slub\LisztCommon\Controller;

use Elasticsearch\ClientBuilder;
use Elasticsearch\Client;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

/***
*
* This file is part of the "Publisher Database" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2020 Matthias Richter <[email protected]>, SLUB Dresden
*
***/
/**
* ClientEnabledController
*/
abstract class ClientEnabledController extends ActionController
{

/**
* elasticClient
* @var Client
*/
protected $elasticClient = null;

/**
* initialize show action: firing up client
*/
public function initializeClient()
{
$this->elasticClient = ElasticClientBuilder::create()->
autoconfig()->
build();
}

}
8 changes: 8 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false
Slub\LisztCommon\:
resource: '../Classes/*'
exclude: '../Classes/Domain/Model/*'
3 changes: 3 additions & 0 deletions Configuration/TypoScript/setup.typoscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
page.includeJSFooterlibs.Elasticsearch = EXT:liszt_common/Resources/Public/JavaScript/Src/elasticsearch.min.js
page.includeJSFooter.UrlManager = EXT:liszt_common/Resources/Public/JavaScript/Src/UrlManager.js
page.includeJSFooter.Constants = EXT:liszt_common/Resources/Public/JavaScript/Src/Constants.js
1 change: 1 addition & 0 deletions Resources/Public/JavaScript/Src/Constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const LISZT_COMMON_MAX_SIZE = 10000;
43 changes: 43 additions & 0 deletions Resources/Public/JavaScript/Src/UrlManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class UrlManager {

#mappings = [];
#body = {};
#url = new URL(location);

get body() {
this.updateMappings();
return this.#body;
}

updateMappings() {
for (let key in this.#mappings) {
const value = this.#url.searchParams.get(key);
const path = this.#mappings[key].split('.');
if (value) {
let cursor = this.#body;
for (let pathSegment of path.slice(0, -1)) {
cursor[pathSegment] ??= {};
cursor = cursor[pathSegment];
}
cursor[path.slice(-1)[0]] = value;
} else {
this.#body = {};
}
}
}

registerMapping(mapping) {
for (let key in mapping) {
this.#mappings[key] = mapping[key];
}
}

setParam(key, value) {
if (value) {
this.#url.searchParams.set(key, value);
} else {
this.#url.searchParams.delete(key);
}
window.history.pushState({}, '', this.#url.href);
}
}
26 changes: 26 additions & 0 deletions Resources/Public/JavaScript/Src/elasticsearch.min.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "slub/liszt-common",
"description": "common tools for the Liszt catalog raisonné",
"type": "typo3-cms-extension",
"license": [
"GPL-2.0-or-later"
],
"require": {
"typo3/cms-core": "^11.5",
"elasticsearch/elasticsearch": "^7"
},
"autoload": {
"psr-4": {
"Slub\\LisztCommon\\": "Classes/"
}
},
"extra": {
"typo3/cms": {
"extension-key": "liszt_common"
}
}
}
8 changes: 8 additions & 0 deletions ext_conf_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# cat=Elasticsearch; type=string; label=LLL:EXT:liszt_common/Resources/Private/Language/Labels.xml:config.elasticHostName
elasticHostName = https://sdvelasticmusikverlage:9200
# cat=Elasticsearch; type=string; label=LLL:EXT:liszt_common/Resources/Private/Language/Labels.xml:config.elasticCredentialsFilePath
elasticCredentialsFilePath = http_ca.crt
# cat=Elasticsearch; type=string; label=LLL:EXT:liszt_common/Resources/Private/Language/Labels.xml:config.elasticPwdFileName
elasticPwdFileName = elastic_pwd.txt
# cat=Elasticsearch; type=string; label=LLL:EXT:liszt_common/Resources/Private/Language/Labels.xml:config.elasticCaFileName
elasticCaFileFilePath = http_ca.crt

0 comments on commit 4b3a522

Please sign in to comment.