Passed
Pull Request — master (#1188)
by Timo
17:16
created

SolrConfigurationStatus::getStatus()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

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