Passed
Push — master ( b70fee...0220a2 )
by Timo
09:45
created

Site::getPages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 5
cts 7
cp 0.7143
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
crap 2.0932
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 2 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 124
     * @var string
82
     */
83 124
    protected $domain;
84
85 124
    /**
86 4
     * @var string
87 4
     */
88 4
    protected $siteHash;
89 4
90
    /**
91
     * @var PagesRepository
92
     */
93 120
    protected $pagesRepository;
94 1
95 1
    /**
96 1
     * Constructor.
97 1
     *
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 119
     * @param string $siteHash The site hash used by this site
102 119
     * @param PagesRepository $pagesRepository
103
     */
104
    public function __construct(TypoScriptConfiguration $configuration, array $page, $domain, $siteHash, PagesRepository $pagesRepository = null)
105
    {
106
        $this->configuration = $configuration;
107
        $this->rootPage = $page;
108
        $this->domain = $domain;
109
        $this->siteHash = $siteHash;
110
        $this->pagesRepository = isset($pagesRepository) ? $pagesRepository : GeneralUtility::makeInstance(PagesRepository::class);
111
    }
112
113
    /**
114
     * Clears the $sitePagesCache
115
     *
116
     */
117
    public static function clearSitePagesCache()
118
    {
119 138
        self::$sitePagesCache = [];
120
    }
121 138
122 137
    /**
123
     * Takes an pagerecord and checks whether the page is marked as root page.
124
     *
125 71
     * @param array $page pagerecord
126
     * @return bool true if the page is marked as root page, false otherwise
127
     */
128
    public static function isRootPage($page)
129
    {
130
        if ($page['is_siteroot']) {
131
            return true;
132
        }
133 46
134
        return false;
135 46
    }
136
137
    /**
138
     * Gets the site's root page ID (uid).
139
     *
140
     * @return int The site's root page ID.
141
     */
142
    public function getRootPageId()
143
    {
144 14
        return (int)$this->rootPage['uid'];
145
    }
146 14
147 14
    /**
148
     * Gets the site's label. The label is build from the the site title and root
149 14
     * page ID (uid).
150 14
     *
151 14
     * @return string The site's label.
152 14
     */
153
    public function getLabel()
154 14
    {
155
        $rootlineTitles = [];
156
        $rootLine = BackendUtility::BEgetRootLine($this->rootPage['uid']);
157
        // Remove last
158
        array_pop($rootLine);
159
        $rootLine = array_reverse($rootLine);
160
        foreach ($rootLine as $rootLineItem) {
161
            $rootlineTitles[] = $rootLineItem['title'];
162 56
        }
163
        return implode(' - ', $rootlineTitles) . ', Root Page ID: ' . $this->rootPage['uid'];
164 56
    }
165
166
    /**
167
     * Gets the site's Solr TypoScript configuration (plugin.tx_solr.*)
168
     *
169
     * @return  \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration The Solr TypoScript configuration
170
     */
171
    public function getSolrConfiguration()
172
    {
173
        return $this->configuration;
174 1
    }
175
176 1
    /**
177
     * Gets the site's default language as configured in
178 1
     * config.sys_language_uid. If sys_language_uid is not set, 0 is assumed to
179 1
     * be the default.
180 1
     *
181
     * @return int The site's default language.
182
     */
183 1
    public function getDefaultLanguage()
184
    {
185 1
        $siteDefaultLanguage = 0;
186
187 1
        $configuration = Util::getConfigurationFromPageId(
188
            $this->rootPage['uid'],
189
            'config'
190
        );
191
192
        $siteDefaultLanguage = $configuration->getValueByPathOrDefaultValue('sys_language_uid', $siteDefaultLanguage);
193
        // default language is set through default L GET parameter -> overruling config.sys_language_uid
194
        $siteDefaultLanguage = $configuration->getValueByPathOrDefaultValue('defaultGetVars.L', $siteDefaultLanguage);
195
196
        return $siteDefaultLanguage;
197
    }
198 12
199
    /**
200 12
     * Generates a list of page IDs in this site. Attention, this includes
201 12
     * all page types! Deleted pages are not included.
202
     *
203 12
     * @param int|string $rootPageId Page ID from where to start collection sub pages
204 12
     * @param int $maxDepth Maximum depth to descend into the site tree
205 12
     * @return array Array of pages (IDs) in this site
206
     */
207
    public function getPages($rootPageId = 'SITE_ROOT', $maxDepth = 999)
208 12
    {
209
        $pageIds = [];
210
        if ($rootPageId === 'SITE_ROOT') {
211 12
            $rootPageId = (int)$this->rootPage['uid'];
212 10
            $pageIds[] = $rootPageId;
213
        }
214
215 12
        $configurationAwareRecordService = GeneralUtility::makeInstance(ConfigurationAwareRecordService::class);
216
        // Fetch configuration in order to be able to read initialPagesAdditionalWhereClause
217
        $solrConfiguration = $this->getSolrConfiguration();
218
        $indexQueueConfigurationName = $configurationAwareRecordService->getIndexingConfigurationName('pages', $this->rootPage['uid'], $solrConfiguration);
219
        $initialPagesAdditionalWhereClause = $solrConfiguration->getInitialPagesAdditionalWhereClause($indexQueueConfigurationName);
220
221
        return array_merge($pageIds, $this->pagesRepository->findAllSubPageIdsByRootPage($rootPageId, $maxDepth, $initialPagesAdditionalWhereClause));
222 12
    }
223
224
    /**
225 12
     * Generates the site's unique Site Hash.
226 12
     *
227
     * The Site Hash is build from the site's main domain, the system encryption
228
     * key, and the extension "tx_solr". These components are concatenated and
229
     * sha1-hashed.
230
     *
231
     * @return string Site Hash.
232
     */
233
    public function getSiteHash()
234
    {
235
        return $this->siteHash;
236
    }
237
238 12
    /**
239
     * Gets the site's main domain. More specifically the first domain record in
240 12
     * the site tree.
241
     *
242
     * @return string The site's main domain.
243 12
     */
244 12
    public function getDomain()
245
    {
246 12
        return $this->domain;
247 12
    }
248 12
249
    /**
250
     * Gets the site's root page.
251 12
     *
252
     * @return array The site's root page.
253 12
     */
254 10
    public function getRootPage()
255
    {
256 10
        return $this->rootPage;
257 10
    }
258
259
    /**
260 12
     * Gets the site's root page's title.
261 12
     *
262
     * @return string The site's root page's title
263
     */
264
    public function getTitle()
265
    {
266
        return $this->rootPage['title'];
267
    }
268
269
    /**
270
     * Gets the site's config.sys_language_mode setting
271
     *
272
     * @param int $languageUid
273 71
     *
274
     * @return string The site's config.sys_language_mode
275
     */
276 71
    public function getSysLanguageMode($languageUid = 0)
277 71
    {
278
        if (is_null($this->sysLanguageMode)) {
279
            Util::initializeTsfe($this->getRootPageId(), $languageUid);
280
            $this->sysLanguageMode = $GLOBALS['TSFE']->sys_language_mode;
281
        }
282
283
        return $this->sysLanguageMode;
284
    }
285
}
286