Passed
Pull Request — main (#3532)
by Markus
44:05
created

Util::getExceptionTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr;
19
20
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
21
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager;
22
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
23
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
24
use TYPO3\CMS\Backend\Utility\BackendUtility;
25
use TYPO3\CMS\Core\Context\Context;
26
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
27
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * Utility class for tx_solr
32
 *
33
 * @author Ingo Renner <[email protected]>
34
 * (c) 2009-2015 Ingo Renner <[email protected]>
35
 */
36
class Util
37
{
38
    /**
39
     * Generates a document id for documents representing page records.
40
     *
41
     * @param int $uid The page's uid
42
     * @param int $typeNum The page's typeNum
43
     * @param int $language the language id, defaults to 0
44
     * @param string $accessGroups comma separated list of uids of groups that have access to that page
45
     * @param string $mountPointParameter The mount point parameter that is used to access the page.
46
     * @return string The document id for that page
47
     * @throws DBALDriverException
48
     */
49 69
    public static function getPageDocumentId(
50
        int $uid,
51
        int $typeNum = 0,
52
        int $language = 0,
53
        string $accessGroups = '0,-1',
54
        string $mountPointParameter = ''
55
    ): string {
56 69
        $additionalParameters = $typeNum . '/' . $language . '/' . $accessGroups;
57
58 69
        if ($mountPointParameter !== '') {
59 2
            $additionalParameters = $mountPointParameter . '/' . $additionalParameters;
60
        }
61
62 69
        return self::getDocumentId('pages', $uid, $uid, $additionalParameters);
63
    }
64
65
    /**
66
     * Generates a document id in the form $siteHash/$type/$uid.
67
     *
68
     * @param string $table The record's table name
69
     * @param int $rootPageId The record's site root id
70
     * @param int $uid The record's uid
71
     * @param string $additionalIdParameters Additional ID parameters
72
     * @return string A document id
73
     * @throws DBALDriverException
74
     */
75 89
    public static function getDocumentId(
76
        string $table,
77
        int $rootPageId,
78
        int $uid,
79
        string $additionalIdParameters = ''
80
    ): string {
81
        /** @var SiteRepository $siteRepository */
82 89
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
83 89
        $site = $siteRepository->getSiteByPageId($rootPageId);
84 89
        $siteHash = $site->getSiteHash();
85
86 89
        $documentId = $siteHash . '/' . $table . '/' . $uid;
87 89
        if (!empty($additionalIdParameters)) {
88 69
            $documentId .= '/' . $additionalIdParameters;
89
        }
90
91 89
        return $documentId;
92
    }
93
94
    /**
95
     * Shortcut to retrieve the TypoScript configuration for EXT:solr
96
     *
97
     * @return TypoScriptConfiguration
98
     */
99 216
    public static function getSolrConfiguration(): TypoScriptConfiguration
100
    {
101 216
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
102 216
        return $configurationManager->getTypoScriptConfiguration();
103
    }
104
105
    /**
106
     * Check if record ($table, $uid) is a workspace record
107
     *
108
     * @param string $table The table the record belongs to
109
     * @param int $uid The record's uid
110
     * @return bool TRUE if the record is in a draft workspace, FALSE if it's a LIVE record
111
     */
112 89
    public static function isDraftRecord(string $table, int $uid): bool
113
    {
114 89
        $isWorkspaceRecord = false;
115
116 89
        if ((ExtensionManagementUtility::isLoaded('workspaces')) && (BackendUtility::isTableWorkspaceEnabled($table))) {
117
            $record = BackendUtility::getRecord($table, $uid, 'pid, t3ver_state');
118
119
            if ($record !== null && ($record['pid'] == '-1' || $record['t3ver_state'] > 0)) {
120
                $isWorkspaceRecord = true;
121
            }
122
        }
123
124 89
        return $isWorkspaceRecord;
125
    }
126
127
    /**
128
     * This function can be used to check if one of the strings in needles is
129
     * contained in the haystack.
130
     *
131
     *
132
     * Example:
133
     *
134
     * haystack: the brown fox
135
     * needles: ['hello', 'world']
136
     * result: false
137
     *
138
     * haystack: the brown fox
139
     * needles: ['is', 'fox']
140
     * result: true
141
     *
142
     * @param string $haystack
143
     * @param array $needles
144
     * @return bool
145
     */
146 64
    public static function containsOneOfTheStrings(string $haystack, array $needles): bool
147
    {
148 64
        foreach ($needles as $needle) {
149 64
            $position = strpos($haystack, $needle);
150 64
            if ($position !== false) {
151 3
                return true;
152
            }
153
        }
154
155 61
        return false;
156
    }
157
158
    /**
159
     * Returns the current language ID from the active context.
160
     * @return int
161
     * @throws AspectNotFoundException
162
     * @todo: Remove all usages of this method for all usages in isolated/capsuled TSFE approach.
163
     */
164 82
    public static function getLanguageUid(): int
165
    {
166 82
        $context = GeneralUtility::makeInstance(Context::class);
167 82
        return (int)$context->getPropertyFromAspect('language', 'id');
168
    }
169
170
    /**
171
     * @return string
172
     * @throws AspectNotFoundException
173
     */
174 7
    public static function getFrontendUserGroupsList(): string
175
    {
176 7
        return implode(',', self::getFrontendUserGroups());
177
    }
178
179
    /**
180
     * @return array
181
     * @throws AspectNotFoundException
182
     */
183 39
    public static function getFrontendUserGroups(): array
184
    {
185 39
        $context = GeneralUtility::makeInstance(Context::class);
186 39
        return $context->getPropertyFromAspect('frontend.user', 'groupIds');
187
    }
188
189
    /**
190
     * Returns the current execution time (formerly known as EXEC_TIME)
191
     * @return int
192
     * @throws AspectNotFoundException
193
     */
194 35
    public static function getExceptionTime(): int
195
    {
196 35
        $context = GeneralUtility::makeInstance(Context::class);
197 35
        return (int)$context->getPropertyFromAspect('date', 'timestamp');
198
    }
199
}
200