Passed
Pull Request — master (#2871)
by
unknown
29:13
created

Util   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Test Coverage

Coverage 57.75%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 27
eloc 56
c 5
b 0
f 0
dl 0
loc 261
ccs 41
cts 71
cp 0.5775
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getPageDocumentId() 0 11 2
A getDocumentId() 0 13 2
A isAllowedPageType() 0 4 1
A getAllowedPageTypes() 0 6 1
A isDraftRecord() 0 13 5
A getAbsRefPrefixFromTSFE() 0 14 3
A getSolrConfigurationFromPageId() 0 7 2
A getSolrConfiguration() 0 4 1
A getFrontendUserGroups() 0 4 1
A containsOneOfTheStrings() 0 10 3
A getLanguageUid() 0 4 1
A getFrontendUserGroupsList() 0 3 1
A initializeTsfe() 0 4 1
A getIsTYPO3VersionBelow10() 0 3 1
A getConfigurationFromPageId() 0 7 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
28
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
29
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager;
30
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationPageResolver;
31
use ApacheSolrForTypo3\Solr\System\Configuration\ExtensionConfiguration;
32
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
33
use TYPO3\CMS\Core\Context\Context;
34
use TYPO3\CMS\Core\Context\LanguageAspect;
35
use TYPO3\CMS\Core\Context\LanguageAspectFactory;
36
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
37
use TYPO3\CMS\Core\Information\Typo3Version;
38
use TYPO3\CMS\Core\Site\SiteFinder;
39
use TYPO3\CMS\Core\TypoScript\TemplateService;
40
use TYPO3\CMS\Core\Utility\RootlineUtility;
41
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
42
use TYPO3\CMS\Backend\Utility\BackendUtility;
43
use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
44
use TYPO3\CMS\Core\Utility\GeneralUtility;
45
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
46
use TYPO3\CMS\Frontend\Page\PageRepository;
47
use TYPO3\CMS\Core\Context\UserAspect;
48
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
49
use TYPO3\CMS\Core\Http\ServerRequest;
50
51
/**
52
 * Utility class for tx_solr
53
 *
54
 * @author Ingo Renner <[email protected]>
55
 */
56
class Util
57
{
58
59
    /**
60
     * Generates a document id for documents representing page records.
61
     *
62
     * @param int $uid The page's uid
63
     * @param int $typeNum The page's typeNum
64
     * @param int $language the language id, defaults to 0
65
     * @param string $accessGroups comma separated list of uids of groups that have access to that page
66
     * @param string $mountPointParameter The mount point parameter that is used to access the page.
67
     * @return string The document id for that page
68
     */
69 31
    public static function getPageDocumentId($uid, $typeNum = 0, $language = 0, $accessGroups = '0,-1', $mountPointParameter = '')
70
    {
71 31
        $additionalParameters = $typeNum . '/' . $language . '/' . $accessGroups;
72
73 31
        if ((string)$mountPointParameter !== '') {
74 2
            $additionalParameters = $mountPointParameter . '/' . $additionalParameters;
75
        }
76
77 31
        $documentId = self::getDocumentId('pages', $uid, $uid, $additionalParameters);
78
79 31
        return $documentId;
80
    }
81
82
    /**
83
     * Generates a document id in the form $siteHash/$type/$uid.
84
     *
85
     * @param string $table The records table name
86
     * @param int $rootPageId The record's site root id
87
     * @param int $uid The record's uid
88
     * @param string $additionalIdParameters Additional ID parameters
89
     * @return string A document id
90
     */
91 50
    public static function getDocumentId($table, $rootPageId, $uid, $additionalIdParameters = '')
92
    {
93
            /** @var SiteRepository $siteRepository */
94 50
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
95 50
        $site = $siteRepository->getSiteByPageId($rootPageId);
96 50
        $siteHash = $site->getSiteHash();
97
98 50
        $documentId = $siteHash . '/' . $table . '/' . $uid;
99 50
        if (!empty($additionalIdParameters)) {
100 31
            $documentId .= '/' . $additionalIdParameters;
101
        }
102
103 50
        return $documentId;
104
    }
105
106
    /**
107
     * Shortcut to retrieve the TypoScript configuration for EXT:solr
108
     *
109
     * @return TypoScriptConfiguration
110
     */
111 158
    public static function getSolrConfiguration()
112
    {
113 158
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
114 158
        return $configurationManager->getTypoScriptConfiguration();
115
    }
116
117
    /**
118
     * Gets the Solr configuration for a specific root page id.
119
     * To be used from the backend.
120
     *
121
     * @param int $pageId Id of the (root) page to get the Solr configuration from.
122
     * @param int $language System language uid, optional, defaults to 0
123
     * @deprecated
124
     *
125
     * @return TypoScriptConfiguration The Solr configuration for the requested tree.
126
     */
127
    public static function getSolrConfigurationFromPageId($pageId, $initializeTsfe = false, $language = 0)
128
    {
129
        trigger_error('solr:deprecation: Method getSolrConfigurationFromPageId is deprecated since EXT:solr 11 and will be removed in v12, use FrontendEnvironment directly.', E_USER_DEPRECATED);
130
        if ($initializeTsfe === true) {
131
            GeneralUtility::makeInstance(FrontendEnvironment::class)->initializeTsfe($pageId, $language);
132
        }
133
        return GeneralUtility::makeInstance(FrontendEnvironment::class)->getSolrConfigurationFromPageId($pageId, $language);
134
    }
135
136
    /**
137
     * Loads the TypoScript configuration for a given page id and language.
138
     * Language usage may be disabled to get the default TypoScript
139
     * configuration.
140
     *
141
     * @param int $pageId Id of the (root) page to get the Solr configuration from.
142
     * @param string $path The TypoScript configuration path to retrieve.
143
     * @param bool $initializeTsfe
144
     * @deprecated
145
     * @param int $language System language uid, optional, defaults to 0
146
     * @param bool $useTwoLevelCache Flag to enable the two level cache for the typoscript configuration array
147
     * @return TypoScriptConfiguration The Solr configuration for the requested tree.
148
     */
149 2
    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

149
    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...
150
    {
151 2
        trigger_error('Method getConfigurationFromPageId is deprecated since EXT:solr 11 and will be removed in v12, use FrontendEnvironment directly.', E_USER_DEPRECATED);
152 2
        if ($initializeTsfe === true) {
153
            GeneralUtility::makeInstance(FrontendEnvironment::class)->initializeTsfe($pageId, $language);
154
        }
155 2
        return GeneralUtility::makeInstance(FrontendEnvironment::class)->getConfigurationFromPageId($pageId, $path, $language);
156
    }
157
158
    /**
159
     * Initializes the TSFE for a given page ID and language.
160
     *
161
     * @param $pageId
162
     * @param int $language
163
     * @param bool $useCache
164
     * @deprecated
165
     * @throws SiteNotFoundException
166
     * @throws \TYPO3\CMS\Core\Error\Http\ServiceUnavailableException
167
     * @throws \TYPO3\CMS\Core\Http\ImmediateResponseException
168
     */
169
    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

169
    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...
170
    {
171
        trigger_error('solr:deprecation: Method initializeTsfe is deprecated since EXT:solr 11 and will be removed in v12, use FrontendEnvironment directly.', E_USER_DEPRECATED);
172
        GeneralUtility::makeInstance(FrontendEnvironment::class)->initializeTsfe($pageId, $language);
173
    }
174
175
    /**
176
     * Check if record ($table, $uid) is a workspace record
177
     *
178
     * @param string $table The table the record belongs to
179
     * @param int $uid The record's uid
180
     * @return bool TRUE if the record is in a draft workspace, FALSE if it's a LIVE record
181
     */
182 46
    public static function isDraftRecord($table, $uid)
183
    {
184 46
        $isWorkspaceRecord = false;
185
186 46
        if ((ExtensionManagementUtility::isLoaded('workspaces')) && (BackendUtility::isTableWorkspaceEnabled($table))) {
187
            $record = BackendUtility::getRecord($table, $uid, 'pid, t3ver_state');
188
189
            if ($record['pid'] == '-1' || $record['t3ver_state'] > 0) {
190
                $isWorkspaceRecord = true;
191
            }
192
        }
193
194 46
        return $isWorkspaceRecord;
195
    }
196
197
    /**
198
     * Check if the page type of a page record is allowed
199
     *
200
     * @param array $pageRecord The pages database row
201
     * @param string $configurationName The name of the configuration to use.
202
     * @deprecated
203
     *
204
     * @return bool TRUE if the page type is allowed, otherwise FALSE
205
     */
206
    public static function isAllowedPageType(array $pageRecord, $configurationName = 'pages')
207
    {
208
        trigger_error('solr:deprecation: Method isAllowedPageType is deprecated since EXT:solr 11 and will be removed in v12, use FrontendEnvironment directly.', E_USER_DEPRECATED);
209
        return GeneralUtility::makeInstance(FrontendEnvironment::class)->isAllowedPageType($pageRecord, $configurationName);
210
    }
211
212
    /**
213
     * Get allowed page types
214
     *
215
     * @param int $pageId Page ID
216
     * @param string $configurationName The name of the configuration to use.
217
     * @deprecated
218
     * @return array Allowed page types to compare to a doktype of a page record
219
     */
220
    public static function getAllowedPageTypes($pageId, $configurationName = 'pages')
221
    {
222
        trigger_error('solr:deprecation: Method getAllowedPageTypes is deprecated since EXT:solr 11 and will be removed in v12, no call required.', E_USER_DEPRECATED);
223
        $rootPath = '';
224
        $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

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