Skip to content

Commit 342c72d

Browse files
authored
Post check individual XML files for know issues (#241)
* Post check individual XML files for know issues * Autofix option for simple encoding issues * Remove debug output * Hint the complete script path * Hint the effect of failed XInclude
1 parent c2fd6c0 commit 342c72d

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

configure.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,28 @@ function dom_saveload( DOMDocument $dom , string $filename = "" ) : string
712712
{
713713
echo "failed.\n";
714714
print_xml_errors();
715+
individual_xml_broken_check();
715716
errors_are_bad(1);
716717
}
717718

719+
function individual_xml_broken_check()
720+
{
721+
$cmd = array();
722+
$cmd[] = $GLOBALS['ac']['PHP'];
723+
$cmd[] = __DIR__ . "/scripts/broken.php";
724+
$cmd[] = $GLOBALS['ac']['LANG'];
725+
foreach ( $cmd as & $part )
726+
$part = escapeshellarg( $part );
727+
$ret = 0;
728+
$cmd = implode( ' ' , $cmd );
729+
passthru( $cmd , $ret );
730+
if ( $ret != 0 )
731+
{
732+
echo "doc-base/scripts/broken.php FAILED.\n";
733+
exit( 1 );
734+
}
735+
}
736+
718737
echo "Running XInclude/XPointer... ";
719738

720739
$total = xinclude_run_byid( $dom );
@@ -823,8 +842,11 @@ function xinclude_residual_fixup( DOMDocument $dom )
823842
foreach( $nodes as $node )
824843
{
825844
if ( $count === 0 )
826-
echo "\nFailed XInclude, inspect {$debugFile} for context:\n";
827-
echo " {$node->getAttribute("xpointer")}\n";
845+
{
846+
echo "\nFailed XIncludes, manual parts will be missing.";
847+
echo " Inspect {$debugFile} for context. Failed targets are:\n";
848+
}
849+
echo "- {$node->getAttribute("xpointer")}\n";
828850
$count++;
829851

830852
$fixup = null;
@@ -1159,8 +1181,5 @@ function phd_version()
11591181
11601182
CAT;
11611183

1162-
if (function_exists('proc_nice') && !is_windows()) {
1163-
echo " (Run `nice php $_SERVER[SCRIPT_NAME]` next time!)\n";
1164-
}
1165-
1166-
exit(0); // Tell the shell that this script finished successfully.
1184+
individual_xml_broken_check();
1185+
exit(0); // Finished successfully.

scripts/broken.php

Lines changed: 31 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 'doc-base/scripts/broken.php --dos2unix langdir'.\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 'doc-base/scripts/broken.php --dos2unix langdir'.\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,13 @@ 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+
system( $cmd );
194+
echo "\n";
195+
}
196+
}

0 commit comments

Comments
 (0)