Skip to content

Commit 54cc1e7

Browse files
authored
[TASK] Optimize output (#12)
- Use verbosity levels from Symfony directly - Remove unnecessary wrapper function for output - Do not throw exception in case of non-error - Clean up output levels - only output lowlevel information in case of debug level - Make sure some necessary output is printed in default output level - Update README.rst to reflect current behaviour
1 parent 79a0023 commit 54cc1e7

File tree

4 files changed

+74
-83
lines changed

4 files changed

+74
-83
lines changed

Classes/Command/RedirectsSanitizerCommand.php

+30-70
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Sypets\RedirectsHelper\Service\RedirectsService;
1313
use Sypets\RedirectsHelper\Service\UrlService;
1414
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
15-
use TYPO3\CMS\Core\Messaging\AbstractMessage;
1615
use TYPO3\CMS\Core\Utility\GeneralUtility;
1716

1817
class RedirectsSanitizerCommand extends Command
@@ -32,21 +31,11 @@ class RedirectsSanitizerCommand extends Command
3231
*/
3332
protected $io;
3433

35-
/**
36-
* @var bool
37-
*/
38-
protected $verbose;
39-
4034
/**
4135
* @var bool
4236
*/
4337
protected $dryRun;
4438

45-
/**
46-
* @var bool
47-
*/
48-
protected $noOutput;
49-
5039
/**
5140
* @var array
5241
*/
@@ -110,15 +99,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
11099
$this->io->title($this->getDescription());
111100

112101
$this->options = $input->getOptions();
113-
$this->verbose = $this->options['verbose'] ?? false;
114102
$this->dryRun = $this->options['dry-run'] ?? false;
115-
$this->noOutput = $this->options['quiet'] ?? false;
116103
$this->interactive = !($this->options['no-interaction'] ?? false);
117104

118105
if ($this->dryRun) {
119-
$this->write('Dry run only - do not change', AbstractMessage::INFO);
106+
$this->output->writeln('Dry run only - do not change');
120107
} else {
121-
$this->write('No dry run - irreversible changes will be made', AbstractMessage::INFO);
108+
$this->output->writeln('No dry run - irreversible changes will be made');
122109
}
123110
$this->convertPathToPageLink();
124111

@@ -159,7 +146,7 @@ protected function convertPathToPageLink(): void
159146
*/
160147
$results = [];
161148

162-
$this->write('Checking ...', AbstractMessage::INFO);
149+
$this->output->writeln('Checking ...', OutputInterface::VERBOSITY_NORMAL);
163150

164151
while ($redirect = $redirects->fetchAssociative()) {
165152
try {
@@ -170,48 +157,57 @@ protected function convertPathToPageLink(): void
170157

171158
$type = $this->redirectsService->getTargetType($redirect);
172159
if ($type !== RedirectsService::TARGET_TYPE_PATH) {
173-
$this->write(sprintf('uid=%d:Skipping, target type is not path (target=%s)', $uid, $originalTarget), AbstractMessage::NOTICE);
160+
$this->output->writeln(
161+
sprintf('uid=%d:Skipping, target type is not path (target=%s)', $uid, $originalTarget),
162+
OutputInterface::VERBOSITY_DEBUG
163+
);
174164
continue;
175165
}
176166
// todo - make alwaysHttps configurable
177167
$url = $this->redirectsService->getTargetPathUrl($redirect, $forceHttps);
178168

179169
if ($url === '') {
180-
$this->write(
170+
$this->output->writeln(
181171
'Skipping: Can\'t build URL: uid=' . $uid . ' host=' . $redirect['source_host'] . ' target=' . $originalTarget,
182-
AbstractMessage::NOTICE
172+
OutputInterface::VERBOSITY_DEBUG
183173
);
184174
continue;
185175
}
186176

187177
$effectiveUrl = $this->urlService->url2Url($url);
188178
if ($effectiveUrl === '') {
189-
$this->write(sprintf(
190-
'Skipping: URL %s does not resolve to valid URL (uid=%d, original target=%s, error=%s)',
179+
$this->output->writeln(sprintf(
180+
'Skipping: URL %s does not resolve to valid URL (uid=%d, original target=%s)',
191181
$url,
192182
$uid,
193-
$originalTarget,
194-
$this->urlService->getErrorMessage()
195-
), AbstractMessage::NOTICE);
183+
$originalTarget
184+
), OutputInterface::VERBOSITY_DEBUG);
196185
continue;
197186
}
198-
// @todo add alwaysLinkToOriginalLanguage
199187
$result = $this->urlService->urlToPageInfo($effectiveUrl, false);
188+
if (!$result) {
189+
$this->output->writeln(sprintf(
190+
'Skipping: URL %s does not resolve to valid page (uid=%d, original target=%s)',
191+
$url,
192+
$uid,
193+
$originalTarget
194+
), OutputInterface::VERBOSITY_DEBUG);
195+
}
200196
$result['isValidUrl'] = true;
201197
$result['originalTarget'] = $originalTarget;
202198

203199
$typolink = $result['typolink'] ?? '';
204200
if ($typolink === '') {
205-
$this->write('Skipping: redirect has no typolink, uid=' . $uid, AbstractMessage::WARNING);
201+
$this->output->writeln('Skipping: redirect has no typolink, uid=' . $uid, OutputInterface::VERBOSITY_DEBUG);
206202
continue;
207203
}
208-
$this->write(sprintf(
204+
$this->output->writeln(sprintf(
209205
'OK: can be converted: uid=%d source=%s, target path %s can be converted to %s',
210206
$uid,
211207
$sourcePath,
212208
$originalTarget,
213209
$typolink
214-
), AbstractMessage::INFO);
210+
));
215211

216212
if ($this->dryRun) {
217213
continue;
@@ -220,66 +216,30 @@ protected function convertPathToPageLink(): void
220216
if ($this->interactive) {
221217
$question = new ConfirmationQuestion('Convert this redirect? (y|n)', false);
222218
if (!$helper->ask($this->input, $this->output, $question)) {
223-
$this->write('Skip ...', AbstractMessage::INFO);
219+
$this->output->writeln('Skip ...', OutputInterface::VERBOSITY_QUIET);
224220
continue;
225221
}
226-
$this->write('Continue ...', AbstractMessage::INFO);
222+
$this->output->writeln('Continue ...', OutputInterface::VERBOSITY_QUIET);
227223
}
228224
$values = [
229225
'target' => $typolink
230226
];
231227
$errorMessage = '';
232228

233-
$this->write(sprintf(
229+
$this->output->writeln(sprintf(
234230
'convert redirect with uid=%d original target=%s new target=%s',
235231
$uid,
236232
$result['originalTarget'],
237233
$typolink
238-
), AbstractMessage::INFO);
234+
));
239235
$result = $this->redirectsService->updateRedirect($uid, $values, [], $errorMessage);
240236
if ($result === false) {
241-
$this->write($errorMessage, AbstractMessage::ERROR);
237+
$this->io->warning($errorMessage);
242238
}
243239
} catch (\Exception | \Throwable $e) {
244-
$this->write($e->getMessage(), AbstractMessage::WARNING);
240+
$this->output->writeln($e->getMessage(), OutputInterface::VERBOSITY_DEBUG);
245241
continue;
246242
}
247243
}
248244
}
249-
250-
/**
251-
* Map input options to supported AbstractMessage. The more severe the message,
252-
* the higher the value. In case of an unexpected event (e.g. warning), the output should be
253-
* visible, even if not explicitly set
254-
*
255-
* Options:
256-
*
257-
* -v : show all output
258-
* -q : no output at all.
259-
*
260-
* By default, >= AbstractMessage::OK is displayed
261-
*
262-
* Starting here, everything will be output by default, unless -q (quiet) is given.
263-
*
264-
* AbstractMessage::NOTICE
265-
* AbstractMessage::INFO
266-
* AbstractMessage::OK
267-
* AbstractMessage::WARNING
268-
* AbstractMessage::ERROR
269-
*
270-
* @param string $msg
271-
* @param int $level
272-
*
273-
* @todo Use OutputInterface, see https://symfony.com/doc/current/console/verbosity.html
274-
*/
275-
protected function write(string $msg, int $level = AbstractMessage::INFO): void
276-
{
277-
if ($this->noOutput) {
278-
return;
279-
}
280-
281-
if ($this->verbose || $level >= AbstractMessage::OK) {
282-
$this->io->writeln($msg);
283-
}
284-
}
285245
}

Classes/Service/UrlService.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,23 @@ public function pageToTypolink(int $pageUid, int $language): string
328328
}
329329

330330
/**
331+
* Converts an URL to page info. This can only work for URL for
332+
* configured sites. The returned information is similar to
333+
* result by getCandidatesForPath(), except that we also add
334+
* the languageId.
335+
*
331336
* @param string $url
332-
* @return array
337+
* @return array<string,mixed>
333338
*/
334339
public function urlToPageCandidate(string $url): array
335340
{
336341
$routeResult = $this->urlToRouteResult($url);
337342
$site = $routeResult->getSite();
338343
$siteIdentifier = $site->getIdentifier();
339344
if ($site instanceof NullSite) {
340-
throw new \InvalidArgumentException('Can\'t get site for URL:' . $url);
345+
// this is not really an error. Because of following redirects, we might be
346+
// trying to get a page for an URL which is not on the site!
347+
return [];
341348
}
342349
$siteLanguage = $routeResult->getLanguage();
343350
$this->initializeSlugCandidateProvider($site);

README.rst

+31-10
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,55 @@ This extension does the following:
2727
Commands
2828
========
2929

30+
.. warning::
31+
32+
There is no going back. Make a backup first.
33+
3034
path2page
3135
---------
3236

3337
Converts the target of redirects. Only those with a path as target
3438
are converted to page link, e.g. "t3://page?uid=83".
3539

40+
Show help:
41+
3642
.. code-block:: shell
3743
38-
vendor/typo3 redirects_helper:path2page
44+
vendor/bin/typo3 redirects_helper:path2page -h
3945
40-
Use dry-run and / or verbose:
46+
Use dry-run (does not change anything):
4147

4248
.. code-block:: shell
4349
44-
# -v: verbose
45-
vendor/typo3 redirects_helper:path2page -v
4650
# -d: dry-run: do not change anything, only show
47-
vendor/typo3 redirects_helper:path2page -d
51+
vendor/bin/typo3 redirects_helper:path2page -d
4852
49-
By default, interactive mode is on, so you must confirm each conversion. If
50-
you are confident, that it works correctly, you can use -n (non-interactive)
53+
You can use -v, -vv or -vvv (with increasing verbosity level):
5154

5255
.. code-block:: shell
5356
5457
# -v: verbose
55-
vendor/typo3 redirects_helper:path2page -n
58+
vendor/bin/typo3 redirects_helper:path2page -d -v
5659
57-
.. warning::
60+
The output will show paths which can be converted (starting with "OK:"). With
61+
verbosity level -vv and above you will also see failed attempts to convert
62+
(which are not an error but due to fact that not all targets can be converted).
5863

59-
There is no going back. Make a backup first.
64+
You can also use this to filter for targets which cannot be resolved:
65+
66+
.. code-block:: shell
67+
68+
# -v: verbose
69+
vendor/bin/typo3 redirects_helper:path2page -d -vvv | grep -E "Skipping: URL.* does not resolve to valid URL"
70+
71+
These are redirects where it might make sense to remove them. But beware, this
72+
is also the case if the target page is hidden.
73+
74+
By default, interactive mode is on, so you must confirm each conversion. If
75+
you are confident, that it works correctly, you can use -n (non-interactive)
76+
77+
.. code-block:: shell
78+
79+
# -v: verbose
80+
vendor/bin/typo3 redirects_helper:path2page -n
6081

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
"config": {
1616
"sort-packages": true,
1717
"vendor-dir": ".Build/vendor",
18-
"bin-dir": ".Build/bin"
18+
"bin-dir": ".Build/bin",
19+
"preferred-install": {
20+
"typo3/cms-core": "source"
21+
}
1922
},
2023
"require": {
2124
"php": ">=7.3.0",

0 commit comments

Comments
 (0)