Issues (202)

Classes/Util.php (1 issue)

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 66
     * @param int $uid The page's uid
63
     * @param int $typeNum The page's typeNum
64 66
     * @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 66
     * @param string $mountPointParameter The mount point parameter that is used to access the page.
67 2
     * @return string The document id for that page
68
     */
69
    public static function getPageDocumentId($uid, $typeNum = 0, $language = 0, $accessGroups = '0,-1', $mountPointParameter = '')
70 66
    {
71
        $additionalParameters = $typeNum . '/' . $language . '/' . $accessGroups;
72 66
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
     * Generates a document id in the form $siteHash/$type/$uid.
84 85
     *
85
     * @param string $table The records table name
86 85
     * @param int $rootPageId The record's site root id
87 85
     * @param int $uid The record's uid
88 85
     * @param string $additionalIdParameters Additional ID parameters
89
     * @return string A document id
90 85
     */
91 85
    public static function getDocumentId($table, $rootPageId, $uid, $additionalIdParameters = '')
92 66
    {
93
            /** @var SiteRepository $siteRepository */
94
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
95 85
        $site = $siteRepository->getSiteByPageId($rootPageId);
96
        $siteHash = $site->getSiteHash();
97
98
        $documentId = $siteHash . '/' . $table . '/' . $uid;
99
        if (!empty($additionalIdParameters)) {
100
            $documentId .= '/' . $additionalIdParameters;
101
        }
102
103
        return $documentId;
104 175
    }
105
106 175
    /**
107 175
     * Shortcut to retrieve the TypoScript configuration for EXT:solr
108
     *
109
     * @return TypoScriptConfiguration
110
     */
111
    public static function getSolrConfiguration()
112
    {
113
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
114
        return $configurationManager->getTypoScriptConfiguration();
115
    }
116
117
    /**
118
     * Gets the Solr configuration for a specific root page id.
119 151
     * To be used from the backend.
120
     *
121 151
     * @param int $pageId Id of the (root) page to get the Solr configuration from.
122 151
     * @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 152
     * Loads the TypoScript configuration for a given page id and language.
138
     * Language usage may be disabled to get the default TypoScript
139 152
     * configuration.
140
     *
141 152
     * @param int $pageId Id of the (root) page to get the Solr configuration from.
142 152
     * @param string $path The TypoScript configuration path to retrieve.
143 152
     * @param bool $initializeTsfe
144 69
     * @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
    public static function getConfigurationFromPageId($pageId, $path, $initializeTsfe = false, $language = 0, $useTwoLevelCache = true)
150 152
    {
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
        if ($initializeTsfe === true) {
153
            GeneralUtility::makeInstance(FrontendEnvironment::class)->initializeTsfe($pageId, $language);
154 151
        }
155
        return GeneralUtility::makeInstance(FrontendEnvironment::class)->getConfigurationFromPageId($pageId, $path, $language);
156 151
    }
157 151
158
    /**
159
     * Initializes the TSFE for a given page ID and language.
160 151
     *
161
     * @param $pageId
162
     * @param int $language
163
     * @param bool $useCache
164
     * @deprecated
165
     * @throws SiteNotFoundException
166 151
     * @throws \TYPO3\CMS\Core\Error\Http\ServiceUnavailableException
167
     * @throws \TYPO3\CMS\Core\Http\ImmediateResponseException
168 151
     */
169 151
    public static function initializeTsfe($pageId, $language = 0, $useCache = true)
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 151
        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 152
    public static function isDraftRecord($table, $uid)
183
    {
184 152
        $isWorkspaceRecord = false;
185 152
186
        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 152
            }
192
        }
193
194
        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 151
     *
204
     * @return bool TRUE if the page type is allowed, otherwise FALSE
205 151
     */
206 7
    public static function isAllowedPageType(array $pageRecord, $configurationName = 'pages')
207 6
    {
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 151
        return GeneralUtility::makeInstance(FrontendEnvironment::class)->isAllowedPageType($pageRecord, $configurationName);
210
    }
211
212 151
    /**
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 152
        $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 152
    }
227 152
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 6
    {
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 6
        if (empty($TSFE->config['config']['absRefPrefix'])) {
241 6
            return $absRefPrefix;
242 6
        }
243 6
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 151
     * This function can be used to check if one of the strings in needles is
254
     * contained in the haystack.
255 151
     *
256 151
     *
257
     * Example:
258
     *
259
     * haystack: the brown fox
260 151
     * needles: ['hello', 'world']
261 151
     * result: false
262
     *
263 151
     * haystack: the brown fox
264 151
     * needles: ['is', 'fox']
265 151
     * result: true
266 81
     *
267 81
     * @param string $haystack
268 81
     * @param array $needles
269 81
     * @return bool
270 81
     */
271 81
    public static function containsOneOfTheStrings($haystack, array $needles)
272 81
    {
273 81
        foreach ($needles as $needle) {
274
            $position = strpos($haystack, $needle);
275
            if ($position !== false) {
276 81
                return true;
277 81
            }
278
        }
279
280 151
        return false;
281 151
    }
282 151
283 151
    /**
284 151
     * Returns the current language ID from the active context.
285
     * @return int
286 151
     */
287 151
    public static function getLanguageUid(): int
288
    {
289 151
        $context = GeneralUtility::makeInstance(Context::class);
290 81
        return (int)$context->getPropertyFromAspect('language', 'id');
291
    }
292 151
293 81
    /**
294
     * @return string
295 151
     */
296
    public static function getFrontendUserGroupsList(): string
297
    {
298
        return implode(',', self::getFrontendUserGroups());
299
    }
300
301
    /**
302
     * @return array
303
     */
304
    public static function getFrontendUserGroups(): array
305
    {
306 24
        $context = GeneralUtility::makeInstance(Context::class);
307
        return $context->getPropertyFromAspect('frontend.user', 'groupIds');
308 24
    }
309
310
    /**
311 24
     * @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 24
     */
314
    public static function getIsTYPO3VersionBelow10()
315 24
    {
316 24
        return GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 10;
317
    }
318
319
}
320