Skip to content

Commit

Permalink
Command line tool for XML sync testing between languages: entities (#219
Browse files Browse the repository at this point in the history
)
  • Loading branch information
alfsb authored Feb 10, 2025
1 parent 181ecbe commit c63809f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 10 deletions.
2 changes: 1 addition & 1 deletion scripts/translation/libqa/OutputBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function addDiff( string $text , int $sourceCount , int $targetCount )

public function addFooter( string $text )
{
$this->footer[] = $text;
// $this->footer[] = $text;
}

public function addLine()
Expand Down
12 changes: 10 additions & 2 deletions scripts/translation/libqa/SyncFileList.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static function load()
}

$lang = trim( file_get_contents( $file ) );
$cache = __DIR__ . "/../../../temp/$lang.oklist";
$cache = __DIR__ . "/../../../temp/qaxml.files.$lang";

if ( file_exists( $cache ) )
{
Expand All @@ -47,7 +47,12 @@ static function load()

foreach( $revdata->fileDetail as $file )
{
if ( $file->status != RevcheckStatus::TranslatedOk )
$source = "{$revcheck->sourceDir}/{$file->path}/{$file->name}";
$target = "{$revcheck->targetDir}/{$file->path}/{$file->name}";

if ( ! file_exists( $source ) )
continue;
if ( ! file_exists( $target ) )
continue;

$item = new SyncFileItem();
Expand All @@ -57,6 +62,9 @@ static function load()
$list[] = $item;
}

if ( count( $list ) == 0 )
throw new Exception( "No files found. Called from wrong directory?" );

$contents = gzencode( serialize( $list ) );
file_put_contents( $cache , $contents );

Expand Down
6 changes: 3 additions & 3 deletions scripts/translation/libqa/XmlFrag.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ static function loadXmlFragmentFile( string $filename )
[ $doc , $ent , $err ] = XmlFrag::loadXmlFragmentText( $contents , "" );

if ( count( $err ) == 0 )
return [ $doc , $err ];
return [ $doc , $ent , $err ];

$dtd = "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE frag [\n";
foreach ( $ent as $e )
$dtd .= "<!ENTITY $e ''>\n";
$dtd .= "]>\n";

[ $doc , $ent , $err ] = XmlFrag::loadXmlFragmentText( $contents , $dtd );
[ $doc , $ign , $err ] = XmlFrag::loadXmlFragmentText( $contents , $dtd );

return [ $doc , $err ];
return [ $doc , $ent , $err ];
}

static function loadXmlFragmentText( string $contents , string $dtd )
Expand Down
8 changes: 4 additions & 4 deletions scripts/translation/qaxml-sync-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
$target = $file->targetDir . '/' . $file->file;
$output = new OutputBuffer( "# qaxml.a" , $target , $ignore );

[ $s , $e ] = XmlFrag::loadXmlFragmentFile( $source );
[ $t , $e ] = XmlFrag::loadXmlFragmentFile( $target );
[ $s , $_ , $_ ] = XmlFrag::loadXmlFragmentFile( $source );
[ $t , $_ , $_ ] = XmlFrag::loadXmlFragmentFile( $target );

$s = XmlFrag::listNodes( $s , XML_ELEMENT_NODE );
$t = XmlFrag::listNodes( $t , XML_ELEMENT_NODE );
Expand All @@ -43,9 +43,9 @@
$match = array();

foreach( $s as $v )
$match[$v] = array( 0 , 0 );
$match[$v] = [ 0 , 0 ];
foreach( $t as $v )
$match[$v] = array( 0 , 0 );
$match[$v] = [ 0 , 0 ];

foreach( $s as $v )
$match[$v][0] += 1;
Expand Down
57 changes: 57 additions & 0 deletions scripts/translation/qaxml-sync-entities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php /*
+----------------------------------------------------------------------+
| Copyright (c) 1997-2025 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected], so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: André L F S Bacci <ae php.net> |
+----------------------------------------------------------------------+
# Description
Compare XML entities usage between two XML leaf/fragment files. */

require_once __DIR__ . '/libqa/all.php';

$ignore = new OutputIgnore( $argv ); // always first, may exit.
$list = SyncFileList::load();

foreach ( $list as $file )
{
$source = $file->sourceDir . '/' . $file->file;
$target = $file->targetDir . '/' . $file->file;
$output = new OutputBuffer( "# qaxml.e" , $target , $ignore );

[ $_ , $s , $_ ] = XmlFrag::loadXmlFragmentFile( $source );
[ $_ , $t , $_ ] = XmlFrag::loadXmlFragmentFile( $target );

if ( implode( "\n" , $s ) == implode( "\n" , $t ) )
continue;

$match = array();

foreach( $s as $v )
$match[$v] = [ 0 , 0 ];
foreach( $t as $v )
$match[$v] = [ 0 , 0 ];

foreach( $s as $v )
$match[$v][0] += 1;
foreach( $t as $v )
$match[$v][1] += 1;

foreach( $match as $k => $v )
{
if ( $v[0] == $v[1] )
continue;
$output->addDiff( $k , $v[0] , $v[1] );
}

$output->print();
}

0 comments on commit c63809f

Please sign in to comment.