Passed
Push — master ( cce3a3...bd1cec )
by Timo
44:23
created

Site::getRootPageLanguageIds()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 0
crap 3
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 137
    public function __construct(TypoScriptConfiguration $configuration, array $page, $domain, $siteHash, PagesRepository $pagesRepository = null)
105
    {
106 137
        $this->configuration = $configuration;
107 137
        $this->rootPage = $page;
108 137
        $this->domain = $domain;
109 137
        $this->siteHash = $siteHash;
110 137
        $this->pagesRepository = $pagesRepository ?? GeneralUtility::makeInstance(PagesRepository::class);
111 137
    }
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 157
    public static function isRootPage($page)
129
    {
130 157
        if ($page['is_siteroot']) {
131 156
            return true;
132
        }
133
134 83
        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 50
    public function getRootPageId()
143
    {
144 50
        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 14
    public function getRootPageLanguageIds() : array
154
    {
155 14
        $rootPageLanguageIds = [];
156 14
        $rootPageId = $this->getRootPageId();
157
        $rootPageOverlays = $this->pagesRepository->findTranslationOverlaysByPageId($rootPageId);
158 14
        if (count($rootPageOverlays)) {
159 14
            foreach ($rootPageOverlays as $rootPageOverlay) {
160 14
                $rootPageLanguageIds[] = $rootPageOverlay['sys_language_uid'];
161 14
            }
162
        }
163 14
        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 58
     */
172
    public function getLabel()
173 58
    {
174
        $rootlineTitles = [];
175
        $rootLine = BackendUtility::BEgetRootLine($this->rootPage['uid']);
176
        // Remove last
177
        array_pop($rootLine);
178
        $rootLine = array_reverse($rootLine);
179
        foreach ($rootLine as $rootLineItem) {
180
            $rootlineTitles[] = $rootLineItem['title'];
181
        }
182
        return implode(' - ', $rootlineTitles) . ', Root Page ID: ' . $this->rootPage['uid'];
183 1
    }
184
185 1
    /**
186
     * Gets the site's Solr TypoScript configuration (plugin.tx_solr.*)
187 1
     *
188 1
     * @return  \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration The Solr TypoScript configuration
189 1
     */
190
    public function getSolrConfiguration()
191
    {
192 1
        return $this->configuration;
193
    }
194 1
195
    /**
196 1
     * 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
    public function getDefaultLanguage()
203
    {
204
        $siteDefaultLanguage = 0;
205
206
        $configuration = Util::getConfigurationFromPageId(
207 12
            $this->rootPage['uid'],
208
            'config'
209 12
        );
210 12
211 12
        $siteDefaultLanguage = $configuration->getValueByPathOrDefaultValue('sys_language_uid', $siteDefaultLanguage);
212 12
        // default language is set through default L GET parameter -> overruling config.sys_language_uid
213
        $siteDefaultLanguage = $configuration->getValueByPathOrDefaultValue('defaultGetVars.L', $siteDefaultLanguage);
214
215 12
        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 12
218 12
    /**
219 12
     * Generates a list of page IDs in this site. Attention, this includes
220
     * all page types! Deleted pages are not included.
221 12
     *
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
    public function getPages($rootPageId = 'SITE_ROOT', $maxDepth = 999)
227
    {
228
        $pageIds = [];
229
        if ($rootPageId === 'SITE_ROOT') {
230
            $rootPageId = (int)$this->rootPage['uid'];
231
            $pageIds[] = $rootPageId;
232
        }
233 86
234
        $configurationAwareRecordService = GeneralUtility::makeInstance(ConfigurationAwareRecordService::class);
235 86
        // Fetch configuration in order to be able to read initialPagesAdditionalWhereClause
236
        $solrConfiguration = $this->getSolrConfiguration();
237
        $indexQueueConfigurationName = $configurationAwareRecordService->getIndexingConfigurationName('pages', $this->rootPage['uid'], $solrConfiguration);
238
        $initialPagesAdditionalWhereClause = $solrConfiguration->getInitialPagesAdditionalWhereClause($indexQueueConfigurationName);
239
240
        return array_merge($pageIds, $this->pagesRepository->findAllSubPageIdsByRootPage($rootPageId, $maxDepth, $initialPagesAdditionalWhereClause));
241
    }
242
243
    /**
244 91
     * Generates the site's unique Site Hash.
245
     *
246 91
     * 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
    public function getSiteHash()
253
    {
254 21
        return $this->siteHash;
255
    }
256 21
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
    public function getDomain()
264
    {
265
        return $this->domain;
266
    }
267
268
    /**
269
     * Gets the site's root page.
270
     *
271
     * @return array The site's root page.
272
     */
273
    public function getRootPage()
274
    {
275
        return $this->rootPage;
276 20
    }
277
278 20
    /**
279 20
     * Gets the site's root page's title.
280 20
     *
281
     * @return string The site's root page's title
282
     */
283 20
    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 40
     *
293
     * @return string The site's config.sys_language_mode
294 40
     */
295 40
    public function getSysLanguageMode($languageUid = 0)
296 6
    {
297
        if (is_null($this->sysLanguageMode)) {
0 ignored issues
show
introduced by
The condition is_null($this->sysLanguageMode) is always false.
Loading history...
298
            Util::initializeTsfe($this->getRootPageId(), $languageUid);
299 40
            $this->sysLanguageMode = $GLOBALS['TSFE']->sys_language_mode;
300
        }
301
302
        return $this->sysLanguageMode;
303
    }
304
305
    /**
306
     * Retrieves the rootPageIds as an array from a set of sites.
307
     *
308
     * @param array $sites
309
     * @return array
310
     */
311
    public static function getRootPageIdsFromSites(array $sites): array
312
    {
313
        $rootPageIds = [];
314
        foreach ($sites as $site) {
315
            $rootPageIds[] = (int)$site->getRootPageId();
316
        }
317
318
        return $rootPageIds;
319
    }
320
}
321