1 | <?php |
||
2 | namespace ApacheSolrForTypo3\Solr\Report; |
||
3 | |||
4 | /*************************************************************** |
||
5 | * Copyright notice |
||
6 | * |
||
7 | * (c) 2011-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\FrontendEnvironment; |
||
28 | use ApacheSolrForTypo3\Solr\System\Configuration\ExtensionConfiguration; |
||
29 | use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository; |
||
30 | use ApacheSolrForTypo3\Solr\System\Records\SystemDomain\SystemDomainRepository; |
||
31 | use ApacheSolrForTypo3\Solr\System\Service\SiteService; |
||
32 | use ApacheSolrForTypo3\Solr\Util; |
||
33 | use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException; |
||
34 | use TYPO3\CMS\Core\Utility\GeneralUtility; |
||
35 | use TYPO3\CMS\Reports\Status; |
||
36 | |||
37 | /** |
||
38 | * Provides an status report, which checks whether the configuration of the |
||
39 | * extension is ok. |
||
40 | * |
||
41 | * @author Ingo Renner <[email protected]> |
||
42 | */ |
||
43 | class SolrConfigurationStatus extends AbstractSolrStatus |
||
44 | { |
||
45 | /** |
||
46 | * @var SystemDomainRepository |
||
47 | */ |
||
48 | protected $systemDomainRepository; |
||
49 | |||
50 | /** |
||
51 | * @var ExtensionConfiguration |
||
52 | 6 | */ |
|
53 | protected $extensionConfiguration; |
||
54 | 6 | ||
55 | 6 | /** |
|
56 | * @var FrontendEnvironment |
||
57 | */ |
||
58 | protected $fronendEnvironment = null; |
||
59 | |||
60 | /** |
||
61 | * SolrConfigurationStatus constructor. |
||
62 | 6 | * @param SystemDomainRepository|null $systemDomainRepository |
|
63 | * @param ExtensionConfiguration|null $extensionConfiguration |
||
64 | 6 | */ |
|
65 | public function __construct( |
||
66 | 6 | SystemDomainRepository $systemDomainRepository = null, |
|
67 | 6 | ExtensionConfiguration $extensionConfiguration = null, |
|
68 | 1 | FrontendEnvironment $frontendEnvironment = null |
|
69 | ) |
||
70 | { |
||
71 | 1 | $this->systemDomainRepository = $systemDomainRepository ?? GeneralUtility::makeInstance(SystemDomainRepository::class); |
|
72 | $this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class); |
||
73 | $this->fronendEnvironment = $frontendEnvironment ?? GeneralUtility::makeInstance(FrontendEnvironment::class); |
||
74 | 5 | } |
|
75 | 5 | ||
76 | 1 | /** |
|
77 | * Compiles a collection of configuration status checks. |
||
78 | * |
||
79 | 5 | * @return array |
|
80 | 5 | */ |
|
81 | 2 | public function getStatus() |
|
82 | { |
||
83 | $reports = []; |
||
84 | 5 | ||
85 | $rootPageFlagStatus = $this->getRootPageFlagStatus(); |
||
86 | if (!is_null($rootPageFlagStatus)) { |
||
87 | $reports[] = $rootPageFlagStatus; |
||
88 | |||
89 | // intended early return, no sense in going on if there are no root pages |
||
90 | return $reports; |
||
91 | } |
||
92 | |||
93 | 6 | $domainRecordAvailableStatus = $this->getDomainRecordAvailableStatus(); |
|
94 | if (!is_null($domainRecordAvailableStatus)) { |
||
95 | 6 | $reports[] = $domainRecordAvailableStatus; |
|
96 | 6 | } |
|
97 | 5 | ||
98 | $configIndexEnableStatus = $this->getConfigIndexEnableStatus(); |
||
99 | if (!is_null($configIndexEnableStatus)) { |
||
100 | 1 | $reports[] = $configIndexEnableStatus; |
|
101 | 1 | } |
|
102 | 1 | ||
103 | 1 | return $reports; |
|
104 | 1 | } |
|
105 | 1 | ||
106 | 1 | /** |
|
107 | * Checks whether the "Use as Root Page" page property has been set for any |
||
108 | * site. |
||
109 | * |
||
110 | * @return NULL|Status An error status is returned if no root pages were found. |
||
111 | */ |
||
112 | protected function getRootPageFlagStatus() |
||
113 | { |
||
114 | $rootPages = $this->getRootPages(); |
||
115 | 5 | if (!empty($rootPages)) { |
|
116 | return null; |
||
117 | 5 | } |
|
118 | 5 | ||
119 | 4 | $report = $this->getRenderedReport('RootPageFlagStatus.html'); |
|
120 | return GeneralUtility::makeInstance( |
||
121 | Status::class, |
||
122 | 1 | /** @scrutinizer ignore-type */ 'Sites', |
|
123 | 1 | /** @scrutinizer ignore-type */ 'No sites found', |
|
124 | 1 | /** @scrutinizer ignore-type */ $report, |
|
125 | 1 | /** @scrutinizer ignore-type */ Status::ERROR |
|
126 | 1 | ); |
|
127 | 1 | } |
|
128 | 1 | ||
129 | /** |
||
130 | * Checks whether a domain record (sys_domain) has been configured for each site root. |
||
131 | * |
||
132 | * @return NULL|Status An error status is returned for each site root page without domain record. |
||
133 | */ |
||
134 | protected function getDomainRecordAvailableStatus() |
||
135 | { |
||
136 | // @deprecated we can drop that check when the legacy site mode is dropped |
||
137 | if (!$this->extensionConfiguration->getIsAllowLegacySiteModeEnabled()) { |
||
138 | 5 | // when no legacy mode is enabled we do not need to check for domain record |
|
139 | return null; |
||
140 | 5 | } |
|
141 | 5 | $rootPagesWithoutDomain = $this->getRootPagesWithoutDomain(); |
|
142 | 3 | if (empty($rootPagesWithoutDomain)) { |
|
143 | return null; |
||
144 | } |
||
145 | 2 | ||
146 | 2 | $report = $this->getRenderedReport('SolrConfigurationStatusDomainRecord.html', ['pages' => $rootPagesWithoutDomain]); |
|
147 | 2 | return GeneralUtility::makeInstance( |
|
148 | 2 | Status::class, |
|
149 | 2 | /** @scrutinizer ignore-type */ 'Domain Records', |
|
150 | 2 | /** @scrutinizer ignore-type */ 'Domain records missing', |
|
151 | 2 | /** @scrutinizer ignore-type */ $report, |
|
152 | /** @scrutinizer ignore-type */ Status::ERROR |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Checks whether config.index_enable is set to 1, otherwise indexing will |
||
158 | * not work. |
||
159 | * |
||
160 | 5 | * @return NULL|Status An error status is returned for each site root page config.index_enable = 0. |
|
161 | */ |
||
162 | 5 | protected function getConfigIndexEnableStatus() |
|
163 | 5 | { |
|
164 | $rootPagesWithIndexingOff = $this->getRootPagesWithIndexingOff(); |
||
165 | 5 | if (empty($rootPagesWithIndexingOff)) { |
|
166 | 5 | return null; |
|
167 | 5 | } |
|
168 | |||
169 | $report = $this->getRenderedReport('SolrConfigurationStatusIndexing.html', ['pages' => $rootPagesWithIndexingOff]); |
||
170 | 5 | return GeneralUtility::makeInstance( |
|
171 | 5 | Status::class, |
|
172 | 5 | /** @scrutinizer ignore-type */ 'Page Indexing', |
|
173 | 5 | /** @scrutinizer ignore-type */ 'Indexing is disabled', |
|
174 | /** @scrutinizer ignore-type */ $report, |
||
175 | 5 | /** @scrutinizer ignore-type */ Status::WARNING |
|
176 | 1 | ); |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | 5 | * Returns an array of rootPages without an existing domain record. |
|
181 | 5 | * |
|
182 | 5 | * @return array |
|
183 | 5 | */ |
|
184 | protected function getRootPagesWithoutDomain() |
||
185 | { |
||
186 | 5 | $rootPagesWithoutDomain = []; |
|
187 | 5 | $rootPages = $this->getRootPages(); |
|
188 | |||
189 | $rootPageIds = []; |
||
190 | 5 | foreach ($rootPages as $rootPage) { |
|
191 | $rootPageIds[] = $rootPage['uid']; |
||
192 | } |
||
193 | |||
194 | $domainRecords = $this->systemDomainRepository->findDomainRecordsByRootPagesIds($rootPageIds); |
||
0 ignored issues
–
show
|
|||
195 | foreach ($rootPageIds as $rootPageId) { |
||
196 | $hasDomainRecord = true; |
||
197 | $hasDomainInTypoScript = true; |
||
198 | 5 | ||
199 | if (!array_key_exists($rootPageId, $domainRecords)) { |
||
200 | 5 | $hasDomainRecord = false; |
|
201 | 5 | } |
|
202 | |||
203 | 5 | /** @var $siteService SiteService */ |
|
204 | $siteService = GeneralUtility::makeInstance(SiteService::class); |
||
205 | 5 | $domain = $siteService->getFirstDomainForRootPage($rootPageId); |
|
206 | 5 | if ($domain === '') { |
|
207 | 5 | $hasDomainInTypoScript = false; |
|
208 | 5 | } |
|
209 | |||
210 | if (!$hasDomainRecord && !$hasDomainInTypoScript) { |
||
211 | $rootPagesWithoutDomain[$rootPageId] = $rootPages[$rootPageId]; |
||
212 | } |
||
213 | } |
||
214 | return $rootPagesWithoutDomain; |
||
215 | } |
||
216 | 5 | ||
217 | /** |
||
218 | * Returns an array of rootPages where the indexing is off and EXT:solr is enabled. |
||
219 | * |
||
220 | * @return array |
||
221 | 5 | */ |
|
222 | protected function getRootPagesWithIndexingOff() |
||
223 | { |
||
224 | $rootPages = $this->getRootPages(); |
||
225 | $rootPagesWithIndexingOff = []; |
||
226 | |||
227 | foreach ($rootPages as $rootPage) { |
||
228 | try { |
||
229 | $this->initializeTSFE($rootPage); |
||
230 | 4 | $solrIsEnabledAndIndexingDisabled = $this->getIsSolrEnabled() && !$this->getIsIndexingEnabled(); |
|
231 | if ($solrIsEnabledAndIndexingDisabled) { |
||
232 | 4 | $rootPagesWithIndexingOff[] = $rootPage; |
|
233 | } |
||
234 | 4 | } catch (\RuntimeException $rte) { |
|
235 | $rootPagesWithIndexingOff[] = $rootPage; |
||
236 | } catch (ServiceUnavailableException $sue) { |
||
237 | if ($sue->getCode() == 1294587218) { |
||
238 | // No TypoScript template found, continue with next site |
||
239 | $rootPagesWithIndexingOff[] = $rootPage; |
||
240 | continue; |
||
241 | } |
||
242 | 3 | } |
|
243 | } |
||
244 | 3 | ||
245 | return $rootPagesWithIndexingOff; |
||
246 | } |
||
247 | 3 | ||
248 | /** |
||
249 | * Gets the site's root pages. The "Is root of website" flag must be set, |
||
250 | * which usually is the case for pages with pid = 0. |
||
251 | * |
||
252 | * @return array An array of (partial) root page records, containing the uid and title fields |
||
253 | */ |
||
254 | protected function getRootPages() |
||
255 | 3 | { |
|
256 | $pagesRepository = GeneralUtility::makeInstance(PagesRepository::class); |
||
257 | 3 | ||
258 | 1 | return $pagesRepository->findAllRootPages(); |
|
259 | } |
||
260 | |||
261 | 2 | /** |
|
262 | * Checks if the solr plugin is enabled with plugin.tx_solr.enabled. |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | protected function getIsSolrEnabled() |
||
267 | 3 | { |
|
268 | if (empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_solr.']['enabled'])) { |
||
269 | 3 | return false; |
|
270 | 3 | } |
|
271 | return (bool)$GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_solr.']['enabled']; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Checks if the indexing is enabled with config.index_enable |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | protected function getIsIndexingEnabled() |
||
280 | { |
||
281 | if (empty($GLOBALS['TSFE']->config['config']['index_enable'])) { |
||
282 | return false; |
||
283 | } |
||
284 | |||
285 | return (bool)$GLOBALS['TSFE']->config['config']['index_enable']; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @param $rootPage |
||
290 | */ |
||
291 | protected function initializeTSFE($rootPage) |
||
292 | { |
||
293 | $this->fronendEnvironment->initializeTsfe($rootPage['uid']); |
||
294 | } |
||
295 | } |
||
296 |
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.