Skip to content

Commit 051611a

Browse files
author
André L F S Bacci
committed
Autofix option for simple encoding issues
1 parent fc7b822 commit 051611a

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

scripts/broken.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,19 @@
2828

2929
if ( count( $argv ) < 2 )
3030
print_usage_exit( $argv[0] );
31-
3231
array_shift( $argv );
32+
33+
$dos2unix = false;
34+
foreach( $argv as & $arg )
35+
{
36+
if ( $arg == "--dos2unix" )
37+
{
38+
$dos2unix = true;
39+
$arg = null;
40+
}
41+
}
42+
$argv = array_filter( $argv );
43+
3344
foreach( $argv as $arg )
3445
{
3546
if ( file_exists( $arg ) )
@@ -46,7 +57,7 @@
4657
function print_usage_exit( $cmd )
4758
{
4859
fwrite( STDERR , " Wrong paramater count. Usage:\n" );
49-
fwrite( STDERR , " {$cmd} path:\n" );
60+
fwrite( STDERR , " {$cmd} [--dos2unix] path\n" );
5061
exit;
5162
}
5263

@@ -83,17 +94,21 @@ function testFile( string $filename , bool $fragment = false )
8394
if ( str_starts_with( $contents , b"\xEF\xBB\xBF" ) )
8495
{
8596
echo "Wrong XML file:\n";
97+
echo " Issue: XML file with BOM. Several tools may misbehave.\n";
8698
echo " Path: $filename\n";
87-
echo " Error: XML file with BOM. Several tools may misbehave.\n";
99+
echo " Hint: You can try autofix this with --dos2unix option.\n";
88100
echo "\n";
101+
autofix_dos2unix( $filename );
89102
}
90103

91104
if ( PHP_EOL == "\n" && str_contains( $contents , "\r") )
92105
{
93106
echo "Wrong XML file:\n";
107+
echo " Issue: XML file contains \\r. Several tools may misbehave.\n";
94108
echo " Path: $filename\n";
95-
echo " Error: XML file contains \\r. Several tools may misbehave.\n";
109+
echo " Hint: You can try autofix this with --dos2unix option.\n";
96110
echo "\n";
111+
autofix_dos2unix( $filename );
97112
}
98113

99114
static $prefix = "", $suffix = "", $extra = "";
@@ -128,10 +143,10 @@ function testFile( string $filename , bool $fragment = false )
128143
$lin = $error->line;
129144
$col = $error->column;
130145
echo "Broken XML file:\n";
146+
echo " Issue: $message\n";
131147
echo " Path: $filename [$lin,$col]\n";
132-
echo " Error: $message\n";
133148
if ( $hintFragDir )
134-
echo " Hint: Dir is marked with .xmlfragmentdir on doc-en? If not, check entity references.\n";
149+
echo " Hint: See source comments about '.xmlfragmentdir', or check entity references outside enclosing tags.\n";
135150
echo "\n";
136151
return;
137152
}
@@ -169,3 +184,14 @@ function testDir( string $dir )
169184
foreach( $subdirs as $dir )
170185
testDir( $dir );
171186
}
187+
188+
function autofix_dos2unix( string $filename )
189+
{
190+
if ( $GLOBALS['dos2unix'] )
191+
{
192+
$cmd = "dos2unix -r " . escapeshellarg( $filename );
193+
echo $cmd;
194+
system( $cmd );
195+
echo "\n";
196+
}
197+
}

0 commit comments

Comments
 (0)