From 3baaa586fca04d311d14c6d623b06be51f5da6ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=20F=20S=20Bacci?= Date: Sun, 8 Dec 2024 12:39:06 -0300 Subject: [PATCH 1/4] Strict errors and global flush --- configure.php | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/configure.php b/configure.php index 6338f1b4d..e1d2dee03 100755 --- a/configure.php +++ b/configure.php @@ -19,10 +19,12 @@ +----------------------------------------------------------------------+ */ -error_reporting(-1); -$cvs_id = '$Id$'; +ini_set( 'display_errors' , 1 ); +ini_set( 'display_startup_errors' , 1 ); +error_reporting( E_ALL ); +ob_implicit_flush(); -echo "configure.php: $cvs_id\n"; +echo "configure.php on PHP " . phpversion() . "\n\n"; const RNG_SCHEMA_DIR = __DIR__ . DIRECTORY_SEPARATOR . 'docbook' . DIRECTORY_SEPARATOR . 'docbook-v5.2-os' . DIRECTORY_SEPARATOR . 'rng' . DIRECTORY_SEPARATOR; const RNG_SCHEMA_FILE = RNG_SCHEMA_DIR . 'docbook.rng'; @@ -101,7 +103,6 @@ function checking($for) // {{{ if ($ac['quiet'] != 'yes') { echo "Checking {$for}... "; - flush(); } } // }}} @@ -348,16 +349,6 @@ function getFileModificationHistory(): array { $php_bin_names = array('php', 'php5', 'cli/php', 'php.exe', 'php5.exe', 'php-cli.exe', 'php-cgi.exe'); // }}} -// Reject old PHP installations {{{ -if (phpversion() < 5) { - echo "PHP 5 or above is required. Version detected: " . phpversion() . "\n"; - exit(100); -} else { - echo "PHP version: " . phpversion() . "\n"; -} // }}} - -echo "\n"; - $acd = array( // {{{ 'srcdir' => $srcdir, 'basedir' => $basedir, @@ -872,7 +863,7 @@ function getFileModificationHistory(): array { } echo "Validating {$ac["INPUT_FILENAME"]}... "; -flush(); + if ($ac['PARTIAL'] != '' && $ac['PARTIAL'] != 'no') { // {{{ $dom->relaxNGValidate(RNG_SCHEMA_FILE); // we don't care if the validation works or not $node = $dom->getElementById($ac['PARTIAL']); @@ -919,7 +910,6 @@ function getFileModificationHistory(): array { if ($dom->relaxNGValidate(RNG_SCHEMA_FILE)) { echo "done.\n"; printf("\nAll good. Saving %s... ", basename($ac["OUTPUT_FILENAME"])); - flush(); $dom->save($mxml); echo "done.\n"; From 8245de6f8e0d801d0f8222efe913a1bc587fa61b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=20F=20S=20Bacci?= Date: Sun, 8 Dec 2024 13:06:50 -0300 Subject: [PATCH 2/4] Functions for consistent DOM loading and reloading --- configure.php | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/configure.php b/configure.php index e1d2dee03..5717f6d04 100755 --- a/configure.php +++ b/configure.php @@ -665,10 +665,6 @@ function getFileModificationHistory(): array { libxml_use_internal_errors(true); } -$compact = defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0; -$big_lines = defined('LIBXML_BIGLINES') ? LIBXML_BIGLINES : 0; -$LIBXML_OPTS = LIBXML_NOENT | $big_lines | $compact; - if ($ac['VERSION_FILES'] === 'yes') { $dom = new DOMDocument; $dom->preserveWhiteSpace = false; @@ -748,22 +744,39 @@ function getFileModificationHistory(): array { checking('whether to save an invalid .manual.xml'); checkvalue($ac['FORCE_DOM_SAVE']); -echo "Loading and parsing {$ac["INPUT_FILENAME"]}... "; -flush(); $dom = new DOMDocument(); -// realpath() is important: omitting it causes severe performance degradation -// and doubled memory usage on Windows. -$didLoad = $dom->load(realpath("{$ac['srcdir']}/{$ac["INPUT_FILENAME"]}"), $LIBXML_OPTS); +function dom_load( DOMDocument $dom , string $filename ) : bool +{ + $filename = realpath( $filename ); + $options = LIBXML_NOENT | LIBXML_COMPACT | LIBXML_BIGLINES | LIBXML_PARSEHUGE; + return $dom->load( $filename , $options ); +} + +function dom_saveload( DOMDocument $dom , string $filename = "" ) +{ + if ( $filename == "" ) + $filename = __DIR__ . "/temp/manual.xml"; + + $dom->save( $filename ); + dom_load( $dom , $filename ); +} + +echo "Loading and parsing {$ac["INPUT_FILENAME"]}... "; -// Check if the XML was simply broken, if so then just bail out -if ($didLoad === false) { +if ( dom_load( $dom , "{$ac['srcdir']}/{$ac["INPUT_FILENAME"]}" ) ) +{ + dom_saveload( $dom ); // correct file/line/column on error messages + echo "done.\n"; +} +else +{ echo "failed.\n"; print_xml_errors(); errors_are_bad(1); } -echo "done.\n"; + echo "Running XInclude/XPointer... "; $total = 0; @@ -905,8 +918,8 @@ function getFileModificationHistory(): array { $mxml = $ac["OUTPUT_FILENAME"]; /* TODO: For some reason libxml does not validate the RelaxNG schema unless reloading the document in full */ -$dom->save($mxml); -$dom->load($mxml, $LIBXML_OPTS); +dom_saveload( $dom ); +$dom->save( $mxml ); // non idempotent historical path if ($dom->relaxNGValidate(RNG_SCHEMA_FILE)) { echo "done.\n"; printf("\nAll good. Saving %s... ", basename($ac["OUTPUT_FILENAME"])); From 4a8a07eb935d9a9d8e71e4357b0ea4abaa07f32a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=20F=20S=20Bacci?= Date: Sun, 8 Dec 2024 14:15:24 -0300 Subject: [PATCH 3/4] Contextual error messages, idempotent and non-idempotent saves --- configure.php | 72 +++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/configure.php b/configure.php index 5717f6d04..6a960c575 100755 --- a/configure.php +++ b/configure.php @@ -202,40 +202,44 @@ function make_scripts_executable($filename) // {{{ } } // }}} -// Loop through and print out all XML validation errors {{{ -function print_xml_errors($details = true) { +function print_xml_errors() +{ global $ac; + $report = $ac['LANG'] == 'en' || $ac['XPOINTER_REPORTING'] == 'yes'; + $output = ( $ac['STDERR_TO_STDOUT'] == 'yes' ) ? STDOUT : STDERR ; + $errors = libxml_get_errors(); - $output = ( $ac['STDERR_TO_STDOUT'] == 'yes' ) ? STDOUT : STDERR; - if ($errors && count($errors) > 0) { - foreach($errors as $err) { - if ($ac['LANG'] != 'en' && // translations - $ac['XPOINTER_REPORTING'] != 'yes' && // can disable - strncmp($err->message, 'XPointer evaluation failed:', 27) == 0) { - continue; - } - $errmsg = wordwrap(" " . trim($err->message), 80, "\n "); - if ($details && $err->file) { - $file = file(urldecode($err->file)); // libxml appears to urlencode() its errors strings - if (isset($file[$err->line])) { - $line = rtrim($file[$err->line - 1]); - $padding = str_repeat("-", $err->column) . "^"; - fprintf($output, "\nERROR (%s:%s:%s)\n%s\n%s\n%s\n", $err->file, $err->line, $err->column, $line, $padding, $errmsg); - } else { - fprintf($output, "\nERROR (%s:unknown)\n%s\n", $err->file, $errmsg); - } - } else { - fprintf($output, "%s\n", $errmsg); - } - // Error too severe, stopping - if ($err->level === LIBXML_ERR_FATAL) { - fprintf($output, "\n\nPrevious errors too severe. Stopping here.\n\n"); - break; - } - } - } libxml_clear_errors(); -} // }}} + + $filePrefix = "file:///"; + $tempPrefix = realpath( __DIR__ . "/temp" ) . "/"; + $rootPrefix = realpath( __DIR__ . "/.." ) . "/"; + + if ( count( $errors ) > 0 ) + fprintf( $output , "\n" ); + + foreach( $errors as $error ) + { + $mssg = rtrim( $error->message ); + $file = $error->file; + $line = $error->line; + $clmn = $error->column; + + if ( str_starts_with( $mssg , 'XPointer evaluation failed:' ) && ! $report ) + continue; // + + if ( str_starts_with( $file , $filePrefix ) ) + $file = substr( $file , strlen( $filePrefix ) ); + if ( str_starts_with( $file , $tempPrefix ) ) + $file = substr( $file , strlen( $tempPrefix ) ); + if ( str_starts_with( $file , $rootPrefix ) ) + $file = substr( $file , strlen( $rootPrefix ) ); + + $prefix = $error->level === LIBXML_ERR_FATAL ? "FATAL" : "ERROR"; + + fwrite( $output , "[$prefix $file {$line}:{$clmn}] {$mssg}\n" ); + } +} function find_xml_files($path) // {{{ { @@ -918,12 +922,12 @@ function dom_saveload( DOMDocument $dom , string $filename = "" ) $mxml = $ac["OUTPUT_FILENAME"]; /* TODO: For some reason libxml does not validate the RelaxNG schema unless reloading the document in full */ -dom_saveload( $dom ); -$dom->save( $mxml ); // non idempotent historical path +dom_saveload( $dom ); // idempotent path +$dom->save($mxml); // non idempotent, historical path if ($dom->relaxNGValidate(RNG_SCHEMA_FILE)) { echo "done.\n"; printf("\nAll good. Saving %s... ", basename($ac["OUTPUT_FILENAME"])); - $dom->save($mxml); + $dom->save($mxml); // save it again... does validations changes somethnig? echo "done.\n"; echo "All you have to do now is run 'phd -d {$mxml}'\n"; From b39ef1258d5081cf26f2c801fbb480fd787a04ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=20F=20S=20Bacci?= Date: Mon, 9 Dec 2024 12:53:20 -0300 Subject: [PATCH 4/4] Review fixes --- configure.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/configure.php b/configure.php index 6a960c575..20192c05b 100755 --- a/configure.php +++ b/configure.php @@ -226,7 +226,7 @@ function print_xml_errors() $clmn = $error->column; if ( str_starts_with( $mssg , 'XPointer evaluation failed:' ) && ! $report ) - continue; // + continue; // Translations can omit these, to focus on fatal errors if ( str_starts_with( $file , $filePrefix ) ) $file = substr( $file , strlen( $filePrefix ) ); @@ -235,7 +235,7 @@ function print_xml_errors() if ( str_starts_with( $file , $rootPrefix ) ) $file = substr( $file , strlen( $rootPrefix ) ); - $prefix = $error->level === LIBXML_ERR_FATAL ? "FATAL" : "ERROR"; + $prefix = $error->level === LIBXML_ERR_FATAL ? "FATAL" : "error"; fwrite( $output , "[$prefix $file {$line}:{$clmn}] {$mssg}\n" ); } @@ -926,10 +926,7 @@ function dom_saveload( DOMDocument $dom , string $filename = "" ) $dom->save($mxml); // non idempotent, historical path if ($dom->relaxNGValidate(RNG_SCHEMA_FILE)) { echo "done.\n"; - printf("\nAll good. Saving %s... ", basename($ac["OUTPUT_FILENAME"])); - $dom->save($mxml); // save it again... does validations changes somethnig? - - echo "done.\n"; + printf("\nAll good. Saved %s\n", basename($ac["OUTPUT_FILENAME"])); echo "All you have to do now is run 'phd -d {$mxml}'\n"; echo "If the script hangs here, you can abort with ^C.\n"; echo <<