Passed
Push — release-11.5.x ( 282d0d...a0b867 )
by Rafael
36:25
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, t3ver_oid');
118
119
            // \TYPO3\CMS\Core\Versioning\VersionState for an explanation of the t3ver_state field
120
            // if it is >0, it is a draft record or
121
            // if it is "0" (DEFAULT_STATE), could also be draft if t3ver_oid points to any uid (modified record)
122
            if ($record !== null &&
123
                ($record['pid'] == '-1' || $record['t3ver_state'] > 0 || (int)$record['t3ver_oid'] > 0)
124
            ) {
125
                $isWorkspaceRecord = true;
126
            }
127
        }
128
129 89
        return $isWorkspaceRecord;
130
    }
131
132
    /**
133
     * This function can be used to check if one of the strings in needles is
134
     * contained in the haystack.
135
     *
136
     *
137
     * Example:
138
     *
139
     * haystack: the brown fox
140
     * needles: ['hello', 'world']
141
     * result: false
142
     *
143
     * haystack: the brown fox
144
     * needles: ['is', 'fox']
145
     * result: true
146
     *
147
     * @param string $haystack
148
     * @param array $needles
149
     * @return bool
150
     */
151 64
    public static function containsOneOfTheStrings(string $haystack, array $needles): bool
152
    {
153 64
        foreach ($needles as $needle) {
154 64
            $position = strpos($haystack, $needle);
155 64
            if ($position !== false) {
156 3
                return true;
157
            }
158
        }
159
160 61
        return false;
161
    }
162
163
    /**
164
     * Returns the current language ID from the active context.
165
     * @return int
166
     * @throws AspectNotFoundException
167
     * @todo: Remove all usages of this method for all usages in isolated/capsuled TSFE approach.
168
     */
169 82
    public static function getLanguageUid(): int
170
    {
171 82
        $context = GeneralUtility::makeInstance(Context::class);
172 82
        return (int)$context->getPropertyFromAspect('language', 'id');
173
    }
174
175
    /**
176
     * @return string
177
     * @throws AspectNotFoundException
178
     */
179 7
    public static function getFrontendUserGroupsList(): string
180
    {
181 7
        return implode(',', self::getFrontendUserGroups());
182
    }
183
184
    /**
185
     * @return array
186
     * @throws AspectNotFoundException
187
     */
188 39
    public static function getFrontendUserGroups(): array
189
    {
190 39
        $context = GeneralUtility::makeInstance(Context::class);
191 39
        return $context->getPropertyFromAspect('frontend.user', 'groupIds');
192
    }
193
194
    /**
195
     * Returns the current execution time (formerly known as EXEC_TIME)
196
     * @return int
197
     * @throws AspectNotFoundException
198
     */
199 35
    public static function getExceptionTime(): int
200
    {
201 35
        $context = GeneralUtility::makeInstance(Context::class);
202 35
        return (int)$context->getPropertyFromAspect('date', 'timestamp');
203
    }
204
}
205