Passed
Pull Request — master (#2925)
by Rafael
35:16
created

Site::getRootPageId()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Site;
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
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\ConfigurationAwareRecordService;
19
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
20
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
21
use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository;
22
use TYPO3\CMS\Backend\Utility\BackendUtility;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * Base Class for Typo3ManagedSite and LegacySite
27
 *
28
 * (c) 2011-2015 Ingo Renner <[email protected]>
29
 */
30
abstract class Site implements SiteInterface
31
{
32
    /**
33
     * @var TypoScriptConfiguration
34
     */
35
    protected $configuration;
36
37
    /**
38
     * Root page record.
39
     *
40
     * @var array
41
     */
42
    protected $rootPage = [];
43
    /**
44
     * @var string
45
     */
46
    protected $domain;
47
48
    /**
49
     * @var string
50
     */
51
    protected $siteHash;
52
53
    /**
54
     * @var PagesRepository
55
     */
56
    protected $pagesRepository;
57
58
    /**
59
     * @var int
60
     */
61
    protected $defaultLanguageId = 0;
62
63
    /**
64
     * @var int[] Available language ids
65
     */
66
    protected $availableLanguageIds = [];
67
68
    /**
69
     * Takes an pagerecord and checks whether the page is marked as root page.
70
     *
71
     * @param array $page pagerecord
72
     * @return bool true if the page is marked as root page, false otherwise
73
     * @todo: move to SiteUtility?
74
     */
75 182
    public static function isRootPage($page)
76
    {
77 182
        if ($page['is_siteroot']) {
78 181
            return true;
79
        }
80
81 93
        return false;
82
    }
83
84
    /**
85
     * Gets the site's root page ID (uid).
86
     *
87
     * @return int The site's root page ID.
88
     */
89 83
    public function getRootPageId()
90
    {
91 83
        return (int)$this->rootPage['uid'];
92
    }
93
94
    /**
95
     * Gets available language id's for this site
96
     *
97
     * @return int[] array or language id's
98
     */
99 86
    public function getAvailableLanguageIds(): array {
100 86
        return $this->availableLanguageIds;
101
    }
102
103
    /**
104
     * Gets the site's label. The label is build from the the site title and root
105
     * page ID (uid).
106
     *
107
     * @return string The site's label.
108
     */
109 3
    public function getLabel()
110
    {
111 3
        $rootlineTitles = [];
112 3
        $rootLine = BackendUtility::BEgetRootLine($this->rootPage['uid']);
113
        // Remove last
114 3
        array_pop($rootLine);
115 3
        $rootLine = array_reverse($rootLine);
116 3
        foreach ($rootLine as $rootLineItem) {
117 3
            $rootlineTitles[] = $rootLineItem['title'];
118
        }
119 3
        return implode(' - ', $rootlineTitles) . ', Root Page ID: ' . $this->rootPage['uid'];
120
    }
121
122
    /**
123
     * Gets the site's Solr TypoScript configuration (plugin.tx_solr.*)
124
     *
125
     * Purpose: Interface and Unit test mocking helper method.
126
     *
127
     * @return  TypoScriptConfiguration The Solr TypoScript configuration
128
     */
129 64
    public function getSolrConfiguration(): TypoScriptConfiguration
130
    {
131 64
        return $this->configuration;
132
    }
133
134
    /**
135
     * Gets the site's default language as configured in
136
     * config.sys_language_uid. If sys_language_uid is not set, 0 is assumed to
137
     * be the default.
138
     *
139
     * @return int The site's default language.
140
     */
141 1
    public function getDefaultLanguage()
142
    {
143 1
        return $this->defaultLanguageId;
144
    }
145
146
    /**
147
     * @inheritDoc
148
     */
149 13
    public function getPages(
150
        ?int $pageId = null,
151
        ?string $indexQueueConfigurationName = null
152
    ): array
153
    {
154 13
        $pageId = $pageId ?? (int)$this->rootPage['uid'];
155
156 13
        $initialPagesAdditionalWhereClause = '';
157
        // Fetch configuration in order to be able to read initialPagesAdditionalWhereClause
158 13
        if ($indexQueueConfigurationName !== null) {
159 12
            $solrConfiguration = $this->getSolrConfiguration();
160 12
            $initialPagesAdditionalWhereClause = $solrConfiguration->getInitialPagesAdditionalWhereClause($indexQueueConfigurationName);
161
        }
162 13
        return $this->pagesRepository->findAllSubPageIdsByRootPage($pageId, $initialPagesAdditionalWhereClause);
163
    }
164
165
    /**
166
     * @param int|null $rootPageId
167
     * @return array
168
     */
169 12
    public function getPagesWithinNoSearchSubEntriesPages(int $rootPageId = null): array
170
    {
171 12
        if ($rootPageId === null) {
172 12
            $rootPageId = (int)$this->rootPage['uid'];
173
        }
174
175
        /* @var ConfigurationAwareRecordService $configurationAwareRecordService */
176 12
        $configurationAwareRecordService = GeneralUtility::makeInstance(ConfigurationAwareRecordService::class);
177
        // Fetch configuration in order to be able to read initialPagesAdditionalWhereClause
178 12
        $solrConfiguration = $this->getSolrConfiguration();
179 12
        $indexQueueConfigurationName = $configurationAwareRecordService->getIndexingConfigurationName('pages', $this->rootPage['uid'], $solrConfiguration);
180 12
        $initialPagesAdditionalWhereClause = $solrConfiguration->getInitialPagesAdditionalWhereClause($indexQueueConfigurationName);
181
182 12
        return $this->pagesRepository->findAllPagesWithinNoSearchSubEntriesMarkedPagesByRootPage(
183 12
            $rootPageId,
184 12
            999,
185 12
            $initialPagesAdditionalWhereClause
186
        );
187
    }
188
189
    /**
190
     * Generates the site's unique Site Hash.
191
     *
192
     * The Site Hash is build from the site's main domain, the system encryption
193
     * key, and the extension "tx_solr". These components are concatenated and
194
     * sha1-hashed.
195
     *
196
     * @return string Site Hash.
197
     */
198 95
    public function getSiteHash()
199
    {
200 95
        return $this->siteHash;
201
    }
202
203
    /**
204
     * Gets the site's main domain. More specifically the first domain record in
205
     * the site tree.
206
     *
207
     * @return string The site's main domain.
208
     */
209 94
    public function getDomain()
210
    {
211 94
        return $this->domain;
212
    }
213
214
    /**
215
     * Gets the site's root page.
216
     *
217
     * @return array The site's root page.
218
     */
219 21
    public function getRootPage()
220
    {
221 21
        return $this->rootPage;
222
    }
223
224
    /**
225
     * Gets the site's root page's title.
226
     *
227
     * @return string The site's root page's title
228
     */
229
    public function getTitle()
230
    {
231
        return $this->rootPage['title'];
232
    }
233
234
    /**
235
     * Retrieves the rootPageIds as an array from a set of sites.
236
     *
237
     * @param array $sites
238
     * @return array
239
     * @todo: move to SiteUtility?
240
     */
241 44
    public static function getRootPageIdsFromSites(array $sites): array
242
    {
243 44
        $rootPageIds = [];
244 44
        foreach ($sites as $site) {
245 7
            $rootPageIds[] = (int)$site->getRootPageId();
246
        }
247
248 44
        return $rootPageIds;
249
    }
250
251
    /**
252
     * @return array
253
     */
254 85
    public function getAllSolrConnectionConfigurations(): array {
255 85
        $configs = [];
256 85
        foreach ($this->getAvailableLanguageIds() as $languageId) {
257
            try {
258 85
                $configs[$languageId] = $this->getSolrConnectionConfiguration($languageId);
259 1
            } catch (NoSolrConnectionFoundException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
260
        }
261 85
        return $configs;
262
    }
263
264 77
    public function isEnabled(): bool
265
    {
266 77
        return !empty($this->getAllSolrConnectionConfigurations());
267
    }
268
269
    /**
270
     * @param int $language
271
     * @return array
272
     */
273
    abstract public function getSolrConnectionConfiguration(int $language = 0): array;
274
}
275