Passed
Pull Request — master (#2809)
by
unknown
34:29
created

Util::getIsTYPO3VersionBelow10()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
18
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager;
19
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
20
use ApacheSolrForTypo3\Solr\System\Configuration\UnifiedConfiguration;
21
use TYPO3\CMS\Core\Context\Context;
22
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
23
use TYPO3\CMS\Core\Information\Typo3Version;
24
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
25
use TYPO3\CMS\Backend\Utility\BackendUtility;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
28
29
/**
30
 * Utility class for tx_solr
31
 *
32
 * @author Ingo Renner <[email protected]>
33
 * @copyright (c) 2009-2015 Ingo Renner <[email protected]>
34
 */
35
class Util
36
{
37
    /**
38
     * Generates a document id for documents representing page records.
39
     *
40
     * @param int $uid The page's uid
41
     * @param int $typeNum The page's typeNum
42
     * @param int $language the language id, defaults to 0
43
     * @param string $accessGroups comma separated list of uids of groups that have access to that page
44
     * @param string $mountPointParameter The mount point parameter that is used to access the page.
45
     * @return string The document id for that page
46
     */
47
    public static function getPageDocumentId($uid, $typeNum = 0, $language = 0, $accessGroups = '0,-1', $mountPointParameter = '')
48
    {
49
        $additionalParameters = $typeNum . '/' . $language . '/' . $accessGroups;
50
51
        if ((string)$mountPointParameter !== '') {
52
            $additionalParameters = $mountPointParameter . '/' . $additionalParameters;
53
        }
54
55
        $documentId = self::getDocumentId('pages', $uid, $uid, $additionalParameters);
56
57
        return $documentId;
58
    }
59
60
    /**
61
     * Generates a document id in the form $siteHash/$type/$uid.
62
     *
63
     * @param string $table The records table name
64
     * @param int $rootPageId The record's site root id
65
     * @param int $uid The record's uid
66
     * @param string $additionalIdParameters Additional ID parameters
67
     * @return string A document id
68
     */
69 30
    public static function getDocumentId($table, $rootPageId, $uid, $additionalIdParameters = '')
70
    {
71 30
            /** @var SiteRepository $siteRepository */
72
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
73 30
        $site = $siteRepository->getSiteByPageId($rootPageId);
74 2
        $siteHash = $site->getSiteHash();
75
76
        $documentId = $siteHash . '/' . $table . '/' . $uid;
77 30
        if (!empty($additionalIdParameters)) {
78
            $documentId .= '/' . $additionalIdParameters;
79 30
        }
80
81
        return $documentId;
82
    }
83
84
    /**
85
     * Shortcut to retrieve the unified configuration for EXT:solr
86
     *
87
     * @return UnifiedConfiguration
88
     */
89
    public static function getUnifiedConfiguration(): UnifiedConfiguration
90
    {
91 49
        /* @var ConfigurationManager $configurationManager */
92
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
93
        return $configurationManager->getUnifiedConfiguration();
94 49
    }
95 49
96 49
    /**
97
     * Shortcut to retrieve the TypoScript configuration for EXT:solr
98 49
     *
99 49
     * @return TypoScriptConfiguration
100 30
     */
101
    public static function getSolrConfiguration(): TypoScriptConfiguration
102
    {
103 49
        /* @var ConfigurationManager $configurationManager */
104
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
105
        return $configurationManager->getTypoScriptConfiguration();
106
    }
107
108
    /**
109
     * Gets the Solr configuration for a specific root page id.
110
     * To be used from the backend.
111 157
     *
112
     * @param int $pageId Id of the (root) page to get the Solr configuration from.
113 157
     * @param int $language System language uid, optional, defaults to 0
114 157
     * @deprecated
115
     *
116
     * @return TypoScriptConfiguration The Solr configuration for the requested tree.
117
     */
118
    public static function getSolrConfigurationFromPageId($pageId, $initializeTsfe = false, $language = 0)
119
    {
120
        trigger_error('solr:deprecation: Method getSolrConfigurationFromPageId is deprecated since EXT:solr 11 and will be removed in v12, use FrontendEnvironment directly.', E_USER_DEPRECATED);
121
        if ($initializeTsfe === true) {
122
            GeneralUtility::makeInstance(FrontendEnvironment::class)->initializeTsfe($pageId, $language);
123
        }
124
        return GeneralUtility::makeInstance(FrontendEnvironment::class)->getSolrConfigurationFromPageId($pageId, $language);
125
    }
126
127
    /**
128
     * Loads the TypoScript configuration for a given page id and language.
129
     * Language usage may be disabled to get the default TypoScript
130
     * configuration.
131
     *
132
     * @param int $pageId Id of the (root) page to get the Solr configuration from.
133
     * @param string $path The TypoScript configuration path to retrieve.
134
     * @param bool $initializeTsfe
135
     * @deprecated
136
     * @param int $language System language uid, optional, defaults to 0
137
     * @param bool $useTwoLevelCache Flag to enable the two level cache for the typoscript configuration array
138
     * @return TypoScriptConfiguration The Solr configuration for the requested tree.
139
     */
140
    public static function getConfigurationFromPageId($pageId, $path, $initializeTsfe = false, $language = 0, $useTwoLevelCache = true)
0 ignored issues
show
Unused Code introduced by
The parameter $useTwoLevelCache is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

140
    public static function getConfigurationFromPageId($pageId, $path, $initializeTsfe = false, $language = 0, /** @scrutinizer ignore-unused */ $useTwoLevelCache = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
    {
142
        trigger_error('Method getConfigurationFromPageId is deprecated since EXT:solr 11 and will be removed in v12, use FrontendEnvironment directly.', E_USER_DEPRECATED);
143
        if ($initializeTsfe === true) {
144
            GeneralUtility::makeInstance(FrontendEnvironment::class)->initializeTsfe($pageId, $language);
145
        }
146
        return GeneralUtility::makeInstance(FrontendEnvironment::class)->getConfigurationFromPageId($pageId, $path, $language);
147
    }
148
149 4
    /**
150
     * Initializes the TSFE for a given page ID and language.
151 4
     *
152 4
     * @param $pageId
153 2
     * @param int $language
154
     * @param bool $useCache
155 4
     * @deprecated
156
     * @throws SiteNotFoundException
157
     * @throws \TYPO3\CMS\Core\Error\Http\ServiceUnavailableException
158
     * @throws \TYPO3\CMS\Core\Http\ImmediateResponseException
159
     */
160
    public static function initializeTsfe($pageId, $language = 0, $useCache = true)
0 ignored issues
show
Unused Code introduced by
The parameter $useCache is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

160
    public static function initializeTsfe($pageId, $language = 0, /** @scrutinizer ignore-unused */ $useCache = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
161
    {
162
        trigger_error('solr:deprecation: Method initializeTsfe is deprecated since EXT:solr 11 and will be removed in v12, use FrontendEnvironment directly.', E_USER_DEPRECATED);
163
        GeneralUtility::makeInstance(FrontendEnvironment::class)->initializeTsfe($pageId, $language);
164
    }
165
166
    /**
167
     * Check if record ($table, $uid) is a workspace record
168
     *
169
     * @param string $table The table the record belongs to
170
     * @param int $uid The record's uid
171
     * @return bool TRUE if the record is in a draft workspace, FALSE if it's a LIVE record
172
     */
173
    public static function isDraftRecord($table, $uid)
174
    {
175
        $isWorkspaceRecord = false;
176
177
        if ((ExtensionManagementUtility::isLoaded('workspaces')) && (BackendUtility::isTableWorkspaceEnabled($table))) {
178
            $record = BackendUtility::getRecord($table, $uid, 'pid, t3ver_state');
179
180
            if ($record['pid'] == '-1' || $record['t3ver_state'] > 0) {
181
                $isWorkspaceRecord = true;
182 46
            }
183
        }
184 46
185
        return $isWorkspaceRecord;
186 46
    }
187
188
    /**
189
     * Check if the page type of a page record is allowed
190
     *
191
     * @param array $pageRecord The pages database row
192
     * @param string $configurationName The name of the configuration to use.
193
     * @deprecated
194 46
     *
195
     * @return bool TRUE if the page type is allowed, otherwise FALSE
196
     */
197
    public static function isAllowedPageType(array $pageRecord, $configurationName = 'pages')
198
    {
199
        trigger_error('solr:deprecation: Method isAllowedPageType is deprecated since EXT:solr 11 and will be removed in v12, use FrontendEnvironment directly.', E_USER_DEPRECATED);
200
        return GeneralUtility::makeInstance(FrontendEnvironment::class)->isAllowedPageType($pageRecord, $configurationName);
201
    }
202
203
    /**
204
     * Get allowed page types
205
     *
206
     * @param int $pageId Page ID
207
     * @param string $configurationName The name of the configuration to use.
208
     * @deprecated
209
     * @return array Allowed page types to compare to a doktype of a page record
210
     */
211
    public static function getAllowedPageTypes($pageId, $configurationName = 'pages')
212
    {
213
        trigger_error('solr:deprecation: Method getAllowedPageTypes is deprecated since EXT:solr 11 and will be removed in v12, no call required.', E_USER_DEPRECATED);
214
        $rootPath = '';
215
        $configuration = self::getConfigurationFromPageId($pageId, $rootPath);
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...nfigurationFromPageId() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

215
        $configuration = /** @scrutinizer ignore-deprecated */ self::getConfigurationFromPageId($pageId, $rootPath);
Loading history...
216
        return $configuration->getIndexQueueAllowedPageTypesArrayByConfigurationName($configurationName);
217
    }
218
219
    /**
220
     * Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix
221
     * is set to "auto".
222
     *
223
     * @param TypoScriptFrontendController $TSFE
224
     * @deprecated
225
     * @return string
226
     */
227
    public static function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE)
228
    {
229
        trigger_error('solr:deprecation: Method getAbsRefPrefixFromTSFE is deprecated since EXT:solr 11 and will be removed in v12, no call required.', E_USER_DEPRECATED);
230
        $absRefPrefix = '';
231
        if (empty($TSFE->config['config']['absRefPrefix'])) {
232
            return $absRefPrefix;
233
        }
234
235
        $absRefPrefix = trim($TSFE->config['config']['absRefPrefix']);
236
        if ($absRefPrefix === 'auto') {
237
            $absRefPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
238
        }
239
240
        return $absRefPrefix;
241
    }
242
243
    /**
244
     * This function can be used to check if one of the strings in needles is
245
     * contained in the haystack.
246
     *
247
     *
248
     * Example:
249
     *
250
     * haystack: the brown fox
251
     * needles: ['hello', 'world']
252
     * result: false
253
     *
254
     * haystack: the brown fox
255
     * needles: ['is', 'fox']
256
     * result: true
257
     *
258
     * @param string $haystack
259
     * @param array $needles
260
     * @return bool
261
     */
262
    public static function containsOneOfTheStrings($haystack, array $needles)
263
    {
264
        foreach ($needles as $needle) {
265
            $position = strpos($haystack, $needle);
266
            if ($position !== false) {
267
                return true;
268
            }
269
        }
270
271 18
        return false;
272
    }
273 18
274 18
    /**
275 18
     * Returns the current language ID from the active context.
276 3
     * @return int
277
     */
278
    public static function getLanguageUid(): int
279
    {
280 15
        $context = GeneralUtility::makeInstance(Context::class);
281
        return (int)$context->getPropertyFromAspect('language', 'id');
282
    }
283
284
    /**
285
     * @return string
286
     */
287 46
    public static function getFrontendUserGroupsList(): string
288
    {
289 46
        return implode(',', self::getFrontendUserGroups());
290 46
    }
291
292
    /**
293
     * @return array
294
     */
295
    public static function getFrontendUserGroups(): array
296 3
    {
297
        $context = GeneralUtility::makeInstance(Context::class);
298 3
        return $context->getPropertyFromAspect('frontend.user', 'groupIds');
299
    }
300
301
    /**
302
     * @todo This method is just added for compatibility checks for TYPO3 version 9 and will be removed when TYPO9 support is dropped
303
     * @return boolean
304 7
     */
305
    public static function getIsTYPO3VersionBelow10()
306 7
    {
307 7
        return GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 10;
308
    }
309
310
}
311