Completed
Push — master ( d4002d...00e24a )
by Timo
41:06 queued 37:53
created

ConnectionManager::getAllConfigurations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 7
cp 0
rs 10
cc 3
nc 3
nop 0
crap 12
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 = GeneralUtility::makeInstance(
137
                NoSolrConnectionFoundException::class,
138
                /** @scrutinizer ignore-type */  'Could not find a Solr connection for page [' . $pageId. '] and language [' . $language . '].',
139
                /** @scrutinizer ignore-type */ 1575396474
140
            );
141
            $noSolrConnectionException->setLanguageId($language);
142
            throw $noSolrConnectionException;
143
        }
144
    }
145
146
    /**
147
     * Gets a Solr connection for a page ID.
148
     *
149
     * @param int $pageId A page ID.
150
     * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0.
151
     * @param string $mount Comma list of MountPoint parameters
152
     * @return SolrConnection A solr connection.
153
     * @throws NoSolrConnectionFoundException
154
     */
155 93
    public function getConnectionByPageId($pageId, $language = 0, $mount = '')
156
    {
157
        try {
158 93
            $site = $this->siteRepository->getSiteByPageId($pageId, $mount);
159 92
            $config = $site->getSolrConnectionConfiguration($language);
160 92
            $solrConnection = $this->getConnectionFromConfiguration($config);
161
162 92
            return $solrConnection;
163 2
        } catch(InvalidArgumentException $e) {
164
            /* @var NoSolrConnectionFoundException $noSolrConnectionException */
165 1
            $noSolrConnectionException = GeneralUtility::makeInstance(
166 1
                NoSolrConnectionFoundException::class,
167 1
                /** @scrutinizer ignore-type */  'Could not find a Solr connection for page [' . $pageId. '] and language [' . $language . '].',
168 1
                /** @scrutinizer ignore-type */ 1575396474
169
            );
170 1
            $noSolrConnectionException->setLanguageId($language);
171 1
            throw $noSolrConnectionException;
172
        }
173
    }
174
175
    /**
176
     * Gets a Solr configuration for a root page ID.
177
     *
178
     * @param int $pageId A root page ID.
179
     * @param int $language The language ID to get the configuration for as the path may differ. Optional, defaults to 0.
180
     * @return array A solr configuration.
181
     * @throws NoSolrConnectionFoundException
182
     * @deprecated will be removed in v11, use Site object/SiteRepository directly
183
     */
184
    public function getConfigurationByRootPageId($pageId, $language = 0)
185
    {
186
        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);
187
188
        try {
189
            $site = $this->siteRepository->getSiteByRootPageId($pageId);
190
            return $site->getSolrConnectionConfiguration($language);
191
        } catch(InvalidArgumentException $e) {
192
            /* @var NoSolrConnectionFoundException $noSolrConnectionException */
193
            $noSolrConnectionException = GeneralUtility::makeInstance(
194
                NoSolrConnectionFoundException::class,
195
                /** @scrutinizer ignore-type */  'Could not find a Solr connection for page [' . $pageId. '] and language [' . $language . '].',
196
                /** @scrutinizer ignore-type */ 1575396474
197
            );
198
            $noSolrConnectionException->setLanguageId($language);
199
            throw $noSolrConnectionException;
200
        }
201
    }
202
203
    /**
204
     * Gets a Solr connection for a root page ID.
205
     *
206
     * @param int $pageId A root page ID.
207
     * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0.
208
     * @return SolrConnection A solr connection.
209
     * @throws NoSolrConnectionFoundException
210
     */
211 12
    public function getConnectionByRootPageId($pageId, $language = 0)
212
    {
213
        try {
214 12
            $site = $this->siteRepository->getSiteByRootPageId($pageId);
215 11
            $config = $site->getSolrConnectionConfiguration($language);
216 11
            $solrConnection = $this->getConnectionFromConfiguration($config);
217 11
            return $solrConnection;
218 1
        } catch (InvalidArgumentException $e) {
219
            /* @var NoSolrConnectionFoundException $noSolrConnectionException */
220 1
            $noSolrConnectionException = GeneralUtility::makeInstance(
221 1
                NoSolrConnectionFoundException::class,
222 1
                /** @scrutinizer ignore-type */  'Could not find a Solr connection for page [' . $pageId. '] and language [' . $language . '].',
223 1
                /** @scrutinizer ignore-type */ 1875396474
224
            );
225 1
            $noSolrConnectionException->setLanguageId($language);
226 1
            throw $noSolrConnectionException;
227
        }
228
    }
229
230
    /**
231
     * Gets all connection configurations found.
232
     *
233
     * @return array An array of connection configurations.
234
     * @throws NoSolrConnectionFoundException
235
     * @deprecated will be removed in v11, use SiteRepository
236
     */
237
    public function getAllConfigurations()
238
    {
239
        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);
240
241
        $solrConfigurations = [];
242
        foreach ($this->siteRepository->getAvailableSites() as $site) {
243
            foreach ($site->getAllSolrConnectionConfigurations() as $solrConfiguration) {
244
                $solrConfigurations[] = $solrConfiguration;
245
            }
246
        }
247
248
        return $solrConfigurations;
249
    }
250
251
    /**
252
     * Stores the connections in the registry.
253
     *
254
     * @param array $solrConfigurations
255
     * @deprecated will be removed in v11, use SiteRepository
256
     */
257
    protected function setAllConfigurations(array $solrConfigurations)
258
    {
259
        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);
260
261
        /** @var $registry Registry */
262
        $registry = GeneralUtility::makeInstance(Registry::class);
263
        $registry->set('tx_solr', 'servers', $solrConfigurations);
264
    }
265
266
    /**
267
     * Gets all connections found.
268
     *
269
     * @return SolrConnection[] An array of initialized ApacheSolrForTypo3\Solr\System\Solr\SolrConnection connections
270
     * @throws NoSolrConnectionFoundException
271
     */
272 4
    public function getAllConnections()
273
    {
274 4
        $solrConnections = [];
275 4
        foreach ($this->siteRepository->getAvailableSites() as $site) {
276
            foreach ($site->getAllSolrConnectionConfigurations() as $solrConfiguration) {
277
                $solrConnections[] = $this->getConnectionFromConfiguration($solrConfiguration);
278
            }
279
        }
280
281 4
        return $solrConnections;
282
    }
283
284
    /**
285
     * Gets all connection configurations for a given site.
286
     *
287
     * @param Site $site A TYPO3 site
288
     * @return array An array of Solr connection configurations for a site
289
     * @throws NoSolrConnectionFoundException
290
     * @deprecated will be removed in v11, use $site->getAllSolrConnectionConfigurations()
291
     */
292
    public function getConfigurationsBySite(Site $site)
293
    {
294
        trigger_error('solr:deprecation: Method getConfigurationsBySite is deprecated since EXT:solr 10 and will be removed in v11, use $site->getAllSolrConnectionConfigurations()', E_USER_DEPRECATED);
295
296
        return $site->getAllSolrConnectionConfigurations();
297
    }
298
299
    /**
300
     * Gets all connections configured for a given site.
301
     *
302
     * @param Site $site A TYPO3 site
303
     * @return SolrConnection[] An array of Solr connection objects (ApacheSolrForTypo3\Solr\System\Solr\SolrConnection)
304
     * @throws NoSolrConnectionFoundException
305
     */
306 13
    public function getConnectionsBySite(Site $site)
307
    {
308 13
        $connections = [];
309
310 13
        foreach ($site->getAllSolrConnectionConfigurations() as $solrConnectionConfiguration) {
311 13
            $connections[] = $this->getConnectionFromConfiguration($solrConnectionConfiguration);
312
        }
313
314 13
        return $connections;
315
    }
316
317
    // updates
318
319
    /**
320
     * Updates the connections in the registry.
321
     *
322
     * @deprecated will be removed in v11, use SiteRepository
323
     */
324
    public function updateConnections()
325
    {
326
        trigger_error('solr:deprecation: Method updateConnections is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
327
328
        $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

328
        $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...
329
        $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

329
        $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...
330
331
        if (!empty($solrConnections)) {
332
            $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

332
            /** @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...
333
        }
334
    }
335
336
    /**
337
     * Updates the Solr connections for a specific root page ID / site.
338
     *
339
     * @param int $rootPageId A site root page id
340
     * @throws NoSolrConnectionFoundException
341
     * @deprecated Use TYPO3 site config to configure site/connection info
342
     */
343
    public function updateConnectionByRootPageId($rootPageId)
344
    {
345
        trigger_error('solr:deprecation: Method updateConnectionByRootPageId is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
346
347
        $systemLanguages = $this->systemLanguageRepository->findSystemLanguages();
348
        /* @var SiteRepository $siteRepository */
349
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
350
        $site = $siteRepository->getSiteByRootPageId($rootPageId);
351
        $rootPage = $site->getRootPage();
352
353
        $updatedSolrConnections = [];
354
        foreach ($systemLanguages as $languageId) {
355
            $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

355
            $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...
356
357
            if (!empty($connection)) {
358
                $updatedSolrConnections[$connection['connectionKey']] = $connection;
359
            }
360
        }
361
362
        $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

362
        $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...
363
        $solrConnections = array_merge($solrConnections, $updatedSolrConnections);
364
        $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

364
        $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...
365
        $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

365
        /** @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...
366
    }
367
368
    /**
369
     * Finds the configured Solr connections. Also respects multi-site
370
     * environments.
371
     *
372
     * @return array An array with connections, each connection with keys rootPageTitle, rootPageUid, solrHost, solrPort, solrPath
373
     * @deprecated will be removed in v11, use SiteRepository
374
     */
375
    protected function getConfiguredSolrConnections()
376
    {
377
        trigger_error('solr:deprecation: Method getConfiguredSolrConnections is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
378
379
        $configuredSolrConnections = [];
380
        // find website roots and languages for this installation
381
        $rootPages = $this->pagesRepositoryAtExtSolr->findAllRootPages();
382
        $languages = $this->systemLanguageRepository->findSystemLanguages();
383
384
        // find solr configurations and add them as function menu entries
385
        foreach ($rootPages as $rootPage) {
386
            foreach ($languages as $languageId) {
387
                $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

387
                $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...
388
389
                if (!empty($connection)) {
390
                    $configuredSolrConnections[$connection['connectionKey']] = $connection;
391
                }
392
            }
393
        }
394
395
        return $configuredSolrConnections;
396
    }
397
398
    /**
399
     * Gets the configured Solr connection for a specific root page and language ID.
400
     *
401
     * @param array $rootPage A root page record with at least title and uid
402
     * @param int $languageId ID of a system language
403
     * @return array A solr connection configuration.
404
     * @deprecated will be removed in v11, use SiteRepository
405
     */
406
    protected function getConfiguredSolrConnectionByRootPage(array $rootPage, $languageId)
407
    {
408
        trigger_error('solr:deprecation: Method getConfiguredSolrConnectionByRootPage is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
409
410
        $connection = [];
411
412
        $languageId = (int)$languageId;
413
        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

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