Passed
Push — master ( ed38f6...30aafa )
by Timo
11:15
created

Site::isRootPage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Site;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2011-2015 Ingo Renner <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\ConfigurationAwareRecordService;
29
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
30
use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository;
31
use ApacheSolrForTypo3\Solr\Util;
32
use TYPO3\CMS\Backend\Utility\BackendUtility;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35
class Site
36
{
37
38
    /**
39
     * @var TypoScriptConfiguration
40
     */
41
    protected $configuration;
42
43
    /**
44
     * Cache for ApacheSolrForTypo3\Solr\Site objects
45
     *
46
     * @var array
47
     */
48
    protected static $sitesCache = [];
49
50
    /**
51
     * Small cache for the list of pages in a site, so that the results of this
52
     * rather expensive operation can be used by all initializers without having
53
     * each initializer do it again.
54
     *
55
     * TODO Move to caching framework once TYPO3 4.6 is the minimum required
56
     * version.
57
     *
58
     * @var array
59
     */
60
    protected static $sitePagesCache = [];
61
62
    /**
63
     * Root page record.
64
     *
65
     * @var array
66
     */
67
    protected $rootPage = [];
68
69
    /**
70
     * The site's sys_language_mode
71
     *
72
     * @var string
73
     */
74
    protected $sysLanguageMode = null;
75
76
    /**
77
     * @var string
78
     */
79
    protected $domain;
80
81
    /**
82
     * @var string
83
     */
84
    protected $siteHash;
85
86
    /**
87
     * @var PagesRepository
88
     */
89
    protected $pagesRepository;
90
91
    /**
92
     * @var int
93
     */
94
    protected $defaultLanguageId = 0;
95
96
    /**
97
     * Constructor.
98
     *
99
     * @param TypoScriptConfiguration $configuration
100
     * @param array $page Site root page ID (uid). The page must be marked as site root ("Use as Root Page" flag).
101
     * @param string $domain The domain record used by this Site
102
     * @param string $siteHash The site hash used by this site
103
     * @param PagesRepository $pagesRepository
104
     * @param int $defaultLanguageId
105
     */
106
    public function __construct(TypoScriptConfiguration $configuration, array $page, $domain, $siteHash, PagesRepository $pagesRepository = null, $defaultLanguageId = 0)
107
    {
108
        $this->configuration = $configuration;
109
        $this->rootPage = $page;
110
        $this->domain = $domain;
111
        $this->siteHash = $siteHash;
112
        $this->pagesRepository = $pagesRepository ?? GeneralUtility::makeInstance(PagesRepository::class);
113
        $this->defaultLanguageId = $defaultLanguageId;
114
    }
115
116
    /**
117
     * Clears the $sitePagesCache
118
     *
119
     */
120
    public static function clearSitePagesCache()
121
    {
122
        self::$sitePagesCache = [];
123
    }
124
125
    /**
126
     * Takes an pagerecord and checks whether the page is marked as root page.
127
     *
128
     * @param array $page pagerecord
129
     * @return bool true if the page is marked as root page, false otherwise
130
     */
131
    public static function isRootPage($page)
132
    {
133
        if ($page['is_siteroot']) {
134
            return true;
135
        }
136
137
        return false;
138
    }
139
140
    /**
141
     * Gets the site's root page ID (uid).
142
     *
143
     * @return int The site's root page ID.
144
     */
145
    public function getRootPageId()
146
    {
147
        return (int)$this->rootPage['uid'];
148
    }
149
150
151
    /**
152
     * Gets the site's root page language IDs (uids).
153
     *
154
     * @return array
155
     */
156
    public function getRootPageLanguageIds() : array
157
    {
158
        $rootPageLanguageIds = [];
159
        $rootPageId = $this->getRootPageId();
160
        $rootPageOverlays = $this->pagesRepository->findTranslationOverlaysByPageId($rootPageId);
161
        if (count($rootPageOverlays)) {
162
            foreach ($rootPageOverlays as $rootPageOverlay) {
163
                $rootPageLanguageIds[] = $rootPageOverlay['sys_language_uid'];
164
            }
165
        }
166
        return $rootPageLanguageIds;
167
    }
168
169
    /**
170
     * Gets the site's label. The label is build from the the site title and root
171
     * page ID (uid).
172
     *
173
     * @return string The site's label.
174
     */
175
    public function getLabel()
176
    {
177
        $rootlineTitles = [];
178
        $rootLine = BackendUtility::BEgetRootLine($this->rootPage['uid']);
179
        // Remove last
180
        array_pop($rootLine);
181
        $rootLine = array_reverse($rootLine);
182
        foreach ($rootLine as $rootLineItem) {
183
            $rootlineTitles[] = $rootLineItem['title'];
184
        }
185
        return implode(' - ', $rootlineTitles) . ', Root Page ID: ' . $this->rootPage['uid'];
186
    }
187
188
    /**
189
     * Gets the site's Solr TypoScript configuration (plugin.tx_solr.*)
190
     *
191
     * @return  \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration The Solr TypoScript configuration
192
     */
193
    public function getSolrConfiguration()
194
    {
195
        return $this->configuration;
196
    }
197
198
    /**
199
     * Gets the site's default language as configured in
200
     * config.sys_language_uid. If sys_language_uid is not set, 0 is assumed to
201
     * be the default.
202
     *
203
     * @return int The site's default language.
204
     */
205
    public function getDefaultLanguage()
206
    {
207
        return $this->defaultLanguageId;
208
    }
209
210
    /**
211
     * Generates a list of page IDs in this site. Attention, this includes
212
     * all page types! Deleted pages are not included.
213
     *
214
     * @param int|string $rootPageId Page ID from where to start collection sub pages
215
     * @param int $maxDepth Maximum depth to descend into the site tree
216
     * @return array Array of pages (IDs) in this site
217
     */
218
    public function getPages($rootPageId = 'SITE_ROOT', $maxDepth = 999)
219
    {
220
        $pageIds = [];
221
        if ($rootPageId === 'SITE_ROOT') {
222
            $rootPageId = (int)$this->rootPage['uid'];
223
            $pageIds[] = $rootPageId;
224
        }
225
226
        $configurationAwareRecordService = GeneralUtility::makeInstance(ConfigurationAwareRecordService::class);
227
        // Fetch configuration in order to be able to read initialPagesAdditionalWhereClause
228
        $solrConfiguration = $this->getSolrConfiguration();
229
        $indexQueueConfigurationName = $configurationAwareRecordService->getIndexingConfigurationName('pages', $this->rootPage['uid'], $solrConfiguration);
230
        $initialPagesAdditionalWhereClause = $solrConfiguration->getInitialPagesAdditionalWhereClause($indexQueueConfigurationName);
231
232
        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

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