• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tomasnorre / crawler / 43495013365

13 Nov 2026 07:44PM UTC coverage: 66.983% (-0.2%) from 67.139%
43495013365

push

github

web-flow
[BUGFIX] Export every log entry in the crawler log CSV, reduce BackendModuleLogService complexity (#1326)

* [TASK] Add test showing the CSV log export drops entries

BackendModuleLogService::addRows() overwrites the CSV row on every log
entry, so only the last entry of each page ends up in the export. This
test currently fails and documents the bug.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* [BUGFIX] Export every log entry in the crawler log CSV

addRows() now returns one CSV row per log entry instead of overwriting
the row on every iteration, and the log controller merges those rows
into a flat list. Pages without log entries no longer add an empty
row to the export.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* [TASK] Reduce cognitive complexity of BackendModuleLogService

Split addRows() into buildRow(), buildRowData(), formatDateTime() and
getModuleUrl(), and return early when a page has no log entries. The
rendered rows and CSV rows are unchanged.

This removes the two cognitive complexity entries for the class from
the PHPStan baseline. The new private methods are added to the Psalm
baseline, like the existing helpers, because Psalm cannot see the
dependency-injected caller of addRows().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

59 of 62 new or added lines in 2 files covered. (95.16%)

1272 of 1899 relevant lines covered (66.98%)

5.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.95
/Classes/Service/BackendModuleLogService.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace AOE\Crawler\Service;
6

7
/*
8
 * (c) 2023-     Tomas Norre Mikkelsen <tomasnorre@gmail.com>
9
 *
10
 * This file is part of the TYPO3 Crawler Extension.
11
 *
12
 * It is free software; you can redistribute it and/or modify it under
13
 * the terms of the GNU General Public License, either version 2
14
 * of the License, or any later version.
15
 *
16
 * For the full copyright and license information, please read the
17
 * LICENSE.txt file that was distributed with this source code.
18
 *
19
 * The TYPO3 project - inspiring people to share!
20
 */
21
use AOE\Crawler\Controller\Backend\BackendModuleCrawlerLogController;
22
use AOE\Crawler\Controller\Backend\Helper\ResultHandler;
23
use AOE\Crawler\Controller\Backend\Helper\UrlBuilder;
24
use Psr\Http\Message\UriInterface;
25
use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException;
26
use TYPO3\CMS\Backend\Utility\BackendUtility;
27
use TYPO3\CMS\Core\Imaging\Icon;
28
use TYPO3\CMS\Core\Imaging\IconFactory;
29
use TYPO3\CMS\Core\Imaging\IconSize;
30
use TYPO3\CMS\Core\Localization\LanguageService;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32

33
class BackendModuleLogService
34
{
35
    public function __construct(
36
        private readonly IconFactory $iconFactory
37
    ) {
38
    }
8 ✔
39

40
    /**
41
     * Create the rows for display of the page tree
42
     * For each page a number of rows are shown displaying GET variable configuration
43
     *
44
     * @throws RouteNotFoundException
45
     *
46
     * @return array{0: non-empty-list<array<string, mixed>>, 1: list<array<string, mixed>>}
47
     */
48
    public function addRows(
49
        array $logEntriesOfPage,
50
        int $setId,
51
        string $titleString,
52
        string $showResultLog,
53
        string $showFeVars,
54
        bool $CSVExport = false,
55
    ): array {
56
        $contentArray = $this->configureContentArray($showResultLog, $showFeVars);
8 ✔
57

58
        if ($logEntriesOfPage === []) {
8 ✔
59
            $contentArray['title'] = $titleString;
1 ✔
60
            $contentArray['noEntries'] = $this->getLanguageService()->sL('crawler.messages:labels.noentries');
1 ✔
61
            return [[$contentArray], []];
1 ✔
62
        }
63

64
        $refreshIcon = $this->iconFactory->getIcon('actions-system-refresh', IconSize::SMALL);
7 ✔
65
        $resultArray = [];
7 ✔
66
        $csvExport = [];
7 ✔
67
        foreach (array_values($logEntriesOfPage) as $index => $logEntry) {
7 ✔
68
            // Only the first row carries the page title, spanning all rows of the page
69
            $contentArray['title'] = $index === 0 ? $titleString : '';
7 ✔
70
            $contentArray['titleRowSpan'] = $index === 0 ? count($logEntriesOfPage) : 1;
7 ✔
71

72
            $contentArray = $this->buildRow(
7 ✔
73
                $logEntry,
7 ✔
74
                $contentArray,
7 ✔
75
                $setId,
7 ✔
76
                $showResultLog,
7 ✔
77
                $showFeVars,
7 ✔
78
                $refreshIcon
7 ✔
79
            );
7 ✔
80
            $resultArray[] = $contentArray;
7 ✔
81
            if ($CSVExport) {
7 ✔
82
                $csvExport[] = $this->extractCsvExport($logEntry, $contentArray['columns']);
2 ✔
83
            }
84
        }
85

86
        return [$resultArray, $csvExport];
7 ✔
87
    }
88

89
    public function configureContentArray(string $showResultLog, string $showFeVars): array
90
    {
91
        $contentArray = [];
8 ✔
92

93
        $contentArray['titleRowSpan'] = 1;
8 ✔
94
        $contentArray['colSpan'] = 9
8 ✔
95
            + ($showResultLog ? -1 : 0)
8 ✔
96
            + ($showFeVars ? 3 : 0);
8 ✔
97
        return $contentArray;
8 ✔
98
    }
99

100
    /**
101
     * @throws RouteNotFoundException
102
     */
103
    private function buildRow(
104
        array $logEntry,
105
        array $contentArray,
106
        int $setId,
107
        string $showResultLog,
108
        string $showFeVars,
109
        Icon $refreshIcon,
110
    ): array {
111
        $resultData = $logEntry['result_data'] ? json_decode((string) $logEntry['result_data'], true) : [];
7 ✔
112
        $resStatus = ResultHandler::getResStatus($resultData);
7 ✔
113
        $isError = str_contains($resStatus, 'Error');
7 ✔
114

115
        $rowData = $this->buildRowData($logEntry, $showResultLog, $resStatus);
7 ✔
116
        $rowData = $this->populateFeVars($showFeVars, $resultData, $rowData);
7 ✔
117

118
        $contentArray['trClass'] = $isError ? 'bg-danger' : '';
7 ✔
119
        $contentArray['qid'] = [
7 ✔
120
            'link' => $this->getModuleUrl('qid_details', $logEntry['qid'], $setId),
7 ✔
121
            'link-text' => htmlspecialchars((string) $logEntry['qid'], ENT_QUOTES | ENT_HTML5),
7 ✔
122
        ];
7 ✔
123
        $contentArray['refresh'] = [
7 ✔
124
            'link' => $this->getModuleUrl('qid_read', $logEntry['qid'], $setId),
7 ✔
125
            'link-text' => $refreshIcon,
7 ✔
126
            'warning' => $isError ? $this->iconFactory->getIcon('actions-ban', IconSize::SMALL) : '',
7 ✔
127
        ];
7 ✔
128

129
        return $this->buildColumnsInContentArray($rowData, $contentArray);
7 ✔
130
    }
131

132
    private function buildRowData(array $logEntry, string $showResultLog, string $resStatus): array
133
    {
134
        $parameters = json_decode((string) $logEntry['parameters'], true);
7 ✔
135

136
        $rowData = [];
7 ✔
137
        if ($showResultLog) {
7 ✔
138
            $rowData['result_log'] = ResultHandler::getResultLog($logEntry);
2 ✔
139
        } else {
140
            $rowData['scheduled'] = $this->formatDateTime((int) $logEntry['scheduled']);
5 ✔
141
            $rowData['exec_time'] = $this->formatDateTime((int) $logEntry['exec_time']);
5 ✔
142
        }
143
        $rowData['result_status'] = GeneralUtility::fixed_lgd_cs($resStatus, 50);
7 ✔
144
        $rowData['url'] = htmlspecialchars(
7 ✔
145
            (string) ($parameters['url'] ?? $parameters['alturl'] ?? ''),
7 ✔
146
            ENT_QUOTES | ENT_HTML5
7 ✔
147
        );
7 ✔
148
        $rowData['feUserGroupList'] = $parameters['feUserGroupList'] ?? '';
7 ✔
149
        $rowData['procInstructions'] = is_array($parameters['procInstructions'] ?? null)
7 ✔
150
            ? implode('; ', $parameters['procInstructions'])
7 ✔
NEW
151
            : '';
×
152
        $rowData['set_id'] = (string) $logEntry['set_id'];
7 ✔
153

154
        return $rowData;
7 ✔
155
    }
156

157
    private function formatDateTime(int $timestamp): string
158
    {
159
        return $timestamp > 0 ? BackendUtility::datetime($timestamp) : '-';
5 ✔
160
    }
161

162
    /**
163
     * @throws RouteNotFoundException
164
     */
165
    private function getModuleUrl(string $parameterName, mixed $queueId, int $setId): UriInterface
166
    {
167
        return UrlBuilder::getBackendModuleUrl(
7 ✔
168
            [
7 ✔
169
                $parameterName => $queueId,
7 ✔
170
                'setID' => $setId,
7 ✔
171
            ],
7 ✔
172
            BackendModuleCrawlerLogController::BACKEND_MODULE
7 ✔
173
        );
7 ✔
174
    }
175

176
    private function getLanguageService(): LanguageService
177
    {
178
        return $GLOBALS['LANG'];
1 ✔
179
    }
180

181
    private function extractCsvExport(array $vv, array $columns): array
182
    {
183
        $resLog = ResultHandler::getResultLog($vv);
2 ✔
184
        $csvExport = [];
2 ✔
185
        // Only for CSV (adding qid and scheduled/exec_time if needed):
186
        $csvExport['scheduled'] = BackendUtility::datetime($vv['scheduled']);
2 ✔
187
        $csvExport['exec_time'] = $vv['exec_time'] ? BackendUtility::datetime($vv['exec_time']) : '-';
2 ✔
188
        $csvExport['result_status'] = $columns['result_status'];
2 ✔
189
        $csvExport['url'] = $columns['url'];
2 ✔
190
        $csvExport['feUserGroupList'] = $columns['feUserGroupList'];
2 ✔
191
        $csvExport['procInstructions'] = $columns['procInstructions'];
2 ✔
192
        $csvExport['set_id'] = $columns['set_id'];
2 ✔
193
        $csvExport['result_log'] = str_replace(chr(10), '// ', $resLog);
2 ✔
194
        $csvExport['qid'] = $vv['qid'];
2 ✔
195
        return $csvExport;
2 ✔
196
    }
197

198
    private function populateFeVars(string $showFeVars, bool|array $resultData, array $rowData): array
199
    {
200
        if ($showFeVars) {
7 ✔
201
            $resFeVars = ResultHandler::getResFeVars(is_array($resultData) ? $resultData : []);
2 ✔
202
            $rowData['tsfe_id'] = $resFeVars['id'] ?? '';
2 ✔
203
            $rowData['tsfe_gr_list'] = $resFeVars['gr_list'] ?? '';
2 ✔
204
        }
205
        return $rowData;
7 ✔
206
    }
207

208
    private function buildColumnsInContentArray(array $rowData, array $contentArray): array
209
    {
210
        foreach ($rowData as $field => $value) {
7 ✔
211
            $contentArray['columns'][$field] = $field === 'url'
7 ✔
212
                ? $value
7 ✔
213
                : $this->escapeValue($value);
7 ✔
214
        }
215

216
        return $contentArray;
7 ✔
217
    }
218

219
    private function escapeValue(mixed $value): string
220
    {
221
        return nl2br(htmlspecialchars((string) $value, ENT_QUOTES | ENT_HTML5));
7 ✔
222
    }
223
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc