From c67ca31feb75011d83dfdaea3364a711531946d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=20F=20S=20Bacci?= Date: Fri, 7 Mar 2025 16:41:20 -0300 Subject: [PATCH 1/7] Workarounds for DOMDocument->xinclude() return --- configure.php | 165 +++++++++++++++++++++++++++----------------------- 1 file changed, 90 insertions(+), 75 deletions(-) diff --git a/configure.php b/configure.php index 926fb1143..7b5b5e194 100755 --- a/configure.php +++ b/configure.php @@ -872,8 +872,7 @@ function dom_saveload( DOMDocument $dom , string $filename = "" ) : string else echo "done. Performed $total XIncludes.\n"; -xinclude_report(); -xinclude_residual( $dom ); +xinclude_residual_fixup( $dom ); function xinclude_run_byid( DOMDocument $dom ) { @@ -927,94 +926,46 @@ function xinclude_run_byid( DOMDocument $dom ) function xinclude_run_xpointer( DOMDocument $dom ) : int { + // The return of xinclude() cannot be used for counting or stoping, as it + // sometimes return zero/negative in cases of partial executions + $total = 0; - $maxrun = 10; //LIBXML_VERSION >= 21100 ? 1 : 10; - for( $run = 0 ; $run < $maxrun ; $run++ ) + for( $run = 0 ; $run < 10 ; $run++ ) { echo "$run "; - $status = (int) $dom->xinclude(); + libxml_clear_errors(); - if ( $status <= 0 ) - { + $was = count( xinclude_residual_list( $dom ) ); + $dom->xinclude(); + $now = count( xinclude_residual_list( $dom ) ); + + $total += $was - $now; + + if ( $was == $now ) return $total; - } - $total += $status; - libxml_clear_errors(); } echo "XInclude nested too deeply (xpointer).\n"; errors_are_bad( 1 ); } -function xinclude_report() +function xinclude_residual_fixup( DOMDocument $dom ) { - 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(); - libxml_clear_errors(); - - if ( ! $report ) - return; - - $count = 0; - $prefix = realpath( __DIR__ ); - - $prevLine = -1; - $prevClmn = -1; - - foreach( $errors as $error ) - { - $msg = $error->message; - $file = $error->file; - $line = $error->line; - $clmn = $error->column; + xinclude_debug_report( $dom ); - 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 ); - } -} - -function xinclude_residual( DOMDocument $dom ) -{ // XInclude failures are soft errors on translations, so remove // residual XInclude tags on translations to keep them building. - $header = false; + $nodes = xinclude_residual_list( $dom ); + + $count = 0; $explain = false; - $xpath = new DOMXPath( $dom ); - $xpath->registerNamespace( "xi" , "http://www.w3.org/2001/XInclude" ); - $nodes = $xpath->query( "//xi:include" ); foreach( $nodes as $node ) { - if ( $header == false ) - { + if ( $count == 0 ) echo "\nFailed XInclude:\n"; - $header = true; - } - echo "- {$node->getAttribute("xpointer")}\n"; + echo " {$node->getAttribute("xpointer")}\n"; + $count++; $fixup = null; $parent = $node->parentNode; @@ -1051,9 +1002,6 @@ function xinclude_residual( DOMDocument $dom ) $node->parentNode->removeChild( $node ); } - if ( $header ) - echo "\n"; - if ( $explain ) { echo << 0 ) + echo "\n"; + // 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 = []; $see = false; - $list = array(); + $xpath = new DOMXPath( $dom ); $nodes = $xpath->query( "//*[@xml:id]" ); foreach( $nodes as $node ) { @@ -1089,6 +1041,69 @@ function xinclude_residual( DOMDocument $dom ) } if ( $see ) echo " See: https://github.com/php/doc-base/blob/master/docs/structure.md#xmlid-structure\n"; + + global $ac; + $fatal = $ac['LANG'] == 'en'; + + if ( $see && $fatal ) + errors_are_bad( 1 ); // Duplicated strucutral xml:ids are fatal on doc-en +} + +function xinclude_residual_list( DOMDocument $dom ) : DOMNodeList +{ + $xpath = new DOMXPath( $dom ); + $xpath->registerNamespace( "xi" , "http://www.w3.org/2001/XInclude" ); + $nodes = $xpath->query( "//xi:include" ); + + return $nodes; +} + +function xinclude_debug_report( DOMDocument $dom ) +{ + $debugFile = __DIR__ . "/temp/xinclude-debug.xml"; + + dom_saveload( $dom , $debugFile ); // preserve state + + libxml_clear_errors(); + $dom->xinclude(); + $errors = libxml_get_errors(); + libxml_clear_errors(); + + dom_saveload( $dom ); // normal output + + $count = 0; + $prefix = realpath( __DIR__ ); + + $prevLine = -1; + $prevClmn = -1; + + foreach( $errors as $error ) + { + $msg = $error->message; + $file = $error->file; + $line = $error->line; + $clmn = $error->column; + +// // XPointer failures double reports, sometimes +// if ( $prevLine == $line && $prevClmn == $clmn ) +// continue; + + $prevLine = $line; + $prevClmn = $clmn; + + $msg = rtrim( $msg ); + if ( str_starts_with( $file , $prefix ) ) + $file = substr( $file , strlen( $prefix ) + 1 ); + + if ( $count == 0 ) + echo "\n"; + + echo "[{$file} {$line}:{$clmn}] $msg\n"; + $count++; + } + + if ( $count == 0 ) + echo "\n"; } echo "Validating {$ac["INPUT_FILENAME"]}... "; From 23c8b240082a0cdf2640ed6ed5d90786a3e8c2c4 Mon Sep 17 00:00:00 2001 From: alfsb Date: Mon, 10 Mar 2025 11:14:29 -0300 Subject: [PATCH 2/7] Review. Co-authored-by: Kamil Tekiela --- configure.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/configure.php b/configure.php index 7b5b5e194..85d537d07 100755 --- a/configure.php +++ b/configure.php @@ -1042,8 +1042,7 @@ function xinclude_residual_fixup( DOMDocument $dom ) if ( $see ) echo " See: https://github.com/php/doc-base/blob/master/docs/structure.md#xmlid-structure\n"; - global $ac; - $fatal = $ac['LANG'] == 'en'; + $fatal = $GLOBALS['ac']['LANG'] == 'en'; if ( $see && $fatal ) errors_are_bad( 1 ); // Duplicated strucutral xml:ids are fatal on doc-en From 11b2032dac340fd8e5e6309f556202dd96519d30 Mon Sep 17 00:00:00 2001 From: alfsb Date: Mon, 10 Mar 2025 11:15:33 -0300 Subject: [PATCH 3/7] Review. Co-authored-by: Kamil Tekiela --- configure.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/configure.php b/configure.php index 85d537d07..241957ad2 100755 --- a/configure.php +++ b/configure.php @@ -1083,10 +1083,6 @@ function xinclude_debug_report( DOMDocument $dom ) $line = $error->line; $clmn = $error->column; -// // XPointer failures double reports, sometimes -// if ( $prevLine == $line && $prevClmn == $clmn ) -// continue; - $prevLine = $line; $prevClmn = $clmn; From 4b13b62079f49dec3a1a01de0449317cf1046da0 Mon Sep 17 00:00:00 2001 From: alfsb Date: Mon, 10 Mar 2025 11:15:42 -0300 Subject: [PATCH 4/7] Review. Co-authored-by: Kamil Tekiela --- configure.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.php b/configure.php index 241957ad2..07c62bbd8 100755 --- a/configure.php +++ b/configure.php @@ -1090,7 +1090,7 @@ function xinclude_debug_report( DOMDocument $dom ) if ( str_starts_with( $file , $prefix ) ) $file = substr( $file , strlen( $prefix ) + 1 ); - if ( $count == 0 ) + if ( $count === 0 ) echo "\n"; echo "[{$file} {$line}:{$clmn}] $msg\n"; From d29e2bfa53204de0db4408b78cefc2d60d661c32 Mon Sep 17 00:00:00 2001 From: alfsb Date: Mon, 10 Mar 2025 11:15:53 -0300 Subject: [PATCH 5/7] Review. Co-authored-by: Kamil Tekiela --- configure.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.php b/configure.php index 07c62bbd8..2585d7709 100755 --- a/configure.php +++ b/configure.php @@ -1097,7 +1097,7 @@ function xinclude_debug_report( DOMDocument $dom ) $count++; } - if ( $count == 0 ) + if ( $count === 0 ) echo "\n"; } From 81dc29ed9acfcf0cb9a720ad42e77420740a7eaf Mon Sep 17 00:00:00 2001 From: alfsb Date: Mon, 10 Mar 2025 11:16:05 -0300 Subject: [PATCH 6/7] Review. Co-authored-by: Kamil Tekiela --- configure.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.php b/configure.php index 2585d7709..7cc04e614 100755 --- a/configure.php +++ b/configure.php @@ -962,7 +962,7 @@ function xinclude_residual_fixup( DOMDocument $dom ) foreach( $nodes as $node ) { - if ( $count == 0 ) + if ( $count === 0 ) echo "\nFailed XInclude:\n"; echo " {$node->getAttribute("xpointer")}\n"; $count++; From 4f9c0b80b46092a59282aac27fed72d576aeeef0 Mon Sep 17 00:00:00 2001 From: alfsb Date: Mon, 10 Mar 2025 11:16:16 -0300 Subject: [PATCH 7/7] Review. Co-authored-by: Kamil Tekiela --- configure.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.php b/configure.php index 7cc04e614..e5a42462f 100755 --- a/configure.php +++ b/configure.php @@ -941,7 +941,7 @@ function xinclude_run_xpointer( DOMDocument $dom ) : int $total += $was - $now; - if ( $was == $now ) + if ( $was === $now ) return $total; } echo "XInclude nested too deeply (xpointer).\n";