|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Site; |
|
4
|
|
|
|
|
5
|
|
|
/*************************************************************** |
|
6
|
|
|
* Copyright notice |
|
7
|
|
|
* |
|
8
|
|
|
* (c) 2017 - Thomas Hohn <[email protected]> |
|
9
|
|
|
* All rights reserved |
|
10
|
|
|
* |
|
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
|
12
|
|
|
* free software; you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU General Public License as published by |
|
14
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
15
|
|
|
* (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* The GNU General Public License can be found at |
|
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
|
19
|
|
|
* |
|
20
|
|
|
* This script is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
|
26
|
|
|
***************************************************************/ |
|
27
|
|
|
|
|
28
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\RootPageResolver; |
|
29
|
|
|
use ApacheSolrForTypo3\Solr\Site; |
|
30
|
|
|
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache; |
|
31
|
|
|
use TYPO3\CMS\Core\Registry; |
|
32
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* SiteRepository |
|
36
|
|
|
* |
|
37
|
|
|
* Responsible to retrieve instances of Site objects |
|
38
|
|
|
* |
|
39
|
|
|
* @author Thomas Hohn <[email protected]> |
|
40
|
|
|
*/ |
|
41
|
|
|
class SiteRepository |
|
42
|
|
|
{ |
|
43
|
|
|
/** |
|
44
|
|
|
* Rootpage resolver |
|
45
|
|
|
* |
|
46
|
|
|
* @var RootPageResolver |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $rootPageResolver; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var TwoLevelCache |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $runtimeCache; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var Registry |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $registry; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* SiteRepository constructor. |
|
62
|
|
|
* |
|
63
|
|
|
* @param RootPageResolver|null $rootPageResolver |
|
64
|
|
|
* @param TwoLevelCache|null $twoLevelCache |
|
65
|
|
|
* @param Registry|null $registry |
|
66
|
|
|
*/ |
|
67
|
115 |
|
public function __construct(RootPageResolver $rootPageResolver = null, TwoLevelCache $twoLevelCache = null, Registry $registry = null) |
|
68
|
|
|
{ |
|
69
|
115 |
|
$this->rootPageResolver = isset($rootPageResolver) ? $rootPageResolver : GeneralUtility::makeInstance(RootPageResolver::class); |
|
70
|
115 |
|
$this->runtimeCache = isset($twoLevelCache) ? $twoLevelCache : GeneralUtility::makeInstance(TwoLevelCache::class, 'cache_runtime'); |
|
71
|
115 |
|
$this->registry = isset($registry) ? $registry : GeneralUtility::makeInstance(Registry::class); |
|
72
|
115 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Gets the Site for a specific page Id. |
|
76
|
|
|
* |
|
77
|
|
|
* @param int $pageId The page Id to get a Site object for. |
|
78
|
|
|
* @return Site Site for the given page Id. |
|
79
|
|
|
*/ |
|
80
|
65 |
|
public function getSiteByPageId($pageId) |
|
81
|
|
|
{ |
|
82
|
65 |
|
$rootPageId = $this->rootPageResolver->getRootPageId($pageId); |
|
83
|
65 |
|
return $this->getSiteByRootPageId($rootPageId); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Gets the Site for a specific root page Id. |
|
88
|
|
|
* |
|
89
|
|
|
* @param int $rootPageId Root page Id to get a Site object for. |
|
90
|
|
|
* @return Site Site for the given page Id. |
|
91
|
|
|
*/ |
|
92
|
73 |
|
public function getSiteByRootPageId($rootPageId) |
|
93
|
|
|
{ |
|
94
|
73 |
|
$cacheId = 'SiteRepository' . '_' . 'getSiteByPageId' . '_' . $rootPageId; |
|
95
|
|
|
|
|
96
|
73 |
|
$methodResult = $this->runtimeCache->get($cacheId); |
|
97
|
73 |
|
if (!empty($methodResult)) { |
|
98
|
59 |
|
return $methodResult; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
73 |
|
$methodResult = $this->buildSite($rootPageId); |
|
102
|
71 |
|
$this->runtimeCache->set($cacheId, $methodResult); |
|
103
|
|
|
|
|
104
|
71 |
|
return $methodResult; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns the first available Site. |
|
109
|
|
|
* |
|
110
|
|
|
* @param bool $stopOnInvalidSite |
|
111
|
|
|
* @return Site |
|
112
|
|
|
*/ |
|
113
|
22 |
|
public function getFirstAvailableSite($stopOnInvalidSite = false) |
|
114
|
|
|
{ |
|
115
|
22 |
|
$sites = $this->getAvailableSites($stopOnInvalidSite); |
|
116
|
22 |
|
return array_shift($sites); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Gets all available TYPO3 sites with Solr configured. |
|
121
|
|
|
* |
|
122
|
|
|
* @param bool $stopOnInvalidSite |
|
123
|
|
|
* @return Site[] An array of available sites |
|
124
|
|
|
*/ |
|
125
|
65 |
|
public function getAvailableSites($stopOnInvalidSite = false) |
|
126
|
|
|
{ |
|
127
|
65 |
|
$sites = []; |
|
128
|
65 |
|
$cacheId = 'SiteRepository' . '_' . 'getAvailableSites'; |
|
129
|
|
|
|
|
130
|
65 |
|
$methodResult = $this->runtimeCache->get($cacheId); |
|
131
|
65 |
|
if (!empty($methodResult)) { |
|
132
|
15 |
|
return $methodResult; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
65 |
|
$servers = $this->getSolrServersFromRegistry(); |
|
136
|
65 |
|
foreach ($servers as $server) { |
|
137
|
65 |
|
if (isset($sites[$server['rootPageUid']])) { |
|
138
|
|
|
//get each site only once |
|
139
|
4 |
|
continue; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
try { |
|
143
|
65 |
|
$sites[$server['rootPageUid']] = $this->buildSite($server['rootPageUid']); |
|
144
|
65 |
|
} catch (\InvalidArgumentException $e) { |
|
145
|
4 |
|
if ($stopOnInvalidSite) { |
|
146
|
|
|
throw $e; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
65 |
|
} |
|
150
|
|
|
|
|
151
|
65 |
|
$methodResult = $sites; |
|
152
|
65 |
|
$this->runtimeCache->set($cacheId, $methodResult); |
|
153
|
|
|
|
|
154
|
65 |
|
return $methodResult; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Gets the system languages (IDs) for which Solr connections have been |
|
159
|
|
|
* configured. |
|
160
|
|
|
* |
|
161
|
|
|
* @return array Array of system language IDs for which connections have been configured on this site. |
|
162
|
|
|
*/ |
|
163
|
1 |
|
public function getAllLanguages(Site $site) |
|
164
|
|
|
{ |
|
165
|
1 |
|
$siteLanguages = []; |
|
166
|
1 |
|
$servers = $this->getSolrServersFromRegistry(); |
|
167
|
|
|
|
|
168
|
1 |
|
foreach ($servers as $connectionKey => $solrConnection) { |
|
169
|
1 |
|
list($siteRootPageId, $systemLanguageId) = explode('|', $connectionKey); |
|
170
|
|
|
|
|
171
|
1 |
|
if ($siteRootPageId == $site->getRootPageId()) { |
|
172
|
1 |
|
$siteLanguages[] = $systemLanguageId; |
|
173
|
1 |
|
} |
|
174
|
1 |
|
} |
|
175
|
|
|
|
|
176
|
1 |
|
return $siteLanguages; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Creates an instance of the Site object. |
|
181
|
|
|
* |
|
182
|
|
|
* @param integer $rootPageId |
|
183
|
|
|
* @return Site |
|
184
|
|
|
*/ |
|
185
|
108 |
|
protected function buildSite($rootPageId) |
|
186
|
|
|
{ |
|
187
|
108 |
|
return GeneralUtility::makeInstance(Site::class, $rootPageId); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Retrieves the configured solr servers from the registry. |
|
192
|
|
|
* |
|
193
|
|
|
* @return array |
|
194
|
|
|
*/ |
|
195
|
65 |
|
protected function getSolrServersFromRegistry() |
|
196
|
|
|
{ |
|
197
|
65 |
|
$servers = (array)$this->registry->get('tx_solr', 'servers', []); |
|
198
|
65 |
|
return $servers; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|