Skip to content

Commit

Permalink
Merge b62b923 into dd76e3d
Browse files Browse the repository at this point in the history
  • Loading branch information
peuter committed Sep 19, 2018
2 parents dd76e3d + b62b923 commit ea20966
Showing 1 changed file with 56 additions and 10 deletions.
66 changes: 56 additions & 10 deletions source/check_config.php
Expand Up @@ -33,10 +33,12 @@ function xml_highlight($s)

function libxml_display_error( $error )
{
global $lines, $error_array;
global $combinedLines, $error_array, $lineMappings;

$error_array[] = $error->line;

$return = "";

switch ($error->level)
{
case LIBXML_ERR_WARNING:
Expand All @@ -52,13 +54,18 @@ function libxml_display_error( $error )

$return .= trim( $error->message );
$return .= ' on <a href="#' . ($error->line-1) . '">line <b>' . $error->line . '</b></a>';
foreach($lineMappings as $mapping) {
if ($error->line >= $mapping['start'] && $error->line <= $mapping['end']) {
$return .= ' [origin in file ' . $mapping['file'] . ' line ' . ($error->line - $mapping['start']) . ']';
}
}

$return .= '<pre>';
for( $i = max( 0, $error->line - 1 - 3); $i <= $error->line - 1 + 3; $i++ )
{
if( $i == $error->line - 1 ) $return .= '<b>';
$return .= sprintf( '%4d: ', $i+1 );
$return .= xml_highlight( $lines[ $i ] );
$return .= xml_highlight( $combinedLines[ $i ] );
if( $i == $error->line - 1 ) $return .= '</b>';
}
$return .= '</pre>';
Expand All @@ -85,7 +92,9 @@ function checkVersion( $dom )
}

// Enable user error handling
libxml_use_internal_errors(true);
libxml_use_internal_errors(true);

$resourceDir = 'resource/';


$dom = new DomDocument();
Expand All @@ -97,7 +106,7 @@ function checkVersion( $dom )
$conffile = "http://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].$_SERVER['SCRIPT_NAME']."/config/visu_config";
}
else {
$conffile = 'resource/config/visu_config';
$conffile = $resourceDir . 'config/visu_config';
}
if (defined('STDIN')) {
$conffile = "visu_config_" . $argv[1];
Expand All @@ -107,7 +116,7 @@ function checkVersion( $dom )
$conffile .= '.xml';

if ( false === is_readable( $conffile ) ) {
$conffile = 'resource/demo/visu_config';
$conffile = $resourceDir . 'demo/visu_config';
if ($_GET['config']) {
$conffile .= "_" . $_GET['config'];
}
Expand All @@ -133,10 +142,47 @@ function checkVersion( $dom )
exit;
}
}

$lines = file( $conffile );
$dom->load( $conffile );
$combinedLines = $lines;
$lineMappings = array();
$mappedLineOffset = 0;

foreach( $lines as $line_num => $line ) {
if (preg_match('/(\s*)(.*)<include src="([^"]+)"\s*\/>(.*)/', $line, $matches)) {
$includedLines = file($resourceDir . $matches[3]);
$func = function($value) {
global $matches;
return $matches[1] . $value;
};
$startLine = $line_num + $mappedLineOffset;
$mappingStart = $startLine;
array_unshift($includedLines, "<!-- Start of " . $matches[3] . " -->\n");
$mappingStart++;
$includedLines[sizeof($includedLines) -1 ] .= "\n";
array_push($includedLines, "<!-- End of " . $matches[3] . " -->\n");
$includedLines = array_map($func, $includedLines);
if (!empty($matches[2])) {
array_unshift($includedLines, $matches[2]);
$mappingStart++;
}

if (!empty($matches[4])) {
array_push($includedLines, $matches[4]);
}
array_splice($combinedLines, ($line_num + $mappedLineOffset), 1, $includedLines);
$lineMappings[] = array(
'file' => $matches[3],
'start' => $mappingStart,
'end' => $startLine + sizeof($includedLines) - 1
);
$mappedLineOffset += sizeof($includedLines) - 1;
}
}
$source = implode('', $combinedLines);
$dom->loadXML( $source );

if( $dom->schemaValidate( 'resource/visu_config.xsd' ) )
if( $dom->schemaValidate( $resourceDir . 'visu_config.xsd' ) )
{
echo 'config <b>' . $conffile . ' is valid </b> XML';
if( $_GET['src'] === 'editor' )
Expand Down Expand Up @@ -165,15 +211,15 @@ function checkVersion( $dom )
echo '<hr />';

echo '<pre>';
foreach( $lines as $line_num => $line )
foreach( $combinedLines as $line_num => $line )
{
$error_in_line = in_array( $line_num+1, $error_array );
if( $error_in_line ) echo '<b>';
printf( '<a name="%s">%4d</a>: ', $line_num, $line_num+1 );
echo xml_highlight( $line );
if( $error_in_line ) echo '</b>';
}
}
echo '</pre>';
?>
?>
</body>
</html>

0 comments on commit ea20966

Please sign in to comment.