12
12
use Sypets \RedirectsHelper \Service \RedirectsService ;
13
13
use Sypets \RedirectsHelper \Service \UrlService ;
14
14
use TYPO3 \CMS \Core \Configuration \ExtensionConfiguration ;
15
- use TYPO3 \CMS \Core \Messaging \AbstractMessage ;
16
15
use TYPO3 \CMS \Core \Utility \GeneralUtility ;
17
16
18
17
class RedirectsSanitizerCommand extends Command
@@ -32,21 +31,11 @@ class RedirectsSanitizerCommand extends Command
32
31
*/
33
32
protected $ io ;
34
33
35
- /**
36
- * @var bool
37
- */
38
- protected $ verbose ;
39
-
40
34
/**
41
35
* @var bool
42
36
*/
43
37
protected $ dryRun ;
44
38
45
- /**
46
- * @var bool
47
- */
48
- protected $ noOutput ;
49
-
50
39
/**
51
40
* @var array
52
41
*/
@@ -110,15 +99,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
110
99
$ this ->io ->title ($ this ->getDescription ());
111
100
112
101
$ this ->options = $ input ->getOptions ();
113
- $ this ->verbose = $ this ->options ['verbose ' ] ?? false ;
114
102
$ this ->dryRun = $ this ->options ['dry-run ' ] ?? false ;
115
- $ this ->noOutput = $ this ->options ['quiet ' ] ?? false ;
116
103
$ this ->interactive = !($ this ->options ['no-interaction ' ] ?? false );
117
104
118
105
if ($ this ->dryRun ) {
119
- $ this ->write ('Dry run only - do not change ' , AbstractMessage:: INFO );
106
+ $ this ->output -> writeln ('Dry run only - do not change ' );
120
107
} 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 ' );
122
109
}
123
110
$ this ->convertPathToPageLink ();
124
111
@@ -159,7 +146,7 @@ protected function convertPathToPageLink(): void
159
146
*/
160
147
$ results = [];
161
148
162
- $ this ->write ('Checking ... ' , AbstractMessage:: INFO );
149
+ $ this ->output -> writeln ('Checking ... ' , OutputInterface:: VERBOSITY_NORMAL );
163
150
164
151
while ($ redirect = $ redirects ->fetchAssociative ()) {
165
152
try {
@@ -170,48 +157,57 @@ protected function convertPathToPageLink(): void
170
157
171
158
$ type = $ this ->redirectsService ->getTargetType ($ redirect );
172
159
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
+ );
174
164
continue ;
175
165
}
176
166
// todo - make alwaysHttps configurable
177
167
$ url = $ this ->redirectsService ->getTargetPathUrl ($ redirect , $ forceHttps );
178
168
179
169
if ($ url === '' ) {
180
- $ this ->write (
170
+ $ this ->output -> writeln (
181
171
'Skipping: Can \'t build URL: uid= ' . $ uid . ' host= ' . $ redirect ['source_host ' ] . ' target= ' . $ originalTarget ,
182
- AbstractMessage:: NOTICE
172
+ OutputInterface:: VERBOSITY_DEBUG
183
173
);
184
174
continue ;
185
175
}
186
176
187
177
$ effectiveUrl = $ this ->urlService ->url2Url ($ url );
188
178
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) ' ,
191
181
$ url ,
192
182
$ uid ,
193
- $ originalTarget ,
194
- $ this ->urlService ->getErrorMessage ()
195
- ), AbstractMessage::NOTICE );
183
+ $ originalTarget
184
+ ), OutputInterface::VERBOSITY_DEBUG );
196
185
continue ;
197
186
}
198
- // @todo add alwaysLinkToOriginalLanguage
199
187
$ 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
+ }
200
196
$ result ['isValidUrl ' ] = true ;
201
197
$ result ['originalTarget ' ] = $ originalTarget ;
202
198
203
199
$ typolink = $ result ['typolink ' ] ?? '' ;
204
200
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 );
206
202
continue ;
207
203
}
208
- $ this ->write (sprintf (
204
+ $ this ->output -> writeln (sprintf (
209
205
'OK: can be converted: uid=%d source=%s, target path %s can be converted to %s ' ,
210
206
$ uid ,
211
207
$ sourcePath ,
212
208
$ originalTarget ,
213
209
$ typolink
214
- ), AbstractMessage:: INFO );
210
+ ));
215
211
216
212
if ($ this ->dryRun ) {
217
213
continue ;
@@ -220,66 +216,30 @@ protected function convertPathToPageLink(): void
220
216
if ($ this ->interactive ) {
221
217
$ question = new ConfirmationQuestion ('Convert this redirect? (y|n) ' , false );
222
218
if (!$ helper ->ask ($ this ->input , $ this ->output , $ question )) {
223
- $ this ->write ('Skip ... ' , AbstractMessage:: INFO );
219
+ $ this ->output -> writeln ('Skip ... ' , OutputInterface:: VERBOSITY_QUIET );
224
220
continue ;
225
221
}
226
- $ this ->write ('Continue ... ' , AbstractMessage:: INFO );
222
+ $ this ->output -> writeln ('Continue ... ' , OutputInterface:: VERBOSITY_QUIET );
227
223
}
228
224
$ values = [
229
225
'target ' => $ typolink
230
226
];
231
227
$ errorMessage = '' ;
232
228
233
- $ this ->write (sprintf (
229
+ $ this ->output -> writeln (sprintf (
234
230
'convert redirect with uid=%d original target=%s new target=%s ' ,
235
231
$ uid ,
236
232
$ result ['originalTarget ' ],
237
233
$ typolink
238
- ), AbstractMessage:: INFO );
234
+ ));
239
235
$ result = $ this ->redirectsService ->updateRedirect ($ uid , $ values , [], $ errorMessage );
240
236
if ($ result === false ) {
241
- $ this ->write ($ errorMessage, AbstractMessage:: ERROR );
237
+ $ this ->io -> warning ($ errorMessage );
242
238
}
243
239
} catch (\Exception | \Throwable $ e ) {
244
- $ this ->write ($ e ->getMessage (), AbstractMessage:: WARNING );
240
+ $ this ->output -> writeln ($ e ->getMessage (), OutputInterface:: VERBOSITY_DEBUG );
245
241
continue ;
246
242
}
247
243
}
248
244
}
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
- }
285
245
}
0 commit comments