Passed
Push — master ( e55157...ed38f6 )
by Timo
27:41
created

Site::getDomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-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\Index\Queue\RecordMonitor\Helper\ConfigurationAwareRecordService;
28
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
29
use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository;
30
use TYPO3\CMS\Backend\Utility\BackendUtility;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * A site is a branch in a TYPO3 installation. Each site's root page is marked
35
 * by the "Use as Root Page" flag.
36
 *
37
 * @author Ingo Renner <[email protected]>
38
 */
39
class Site
40
{
41
42
    /**
43
     * @var TypoScriptConfiguration
44
     */
45
    protected $configuration;
46
47
    /**
48
     * Cache for ApacheSolrForTypo3\Solr\Site objects
49
     *
50
     * @var array
51
     */
52
    protected static $sitesCache = [];
53
54
    /**
55
     * Small cache for the list of pages in a site, so that the results of this
56
     * rather expensive operation can be used by all initializers without having
57
     * each initializer do it again.
58
     *
59
     * TODO Move to caching framework once TYPO3 4.6 is the minimum required
60
     * version.
61
     *
62
     * @var array
63
     */
64
    protected static $sitePagesCache = [];
65
66
    /**
67
     * Root page record.
68
     *
69
     * @var array
70
     */
71
    protected $rootPage = [];
72
73
    /**
74
     * The site's sys_language_mode
75
     *
76
     * @var string
77
     */
78
    protected $sysLanguageMode = null;
79
80
    /**
81
     * @var string
82
     */
83
    protected $domain;
84
85
    /**
86
     * @var string
87
     */
88
    protected $siteHash;
89
90
    /**
91
     * @var PagesRepository
92
     */
93
    protected $pagesRepository;
94
95
    /**
96
     * Constructor.
97
     *
98
     * @param TypoScriptConfiguration $configuration
99
     * @param array $page Site root page ID (uid). The page must be marked as site root ("Use as Root Page" flag).
100
     * @param string $domain The domain record used by this Site
101
     * @param string $siteHash The site hash used by this site
102
     * @param PagesRepository $pagesRepository
103
     */
104 144
    public function __construct(TypoScriptConfiguration $configuration, array $page, $domain, $siteHash, PagesRepository $pagesRepository = null)
105
    {
106 144
        $this->configuration = $configuration;
107 144
        $this->rootPage = $page;
108 144
        $this->domain = $domain;
109 144
        $this->siteHash = $siteHash;
110 144
        $this->pagesRepository = $pagesRepository ?? GeneralUtility::makeInstance(PagesRepository::class);
111 144
    }
112
113
    /**
114
     * Clears the $sitePagesCache
115
     *
116
     */
117
    public static function clearSitePagesCache()
118
    {
119
        self::$sitePagesCache = [];
120
    }
121
122
    /**
123
     * Takes an pagerecord and checks whether the page is marked as root page.
124
     *
125
     * @param array $page pagerecord
126
     * @return bool true if the page is marked as root page, false otherwise
127
     */
128 164
    public static function isRootPage($page)
129
    {
130 164
        if ($page['is_siteroot']) {
131 163
            return true;
132
        }
133
134 85
        return false;
135
    }
136
137
    /**
138
     * Gets the site's root page ID (uid).
139
     *
140
     * @return int The site's root page ID.
141
     */
142 84
    public function getRootPageId()
143
    {
144 84
        return (int)$this->rootPage['uid'];
145
    }
146
147
148
    /**
149
     * Gets the site's root page language IDs (uids).
150
     *
151
     * @return array
152
     */
153 1
    public function getRootPageLanguageIds() : array
154
    {
155 1
        $rootPageLanguageIds = [];
156 1
        $rootPageId = $this->getRootPageId();
157 1
        $rootPageOverlays = $this->pagesRepository->findTranslationOverlaysByPageId($rootPageId);
158 1
        if (count($rootPageOverlays)) {
159 1
            foreach ($rootPageOverlays as $rootPageOverlay) {
160 1
                $rootPageLanguageIds[] = $rootPageOverlay['sys_language_uid'];
161
            }
162
        }
163 1
        return $rootPageLanguageIds;
164
    }
165
166
    /**
167
     * Gets the site's label. The label is build from the the site title and root
168
     * page ID (uid).
169
     *
170
     * @return string The site's label.
171
     */
172 2
    public function getLabel()
173
    {
174 2
        $rootlineTitles = [];
175 2
        $rootLine = BackendUtility::BEgetRootLine($this->rootPage['uid']);
176
        // Remove last
177 2
        array_pop($rootLine);
178 2
        $rootLine = array_reverse($rootLine);
179 2
        foreach ($rootLine as $rootLineItem) {
180 2
            $rootlineTitles[] = $rootLineItem['title'];
181
        }
182 2
        return implode(' - ', $rootlineTitles) . ', Root Page ID: ' . $this->rootPage['uid'];
183
    }
184
185
    /**
186
     * Gets the site's Solr TypoScript configuration (plugin.tx_solr.*)
187
     *
188
     * @return  \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration The Solr TypoScript configuration
189
     */
190 59
    public function getSolrConfiguration()
191
    {
192 59
        return $this->configuration;
193
    }
194
195
    /**
196
     * Gets the site's default language as configured in
197
     * config.sys_language_uid. If sys_language_uid is not set, 0 is assumed to
198
     * be the default.
199
     *
200
     * @return int The site's default language.
201
     */
202 1
    public function getDefaultLanguage()
203
    {
204 1
        $siteDefaultLanguage = 0;
205
206 1
        $configuration = Util::getConfigurationFromPageId(
207 1
            $this->rootPage['uid'],
208 1
            'config'
209
        );
210
211 1
        $siteDefaultLanguage = $configuration->getValueByPathOrDefaultValue('sys_language_uid', $siteDefaultLanguage);
212
        // default language is set through default L GET parameter -> overruling config.sys_language_uid
213 1
        $siteDefaultLanguage = $configuration->getValueByPathOrDefaultValue('defaultGetVars.L', $siteDefaultLanguage);
214
215 1
        return $siteDefaultLanguage;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $siteDefaultLanguage also could return the type array which is incompatible with the documented return type integer.
Loading history...
216
    }
217
218
    /**
219
     * Generates a list of page IDs in this site. Attention, this includes
220
     * all page types! Deleted pages are not included.
221
     *
222
     * @param int|string $rootPageId Page ID from where to start collection sub pages
223
     * @param int $maxDepth Maximum depth to descend into the site tree
224
     * @return array Array of pages (IDs) in this site
225
     */
226 12
    public function getPages($rootPageId = 'SITE_ROOT', $maxDepth = 999)
227
    {
228 12
        $pageIds = [];
229 12
        if ($rootPageId === 'SITE_ROOT') {
230 12
            $rootPageId = (int)$this->rootPage['uid'];
231 12
            $pageIds[] = $rootPageId;
232
        }
233
234 12
        $configurationAwareRecordService = GeneralUtility::makeInstance(ConfigurationAwareRecordService::class);
235
        // Fetch configuration in order to be able to read initialPagesAdditionalWhereClause
236 12
        $solrConfiguration = $this->getSolrConfiguration();
237 12
        $indexQueueConfigurationName = $configurationAwareRecordService->getIndexingConfigurationName('pages', $this->rootPage['uid'], $solrConfiguration);
238 12
        $initialPagesAdditionalWhereClause = $solrConfiguration->getInitialPagesAdditionalWhereClause($indexQueueConfigurationName);
239
240 12
        return array_merge($pageIds, $this->pagesRepository->findAllSubPageIdsByRootPage($rootPageId, $maxDepth, $initialPagesAdditionalWhereClause));
0 ignored issues
show
Bug introduced by
It seems like $rootPageId can also be of type string; however, parameter $rootPageId of ApacheSolrForTypo3\Solr\...lSubPageIdsByRootPage() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

240
        return array_merge($pageIds, $this->pagesRepository->findAllSubPageIdsByRootPage(/** @scrutinizer ignore-type */ $rootPageId, $maxDepth, $initialPagesAdditionalWhereClause));
Loading history...
241
    }
242
243
    /**
244
     * Generates the site's unique Site Hash.
245
     *
246
     * The Site Hash is build from the site's main domain, the system encryption
247
     * key, and the extension "tx_solr". These components are concatenated and
248
     * sha1-hashed.
249
     *
250
     * @return string Site Hash.
251
     */
252 89
    public function getSiteHash()
253
    {
254 89
        return $this->siteHash;
255
    }
256
257
    /**
258
     * Gets the site's main domain. More specifically the first domain record in
259
     * the site tree.
260
     *
261
     * @return string The site's main domain.
262
     */
263 94
    public function getDomain()
264
    {
265 94
        return $this->domain;
266
    }
267
268
    /**
269
     * Gets the site's root page.
270
     *
271
     * @return array The site's root page.
272
     */
273 22
    public function getRootPage()
274
    {
275 22
        return $this->rootPage;
276
    }
277
278
    /**
279
     * Gets the site's root page's title.
280
     *
281
     * @return string The site's root page's title
282
     */
283
    public function getTitle()
284
    {
285
        return $this->rootPage['title'];
286
    }
287
288
    /**
289
     * Gets the site's config.sys_language_mode setting
290
     *
291
     * @param int $languageUid
292
     *
293
     * @return string The site's config.sys_language_mode
294
     */
295 21
    public function getSysLanguageMode($languageUid = 0)
296
    {
297 21
        if (!is_null($this->sysLanguageMode)) {
0 ignored issues
show
introduced by
The condition is_null($this->sysLanguageMode) is always false.
Loading history...
298 3
            return $this->sysLanguageMode;
299
        }
300
301
        try {
302 21
            Util::initializeTsfe($this->getRootPageId(), $languageUid);
303 21
            $this->sysLanguageMode = $GLOBALS['TSFE']->sys_language_mode;
304 21
            return $this->sysLanguageMode;
305
306
        } catch (\TYPO3\CMS\Core\Error\Http\ServiceUnavailableException $e) {
307
            // when there is an error during initialization we return the default sysLanguageMode
308
            return $this->sysLanguageMode;
309
        }
310
    }
311
312
    /**
313
     * Retrieves the rootPageIds as an array from a set of sites.
314
     *
315
     * @param array $sites
316
     * @return array
317
     */
318 41
    public static function getRootPageIdsFromSites(array $sites): array
319
    {
320 41
        $rootPageIds = [];
321 41
        foreach ($sites as $site) {
322 6
            $rootPageIds[] = (int)$site->getRootPageId();
323
        }
324
325 41
        return $rootPageIds;
326
    }
327
}
328