Issues (202)

Classes/System/Util/SiteUtility.php (4 issues)

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
class SiteUtility
38
{
39
40
    /**
41
     * Determines if the site where the page belongs to is managed with the TYPO3 site management.
42
     *
43
     * @param int $pageId
44
     * @return boolean
45
     */
46
    public static function getIsSiteManagedSite(int $pageId): bool
47
    {
48
49
        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
50
        try {
51
            /* @var SiteFinder $siteFinder */
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
     * Note: Language context properties have precedence over global settings.
64
     *
65
     * The configuration is done in the globals configuration of a site, and be extended in the language specific configuration
66
     * of a site.
67
     *
68
     * Typically everything except the core name is configured on the global level and the core name differs for each language.
69
     *
70
     * In addition every property can be defined for the ```read``` and ```write``` scope.
71
     *
72
     * The convention for property keys is "solr_{propertyName}_{scope}". With the configuration "solr_host_read" you define the host
73
     * for the solr read connection.
74
     *
75
     * @param Site $typo3Site
76
     * @param string $property
77
     * @param int $languageId
78
     * @param string $scope
79
     * @param string $defaultValue
80
     * @return string
81
     */
82
    public static function getConnectionProperty(Site $typo3Site, string $property, int $languageId, string $scope, string $defaultValue = null): string
83
    {
84
        $value = self::getConnectionPropertyOrFallback($typo3Site, $property, $languageId, $scope);
85
        if ($value === null) {
86
            return $defaultValue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $defaultValue could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
87
        }
88
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value could return the type boolean which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
89
    }
90
91
    /**
92
     * Resolves site configuration properties.
93
     * Language context properties have precedence over global settings.
94
     *
95
     * @param Site $typo3Site
96
     * @param string $property
97
     * @param int $languageId
98
     * @param string $scope
99
     * @return mixed
100
     */
101
    protected static function getConnectionPropertyOrFallback(Site $typo3Site, string $property, int $languageId, string $scope)
102
    {
103
        if ($scope === 'write' && !self::writeConnectionIsEnabled($typo3Site, $languageId)) {
104
            $scope = 'read';
105
        }
106
107
        // convention key solr_$property_$scope
108
        $keyToCheck = 'solr_' . $property . '_' . $scope;
109
110
        // convention fallback key solr_$property_read
111
        $fallbackKey = 'solr_' . $property . '_read';
112
113
        // try to find language specific setting if found return it
114
        $languageSpecificConfiguration = $typo3Site->getLanguageById($languageId)->toArray();
115
        $value = self::getValueOrFallback($languageSpecificConfiguration, $keyToCheck, $fallbackKey);
116
        if ($value !== null) {
117
            return $value;
118
        }
119
120
        // if not found check global configuration
121
        $siteBaseConfiguration = $typo3Site->getConfiguration();
122
        return self::getValueOrFallback($siteBaseConfiguration, $keyToCheck, $fallbackKey);
123
    }
124
125
    /**
126
     * Checks whether write connection is enabled.
127
     * Language context properties have precedence over global settings.
128
     *
129
     * @param Site $typo3Site
130
     * @param int $languageId
131
     * @return bool
132
     */
133
    protected static function writeConnectionIsEnabled(Site $typo3Site, int $languageId): bool
134
    {
135
        $languageSpecificConfiguration = $typo3Site->getLanguageById($languageId)->toArray();
136
        $value = self::getValueOrFallback($languageSpecificConfiguration, 'solr_use_write_connection', 'solr_use_write_connection');
137
        if ($value !== null) {
138
            return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value could return the type string which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
139
        }
140
141
        $siteBaseConfiguration = $typo3Site->getConfiguration();
142
        $value = self::getValueOrFallback($siteBaseConfiguration, 'solr_use_write_connection', 'solr_use_write_connection');
143
        if ($value !== null) {
144
            return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value could return the type string which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
145
        }
146
        return false;
147
    }
148
149
    /**
150
     * @param array $data
151
     * @param string $keyToCheck
152
     * @param string $fallbackKey
153
     * @return string|bool|null
154
     */
155
    protected static function getValueOrFallback(array $data, string $keyToCheck, string $fallbackKey)
156
    {
157
        $value = $data[$keyToCheck] ?? null;
158
        if ($value === '0' || $value === 0 || !empty($value)) {
159
            return self::evaluateConfigurationData($value);
160
        }
161
162
        return self::evaluateConfigurationData($data[$fallbackKey] ?? null);
163
    }
164
165
    /**
166
     * Evaluate configuration data
167
     *
168
     * Setting boolean values via environment variables
169
     * results in strings like 'false' that may be misinterpreted
170
     * thus we check for boolean values in strings.
171
     *
172
     * @param string|bool|null $value
173
     * @return string|bool|null
174
     */
175
    protected static function evaluateConfigurationData($value)
176
    {
177
        if ($value === 'true') {
178
            return true;
179
        } elseif ($value === 'false') {
180
            return false;
181
        }
182
183
        return $value;
184
    }
185
}
186