Passed
Push — master ( 278bfb...0c15ec )
by Timo
04:00
created

getRootPagesWithoutDomain()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

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