Skip to content

Commit

Permalink
Rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
André L F S Bacci committed Dec 12, 2024
1 parent 0ef7804 commit 21d5d35
Showing 1 changed file with 143 additions and 32 deletions.
175 changes: 143 additions & 32 deletions configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
| Authors: Dave Barr <[email protected]> |
| Hannes Magnusson <[email protected]> |
| Gwynne Raskind <[email protected]> |
| André L F S Bacci <[email protected]> |
+----------------------------------------------------------------------+
*/

Expand Down Expand Up @@ -803,46 +804,140 @@ function dom_saveload( DOMDocument $dom , string $filename = "" )


echo "Running XInclude/XPointer... ";
$total = 0;
$maxrun = 10; //LIBXML_VERSION >= 21100 ? 1 : 10;
for( $run = 0 ; $run < $maxrun ; $run++ )

$total = xinclude_run_byid( $dom );
$total += xinclude_run_xpointer( $dom );

if ( $total == 0 )
echo "failed.\n";
else
echo "done. Performed $total XIncludes.\n";

xinclude_report();
xinclude_residual( $dom );

function xinclude_run_byid( DOMDocument $dom )
{
if ( $run > 0 )
echo "$run ";
libxml_clear_errors();
$status = (int) $dom->xinclude();
if ( $status <= 0 )
break;
$total += $status;
if ( $maxrun > 1 && $run + 1 >= $maxrun )
$total = 0;
$maxrun = 10; //LIBXML_VERSION >= 21100 ? 1 : 10;
for( $run = 0 ; $run < $maxrun ; $run++ )
{
echo "Recursive XInclude is too deep.\n";
errors_are_bad(-1);
echo "$run ";
$xpath = new DOMXPath( $dom );
$xpath->registerNamespace( "xi" , "http://www.w3.org/2001/XInclude" );
$xincludes = $xpath->query( "//xi:include" );

$changed = false;
foreach( $xincludes as $xinclude )
{
$xpointer = $xinclude->getAttribute( "xpointer" );
$target = $xinclude->ownerDocument->getElementById( $xpointer );

if ( $target == null )
continue;

$other = new DOMDocument( '1.0' , 'utf8' );
$frags = $other->createDocumentFragment();
$other->append( $frags );
$frags->append( $other->importNode( $target , true ) ); // dup add

// "attributes in xml: namespace are not copied"

$oxpth = new DOMXPath( $other );
$attribs = $oxpth->query( "//@*" );

foreach( $attribs as $attrib )
if ( $attrib->prefix == "xml" )
$attrib->parentNode->removeAttribute( $attrib->nodeName );

$insert = $dom->importNode( $frags , true ); // dup
$xinclude->parentNode->insertBefore( $insert , $xinclude ); // add
$xinclude->parentNode->removeChild( $xinclude ); // del

$total++;
$changed = true;
libxml_clear_errors();
}

if ( ! $changed )
return $total;
}
echo "XInclude nested too deeply (xml:id).\n";
errors_are_bad( -1 );
}

if ($total == 0) {
echo "failed.\n";
} else {
echo "done. Performed $total XIncludes.\n";
function xinclude_run_xpointer( DOMDocument $dom ) : int
{
$total = 0;
$maxrun = 10; //LIBXML_VERSION >= 21100 ? 1 : 10;
for( $run = 0 ; $run < $maxrun ; $run++ )
{
echo "$run ";
$status = (int) $dom->xinclude();

if ( $status <= 0 )
{
return $total;
}
$total += $status;
libxml_clear_errors();
}
echo "XInclude nested too deeply (xpointer).\n";
errors_are_bad( -1 );
}
flush();

if ( $ac['XPOINTER_REPORTING'] == 'yes' || $ac['LANG'] == 'en' )
function xinclude_report()
{
global $ac;

$report = $ac['XPOINTER_REPORTING'] == 'yes' || $ac['LANG'] == 'en';
$output = $ac['STDERR_TO_STDOUT'] == 'yes' ? STDOUT : STDERR;
$fatal = $ac['LANG'] == 'en';

$errors = libxml_get_errors();
$output = ( $ac['STDERR_TO_STDOUT'] == 'yes' ) ? STDOUT : STDERR;
if ( count( $errors ) > 0 )
libxml_clear_errors();

if ( ! $report )
return;

$count = 0;
$prefix = realpath( __DIR__ );

$prevLine = -1;
$prevClmn = -1;

foreach( $errors as $error )
{
fprintf( $output , "\n");
foreach( $errors as $error )
fprintf( $output , "{$error->message}\n");
if ( $ac['LANG'] == 'en' )
errors_are_bad(1);
$msg = $error->message;
$file = $error->file;
$line = $error->line;
$clmn = $error->column;

if ( $prevLine == $line && $prevClmn == $clmn )
continue; // XPointer failures double reports sometimes
$prevLine = $line;
$prevClmn = $clmn;

$msg = rtrim( $msg );
if ( str_starts_with( $file , $prefix ) )
$file = substr( $file , strlen( $prefix ) + 1 );

if ( $count == 0 )
fprintf( $output , "\n" );

fprintf( $output , "[{$file} {$line}:{$clmn}] $msg\n" );
$count++;
}

if ( $count > 0 )
{
fprintf( $output , "\n" );
if ( $fatal )
errors_are_bad( 1 );
}
}

if ( $ac['LANG'] != 'en' )
function xinclude_residual( DOMDocument $dom )
{
// XInclude failures are soft errors on translations, so remove
// residual XInclude tags on translations to keep them building.
Expand All @@ -868,11 +963,11 @@ function dom_saveload( DOMDocument $dom , string $filename = "" )
case "tbody":
$fixup = "<row><entry></entry></row>";
break;
// case "variablelist":
// $fixup = "<varlistentry><term>></term><listitem><simpara></simpara></listitem></varlistentry>";
// break;
case "variablelist":
$fixup = "<varlistentry><term></term><listitem><simpara></simpara></listitem></varlistentry>";
break;
default:
echo "Unknown parent element of failed XInclude: $tagName\n";
echo "Unknown parent of failed XInclude: $tagName\n";
$explain = true;
continue 2;
}
Expand All @@ -897,6 +992,23 @@ function dom_saveload( DOMDocument $dom , string $filename = "" )
MSG;
exit(-1); // stop here, do not let more messages further confuse the matter
}

// XInclude by xml:id never duplicates xml:id, horever, also using
// XInclude by XPath/XPointer may start causing duplications
// (see docs/structure.md). Crude and ugly fixup ahead, beware!

$list = array();
$nodes = $xpath->query( "//*[@xml:id]" );
foreach( $nodes as $node )
{
$id = $node->getAttribute( "xml:id" );
if ( isset( $list[ $id ] ) )
{
echo " Random removing duplicated xml:id: $id\n";
$node->removeAttribute( "xml:id" );
}
$list[ $id ] = $id;
}
}

echo "Validating {$ac["INPUT_FILENAME"]}... ";
Expand Down Expand Up @@ -997,4 +1109,3 @@ function dom_saveload( DOMDocument $dom , string $filename = "" )

errors_are_bad(1); // Tell the shell that this script finished with an error.
}
?>

0 comments on commit 21d5d35

Please sign in to comment.