Passed
Push — master ( 786f47...b7dc69 )
by Timo
20:05 queued 23s
created

SolrConfigurationStatus::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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