Passed
Push — master ( 96ad23...e674df )
by Timo
12:01
created

Util   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 260
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 28
eloc 58
c 5
b 0
f 0
dl 0
loc 260
ccs 88
cts 92
cp 0.9565
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getDocumentId() 0 12 2
A isAllowedPageType() 0 4 1
A getAllowedPageTypes() 0 6 1
A isDraftRecord() 0 13 5
A getConfigurationFromPageId() 0 7 2
A getIsTYPO3VersionBelow10() 0 6 2
A getAbsRefPrefixFromTSFE() 0 14 3
A getSolrConfigurationFromPageId() 0 7 2
A getPageDocumentId() 0 11 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
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 68
     *
62
     * @param int $uid The page's uid
63 68
     * @param int $typeNum The page's typeNum
64
     * @param int $language the language id, defaults to 0
65 68
     * @param string $accessGroups comma separated list of uids of groups that have access to that page
66 2
     * @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 68
    public static function getPageDocumentId($uid, $typeNum = 0, $language = 0, $accessGroups = '0,-1', $mountPointParameter = '')
70
    {
71 68
        $additionalParameters = $typeNum . '/' . $language . '/' . $accessGroups;
72
73
        if ((string)$mountPointParameter !== '') {
74
            $additionalParameters = $mountPointParameter . '/' . $additionalParameters;
75
        }
76
77
        $documentId = self::getDocumentId('pages', $uid, $uid, $additionalParameters);
78
79
        return $documentId;
80
    }
81
82
    /**
83 88
     * Generates a document id in the form $siteHash/$type/$uid.
84
     *
85 88
     * @param string $table The records table name
86 88
     * @param int $rootPageId The record's site root id
87 88
     * @param int $uid The record's uid
88
     * @param string $additionalIdParameters Additional ID parameters
89 88
     * @return string A document id
90 88
     */
91 68
    public static function getDocumentId($table, $rootPageId, $uid, $additionalIdParameters = '')
92
    {
93
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
94 88
        $site = $siteRepository->getSiteByPageId($rootPageId);
95
        $siteHash = $site->getSiteHash();
96
97
        $documentId = $siteHash . '/' . $table . '/' . $uid;
98
        if (!empty($additionalIdParameters)) {
99
            $documentId .= '/' . $additionalIdParameters;
100
        }
101
102
        return $documentId;
103 201
    }
104
105 201
    /**
106 201
     * Shortcut to retrieve the TypoScript configuration for EXT:solr
107
     *
108
     * @return TypoScriptConfiguration
109
     */
110
    public static function getSolrConfiguration()
111
    {
112
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
113
        return $configurationManager->getTypoScriptConfiguration();
114
    }
115
116
    /**
117
     * Gets the Solr configuration for a specific root page id.
118 158
     * To be used from the backend.
119
     *
120 158
     * @param int $pageId Id of the (root) page to get the Solr configuration from.
121 158
     * @param int $language System language uid, optional, defaults to 0
122
     * @return TypoScriptConfiguration The Solr configuration for the requested tree.
123
     */
124
    public static function getSolrConfigurationFromPageId($pageId, $initializeTsfe = false, $language = 0)
125
    {
126
        trigger_error('Method getSolrConfigurationFromPageId is deprecated since EXT:solr 11 and will be removed in v12, use FrontendEnvironment directly.', E_USER_DEPRECATED);
127
        if ($initializeTsfe === true) {
128
            GeneralUtility::makeInstance(FrontendEnvironment::class)->initializeTsfe($pageId, $language);
129
        }
130
        return GeneralUtility::makeInstance(FrontendEnvironment::class)->getSolrConfigurationFromPageId($pageId, $language);
131
    }
132
133
    /**
134
     * Loads the TypoScript configuration for a given page id and language.
135
     * Language usage may be disabled to get the default TypoScript
136 159
     * configuration.
137
     *
138 159
     * @param int $pageId Id of the (root) page to get the Solr configuration from.
139
     * @param string $path The TypoScript configuration path to retrieve.
140 159
     * @param bool $initializeTsfe
141 159
     * @param int $language System language uid, optional, defaults to 0
142 159
     * @param bool $useTwoLevelCache Flag to enable the two level cache for the typoscript configuration array
143 71
     * @return TypoScriptConfiguration The Solr configuration for the requested tree.
144
     */
145
    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

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

165
    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...
166
    {
167 157
        trigger_error('Method initializeTsfe is deprecated since EXT:solr 11 and will be removed in v12, use FrontendEnvironment directly.', E_USER_DEPRECATED);
168 157
        GeneralUtility::makeInstance(FrontendEnvironment::class)->initializeTsfe($pageId, $language);
169
    }
170
171 157
172
    /**
173
     * Check if record ($table, $uid) is a workspace record
174
     *
175
     * @param string $table The table the record belongs to
176
     * @param int $uid The record's uid
177
     * @return bool TRUE if the record is in a draft workspace, FALSE if it's a LIVE record
178
     */
179
    public static function isDraftRecord($table, $uid)
180
    {
181 159
        $isWorkspaceRecord = false;
182
183 159
        if ((ExtensionManagementUtility::isLoaded('workspaces')) && (BackendUtility::isTableWorkspaceEnabled($table))) {
184 159
            $record = BackendUtility::getRecord($table, $uid, 'pid, t3ver_state');
185
186
            if ($record['pid'] == '-1' || $record['t3ver_state'] > 0) {
187
                $isWorkspaceRecord = true;
188
            }
189
        }
190 159
191
        return $isWorkspaceRecord;
192
    }
193
194
    /**
195
     * Check if the page type of a page record is allowed
196
     *
197
     * @param array $pageRecord The pages database row
198
     * @param string $configurationName The name of the configuration to use.
199
     *
200
     * @return bool TRUE if the page type is allowed, otherwise FALSE
201
     */
202 157
    public static function isAllowedPageType(array $pageRecord, $configurationName = 'pages')
203
    {
204 157
        trigger_error('Method isAllowedPageType is deprecated since EXT:solr 11 and will be removed in v12, use FrontendEnvironment directly.', E_USER_DEPRECATED);
205 19
        return GeneralUtility::makeInstance(FrontendEnvironment::class)->isAllowedPageType($pageRecord, $configurationName);
206 19
    }
207
208 157
    /**
209
     * Get allowed page types
210
     *
211 157
     * @param int $pageId Page ID
212
     * @param string $configurationName The name of the configuration to use.
213
     *
214
     * @return array Allowed page types to compare to a doktype of a page record
215
     */
216
    public static function getAllowedPageTypes($pageId, $configurationName = 'pages')
217
    {
218
        trigger_error('Method getAllowedPageTypes is deprecated since EXT:solr 11 and will be removed in v12, no call required.', E_USER_DEPRECATED);
219
        $rootPath = '';
220
        $configuration = self::getConfigurationFromPageId($pageId, $rootPath);
221
        return $configuration->getIndexQueueAllowedPageTypesArrayByConfigurationName($configurationName);
222
    }
223 159
224
    /**
225 159
     * Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix
226 159
     * is set to "auto".
227
     *
228
     * @param TypoScriptFrontendController $TSFE
229
     * @return string
230
     */
231
    public static function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE)
232
    {
233
        trigger_error('Method getAbsRefPrefixFromTSFE is deprecated since EXT:solr 11 and will be removed in v12, no call required.', E_USER_DEPRECATED);
234
        $absRefPrefix = '';
235
        if (empty($TSFE->config['config']['absRefPrefix'])) {
236 19
            return $absRefPrefix;
237
        }
238
239 19
        $absRefPrefix = trim($TSFE->config['config']['absRefPrefix']);
240 19
        if ($absRefPrefix === 'auto') {
241 19
            $absRefPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
242 19
        }
243
244
        return $absRefPrefix;
245
    }
246
247
248
    /**
249
     * This function can be used to check if one of the strings in needles is
250
     * contained in the haystack.
251
     *
252
     *
253 157
     * Example:
254
     *
255 157
     * haystack: the brown fox
256 157
     * needles: ['hello', 'world']
257
     * result: false
258
     *
259
     * haystack: the brown fox
260 157
     * needles: ['is', 'fox']
261 157
     * result: true
262
     *
263 157
     * @param string $haystack
264 157
     * @param array $needles
265 157
     * @return bool
266 85
     */
267 85
    public static function containsOneOfTheStrings($haystack, array $needles)
268 85
    {
269 85
        foreach ($needles as $needle) {
270 85
            $position = strpos($haystack, $needle);
271 85
            if ($position !== false) {
272 85
                return true;
273 85
            }
274
        }
275
276 85
        return false;
277 85
    }
278
279
    /**
280 157
     * Returns the current language ID from the active context.
281 157
     * @return int
282 157
     */
283 157
    public static function getLanguageUid(): int
284 157
    {
285
        $context = GeneralUtility::makeInstance(Context::class);
286 157
        return (int)$context->getPropertyFromAspect('language', 'id');
287 157
    }
288
289 157
    /**
290 85
     * @return string
291
     */
292 157
    public static function getFrontendUserGroupsList(): string
293 85
    {
294
        return implode(',', self::getFrontendUserGroups());
295 157
    }
296
297
    /**
298
     * @return array
299
     */
300
    public static function getFrontendUserGroups(): array
301
    {
302
        $context = GeneralUtility::makeInstance(Context::class);
303
        return $context->getPropertyFromAspect('frontend.user', 'groupIds');
304
    }
305
306
    /**
307 24
     * @todo This method is just added for compatibility checks for TYPO3 version 9 and will be removed when TYPO9 support is dropped
308
     * @return boolean
309 24
     */
310
    public static function getIsTYPO3VersionBelow10()
311
    {
312 24
        if (!defined('TYPO3_branch')) {
313
            $typo3Version = GeneralUtility::makeInstance(Typo3Version::class);
0 ignored issues
show
Unused Code introduced by
The assignment to $typo3Version is dead and can be removed.
Loading history...
314 24
        }
315
        return (bool)version_compare(TYPO3_branch, '10.0', '<');
316 24
    }
317 24
318
}
319