Skip to content

Commit

Permalink
[TASK] Add cObject support for solr settings
Browse files Browse the repository at this point in the history
This feature allows you use cObjects in the solr typoscript configuration. This enables you to read the configuration
e.g. from the AdditionConfiguration.php, which is most likely the place to put environment specific settings in deployment scenarios.

The following example shows the usage:

Addition to AdditionalConfiguration.php:

```
   $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['host'] = 'mysolrserver.de';
```

Usage in typoscript:

```
    plugin.tx_solr.solr {
       host = TEXT
       host {
         value = localhost
         override.data = global:TYPO3_CONF_VARS|EXTCONF|solr|host
       }
    }
```

Fixes: #868
  • Loading branch information
timohund committed Jan 12, 2017
1 parent 6ceb43a commit 1a3a131
Show file tree
Hide file tree
Showing 9 changed files with 566 additions and 40 deletions.
62 changes: 28 additions & 34 deletions Classes/ConnectionManager.php
Expand Up @@ -27,6 +27,7 @@
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
use ApacheSolrForTypo3\Solr\Site;
use ApacheSolrForTypo3\Solr\SolrService;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use ApacheSolrForTypo3\Solr\System\Page\Rootline;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Toolbar\ClearCacheActionsHookInterface;
Expand Down Expand Up @@ -85,13 +86,12 @@ public function getConnection($host = '', $port = 8983, $path = '/solr/', $schem
);

$configuration = Util::getSolrConfiguration();
$solrConfiguration = $configuration->getValueByPathOrDefaultValue('plugin.tx_solr.solr.', []);
$host = $solrConfiguration['host'];
$port = $solrConfiguration['port'];
$path = $solrConfiguration['path'];
$scheme = $solrConfiguration['scheme'];
$username = $solrConfiguration['username'];
$password = $solrConfiguration['password'];
$host = $configuration->getSolrHost();
$port = $configuration->getSolrPort();
$path = $configuration->getSolrPath();
$scheme = $configuration->getSolrScheme();
$username = $configuration->getSolrUsername();
$password = $configuration->getSolrPassword();
}

$connectionHash = md5($scheme . '://' . $host . $port . $path . $username . $password);
Expand Down Expand Up @@ -174,7 +174,6 @@ public function getConnectionByPageId($pageId, $language = 0, $mount = '')
{
$solrServer = $this->getConfigurationByPageId($pageId, $language, $mount);
$solrConnection = $this->getConnectionFromConfiguration($solrServer);

return $solrConnection;
}

Expand Down Expand Up @@ -434,41 +433,36 @@ protected function getConfiguredSolrConnectionByRootPage(array $rootPage, $langu
// fake micro TSFE to get correct condition parsing
$GLOBALS['TSFE'] = new \stdClass();
$GLOBALS['TSFE']->tmpl = new \stdClass();
$GLOBALS['TSFE']->cObjectDepthCounter = 50;
$GLOBALS['TSFE']->tmpl->rootLine = $rootLine;
$GLOBALS['TSFE']->sys_page = $pageSelect;
$GLOBALS['TSFE']->id = $rootPage['uid'];
$GLOBALS['TSFE']->page = $rootPage;

$tmpl->generateConfig();
$GLOBALS['TSFE']->tmpl->setup = $tmpl->setup;

list($solrSetup) = $tmpl->ext_getSetup($tmpl->setup,
'plugin.tx_solr.solr');
list(, $solrEnabled) = $tmpl->ext_getSetup($tmpl->setup,
'plugin.tx_solr.enabled');
$solrEnabled = !empty($solrEnabled) ? true : false;

if (!empty($solrSetup) && $solrEnabled) {
$solrPath = trim($solrSetup['path'], '/');
$solrPath = '/' . $solrPath . '/';

$connection = [
'connectionKey' => $connectionKey,

'rootPageTitle' => $rootPage['title'],
'rootPageUid' => $rootPage['uid'],

'solrScheme' => $solrSetup['scheme'],
'solrHost' => $solrSetup['host'],
'solrPort' => $solrSetup['port'],
'solrPath' => $solrPath,
'solrUsername' => $solrSetup['username'],
'solrPassword' => $solrSetup['password'],

'language' => $languageId
];
$connection['label'] = $this->buildConnectionLabel($connection);
$configuration = Util::getSolrConfiguration();
$solrIsEnabledAndConfigured = $configuration->getEnabled() && $configuration->getSolrHasConnectionConfiguration();
if (!$solrIsEnabledAndConfigured) {
return $connection;
}

$connection = [
'connectionKey' => $connectionKey,
'rootPageTitle' => $rootPage['title'],
'rootPageUid' => $rootPage['uid'],
'solrScheme' => $configuration->getSolrScheme(),
'solrHost' => $configuration->getSolrHost(),
'solrPort' => $configuration->getSolrPort(),
'solrPath' => $configuration->getSolrPath(),
'solrUsername' => $configuration->getSolrUsername(),
'solrPassword' => $configuration->getSolrPassword(),

'language' => $languageId
];

$connection['label'] = $this->buildConnectionLabel($connection);
return $connection;
}

Expand Down
171 changes: 166 additions & 5 deletions Classes/System/Configuration/TypoScriptConfiguration.php
Expand Up @@ -2,6 +2,7 @@

namespace ApacheSolrForTypo3\Solr\System\Configuration;

use ApacheSolrForTypo3\Solr\System\ContentObject\ContentObjectService;
use ApacheSolrForTypo3\Solr\System\Util\ArrayAccessor;
use InvalidArgumentException;
use TYPO3\CMS\Core\Utility\ArrayUtility;
Expand Down Expand Up @@ -45,14 +46,21 @@ class TypoScriptConfiguration
*/
protected $contextPageId = 0;

/**
* @var ContentObjectService
*/
protected $contentObjectService;

/**
* @param array $configuration
* @param int $contextPageId
* @param ContentObjectService $contentObjectService
*/
public function __construct(array $configuration, $contextPageId = 0)
public function __construct(array $configuration, $contextPageId = 0, ContentObjectService $contentObjectService = null)
{
$this->configurationAccess = new ArrayAccessor($configuration, '.', true);
$this->contextPageId = $contextPageId;
$this->contentObjectService = is_null($contentObjectService) ? GeneralUtility::makeInstance(ContentObjectService::class) : $contentObjectService;
}

/**
Expand Down Expand Up @@ -230,6 +238,19 @@ public function mergeSolrConfiguration(array $configurationToMerge, $addKeys = t
return $this;
}

/**
* Returns true when ext_solr is enabled
*
* @param boolean $defaultIfEmpty
* @return boolean
*/
public function getEnabled($defaultIfEmpty = false)
{
$path = 'plugin.tx_solr.enabled';
$result = $this->getValueByPathOrDefaultValue($path, $defaultIfEmpty);
return $this->getBool($result);
}

/**
* Returns the configured css file for a specific fileKey.
*
Expand Down Expand Up @@ -406,7 +427,7 @@ public function getIndexQueueMonitoredTables()
{
$monitoredTables = [];

$indexingConfigurations = $this->getEnabledIndexQueueConfigurationNames();
$indexingConfigurations = $this->getEnabledIndexQueueConfigurationNames();
foreach ($indexingConfigurations as $indexingConfigurationName) {
$monitoredTable = $this->getIndexQueueTableNameOrFallbackToConfigurationName($indexingConfigurationName);
$monitoredTables[] = $monitoredTable;
Expand Down Expand Up @@ -532,13 +553,13 @@ public function getEnabledIndexQueueConfigurationNames($defaultIfEmpty = array()
public function getInitialPagesAdditionalWhereClause($defaultIfEmpty = ' AND 1=1')
{
$path = 'plugin.tx_solr.index.queue.pages' . '.initialPagesAdditionalWhereClause';
$initialPagesAdditionalWhereClause = $this->getValueByPathOrDefaultValue($path, '');
$initialPagesAdditionalWhereClause = $this->getValueByPathOrDefaultValue($path, '');

if (trim($initialPagesAdditionalWhereClause) === '') {
return $defaultIfEmpty;
}

return ' AND ' . $initialPagesAdditionalWhereClause;
return ' AND ' . $initialPagesAdditionalWhereClause;
}

/**
Expand Down Expand Up @@ -879,10 +900,24 @@ public function getEnabledDebugMode($defaultIfEmpty = false)
return $this->getBool($result);
}

/**
* Returns true or false if something is configured below plugin.tx_solr.solr.
*
* plugin.tx_solr.solr.
*
* @param boolean $defaultIfEmpty
* @return boolean
*/
public function getSolrHasConnectionConfiguration($defaultIfEmpty = false)
{
$configuration = $this->getObjectByPathOrDefault('plugin.tx_solr.solr.', []);
return $configuration !== [] ? true : $defaultIfEmpty;
}

/**
* Returns the defaultTimeout used for requests to the Solr server
*
* plugin.tx_solr.solr.defaultTimeout
* plugin.tx_solr.solr.timeout
*
* @param float $defaultIfEmpty
* @return float
Expand All @@ -892,6 +927,113 @@ public function getSolrTimeout($defaultIfEmpty = 0.0)
return (float)$this->getValueByPathOrDefaultValue('plugin.tx_solr.solr.timeout', $defaultIfEmpty);
}

/**
* Returns the scheme used for requests to the Solr server
*
* plugin.tx_solr.solr.scheme
*
* Applies stdWrap on the configured setting
*
* @param string $defaultIfEmpty
* @return string
*/
public function getSolrScheme($defaultIfEmpty = 'http')
{
$valuePath = 'plugin.tx_solr.solr.scheme';
$value = (string)$this->getValueByPathOrDefaultValue($valuePath, $defaultIfEmpty);
return $this->renderContentElementOfConfigured($valuePath, $value);
}

/**
* Returns the hostname used for requests to the Solr server
*
* plugin.tx_solr.solr.host
*
* Applies stdWrap on the configured setting
*
* @param string $defaultIfEmpty
* @return string
*/
public function getSolrHost($defaultIfEmpty = 'localhost')
{
$valuePath = 'plugin.tx_solr.solr.host';
$value = (string)$this->getValueByPathOrDefaultValue($valuePath, $defaultIfEmpty);
return $this->renderContentElementOfConfigured($valuePath, $value);
}

/**
* Returns the port used for requests to the Solr server
*
* plugin.tx_solr.solr.port
*
* Applies stdWrap on the configured setting
*
* @param int $defaultIfEmpty
* @return int
*/
public function getSolrPort($defaultIfEmpty = 8983)
{
$valuePath = 'plugin.tx_solr.solr.port';
$value = (string)$this->getValueByPathOrDefaultValue($valuePath, $defaultIfEmpty);
return $this->renderContentElementOfConfigured($valuePath, $value);
}

/**
* Returns the path used for requests to the Solr server
*
* plugin.tx_solr.solr.path
*
* Applies stdWrap on the configured setting
*
* @param string $defaultIfEmpty
* @return string
*/
public function getSolrPath($defaultIfEmpty = '/solr/core_en/')
{
$valuePath = 'plugin.tx_solr.solr.path';
$value = (string)$this->getValueByPathOrDefaultValue($valuePath, $defaultIfEmpty);
$solrPath = $this->renderContentElementOfConfigured($valuePath, $value);

$solrPath = trim($solrPath, '/');
$solrPath = '/' . $solrPath . '/';

return $solrPath;
}

/**
* Returns the username used for requests to the Solr server
*
* plugin.tx_solr.solr.username
*
* Applies stdWrap on the configured setting
*
* @param string $defaultIfEmpty
* @return string
*/
public function getSolrUsername($defaultIfEmpty = '')
{
$valuePath = 'plugin.tx_solr.solr.username';
$value = (string)$this->getValueByPathOrDefaultValue($valuePath, $defaultIfEmpty);
return $this->renderContentElementOfConfigured($valuePath, $value);
}

/**
* Returns the password used for requests to the Solr server
*
* plugin.tx_solr.solr.password
*
* Applies stdWrap on the configured setting
*
* @param string $defaultIfEmpty
* @return string
*/
public function getSolrPassword($defaultIfEmpty = '')
{
$valuePath = 'plugin.tx_solr.solr.password';
$value = (string)$this->getValueByPathOrDefaultValue($valuePath, $defaultIfEmpty);
return $this->renderContentElementOfConfigured($valuePath, $value);
}

/**
* Retrieves the complete search configuration
*
Expand Down Expand Up @@ -1963,4 +2105,23 @@ public function getEnableCommits($defaultIfEmpty = true)
$enableCommits = $this->getValueByPathOrDefaultValue('plugin.tx_solr.index.enableCommits', $defaultIfEmpty);
$this->getBool($enableCommits);
}

/*
* Applies the stdWrap if it is configured for the path, otherwise the unprocessed value will be returned.
*
* @param string $valuePath
* @param mixed $value
* @return mixed
*/
protected function renderContentElementOfConfigured($valuePath, $value)
{
$configurationPath = $valuePath . '.';
$configuration = $this->getObjectByPath($configurationPath);

if ($configuration == null) {
return $value;
}

return $this->contentObjectService->renderSingleContentObject($value, $configuration);
}
}

0 comments on commit 1a3a131

Please sign in to comment.