|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ApacheSolrForTypo3\Solr\System\Util; |
|
4
|
|
|
|
|
5
|
|
|
/*************************************************************** |
|
6
|
|
|
* Copyright notice |
|
7
|
|
|
* |
|
8
|
|
|
* (c) 2019 Timo Hund <[email protected]> |
|
9
|
|
|
* All rights reserved |
|
10
|
|
|
* |
|
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
|
12
|
|
|
* free software; you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU General Public License as published by |
|
14
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
15
|
|
|
* (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* The GNU General Public License can be found at |
|
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
|
19
|
|
|
* |
|
20
|
|
|
* This script is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
|
26
|
|
|
***************************************************************/ |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
use TYPO3\CMS\Core\Exception\SiteNotFoundException; |
|
30
|
|
|
use TYPO3\CMS\Core\Site\Entity\Site; |
|
31
|
|
|
use TYPO3\CMS\Core\Site\SiteFinder; |
|
32
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* This class contains related functions for the new site management that was introduced with TYPO3 9. |
|
36
|
|
|
* |
|
37
|
|
|
* @package ApacheSolrForTypo3\Solr\System\Util |
|
38
|
|
|
*/ |
|
39
|
|
|
class SiteUtility |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Determines if the site where the page belongs to is managed with the TYPO3 site management. |
|
44
|
|
|
* |
|
45
|
|
|
* @return boolean |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function getIsSiteManagedSite($pageId) |
|
48
|
|
|
{ |
|
49
|
|
|
|
|
50
|
|
|
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class); |
|
51
|
|
|
try { |
|
52
|
|
|
$site = $siteFinder->getSiteByPageId($pageId); |
|
53
|
|
|
} catch (SiteNotFoundException $e) { |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $site instanceof Site; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* This method is used to retrieve the connection configuration from the TYPO3 site configuration. |
|
62
|
|
|
* |
|
63
|
|
|
* The configuration is done in the globals configuration of a site, and be extended in the language specific configuration |
|
64
|
|
|
* of a site. |
|
65
|
|
|
* |
|
66
|
|
|
* Typically everything except the core name is configured on the global level and the core name differs for each language. |
|
67
|
|
|
* |
|
68
|
|
|
* In addition every property can be defined for the ```read``` and ```write``` scope. |
|
69
|
|
|
* |
|
70
|
|
|
* The convension for propery keys is "solr_{propertyName}_{scope}". With the configuration "solr_host_read" you define the host |
|
71
|
|
|
* for the solr read connection. |
|
72
|
|
|
* |
|
73
|
|
|
* @param Site $typo3Site |
|
74
|
|
|
* @param $property |
|
75
|
|
|
* @param $languageId |
|
76
|
|
|
* @param $scope |
|
77
|
|
|
* @param null $defaultValue |
|
|
|
|
|
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function getConnectionProperty(Site $typo3Site, $property, $languageId, $scope, $defaultValue = null): string |
|
81
|
|
|
{ |
|
82
|
|
|
|
|
83
|
|
|
// convention kez solr_$property_$scope |
|
84
|
|
|
$keyToCheck = 'solr_' . $property . '_' . $scope; |
|
85
|
|
|
|
|
86
|
|
|
// convention fallback key solr_$property_read |
|
87
|
|
|
$fallbackKey = 'solr_' . $property . '_read'; |
|
88
|
|
|
|
|
89
|
|
|
// try to find language specific setting if found return it |
|
90
|
|
|
$languageSpecificConfiguration = $typo3Site->getLanguageById($languageId)->toArray(); |
|
91
|
|
|
$value = self::getValueOrFallback($languageSpecificConfiguration, $keyToCheck, $fallbackKey); |
|
92
|
|
|
|
|
93
|
|
|
if ($value !== null) { |
|
94
|
|
|
return $value; |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// if not found check global configuration |
|
98
|
|
|
$siteBaseConfiguration = $typo3Site->getConfiguration(); |
|
99
|
|
|
|
|
100
|
|
|
$value = self::getValueOrFallback($siteBaseConfiguration, $keyToCheck, $fallbackKey); |
|
101
|
|
|
if ($value === null) { |
|
102
|
|
|
return $defaultValue; |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
return $value; |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param array $data |
|
111
|
|
|
* @param string $keyToCheck |
|
112
|
|
|
* @param string $fallbackKey |
|
113
|
|
|
* @return string|bool|null |
|
114
|
|
|
*/ |
|
115
|
|
|
protected static function getValueOrFallback(array $data, string $keyToCheck, string $fallbackKey) |
|
116
|
|
|
{ |
|
117
|
|
|
$value = $data[$keyToCheck] ?? null; |
|
118
|
|
|
if (!empty($value)) { |
|
119
|
|
|
return $value; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $data[$fallbackKey] ?? null; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|