Skip to content

XInclude by xml:id and automatic <xi:fallback/> #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 135 additions & 20 deletions configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
+----------------------------------------------------------------------+
*/

error_reporting(-1);
$cvs_id = '$Id$';
ini_set( 'display_errors' , 1 );
ini_set( 'display_startup_errors' , 1 );
error_reporting( E_ALL );

$cvs_id = '$Id$';
echo "configure.php: $cvs_id\n";

const RNG_SCHEMA_DIR = __DIR__ . DIRECTORY_SEPARATOR . 'docbook' . DIRECTORY_SEPARATOR . 'docbook-v5.2-os' . DIRECTORY_SEPARATOR . 'rng' . DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -774,29 +776,142 @@ function getFileModificationHistory(): array {
}
echo "done.\n";

echo "Running XInclude/XPointer... ";
$status = $dom->xinclude();
if ($status === -1) {
echo "failed.\n";
} else {
/* For some dumb reason when no substitution are made it returns false instead of 0... */
$status = (int) $status;
echo "done. Performed $status XIncludes\n";
}
flush();

if ( $ac['XPOINTER_REPORTING'] == 'yes' || $ac['LANG'] == 'en' )
{
{ // XInclude/XPointer

echo "Running XInclude/XPointer... ";

// Always report failures on doc-en.
// Translations can --disable-xpointer-reporting.
// Failures on doc-en are fatal.

$report = $ac['XPOINTER_REPORTING'] == 'yes' || $ac['LANG'] == 'en';
$print = $ac['STDERR_TO_STDOUT'] == 'yes' ? STDOUT : STDERR;
$fatal = $ac['LANG'] == 'en';
$xpath = new DOMXPath( $dom );

// pre run id capture

$prevIdPath = [];

$nodes = $xpath->query( "//*[@xml:id]" );
foreach( $nodes as $node )
{
$id = $node->getAttribute( "xml:id" );
$path = $node->getNodePath();
if ( isset( $prevIdPath[ $id ] ) )
{
echo "failed.\nDuplicated xml:id '$id' before XInclude!\n";
errors_are_bad( 1 );
}
$prevIdPath[ $id ] = $path;
}

// automatic xi:fallback

$nodes = $xpath->query( "//*[local-name()='include']" );
foreach( $nodes as $node )
{
$target = $node->getAttribute( "xpointer" );
$failback = null;

$elements = $node->getElementsByTagName( "fallback" );
if ( count( $elements ) > 0 )
{
$failback = $elements[0];
}
else
{
$failback = $node->ownerDocument->createElementNS( "http://www.w3.org/2001/XInclude" , "fallback" );
//$failback = $node->ownerDocument->createElement( "xi:fallback" );
$node->append( $failback );
}

$comment = $failback->ownerDocument->createComment( "xi:fallback $target" );
$failback->append( $comment );

unset( $failback ); // otherwise core dumps on PHP 8.1.2-1ubuntu2.19
unset( $comment ); // otherwise core dumps on PHP 8.1.2-1ubuntu2.19
}

// run

$ret = $dom->xinclude();

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

$status = $ret === -1 ? "failed" : "done";
$countOk = $ret === -1 ? 0 : (int) $ret;
$countErr = count( $errors );
echo "$status. Performed $countOk XIncludes, $countErr failures.\n";

// failures

if ( $report )
{
fprintf( $output , "\n");
// xi:include failures

foreach( $errors as $error )
fprintf( $output , "{$error->message}\n");
if ( $ac['LANG'] == 'en' )
errors_are_bad(1);
fprintf( $print , "\n" . rtrim( $error->message ) . "\n");

if ( $fatal && count( $errors ) )
errors_are_bad( 1 );

// xi:failback failures

$count = 0;
$nodes = $xpath->query( "//comment()" );

foreach( $nodes as $node )
{
$payload = $node->data;
if ( str_starts_with( $payload , "xi:fallback" ) )
{
$count++;
$path = getNodePathWithIds( $node->parentNode );
fprintf( $print , "\nFailed xi:include at: $path\n");

if ( trim( $payload) != "xi:fallback" )
{
$payload = trim( substr( $payload , 12 ) );
fprintf( $print , "Failed xi:include target is: $payload\n");
}
}
}

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

// post run id fixup

$nodes = $xpath->query( "//*[@xml:id]" );
foreach( $nodes as $node )
{
$id = $node->getAttribute( "xml:id" );
$path = $node->getNodePath();
if ( $prevIdPath[ $id ] != $path )
$node->removeAttribute( "xml:id" );
}

flush();
}

function getNodePathWithIds( DOMNode|null $node ) : string
{
if ( $node == null || $node->nodeType == XML_DOCUMENT_NODE )
return "";
$path = getNodePathWithIds( $node->parentNode );
$path .= "/{$node->nodeName}";
$id = $node->getAttribute( "xml:id" );
if ( $id != "" )
$path .= "($id)";
return $path;
}

echo "Validating {$ac["INPUT_FILENAME"]}... ";
Expand Down
Loading