Passed
Push — master ( e1aede...36be10 )
by Timo
33:39 queued 01:53
created

SolrConfigurationStatus::getRootPages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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