Passed
Push — master ( 9fb38e...3b1a8c )
by Timo
04:38
created

ConnectionManager::buildNoConnectionException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

312
        $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...
313
        $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

313
        $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...
314 13
315
        if (!empty($solrConnections)) {
316
            $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

316
            /** @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...
317
        }
318
    }
319
320
    /**
321
     * Updates the Solr connections for a specific root page ID / site.
322
     *
323
     * @param int $rootPageId A site root page id
324
     * @throws NoSolrConnectionFoundException
325
     * @deprecated Use TYPO3 site config to configure site/connection info
326
     */
327
    public function updateConnectionByRootPageId($rootPageId)
328
    {
329
        trigger_error('solr:deprecation: Method updateConnectionByRootPageId is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
330
331
        $systemLanguages = $this->systemLanguageRepository->findSystemLanguages();
332
        /* @var SiteRepository $siteRepository */
333
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
334
        $site = $siteRepository->getSiteByRootPageId($rootPageId);
335
        $rootPage = $site->getRootPage();
336
337
        $updatedSolrConnections = [];
338
        foreach ($systemLanguages as $languageId) {
339
            $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

339
            $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...
340
341
            if (!empty($connection)) {
342
                $updatedSolrConnections[$connection['connectionKey']] = $connection;
343
            }
344
        }
345
346
        $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

346
        $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...
347
        $solrConnections = array_merge($solrConnections, $updatedSolrConnections);
348
        $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

348
        $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...
349
        $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

349
        /** @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...
350
    }
351
352
    /**
353
     * Finds the configured Solr connections. Also respects multi-site
354
     * environments.
355
     *
356
     * @return array An array with connections, each connection with keys rootPageTitle, rootPageUid, solrHost, solrPort, solrPath
357
     * @deprecated will be removed in v11, use SiteRepository
358
     */
359
    protected function getConfiguredSolrConnections()
360
    {
361
        trigger_error('solr:deprecation: Method getConfiguredSolrConnections is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
362
363
        $configuredSolrConnections = [];
364
        // find website roots and languages for this installation
365
        $rootPages = $this->pagesRepositoryAtExtSolr->findAllRootPages();
366
        $languages = $this->systemLanguageRepository->findSystemLanguages();
367
368
        // find solr configurations and add them as function menu entries
369
        foreach ($rootPages as $rootPage) {
370
            foreach ($languages as $languageId) {
371
                $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

371
                $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...
372
373
                if (!empty($connection)) {
374
                    $configuredSolrConnections[$connection['connectionKey']] = $connection;
375
                }
376
            }
377
        }
378
379
        return $configuredSolrConnections;
380
    }
381
382
    /**
383
     * Gets the configured Solr connection for a specific root page and language ID.
384
     *
385
     * @param array $rootPage A root page record with at least title and uid
386
     * @param int $languageId ID of a system language
387
     * @return array A solr connection configuration.
388
     * @deprecated will be removed in v11, use SiteRepository
389
     */
390
    protected function getConfiguredSolrConnectionByRootPage(array $rootPage, $languageId)
391
    {
392
        trigger_error('solr:deprecation: Method getConfiguredSolrConnectionByRootPage is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
393
394
        $connection = [];
395
396
        $languageId = (int)$languageId;
397
        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

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