Passed
Push — master ( 90cea8...3d6c73 )
by Timo
01:26
created

Site::getLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 4
nc 1
nop 0
crap 2
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\Backend\SiteSelectorField;
28
use ApacheSolrForTypo3\Solr\Domain\Site\SiteHashService;
29
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\ConfigurationAwareRecordService;
30
use TYPO3\CMS\Backend\Utility\BackendUtility;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Frontend\Page\PageRepository;
33
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
34
35
/**
36
 * A site is a branch in a TYPO3 installation. Each site's root page is marked
37
 * by the "Use as Root Page" flag.
38
 *
39
 * @author Ingo Renner <[email protected]>
40
 */
41
class Site
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
     * Constructor.
78
     *
79
     * @param int $rootPageId Site root page ID (uid). The page must be marked as site root ("Use as Root Page" flag).
80
     */
81 113
    public function __construct($rootPageId)
82
    {
83 113
        $page = (array)BackendUtility::getRecord('pages', $rootPageId);
84
85 113
        if (empty($page)) {
86 8
            throw new \InvalidArgumentException(
87
                'The page for the given page ID \'' . $rootPageId
88 8
                . '\' could not be found in the database and can therefore not be used as site root page.',
89
                1487326416
90 8
            );
91
        }
92
93 105
        if (!self::isRootPage($page)) {
94 1
            throw new \InvalidArgumentException(
95
                'The page for the given page ID \'' . $rootPageId
96 1
                . '\' is not marked as root page and can therefore not be used as site root page.',
97
                1309272922
98 1
            );
99
        }
100
101 104
        $this->rootPage = $page;
102 104
    }
103
104
    /**
105
     * Clears the $sitePagesCache
106
     *
107
     */
108
    public static function clearSitePagesCache()
109
    {
110
        self::$sitePagesCache = [];
111
    }
112
113
    /**
114
     * Takes an pagerecord and checks whether the page is marked as root page.
115
     *
116
     * @param array $page pagerecord
117
     * @return bool true if the page is marked as root page, false otherwise
118
     */
119
    public static function isRootPage($page)
120
    {
121
        if ($page['is_siteroot']) {
122
            return true;
123
        }
124
125
        return false;
126
    }
127
128
    /**
129
     * Gets the site's root page ID (uid).
130
     *
131
     * @return int The site's root page ID.
132
     */
133
    public function getRootPageId()
134
    {
135
        return $this->rootPage['uid'];
136
    }
137
138
    /**
139
     * Gets the site's label. The label is build from the the site title and root
140
     * page ID (uid).
141
     *
142
     * @return string The site's label.
143
     */
144
    public function getLabel()
145
    {
146
        $rootlineTitles = [];
147
        $rootLine = BackendUtility::BEgetRootLine($this->rootPage['uid']);
148
        // Remove last
149
        array_pop($rootLine);
150
        $rootLine = array_reverse($rootLine);
151
        foreach ($rootLine as $rootLineItem) {
152
            $rootlineTitles[] = $rootLineItem['title'];
153
        }
154
        return implode(' - ', $rootlineTitles) . ', Root Page ID: ' . $this->rootPage['uid'];
155
    }
156
157
    /**
158
     * Gets the site's Solr TypoScript configuration (plugin.tx_solr.*)
159
     *
160
     * @return  \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration The Solr TypoScript configuration
161
     */
162
    public function getSolrConfiguration()
163
    {
164
        return Util::getSolrConfigurationFromPageId($this->rootPage['uid']);
165
    }
166
167
    /**
168
     * Gets the site's default language as configured in
169
     * config.sys_language_uid. If sys_language_uid is not set, 0 is assumed to
170
     * be the default.
171
     *
172
     * @return int The site's default language.
173
     */
174
    public function getDefaultLanguage()
175
    {
176
        $siteDefaultLanguage = 0;
177
178
        $configuration = Util::getConfigurationFromPageId(
179
            $this->rootPage['uid'],
180
            'config'
181
        );
182
183
        $siteDefaultLanguage = $configuration->getValueByPathOrDefaultValue('sys_language_uid', $siteDefaultLanguage);
184
        // default language is set through default L GET parameter -> overruling config.sys_language_uid
185
        $siteDefaultLanguage = $configuration->getValueByPathOrDefaultValue('defaultGetVars.L', $siteDefaultLanguage);
186 115
187
        return $siteDefaultLanguage;
188 115
    }
189 114
190
    /**
191
     * Generates a list of page IDs in this site. Attention, this includes
192 56
     * all page types! Deleted pages are not included.
193
     *
194
     * @param int|string $rootPageId Page ID from where to start collection sub pages
195
     * @param int $maxDepth Maximum depth to descend into the site tree
196
     * @return array Array of pages (IDs) in this site
197
     */
198
    public function getPages($rootPageId = 'SITE_ROOT', $maxDepth = 999)
199
    {
200 36
        $pageIds = [];
201
        $maxDepth = intval($maxDepth);
202 36
203
        if ($rootPageId == 'SITE_ROOT') {
204
            $rootPageId = $this->rootPage['uid'];
205
            $pageIds[] = (int)$this->rootPage['uid'];
206
        }
207
208
        $recursionRootPageId = intval($rootPageId);
209
210
        // when we have a cached value, we can return it.
211 10
        if (!empty(self::$sitePagesCache[$rootPageId])) {
212
            return self::$sitePagesCache[$rootPageId];
213 10
        }
214 10
215
        if ($maxDepth <= 0) {
216 10
            // exiting the recursion loop, may write to cache now
217 10
            self::$sitePagesCache[$rootPageId] = $pageIds;
218 10
            return $pageIds;
219 10
        }
220 10
221 10
        // get the page ids of the current level and if needed call getPages recursive
222
        $pageIds = $this->getPageIdsFromCurrentDepthAndCallRecursive($maxDepth, $recursionRootPageId, $pageIds);
223
224
        // exiting the recursion loop, may write to cache now
225
        self::$sitePagesCache[$rootPageId] = $pageIds;
226
        return $pageIds;
227
    }
228
229 53
    /**
230
     * This method retrieves the pages ids from the current tree level an calls getPages recursive,
231 53
     * when the maxDepth has not been reached.
232
     *
233
     * @param int $maxDepth
234
     * @param int $recursionRootPageId
235
     * @param array $pageIds
236
     * @return array
237
     */
238
    protected function getPageIdsFromCurrentDepthAndCallRecursive($maxDepth, $recursionRootPageId, $pageIds)
239
    {
240
        static $initialPagesAdditionalWhereClause;
241
242
        // Only fetch $initialPagesAdditionalWhereClause on first call
243
        if (empty($initialPagesAdditionalWhereClause)) {
244
            $configurationAwareRecordService = GeneralUtility::makeInstance(ConfigurationAwareRecordService::class);
245
            // Fetch configuration in order to be able to read initialPagesAdditionalWhereClause
246
            $solrConfiguration = $this->getSolrConfiguration();
247
            $indexQueueConfigurationName = $configurationAwareRecordService->getIndexingConfigurationName('pages', $this->rootPage['uid'], $solrConfiguration);
248
            $initialPagesAdditionalWhereClause = $solrConfiguration->getInitialPagesAdditionalWhereClause($indexQueueConfigurationName);
249
        }
250
251
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'pages', 'pid = ' . $recursionRootPageId . ' ' . BackendUtility::deleteClause('pages') . $initialPagesAdditionalWhereClause);
252
253
        while ($page = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
254
            $pageIds[] = (int)$page['uid'];
255 1
256
            if ($maxDepth > 1) {
257 1
                $pageIds = array_merge($pageIds, $this->getPages($page['uid'], $maxDepth - 1));
258
            }
259 1
        }
260 1
        $GLOBALS['TYPO3_DB']->sql_free_result($result);
261
        return $pageIds;
262 1
    }
263
264 1
    /**
265
     * Generates the site's unique Site Hash.
266 1
     *
267
     * The Site Hash is build from the site's main domain, the system encryption
268 1
     * key, and the extension "tx_solr". These components are concatenated and
269
     * sha1-hashed.
270
     *
271
     * @return string Site Hash.
272
     */
273
    public function getSiteHash()
274
    {
275
        /** @var $siteHashService SiteHashService */
276
        $siteHashService = GeneralUtility::makeInstance(SiteHashService::class);
277
        return $siteHashService->getSiteHashForDomain($this->getDomain());
278
    }
279 9
280
    /**
281 9
     * Gets the site's main domain. More specifically the first domain record in
282 9
     * the site tree.
283
     *
284 9
     * @return string The site's main domain.
285 9
     */
286 9
    public function getDomain()
287 9
    {
288
        $pageSelect = GeneralUtility::makeInstance(PageRepository::class);
289 9
        $rootLine = $pageSelect->getRootLine($this->rootPage['uid']);
290
291
        return BackendUtility::firstDomainRecord($rootLine);
292 9
    }
293 7
294
    /**
295
     * Gets the site's root page.
296 9
     *
297
     * @return array The site's root page.
298
     */
299
    public function getRootPage()
300
    {
301
        return $this->rootPage;
302
    }
303 9
304
    /**
305
     * Gets the site's root page's title.
306 9
     *
307 9
     * @return string The site's root page's title
308
     */
309
    public function getTitle()
310
    {
311
        return $this->rootPage['title'];
312
    }
313
314
    /**
315
     * Gets the site's config.sys_language_mode setting
316
     *
317
     * @return string The site's config.sys_language_mode
318
     */
319 9
    public function getSysLanguageMode()
320
    {
321 9
        if (is_null($this->sysLanguageMode)) {
322
            Util::initializeTsfe($this->getRootPageId());
323
            $this->sysLanguageMode = $GLOBALS['TSFE']->sys_language_mode;
324 9
        }
325 9
326
        return $this->sysLanguageMode;
327 9
    }
328
}
329