Completed
Push — master ( 3b0ef5...7e6ca3 )
by Timo
44:49 queued 41:28
created

ConnectionManager::getConfiguredSolrConnections()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.9332
cc 4
nc 4
nop 0
crap 4
1
<?php
2
namespace ApacheSolrForTypo3\Solr;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-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\Site\Site;
28
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
29
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
30
use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository as PagesRepositoryAtExtSolr;
31
use ApacheSolrForTypo3\Solr\System\Records\SystemLanguage\SystemLanguageRepository;
32
use ApacheSolrForTypo3\Solr\System\Solr\Node;
33
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
34
use TYPO3\CMS\Core\Registry;
35
use TYPO3\CMS\Core\SingletonInterface;
36
use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
37
use TYPO3\CMS\Core\Utility\GeneralUtility;
38
use TYPO3\CMS\Frontend\Page\PageRepository;
39
40
/**
41
 * ConnectionManager is responsible to create SolrConnection objects.
42
 *
43
 * @author Ingo Renner <[email protected]>
44
 */
45
class ConnectionManager implements SingletonInterface
46
{
47
48
    /**
49
     * @var array
50
     */
51
    protected static $connections = [];
52
53
    /**
54
     * @var \ApacheSolrForTypo3\Solr\System\Records\SystemLanguage\SystemLanguageRepository
55
     */
56
    protected $systemLanguageRepository;
57
58
    /**
59
     * @var PagesRepositoryAtExtSolr
60
     */
61
    protected $pagesRepositoryAtExtSolr;
62
63
    /**
64
     * @var SiteRepository
65
     */
66
    protected $siteRepository;
67
68
    /**
69
     * @param SystemLanguageRepository $systemLanguageRepository
70
     * @param PagesRepositoryAtExtSolr|null $pagesRepositoryAtExtSolr
71
     * @param SiteRepository $siteRepository
72
     */
73 119
    public function __construct(SystemLanguageRepository $systemLanguageRepository = null, PagesRepositoryAtExtSolr $pagesRepositoryAtExtSolr = null, SiteRepository $siteRepository = null)
74
    {
75 119
        $this->systemLanguageRepository = $systemLanguageRepository ?? GeneralUtility::makeInstance(SystemLanguageRepository::class);
76 119
        $this->siteRepository           = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
77 119
        $this->pagesRepositoryAtExtSolr = $pagesRepositoryAtExtSolr ?? GeneralUtility::makeInstance(PagesRepositoryAtExtSolr::class);
78 119
    }
79
80
    /**
81
     * Creates a solr connection for read and write endpoints
82
     *
83
     * @param array $readNodeConfiguration
84
     * @param array $writeNodeConfiguration
85
     * @return SolrConnection|object
86
     */
87 106
    public function getSolrConnectionForNodes(array $readNodeConfiguration, array $writeNodeConfiguration)
88
    {
89 106
        $connectionHash = md5(\json_encode($readNodeConfiguration) .  \json_encode($writeNodeConfiguration));
90 106
        if (!isset(self::$connections[$connectionHash])) {
91 106
            $readNode = Node::fromArray($readNodeConfiguration);
92 106
            $writeNode = Node::fromArray($writeNodeConfiguration);
93 106
            self::$connections[$connectionHash] = GeneralUtility::makeInstance(SolrConnection::class, $readNode, $writeNode);
94
        }
95 106
        return self::$connections[$connectionHash];
96
    }
97
98
    /**
99
     * Creates a solr configuration from the configuration array and returns it.
100
     *
101
     * @param array $config The solr configuration array
102
     * @return SolrConnection
103
     */
104 108
    public function getConnectionFromConfiguration(array $config)
105
    {
106 108
        if(empty($config['read']) && !empty($config['solrHost'])) {
107
            throw new \InvalidArgumentException('Invalid registry data please re-initialize your solr connections');
108
        }
109
110 108
        return $this->getSolrConnectionForNodes($config['read'], $config['write']);
111
    }
112
113
    /**
114
     * Gets a Solr configuration for a page ID.
115
     *
116
     * @param int $pageId A page ID.
117
     * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0.
118
     * @param string $mount Comma list of MountPoint parameters
119
     * @deprecated will be removed in v11, use Site object/SiteRepository directly
120
     * @return array A solr configuration.
121
     * @throws NoSolrConnectionFoundException
122
     */
123
    public function getConfigurationByPageId($pageId, $language = 0, $mount = '')
124
    {
125
        trigger_error('Method getConfigurationByPageId is deprecated since EXT:solr 10 and will be removed in v11, use Site object/SiteRepository directly.', E_USER_DEPRECATED);
126
127
        try {
128
            $site = $this->siteRepository->getSiteByPageId($pageId, $mount);
129
            return $site->getSolrConnectionConfiguration($language);
130
        } catch(\InvalidArgumentException $e) {
131
            $noSolrConnectionException = GeneralUtility::makeInstance(
132
                NoSolrConnectionFoundException::class,
133
                /** @scrutinizer ignore-type */  'Could not find a Solr connection for page [' . $pageId. '] and language [' . $language . '].',
134
                /** @scrutinizer ignore-type */ 1575396474
135
            );
136
            $noSolrConnectionException->setLanguageId($language);
137
            throw $noSolrConnectionException;
138
        }
139
    }
140
141
    /**
142
     * Gets a Solr connection for a page ID.
143
     *
144
     * @param int $pageId A page ID.
145
     * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0.
146
     * @param string $mount Comma list of MountPoint parameters
147
     * @return SolrConnection A solr connection.
148
     * @throws NoSolrConnectionFoundException
149
     */
150 98
    public function getConnectionByPageId($pageId, $language = 0, $mount = '')
151
    {
152
        try {
153 98
            $site = $this->siteRepository->getSiteByPageId($pageId, $mount);
154 97
            $config = $site->getSolrConnectionConfiguration($language);
155 97
            $solrConnection = $this->getConnectionFromConfiguration($config);
156
157 97
            return $solrConnection;
158 2
        } catch(\InvalidArgumentException $e) {
159 1
            $noSolrConnectionException = GeneralUtility::makeInstance(
160 1
                NoSolrConnectionFoundException::class,
161 1
                /** @scrutinizer ignore-type */  'Could not find a Solr connection for page [' . $pageId. '] and language [' . $language . '].',
162 1
                /** @scrutinizer ignore-type */ 1575396474
163
            );
164 1
            $noSolrConnectionException->setLanguageId($language);
165 1
            throw $noSolrConnectionException;
166
        }
167
    }
168
169
    /**
170
     * Gets a Solr configuration for a root page ID.
171
     *
172
     * @param int $pageId A root page ID.
173
     * @param int $language The language ID to get the configuration for as the path may differ. Optional, defaults to 0.
174
     * @return array A solr configuration.
175
     * @throws NoSolrConnectionFoundException
176
     * @deprecated will be removed in v11, use Site object/SiteRepository directly
177
     */
178 1
    public function getConfigurationByRootPageId($pageId, $language = 0)
179
    {
180 1
        trigger_error('Method getConfigurationByRootPageId is deprecated since EXT:solr 10 and will be removed in v11, use Site object/SiteRepository directly.', E_USER_DEPRECATED);
181
182
        try {
183 1
            $site = $this->siteRepository->getSiteByRootPageId($pageId);
184
            return $site->getSolrConnectionConfiguration($language);
185 1
        } catch(\InvalidArgumentException $e) {
186 1
            $noSolrConnectionException = GeneralUtility::makeInstance(
187 1
                NoSolrConnectionFoundException::class,
188 1
                /** @scrutinizer ignore-type */  'Could not find a Solr connection for page [' . $pageId. '] and language [' . $language . '].',
189 1
                /** @scrutinizer ignore-type */ 1575396474
190
            );
191 1
            $noSolrConnectionException->setLanguageId($language);
192 1
            throw $noSolrConnectionException;
193
        }
194
    }
195
196
    /**
197
     * Gets a Solr connection for a root page ID.
198
     *
199
     * @param int $pageId A root page ID.
200
     * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0.
201
     * @return SolrConnection A solr connection.
202
     * @throws NoSolrConnectionFoundException
203
     */
204 10
    public function getConnectionByRootPageId($pageId, $language = 0)
205
    {
206
        try {
207 10
            $site = $this->siteRepository->getSiteByRootPageId($pageId);
208 10
            $config = $site->getSolrConnectionConfiguration($language);
209 10
            $solrConnection = $this->getConnectionFromConfiguration($config);
210 10
            return $solrConnection;
211
        } catch (\InvalidArgumentException $e) {
212
            $noSolrConnectionException = GeneralUtility::makeInstance(
213
                NoSolrConnectionFoundException::class,
214
                /** @scrutinizer ignore-type */  'Could not find a Solr connection for page [' . $pageId. '] and language [' . $language . '].',
215
                /** @scrutinizer ignore-type */ 1875396474
216
            );
217
            $noSolrConnectionException->setLanguageId($language);
218
            throw $noSolrConnectionException;
219
        }
220
    }
221
222
    /**
223
     * Gets all connection configurations found.
224
     *
225
     * @return array An array of connection configurations.
226
     * @deprecated will be removed in v11, use SiteRepository
227
     */
228 1
    public function getAllConfigurations()
229
    {
230 1
        trigger_error('Method getAllConfigurations is deprecated since EXT:solr 10 and will be removed in v11, use Site object/SiteRepository directly.', E_USER_DEPRECATED);
231
232 1
        $solrConfigurations = [];
233 1
        foreach ($this->siteRepository->getAvailableSites() as $site) {
234
            foreach ($site->getAllSolrConnectionConfigurations() as $solrConfiguration) {
235
                $solrConfigurations[] = $solrConfiguration;
236
            }
237
        }
238
239 1
        return $solrConfigurations;
240
    }
241
242
    /**
243
     * Stores the connections in the registry.
244
     *
245
     * @param array $solrConfigurations
246
     * @deprecated will be removed in v11, use SiteRepository
247
     */
248 3
    protected function setAllConfigurations(array $solrConfigurations)
249
    {
250 3
        trigger_error('Method setAllConfigurations is deprecated since EXT:solr 10 and will be removed in v11, use Site object/SiteRepository directly.', E_USER_DEPRECATED);
251
252
        /** @var $registry Registry */
253 3
        $registry = GeneralUtility::makeInstance(Registry::class);
254 3
        $registry->set('tx_solr', 'servers', $solrConfigurations);
255 3
    }
256
257
    /**
258
     * Gets all connections found.
259
     *
260
     * @return SolrConnection[] An array of initialized ApacheSolrForTypo3\Solr\System\Solr\SolrConnection connections
261
     */
262 7
    public function getAllConnections()
263
    {
264 7
        $solrConnections = [];
265 7
        foreach ($this->siteRepository->getAvailableSites() as $site) {
266 3
            foreach ($site->getAllSolrConnectionConfigurations() as $solrConfiguration) {
267 3
                $solrConnections[] = $this->getConnectionFromConfiguration($solrConfiguration);
268
            }
269
        }
270
271 7
        return $solrConnections;
272
    }
273
274
    /**
275
     * Gets all connection configurations for a given site.
276
     *
277
     * @param Site $site A TYPO3 site
278
     * @return array An array of Solr connection configurations for a site
279
     * @deprecated will be removed in v11, use $site->getAllSolrConnectionConfigurations()
280
     */
281 22
    public function getConfigurationsBySite(Site $site)
282
    {
283 22
        trigger_error('Method getConfigurationsBySite is deprecated since EXT:solr 10 and will be removed in v11, use $site->getAllSolrConnectionConfigurations()', E_USER_DEPRECATED);
284
285 22
        return $site->getAllSolrConnectionConfigurations();
286
    }
287
288
    /**
289
     * Gets all connections configured for a given site.
290
     *
291
     * @param Site $site A TYPO3 site
292
     * @return SolrConnection[] An array of Solr connection objects (ApacheSolrForTypo3\Solr\System\Solr\SolrConnection)
293
     */
294 18
    public function getConnectionsBySite(Site $site)
295
    {
296 18
        $connections = [];
297
298 18
        foreach ($site->getAllSolrConnectionConfigurations() as $solrConnectionConfiguration) {
299 18
            $connections[] = $this->getConnectionFromConfiguration($solrConnectionConfiguration);
300
        }
301
302 18
        return $connections;
303
    }
304
305
    // updates
306
307
    /**
308
     * Updates the connections in the registry.
309
     *
310
     * @deprecated will be removed in v11, use SiteRepository
311
     */
312 2
    public function updateConnections()
313
    {
314 2
        trigger_error('Method updateConnections is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
315
316 2
        $solrConnections = $this->getConfiguredSolrConnections();
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...iguredSolrConnections() has been deprecated: will be removed in v11, use SiteRepository ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

316
        $solrConnections = /** @scrutinizer ignore-deprecated */ $this->getConfiguredSolrConnections();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
317 2
        $solrConnections = $this->filterDuplicateConnections($solrConnections);
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...rDuplicateConnections() has been deprecated: will be removed in v11, use SiteRepository ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

317
        $solrConnections = /** @scrutinizer ignore-deprecated */ $this->filterDuplicateConnections($solrConnections);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
318
319 2
        if (!empty($solrConnections)) {
320 2
            $this->setAllConfigurations($solrConnections);
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...:setAllConfigurations() has been deprecated: will be removed in v11, use SiteRepository ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

320
            /** @scrutinizer ignore-deprecated */ $this->setAllConfigurations($solrConnections);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
321
        }
322 2
    }
323
324
    /**
325
     * Updates the Solr connections for a specific root page ID / site.
326
     *
327
     * @param int $rootPageId A site root page id
328
     * @deprecated Use TYPO3 site config to configure site/connection info
329
     */
330 1
    public function updateConnectionByRootPageId($rootPageId)
331
    {
332 1
        trigger_error('Method updateConnectionByRootPageId is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
333
334 1
        $systemLanguages = $this->systemLanguageRepository->findSystemLanguages();
335 1
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
336 1
        $site = $siteRepository->getSiteByRootPageId($rootPageId);
337 1
        $rootPage = $site->getRootPage();
338
339 1
        $updatedSolrConnections = [];
340 1
        foreach ($systemLanguages as $languageId) {
341 1
            $connection = $this->getConfiguredSolrConnectionByRootPage($rootPage, $languageId);
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...rConnectionByRootPage() has been deprecated: will be removed in v11, use SiteRepository ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

341
            $connection = /** @scrutinizer ignore-deprecated */ $this->getConfiguredSolrConnectionByRootPage($rootPage, $languageId);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
342
343 1
            if (!empty($connection)) {
344 1
                $updatedSolrConnections[$connection['connectionKey']] = $connection;
345
            }
346
        }
347
348 1
        $solrConnections = $this->getAllConfigurations();
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...:getAllConfigurations() has been deprecated: will be removed in v11, use SiteRepository ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

348
        $solrConnections = /** @scrutinizer ignore-deprecated */ $this->getAllConfigurations();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
349 1
        $solrConnections = array_merge($solrConnections, $updatedSolrConnections);
350 1
        $solrConnections = $this->filterDuplicateConnections($solrConnections);
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...rDuplicateConnections() has been deprecated: will be removed in v11, use SiteRepository ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

350
        $solrConnections = /** @scrutinizer ignore-deprecated */ $this->filterDuplicateConnections($solrConnections);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
351 1
        $this->setAllConfigurations($solrConnections);
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...:setAllConfigurations() has been deprecated: will be removed in v11, use SiteRepository ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

351
        /** @scrutinizer ignore-deprecated */ $this->setAllConfigurations($solrConnections);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
352 1
    }
353
354
    /**
355
     * Finds the configured Solr connections. Also respects multi-site
356
     * environments.
357
     *
358
     * @return array An array with connections, each connection with keys rootPageTitle, rootPageUid, solrHost, solrPort, solrPath
359
     * @deprecated will be removed in v11, use SiteRepository
360
     */
361 2
    protected function getConfiguredSolrConnections()
362
    {
363 2
        trigger_error('Method getConfiguredSolrConnections is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
364
365 2
        $configuredSolrConnections = [];
366
        // find website roots and languages for this installation
367 2
        $rootPages = $this->pagesRepositoryAtExtSolr->findAllRootPages();
368 2
        $languages = $this->systemLanguageRepository->findSystemLanguages();
369
370
        // find solr configurations and add them as function menu entries
371 2
        foreach ($rootPages as $rootPage) {
372 2
            foreach ($languages as $languageId) {
373 2
                $connection = $this->getConfiguredSolrConnectionByRootPage($rootPage, $languageId);
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...rConnectionByRootPage() has been deprecated: will be removed in v11, use SiteRepository ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

373
                $connection = /** @scrutinizer ignore-deprecated */ $this->getConfiguredSolrConnectionByRootPage($rootPage, $languageId);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
374
375 2
                if (!empty($connection)) {
376 2
                    $configuredSolrConnections[$connection['connectionKey']] = $connection;
377
                }
378
            }
379
        }
380
381 2
        return $configuredSolrConnections;
382
    }
383
384
    /**
385
     * Gets the configured Solr connection for a specific root page and language ID.
386
     *
387
     * @param array $rootPage A root page record with at least title and uid
388
     * @param int $languageId ID of a system language
389
     * @return array A solr connection configuration.
390
     * @deprecated will be removed in v11, use SiteRepository
391
     */
392 3
    protected function getConfiguredSolrConnectionByRootPage(array $rootPage, $languageId)
393
    {
394 3
        trigger_error('Method getConfiguredSolrConnectionByRootPage is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
395
396 3
        $connection = [];
397
398 3
        $languageId = (int)$languageId;
399 3
        GeneralUtility::_GETset($languageId, 'L');
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Core\Utility\GeneralUtility::_GETset() has been deprecated: since TYPO3 v9 LTS, will be removed in TYPO3 v10.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

399
        /** @scrutinizer ignore-deprecated */ GeneralUtility::_GETset($languageId, 'L');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
400 3
        $connectionKey = $rootPage['uid'] . '|' . $languageId;
401
402 3
        $pageSelect = GeneralUtility::makeInstance(PageRepository::class);
403 3
        $rootLine = $pageSelect->getRootLine($rootPage['uid']);
404
405 3
        $tmpl = GeneralUtility::makeInstance(ExtendedTemplateService::class);
406 3
        $tmpl->tt_track = false; // Do not log time-performance information
407 3
        $tmpl->init();
408 3
        $tmpl->runThroughTemplates($rootLine); // This generates the constants/config + hierarchy info for the template.
409
410
        // fake micro TSFE to get correct condition parsing
411 3
        $GLOBALS['TSFE'] = new \stdClass();
412 3
        $GLOBALS['TSFE']->tmpl = new \stdClass();
413 3
        $GLOBALS['TSFE']->cObjectDepthCounter = 50;
414 3
        $GLOBALS['TSFE']->tmpl->rootLine = $rootLine;
415 3
        $GLOBALS['TSFE']->sys_page = $pageSelect;
416 3
        $GLOBALS['TSFE']->id = $rootPage['uid'];
417 3
        $GLOBALS['TSFE']->page = $rootPage;
418
419 3
        $tmpl->generateConfig();
420 3
        $GLOBALS['TSFE']->tmpl->setup = $tmpl->setup;
421
422 3
        $configuration = Util::getSolrConfigurationFromPageId($rootPage['uid'], false, $languageId);
423
424 3
        $solrIsEnabledAndConfigured = $configuration->getEnabled() && $configuration->getSolrHasConnectionConfiguration();
425 3
        if (!$solrIsEnabledAndConfigured) {
426
            return $connection;
427
        }
428
429
        $connection = [
430 3
            'connectionKey' => $connectionKey,
431 3
            'rootPageTitle' => $rootPage['title'],
432 3
            'rootPageUid' => $rootPage['uid'],
433
            'read' => [
434 3
                'scheme' => $configuration->getSolrScheme(),
435 3
                'host' => $configuration->getSolrHost(),
436 3
                'port' => $configuration->getSolrPort(),
437 3
                'path' => $configuration->getSolrPath(),
438 3
                'username' => $configuration->getSolrUsername(),
439 3
                'password' => $configuration->getSolrPassword(),
440 3
                'timeout' => $configuration->getSolrTimeout()
441
            ],
442
            'write' => [
443 3
                'scheme' => $configuration->getSolrScheme('http', 'write'),
444 3
                'host' => $configuration->getSolrHost('localhost', 'write'),
445 3
                'port' => $configuration->getSolrPort(8983, 'write'),
446 3
                'path' => $configuration->getSolrPath('/solr/core_en/', 'write'),
447 3
                'username' => $configuration->getSolrUsername('', 'write'),
448 3
                'password' => $configuration->getSolrPassword('', 'write'),
449 3
                'timeout' => $configuration->getSolrTimeout(0, 'write')
450
            ],
451
452 3
            'language' => $languageId
453
        ];
454
455 3
        $connection['label'] = $this->buildConnectionLabel($connection);
456 3
        return $connection;
457
    }
458
459
    /**
460
     * Creates a human readable label from the connections' configuration.
461
     *
462
     * @param array $connection Connection configuration
463
     * @return string Connection label
464
     */
465 3
    protected function buildConnectionLabel(array $connection)
466
    {
467 3
        return $connection['rootPageTitle']
468 3
            . ' (pid: ' . $connection['rootPageUid']
469 3
            . ', language: ' . $this->systemLanguageRepository->findOneLanguageTitleByLanguageId($connection['language'])
470 3
            . ') - Read node: '
471 3
            . $connection['read']['host'] . ':'
472 3
            . $connection['read']['port']
473 3
            . $connection['read']['path']
474 3
            .' - Write node: '
475 3
            . $connection['write']['host'] . ':'
476 3
            . $connection['write']['port']
477 3
            . $connection['write']['path'];
478
    }
479
480
    /**
481
     * Filters duplicate connections. When detecting the configured connections
482
     * this is done with a little brute force by simply combining all root pages
483
     * with all languages, this method filters out the duplicates.
484
     *
485
     * @param array $connections An array of unfiltered connections, containing duplicates
486
     * @return array An array with connections, no duplicates.
487
     * @deprecated will be removed in v11, use SiteRepository
488
     */
489 3
    protected function filterDuplicateConnections(array $connections)
490
    {
491 3
        trigger_error('Method filterDuplicateConnections is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
492
493 3
        $hashedConnections = [];
494 3
        $filteredConnections = [];
495
496
        // array_unique() doesn't work on multi dimensional arrays, so we need to flatten it first
497 3
        foreach ($connections as $key => $connection) {
498 3
            unset($connection['language']);
499 3
            $connectionHash = md5(implode('|', $connection));
500 3
            $hashedConnections[$key] = $connectionHash;
501
        }
502
503 3
        $hashedConnections = array_unique($hashedConnections);
504
505 3
        foreach ($hashedConnections as $key => $hash) {
506 3
            $filteredConnections[$key] = $connections[$key];
507
        }
508
509 3
        return $filteredConnections;
510
    }
511
}
512