Passed
Push — master ( 2d0a21...d7cfa8 )
by Timo
20:23 queued 19:26
created

SolrConfigurationStatus::getIsSolrEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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