Passed
Push — master ( c7951d...b759e5 )
by Timo
12:28
created

buildNoConnectionExceptionForPageAndLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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
    public function __construct(SystemLanguageRepository $systemLanguageRepository = null, PagesRepositoryAtExtSolr $pagesRepositoryAtExtSolr = null, SiteRepository $siteRepository = null)
78 118
    {
79
        $this->systemLanguageRepository = $systemLanguageRepository ?? GeneralUtility::makeInstance(SystemLanguageRepository::class);
80 118
        $this->siteRepository           = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
81 118
        $this->pagesRepositoryAtExtSolr = $pagesRepositoryAtExtSolr ?? GeneralUtility::makeInstance(PagesRepositoryAtExtSolr::class);
82 118
    }
83 118
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
    public function getSolrConnectionForNodes(array $readNodeConfiguration, array $writeNodeConfiguration)
92
    {
93
        $connectionHash = md5(json_encode($readNodeConfiguration) .  json_encode($writeNodeConfiguration));
94
        if (!isset(self::$connections[$connectionHash])) {
95
            $readNode = Node::fromArray($readNodeConfiguration);
96
            $writeNode = Node::fromArray($writeNodeConfiguration);
97
            self::$connections[$connectionHash] = GeneralUtility::makeInstance(SolrConnection::class, $readNode, $writeNode);
98
        }
99
        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
    public function getConnectionFromConfiguration(array $config)
109
    {
110
        if(empty($config['read']) && !empty($config['solrHost'])) {
111
            throw new InvalidArgumentException('Invalid registry data please re-initialize your solr connections');
112
        }
113
114
        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 109
     * @param string $mount Comma list of MountPoint parameters
123
     * @deprecated will be removed in v11, use Site object/SiteRepository directly
124 109
     * @return array A solr configuration.
125 109
     * @throws NoSolrConnectionFoundException
126 109
     */
127 109
    public function getConfigurationByPageId(int $pageId, int $language = 0, string $mount = '')
128 109
    {
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 109
131
        try {
132
            $site = $this->siteRepository->getSiteByPageId($pageId, $mount);
133
            $this->throwExceptionOnInvalidSite($site, 'No site for pageId ' . $pageId);
134
            return $site->getSolrConnectionConfiguration($language);
135
        } catch(InvalidArgumentException $e) {
136
            /* @var NoSolrConnectionFoundException $noSolrConnectionException */
137
            $noSolrConnectionException = $this->buildNoConnectionExceptionForPageAndLanguage($pageId, $language);
138
            throw $noSolrConnectionException;
139 109
        }
140
    }
141 109
142
    /**
143
     * Gets a Solr connection for a page ID.
144
     *
145 109
     * @param int $pageId A page ID.
146
     * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0.
147
     * @param string $mount Comma list of MountPoint parameters
148
     * @return SolrConnection A solr connection.
149
     * @throws NoSolrConnectionFoundException
150
     */
151
    public function getConnectionByPageId($pageId, $language = 0, $mount = '')
152
    {
153
        try {
154
            $site = $this->siteRepository->getSiteByPageId($pageId, $mount);
155
            $this->throwExceptionOnInvalidSite($site, 'No site for pageId ' . $pageId);
156
            $config = $site->getSolrConnectionConfiguration($language);
157 97
            $solrConnection = $this->getConnectionFromConfiguration($config);
158
            return $solrConnection;
159
        } catch(InvalidArgumentException $e) {
160 97
            $noSolrConnectionException = $this->buildNoConnectionExceptionForPageAndLanguage($pageId, $language);
161
            throw $noSolrConnectionException;
162
        }
163 97
    }
164 97
165
    /**
166
     * Gets a Solr configuration for a root page ID.
167 97
     *
168 4
     * @param int $pageId A root page ID.
169
     * @param int $language The language ID to get the configuration for as the path may differ. Optional, defaults to 0.
170 4
     * @return array A solr configuration.
171 4
     * @throws NoSolrConnectionFoundException
172 4
     * @deprecated will be removed in v11, use Site object/SiteRepository directly
173 4
     */
174
    public function getConfigurationByRootPageId($pageId, $language = 0)
175 4
    {
176
        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);
177 4
178
        try {
179
            $site = $this->siteRepository->getSiteByRootPageId($pageId);
180 94
            $this->throwExceptionOnInvalidSite($site, 'No site for pageId ' . $pageId);
181
182
            return $site->getSolrConnectionConfiguration($language);
183
        } catch(InvalidArgumentException $e) {
184
            /* @var NoSolrConnectionFoundException $noSolrConnectionException */
185
            $noSolrConnectionException = $this->buildNoConnectionExceptionForPageAndLanguage($pageId, $language);
186
            throw $noSolrConnectionException;
187
        }
188
    }
189
190
    /**
191
     * Gets a Solr connection for a root page ID.
192 97
     *
193
     * @param int $pageId A root page ID.
194 97
     * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0.
195 94
     * @return SolrConnection A solr connection.
196 94
     * @throws NoSolrConnectionFoundException
197
     */
198
    public function getConnectionByRootPageId($pageId, $language = 0)
199
    {
200
        try {
201
            $site = $this->siteRepository->getSiteByRootPageId($pageId);
202
            $this->throwExceptionOnInvalidSite($site, 'No site for pageId ' . $pageId);
203
            $config = $site->getSolrConnectionConfiguration($language);
204
            $solrConnection = $this->getConnectionFromConfiguration($config);
205
            return $solrConnection;
206
        } catch (InvalidArgumentException $e) {
207 98
            /* @var NoSolrConnectionFoundException $noSolrConnectionException */
208
            $noSolrConnectionException = $this->buildNoConnectionExceptionForPageAndLanguage($pageId, $language);
209 98
            throw $noSolrConnectionException;
210 98
        }
211
    }
212 98
213 96
    /**
214
     * Gets all connection configurations found.
215
     *
216 5
     * @return array An array of connection configurations.
217 5
     * @throws NoSolrConnectionFoundException
218 5
     * @deprecated will be removed in v11, use SiteRepository
219 5
     */
220
    public function getAllConfigurations()
221 5
    {
222 5
        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);
223
224 5
        $solrConfigurations = [];
225
        foreach ($this->siteRepository->getAvailableSites() as $site) {
226
            foreach ($site->getAllSolrConnectionConfigurations() as $solrConfiguration) {
227 96
                $solrConfigurations[] = $solrConfiguration;
228
            }
229
        }
230
231
        return $solrConfigurations;
232
    }
233
234
    /**
235
     * Stores the connections in the registry.
236
     *
237
     * @param array $solrConfigurations
238 10
     * @deprecated will be removed in v11, use SiteRepository
239
     */
240 10
    protected function setAllConfigurations(array $solrConfigurations)
241 10
    {
242
        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);
243 10
244
        /** @var $registry Registry */
245
        $registry = GeneralUtility::makeInstance(Registry::class);
246
        $registry->set('tx_solr', 'servers', $solrConfigurations);
247
    }
248
249
    /**
250
     * Gets all connections found.
251 111
     *
252
     * @return SolrConnection[] An array of initialized ApacheSolrForTypo3\Solr\System\Solr\SolrConnection connections
253
     * @throws NoSolrConnectionFoundException
254 111
     */
255 111
    public function getAllConnections()
256
    {
257 111
        $solrConnections = [];
258
        foreach ($this->siteRepository->getAvailableSites() as $site) {
259
            foreach ($site->getAllSolrConnectionConfigurations() as $solrConfiguration) {
260
                $solrConnections[] = $this->getConnectionFromConfiguration($solrConfiguration);
261
            }
262
        }
263
264
        return $solrConnections;
265 3
    }
266
267
    /**
268 3
     * Gets all connection configurations for a given site.
269 3
     *
270 3
     * @param Site $site A TYPO3 site
271
     * @return array An array of Solr connection configurations for a site
272
     * @throws NoSolrConnectionFoundException
273
     * @deprecated will be removed in v11, use $site->getAllSolrConnectionConfigurations()
274
     */
275
    public function getConfigurationsBySite(Site $site)
276
    {
277 7
        trigger_error('solr:deprecation: Method getConfigurationsBySite is deprecated since EXT:solr 10 and will be removed in v11, use $site->getAllSolrConnectionConfigurations()', E_USER_DEPRECATED);
278
279 7
        return $site->getAllSolrConnectionConfigurations();
280
    }
281 7
282 7
    /**
283 7
     * Gets all connections configured for a given site.
284
     *
285
     * @param Site $site A TYPO3 site
286 7
     * @return SolrConnection[] An array of Solr connection objects (ApacheSolrForTypo3\Solr\System\Solr\SolrConnection)
287
     * @throws NoSolrConnectionFoundException
288
     */
289
    public function getConnectionsBySite(Site $site)
290
    {
291
        $connections = [];
292
293
        foreach ($site->getAllSolrConnectionConfigurations() as $solrConnectionConfiguration) {
294
            $connections[] = $this->getConnectionFromConfiguration($solrConnectionConfiguration);
295 31
        }
296
297 31
        return $connections;
298
    }
299 31
300 31
    // updates
301 31
302 31
    /**
303
     * Updates the connections in the registry.
304
     *
305
     * @deprecated will be removed in v11, use SiteRepository
306 31
     */
307
    public function updateConnections()
308
    {
309
        trigger_error('solr:deprecation: Method updateConnections is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
310
311
        $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

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

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

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

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

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

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

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

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

396
        /** @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...
397
        $connectionKey = $rootPage['uid'] . '|' . $languageId;
398
399
        $pageSelect = GeneralUtility::makeInstance(PageRepository::class);
400 2
401
        $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $rootPage['uid']);
402 2
        try {
403
            $rootLine = $rootlineUtility->get();
404 2
        } catch (RuntimeException $e) {
405 2
            $rootLine = [];
406
        }
407
408 2
        $tmpl = GeneralUtility::makeInstance(ExtendedTemplateService::class);
409 2
        $tmpl->tt_track = false; // Do not log time-performance information
410 2
        $tmpl->init();
411
        $tmpl->runThroughTemplates($rootLine); // This generates the constants/config + hierarchy info for the template.
412 2
413 2
        // fake micro TSFE to get correct condition parsing
414
        $GLOBALS['TSFE'] = new stdClass();
415
        $GLOBALS['TSFE']->tmpl = new stdClass();
416
        $GLOBALS['TSFE']->cObjectDepthCounter = 50;
417
        $GLOBALS['TSFE']->tmpl->rootLine = $rootLine;
418 2
        // @extensionScannerIgnoreLine
419
        $GLOBALS['TSFE']->sys_page = $pageSelect;
420
        $GLOBALS['TSFE']->id = $rootPage['uid'];
421
        $GLOBALS['TSFE']->page = $rootPage;
422
423
        $tmpl->generateConfig();
424
        $GLOBALS['TSFE']->tmpl->setup = $tmpl->setup;
425
426
        $configuration = Util::getSolrConfigurationFromPageId($rootPage['uid'], false, $languageId);
427
428 3
        $solrIsEnabledAndConfigured = $configuration->getEnabled() && $configuration->getSolrHasConnectionConfiguration();
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...nnectionConfiguration() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

428
        $solrIsEnabledAndConfigured = $configuration->getEnabled() && /** @scrutinizer ignore-deprecated */ $configuration->getSolrHasConnectionConfiguration();

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...
429
        if (!$solrIsEnabledAndConfigured) {
430 3
            return $connection;
431
        }
432 3
433 3
        $connection = [
434 3
            'connectionKey' => $connectionKey,
435
            'rootPageTitle' => $rootPage['title'],
436 3
            'rootPageUid' => $rootPage['uid'],
437 3
            'read' => [
438
                'scheme' => $configuration->getSolrScheme(),
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...ration::getSolrScheme() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

438
                'scheme' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrScheme(),

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...
439 3
                'host' => $configuration->getSolrHost(),
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...guration::getSolrHost() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

439
                'host' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrHost(),

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...
440 3
                'port' => $configuration->getSolrPort(),
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...guration::getSolrPort() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

440
                'port' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrPort(),

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...
441 3
                'path' => $configuration->getSolrPath(),
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...guration::getSolrPath() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

441
                'path' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrPath(),

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...
442 3
                'username' => $configuration->getSolrUsername(),
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...tion::getSolrUsername() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

442
                'username' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrUsername(),

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...
443
                'password' => $configuration->getSolrPassword(),
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...tion::getSolrPassword() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

443
                'password' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrPassword(),

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...
444
                'timeout' => $configuration->getSolrTimeout()
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...ation::getSolrTimeout() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

444
                'timeout' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrTimeout()

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...
445 3
            ],
446 3
            'write' => [
447 3
                'scheme' => $configuration->getSolrScheme('http', 'write'),
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...ration::getSolrScheme() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

447
                'scheme' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrScheme('http', 'write'),

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...
448 3
                'host' => $configuration->getSolrHost('localhost', 'write'),
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...guration::getSolrHost() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

448
                'host' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrHost('localhost', 'write'),

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...
449 3
                'port' => $configuration->getSolrPort(8983, 'write'),
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...guration::getSolrPort() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

449
                'port' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrPort(8983, 'write'),

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...
450 3
                'path' => $configuration->getSolrPath('/solr/core_en/', 'write'),
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...guration::getSolrPath() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

450
                'path' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrPath('/solr/core_en/', 'write'),

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...
451 3
                'username' => $configuration->getSolrUsername('', 'write'),
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...tion::getSolrUsername() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

451
                'username' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrUsername('', 'write'),

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...
452
                'password' => $configuration->getSolrPassword('', 'write'),
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...tion::getSolrPassword() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

452
                'password' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrPassword('', 'write'),

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...
453 3
                'timeout' => $configuration->getSolrTimeout(0, 'write')
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...ation::getSolrTimeout() has been deprecated: Configuring solr connections with TypoScript is deprecated please use the site handling. Will be dropped with EXT:solr 11 ( Ignorable by Annotation )

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

453
                'timeout' => /** @scrutinizer ignore-deprecated */ $configuration->getSolrTimeout(0, 'write')

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...
454 3
            ],
455
456 3
            'language' => $languageId
457
        ];
458 3
459 3
        $connection['label'] = $this->buildConnectionLabel($connection);
460
        return $connection;
461
    }
462
463
    /**
464 3
     * Creates a human readable label from the connections' configuration.
465 3
     *
466 3
     * @param array $connection Connection configuration
467
     * @return string Connection label
468 3
     */
469 3
    protected function buildConnectionLabel(array $connection)
470 3
    {
471 3
        return $connection['rootPageTitle']
472 3
            . ' (pid: ' . $connection['rootPageUid']
473 3
            . ', language: ' . $this->systemLanguageRepository->findOneLanguageTitleByLanguageId($connection['language'])
474 3
            . ') - Read node: '
475
            . $connection['read']['host'] . ':'
476
            . $connection['read']['port']
477 3
            . $connection['read']['path']
478 3
            .' - Write node: '
479 3
            . $connection['write']['host'] . ':'
480 3
            . $connection['write']['port']
481 3
            . $connection['write']['path'];
482 3
    }
483 3
484
    /**
485
     * Filters duplicate connections. When detecting the configured connections
486 3
     * this is done with a little brute force by simply combining all root pages
487
     * with all languages, this method filters out the duplicates.
488
     *
489 3
     * @param array $connections An array of unfiltered connections, containing duplicates
490 3
     * @return array An array with connections, no duplicates.
491
     * @deprecated will be removed in v11, use SiteRepository
492
     */
493
    protected function filterDuplicateConnections(array $connections)
494
    {
495
        trigger_error('solr:deprecation: Method filterDuplicateConnections is deprecated since EXT:solr 10 and will be removed in v11, use sitehandling instead', E_USER_DEPRECATED);
496
497
        $hashedConnections = [];
498
        $filteredConnections = [];
499
500
        // array_unique() doesn't work on multi dimensional arrays, so we need to flatten it first
501 3
        foreach ($connections as $key => $connection) {
502
            unset($connection['language']);
503 3
            $connectionHash = md5(implode('|', $connection));
504 3
            $hashedConnections[$key] = $connectionHash;
505 3
        }
506 3
507 3
        $hashedConnections = array_unique($hashedConnections);
508 3
509 3
        foreach ($hashedConnections as $key => $hash) {
510 3
            $filteredConnections[$key] = $connections[$key];
511 3
        }
512 3
513 3
        return $filteredConnections;
514
    }
515
516
    /**
517
     * @param $pageId
518
     * @param $language
519
     * @return NoSolrConnectionFoundException
520
     */
521
    protected function buildNoConnectionExceptionForPageAndLanguage($pageId, $language): NoSolrConnectionFoundException
522
    {
523
        $message = 'Could not find a Solr connection for page [' . $pageId . '] and language [' . $language . '].';
524 3
        $noSolrConnectionException = $this->buildNoConnectionException($message);
525
526 3
        $noSolrConnectionException->setLanguageId($language);
527 3
        return $noSolrConnectionException;
528
    }
529
530 3
    /**
531 3
     * Throws a no connection exception when no site was passed.
532 3
     *
533 3
     * @param Site|null $site
534
     * @param $message
535
     * @throws NoSolrConnectionFoundException
536 3
     */
537
    protected function throwExceptionOnInvalidSite(?Site $site, string $message)
538 3
    {
539 3
        if (!is_null($site)) {
540
            return;
541
        }
542 3
543
        throw $this->buildNoConnectionException($message);
544
    }
545
546
    /**
547
     * Build a NoSolrConnectionFoundException with the passed message.
548
     * @param string $message
549
     * @return NoSolrConnectionFoundException
550
     */
551
    protected function buildNoConnectionException(string $message): NoSolrConnectionFoundException
552
    {
553
        /* @var NoSolrConnectionFoundException $noSolrConnectionException */
554
        $noSolrConnectionException = GeneralUtility::makeInstance(
555
            NoSolrConnectionFoundException::class,
556
            /** @scrutinizer ignore-type */
557
            $message,
558
            /** @scrutinizer ignore-type */
559
            1575396474
560
        );
561
        return $noSolrConnectionException;
562
    }
563
}
564