Passed
Push — master ( ab955c...03080d )
by Timo
10:19
created

Util::initializeTsfe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 4
rs 10
ccs 1
cts 3
cp 0.3333
cc 1
nc 1
nop 3
crap 1.2963
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 68
     * @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 68
     * @param string $mountPointParameter The mount point parameter that is used to access the page.
67
     * @return string The document id for that page
68 68
     */
69 2
    public static function getPageDocumentId($uid, $typeNum = 0, $language = 0, $accessGroups = '0,-1', $mountPointParameter = '')
70
    {
71
        $additionalParameters = $typeNum . '/' . $language . '/' . $accessGroups;
72 68
73
        if ((string)$mountPointParameter !== '') {
74 68
            $additionalParameters = $mountPointParameter . '/' . $additionalParameters;
75
        }
76
77
        $documentId = self::getDocumentId('pages', $uid, $uid, $additionalParameters);
78
79
        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 86
     * @param int $rootPageId The record's site root id
87
     * @param int $uid The record's uid
88 86
     * @param string $additionalIdParameters Additional ID parameters
89 86
     * @return string A document id
90 86
     */
91
    public static function getDocumentId($table, $rootPageId, $uid, $additionalIdParameters = '')
92 86
    {
93 86
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
94 68
        $site = $siteRepository->getSiteByPageId($rootPageId);
95
        $siteHash = $site->getSiteHash();
96
97 86
        $documentId = $siteHash . '/' . $table . '/' . $uid;
98
        if (!empty($additionalIdParameters)) {
99
            $documentId .= '/' . $additionalIdParameters;
100
        }
101
102
        return $documentId;
103
    }
104
105
    /**
106 196
     * Shortcut to retrieve the TypoScript configuration for EXT:solr
107
     *
108 196
     * @return TypoScriptConfiguration
109 196
     */
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
     * To be used from the backend.
119
     *
120
     * @param int $pageId Id of the (root) page to get the Solr configuration from.
121 168
     * @param int $language System language uid, optional, defaults to 0
122
     * @return TypoScriptConfiguration The Solr configuration for the requested tree.
123 168
     */
124 168
    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
     * configuration.
137
     *
138
     * @param int $pageId Id of the (root) page to get the Solr configuration from.
139 172
     * @param string $path The TypoScript configuration path to retrieve.
140
     * @param bool $initializeTsfe
141 172
     * @param int $language System language uid, optional, defaults to 0
142
     * @param bool $useTwoLevelCache Flag to enable the two level cache for the typoscript configuration array
143 172
     * @return TypoScriptConfiguration The Solr configuration for the requested tree.
144 172
     */
145 172
    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 71
    {
147 5
        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 71
            GeneralUtility::makeInstance(FrontendEnvironment::class)->initializeTsfe($pageId, $language);
150
        }
151
        return GeneralUtility::makeInstance(FrontendEnvironment::class)->getConfigurationFromPageId($pageId, $path, $language);
152
    }
153
154
155 172
    /**
156 3
     * Initializes the TSFE for a given page ID and language.
157
     *
158
     * @param $pageId
159 170
     * @param int $language
160
     * @param bool $useCache
161 170
     * @throws SiteNotFoundException
162 170
     * @throws \TYPO3\CMS\Core\Error\Http\ServiceUnavailableException
163
     * @throws \TYPO3\CMS\Core\Http\ImmediateResponseException
164
     */
165 170
    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
        trigger_error('Method initializeTsfe is deprecated since EXT:solr 11 and will be removed in v12, use FrontendEnvironment directly.', E_USER_DEPRECATED);
168
        GeneralUtility::makeInstance(FrontendEnvironment::class)->initializeTsfe($pageId, $language);
169
    }
170
171
172
    /**
173
     * Check if record ($table, $uid) is a workspace record
174 170
     *
175
     * @param string $table The table the record belongs to
176 170
     * @param int $uid The record's uid
177 170
     * @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 170
    {
181
        $isWorkspaceRecord = false;
182
183
        if ((ExtensionManagementUtility::isLoaded('workspaces')) && (BackendUtility::isTableWorkspaceEnabled($table))) {
184
            $record = BackendUtility::getRecord($table, $uid, 'pid, t3ver_state');
185
186
            if ($record['pid'] == '-1' || $record['t3ver_state'] > 0) {
187
                $isWorkspaceRecord = true;
188
            }
189
        }
190 172
191
        return $isWorkspaceRecord;
192 172
    }
193 172
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 172
     *
200
     * @return bool TRUE if the page type is allowed, otherwise FALSE
201
     */
202
    public static function isAllowedPageType(array $pageRecord, $configurationName = 'pages')
203
    {
204
        trigger_error('Method isAllowedPageType is deprecated since EXT:solr 11 and will be removed in v12, use FrontendEnvironment directly.', E_USER_DEPRECATED);
205
        return GeneralUtility::makeInstance(FrontendEnvironment::class)->isAllowedPageType($pageRecord, $configurationName);
206
    }
207
208
    /**
209
     * Get allowed page types
210
     *
211 170
     * @param int $pageId Page ID
212
     * @param string $configurationName The name of the configuration to use.
213 170
     *
214 19
     * @return array Allowed page types to compare to a doktype of a page record
215 19
     */
216
    public static function getAllowedPageTypes($pageId, $configurationName = 'pages')
217 168
    {
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 170
        $configuration = self::getConfigurationFromPageId($pageId, $rootPath);
221
        return $configuration->getIndexQueueAllowedPageTypesArrayByConfigurationName($configurationName);
222
    }
223
224
    /**
225
     * Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix
226
     * is set to "auto".
227
     *
228
     * @param TypoScriptFrontendController $TSFE
229
     * @return string
230
     */
231
    public static function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE)
232 172
    {
233
        trigger_error('Method getAbsRefPrefixFromTSFE is deprecated since EXT:solr 11 and will be removed in v12, no call required.', E_USER_DEPRECATED);
234 172
        $absRefPrefix = '';
235 172
        if (empty($TSFE->config['config']['absRefPrefix'])) {
236
            return $absRefPrefix;
237
        }
238
239
        $absRefPrefix = trim($TSFE->config['config']['absRefPrefix']);
240
        if ($absRefPrefix === 'auto') {
241
            $absRefPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
242
        }
243
244
        return $absRefPrefix;
245 19
    }
246
247
248 19
    /**
249 19
     * This function can be used to check if one of the strings in needles is
250 19
     * contained in the haystack.
251 19
     *
252
     *
253
     * Example:
254
     *
255
     * haystack: the brown fox
256
     * needles: ['hello', 'world']
257
     * result: false
258
     *
259
     * haystack: the brown fox
260
     * needles: ['is', 'fox']
261
     * result: true
262 168
     *
263
     * @param string $haystack
264 168
     * @param array $needles
265 168
     * @return bool
266
     */
267
    public static function containsOneOfTheStrings($haystack, array $needles)
268
    {
269 168
        foreach ($needles as $needle) {
270
            $position = strpos($haystack, $needle);
271 168
            if ($position !== false) {
272
                return true;
273 168
            }
274
        }
275
276
        return false;
277
    }
278 168
279 168
    /**
280 168
     * Returns the current language ID from the active context.
281 93
     * @return int
282 93
     */
283 93
    public static function getLanguageUid(): int
284 93
    {
285 93
        $context = GeneralUtility::makeInstance(Context::class);
286 93
        return (int)$context->getPropertyFromAspect('language', 'id');
287 93
    }
288 93
289
    /**
290
     * @return string
291 93
     */
292 93
    public static function getFrontendUserGroupsList(): string
293
    {
294
        return implode(',', self::getFrontendUserGroups());
295 168
    }
296 168
297 168
    /**
298 168
     * @return array
299 168
     */
300
    public static function getFrontendUserGroups(): array
301 168
    {
302 168
        $context = GeneralUtility::makeInstance(Context::class);
303
        return $context->getPropertyFromAspect('frontend.user', 'groupIds');
304 168
    }
305 93
306
    /**
307 168
     * @todo This method is just added for compatibility checks for TYPO3 version 9 and will be removed when TYPO9 support is dropped
308 93
     * @return boolean
309
     */
310 168
    public static function getIsTYPO3VersionBelow10()
311
    {
312
        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
        }
315
        return (bool)version_compare(TYPO3_branch, '10.0', '<');
316
    }
317
318
}
319