Revert "[TASK] Avoid slow array functions in loops"
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Report / Status / Status.php
1 <?php
2 namespace TYPO3\CMS\Reports\Report\Status;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Core\Localization\LanguageService;
19 use TYPO3\CMS\Core\Registry;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Reports\ExtendedStatusProviderInterface;
22 use TYPO3\CMS\Reports\RequestAwareReportInterface;
23 use TYPO3\CMS\Reports\RequestAwareStatusProviderInterface;
24 use TYPO3\CMS\Reports\Status as ReportStatus;
25 use TYPO3\CMS\Reports\StatusProviderInterface;
26
27 /**
28 * The status report
29 */
30 class Status implements RequestAwareReportInterface
31 {
32 /**
33 * @var StatusProviderInterface[][]
34 */
35 protected $statusProviders = [];
36
37 /**
38 * Constructor for class tx_reports_report_Status
39 */
40 public function __construct()
41 {
42 $this->getLanguageService()->includeLLFile('EXT:reports/Resources/Private/Language/locallang_reports.xlf');
43 $this->getStatusProviders();
44 }
45
46 /**
47 * Takes care of creating / rendering the status report
48 *
49 * @param ServerRequestInterface|null $request the currently handled request
50 * @return string The status report as HTML
51 */
52 public function getReport(ServerRequestInterface $request = null)
53 {
54 $content = '';
55 $status = $this->getSystemStatus($request);
56 $highestSeverity = $this->getHighestSeverity($status);
57 // Updating the registry
58 $registry = GeneralUtility::makeInstance(Registry::class);
59 $registry->set('tx_reports', 'status.highestSeverity', $highestSeverity);
60 $content .= '<p class="lead">' . $this->getLanguageService()->getLL('status_report_explanation') . '</p>';
61 return $content . $this->renderStatus($status);
62 }
63
64 /**
65 * Gets all registered status providers and creates instances of them.
66 */
67 protected function getStatusProviders()
68 {
69 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['tx_reports']['status']['providers'] as $key => $statusProvidersList) {
70 $this->statusProviders[$key] = [];
71 foreach ($statusProvidersList as $statusProvider) {
72 $statusProviderInstance = GeneralUtility::makeInstance($statusProvider);
73 if ($statusProviderInstance instanceof StatusProviderInterface) {
74 $this->statusProviders[$key][] = $statusProviderInstance;
75 }
76 }
77 }
78 }
79
80 /**
81 * Runs through all status providers and returns all statuses collected.
82 *
83 * @param ServerRequestInterface $request
84 * @return \TYPO3\CMS\Reports\Status[]
85 */
86 public function getSystemStatus(ServerRequestInterface $request = null)
87 {
88 $status = [];
89 foreach ($this->statusProviders as $statusProviderId => $statusProviderList) {
90 $status[$statusProviderId] = [];
91 foreach ($statusProviderList as $statusProvider) {
92 if ($statusProvider instanceof RequestAwareStatusProviderInterface) {
93 $statuses = $statusProvider->getStatus($request);
94 } else {
95 $statuses = $statusProvider->getStatus();
96 }
97 $status[$statusProviderId] = array_merge($status[$statusProviderId], $statuses);
98 }
99 }
100 return $status;
101 }
102
103 /**
104 * Runs through all status providers and returns all statuses collected, which are detailed.
105 *
106 * @return \TYPO3\CMS\Reports\Status[]
107 */
108 public function getDetailedSystemStatus()
109 {
110 $status = [];
111 foreach ($this->statusProviders as $statusProviderId => $statusProviderList) {
112 $status[$statusProviderId] = [];
113 foreach ($statusProviderList as $statusProvider) {
114 if ($statusProvider instanceof ExtendedStatusProviderInterface) {
115 $statuses = $statusProvider->getDetailedStatus();
116 $status[$statusProviderId] = array_merge($status[$statusProviderId], $statuses);
117 }
118 }
119 }
120 return $status;
121 }
122
123 /**
124 * Determines the highest severity from the given statuses.
125 *
126 * @param array $statusCollection An array of \TYPO3\CMS\Reports\Status objects.
127 * @return int The highest severity found from the statuses.
128 */
129 public function getHighestSeverity(array $statusCollection)
130 {
131 $highestSeverity = ReportStatus::NOTICE;
132 foreach ($statusCollection as $statusProvider => $providerStatuses) {
133 /** @var ReportStatus $status */
134 foreach ($providerStatuses as $status) {
135 if ($status->getSeverity() > $highestSeverity) {
136 $highestSeverity = $status->getSeverity();
137 }
138 // Reached the highest severity level, no need to go on
139 if ($highestSeverity == ReportStatus::ERROR) {
140 break;
141 }
142 }
143 }
144 return $highestSeverity;
145 }
146
147 /**
148 * Renders the system's status
149 *
150 * @param array $statusCollection An array of statuses as returned by the available status providers
151 * @return string The system status as an HTML table
152 */
153 protected function renderStatus(array $statusCollection)
154 {
155 $content = '';
156 $template = '
157 <tr>
158 <td class="###CLASS### col-xs-6">###HEADER###</td>
159 <td class="###CLASS### col-xs-6">###STATUS###<br>###CONTENT###</td>
160 </tr>
161 ';
162 $statuses = $this->sortStatusProviders($statusCollection);
163 $id = 0;
164 foreach ($statuses as $provider => $providerStatus) {
165 $providerState = $this->sortStatuses($providerStatus);
166 $id++;
167 $classes = [
168 ReportStatus::NOTICE => 'notice',
169 ReportStatus::INFO => 'info',
170 ReportStatus::OK => 'success',
171 ReportStatus::WARNING => 'warning',
172 ReportStatus::ERROR => 'danger'
173 ];
174 $messages = '';
175 /** @var ReportStatus $status */
176 foreach ($providerState as $status) {
177 $severity = $status->getSeverity();
178 $messages .= strtr($template, [
179 '###CLASS###' => $classes[$severity],
180 '###HEADER###' => $status->getTitle(),
181 '###STATUS###' => $status->getValue(),
182 '###CONTENT###' => $status->getMessage()
183 ]);
184 }
185 $header = '<h2>' . $provider . '</h2>';
186 $table = '<table class="table table-striped table-hover">';
187 $table .= '<tbody>' . $messages . '</tbody>';
188 $table .= '</table>';
189
190 $content .= $header . $table;
191 }
192 return $content;
193 }
194
195 /**
196 * Sorts the status providers (alphabetically and puts primary status providers at the beginning)
197 *
198 * @param array $statusCollection A collection of statuses (with providers)
199 * @return array The collection of statuses sorted by provider (beginning with provider "_install")
200 */
201 protected function sortStatusProviders(array $statusCollection)
202 {
203 // Extract the primary status collections, i.e. the status groups
204 // that must appear on top of the status report
205 // Change their keys to localized collection titles
206 $primaryStatuses = [
207 $this->getLanguageService()->getLL('status_typo3') => $statusCollection['typo3'],
208 $this->getLanguageService()->getLL('status_system') => $statusCollection['system'],
209 $this->getLanguageService()->getLL('status_security') => $statusCollection['security'],
210 $this->getLanguageService()->getLL('status_configuration') => $statusCollection['configuration']
211 ];
212 unset($statusCollection['typo3'], $statusCollection['system'], $statusCollection['security'], $statusCollection['configuration']);
213 // Assemble list of secondary status collections with left-over collections
214 // Change their keys using localized labels if available
215 // @todo extract into getLabel() method
216 $secondaryStatuses = [];
217 foreach ($statusCollection as $statusProviderId => $collection) {
218 if (strpos($statusProviderId, 'LLL:') === 0) {
219 // Label provided by extension
220 $label = $this->getLanguageService()->sL($statusProviderId);
221 } else {
222 // Generic label
223 $label = $this->getLanguageService()->getLL('status_' . $statusProviderId);
224 }
225 $providerLabel = empty($label) ? $statusProviderId : $label;
226 $secondaryStatuses[$providerLabel] = $collection;
227 }
228 // Sort the secondary status collections alphabetically
229 ksort($secondaryStatuses);
230 $orderedStatusCollection = array_merge($primaryStatuses, $secondaryStatuses);
231 return $orderedStatusCollection;
232 }
233
234 /**
235 * Sorts the statuses by severity
236 *
237 * @param array $statusCollection A collection of statuses per provider
238 * @return array The collection of statuses sorted by severity
239 */
240 protected function sortStatuses(array $statusCollection)
241 {
242 $statuses = [];
243 $sortTitle = [];
244 $header = null;
245 /** @var ReportStatus $status */
246 foreach ($statusCollection as $status) {
247 if ($status->getTitle() === 'TYPO3') {
248 $header = $status;
249 continue;
250 }
251 $statuses[] = $status;
252 $sortTitle[] = $status->getSeverity();
253 }
254 array_multisort($sortTitle, SORT_DESC, $statuses);
255 // Making sure that the core version information is always on the top
256 if (is_object($header)) {
257 array_unshift($statuses, $header);
258 }
259 return $statuses;
260 }
261
262 /**
263 * @return LanguageService
264 */
265 protected function getLanguageService()
266 {
267 return $GLOBALS['LANG'];
268 }
269 }