Skip to content

Commit 1768961

Browse files
authored
Command line tool for XML sync testing between languages: attributes (#215)
1 parent 07b4806 commit 1768961

File tree

8 files changed

+611
-1
lines changed

8 files changed

+611
-1
lines changed

configure.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ function getFileModificationHistory(): array {
628628
$ac["TRANSLATION_ONLY_INCL_END"] = "-->";
629629
}
630630
checkvalue($ac['LANG']);
631+
file_put_contents( __DIR__ . "/temp/lang" , $ac['LANG'] );
631632

632633
checking("whether the language is supported");
633634
$LANGDIR = "{$ac['rootdir']}/{$ac['LANG']}";
@@ -850,8 +851,9 @@ function dom_saveload( DOMDocument $dom , string $filename = "" ) : string
850851

851852
if ( dom_load( $dom , "{$ac['srcdir']}/{$ac["INPUT_FILENAME"]}" ) )
852853
{
854+
echo "1 ";
853855
dom_saveload( $dom ); // correct file/line/column on error messages
854-
echo "done.\n";
856+
echo "2 done.\n";
855857
}
856858
else
857859
{
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php /*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) 1997-2025 The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt. |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected], so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: André L F S Bacci <ae php.net> |
14+
+----------------------------------------------------------------------+
15+
16+
# Description
17+
18+
This class caches formatted output, and calculates if this output is not
19+
previously marked as ignored, before printing it. */
20+
21+
class OutputBuffer
22+
{
23+
private string $filename = "";
24+
private string $header = "";
25+
private array $matter = [];
26+
private array $footer = [];
27+
28+
private OutputIgnore $ignore;
29+
private string $options;
30+
31+
public function __construct( string $header , string $filename , OutputIgnore $ignore )
32+
{
33+
$filename = str_replace( "/./" , "/" , $filename );
34+
35+
$this->header = $header . ": " . $filename . "\n\n";
36+
$this->filename = $filename;
37+
$this->ignore = $ignore;
38+
39+
$copy = $ignore->residualArgv;
40+
array_shift( $copy );
41+
$this->options = implode( " " , $copy );
42+
}
43+
44+
public function add( string $text )
45+
{
46+
$this->matter[] = $text;
47+
}
48+
49+
public function addDiff( string $text , int $sourceCount , int $targetCount )
50+
{
51+
if ( $sourceCount == $targetCount )
52+
return;
53+
$prefix = "* ";
54+
$suffix = " -{$targetCount} +{$sourceCount}";
55+
if ( $sourceCount == 0 )
56+
{
57+
$prefix = "- ";
58+
$suffix = $targetCount == 1 ? "" : " -{$targetCount}";
59+
}
60+
if ( $targetCount == 0 )
61+
{
62+
$prefix = "+ ";
63+
$suffix = $sourceCount == 1 ? "" : " +{$sourceCount}";
64+
}
65+
$this->add( "{$prefix}{$text}{$suffix}\n" );
66+
}
67+
68+
public function addFooter( string $text )
69+
{
70+
$this->footer[] = $text;
71+
}
72+
73+
public function addLine()
74+
{
75+
if ( count( $this->matter ) > 0 && end( $this->matter ) != "\n" )
76+
$this->add( "\n" );
77+
}
78+
79+
public function print( bool $useAlternatePrinting = false )
80+
{
81+
if ( count( $this->matter ) == 0 && count( $this->footer ) == 0 )
82+
return;
83+
84+
$hashHead = $this->hash( false );
85+
$hashFull = $this->hash( true );
86+
87+
if ( $this->ignore->shouldIgnore( $this , $this->filename , $hashHead , $hashFull ) )
88+
return;
89+
90+
print $this->header;
91+
92+
if ( $useAlternatePrinting )
93+
$this->printMatterAlternate();
94+
else
95+
foreach( $this->matter as $text )
96+
print $text;
97+
98+
if ( count( $this->matter ) )
99+
print "\n";
100+
101+
foreach( $this->footer as $text )
102+
print $text;
103+
104+
if ( count( $this->footer ) )
105+
print "\n";
106+
}
107+
108+
private function printMatterAlternate() : void
109+
{
110+
$add = array();
111+
$del = array();
112+
$rst = array();
113+
114+
foreach( $this->matter as $text )
115+
{
116+
if ( $text[0] == '+' ) $add[] = $text;
117+
elseif ( $text[0] == '-' ) $del[] = $text;
118+
else $rst[] = $text;
119+
}
120+
121+
for ( $idx = 0 ; $idx < count( $this->matter ) ; $idx++ )
122+
{
123+
if ( isset( $add[ $idx ] ) ) print $add[ $idx ];
124+
if ( isset( $del[ $idx ] ) ) print $del[ $idx ];
125+
}
126+
127+
foreach( $rst as $text )
128+
print $text;
129+
}
130+
131+
private function hash( bool $withContents ) : string
132+
{
133+
$text = $this->header . $this->options;
134+
if ( $withContents )
135+
$text .= implode( "" , $this->matter );
136+
$text = str_replace( " " , "" , $text );
137+
$text = str_replace( "\n" , "" , $text );
138+
$text = str_replace( "\r" , "" , $text );
139+
$text = str_replace( "\t" , "" , $text );
140+
return hash( "crc32b" , $text );
141+
}
142+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php /*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) 1997-2025 The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt. |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected], so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: André L F S Bacci <ae php.net> |
14+
+----------------------------------------------------------------------+
15+
16+
# Description
17+
18+
This class process commands for ignoring outputs, and complement non
19+
ignored outputs with these commands. */
20+
21+
class OutputIgnore
22+
{
23+
public array $residualArgv;
24+
25+
private bool $appendIgnores = true;
26+
private bool $showIgnore = true;
27+
private string $filename = ".syncxml.ignores";
28+
private string $argv0 = "";
29+
30+
public function __construct( array & $argv )
31+
{
32+
$this->argv0 = escapeshellarg( $argv[0] );
33+
34+
foreach( $argv as $key => $arg )
35+
{
36+
if ( str_starts_with( $arg , "--add-ignore=" ) )
37+
{
38+
$list = $this->loadIgnores();
39+
$line = substr( $arg , 13 );
40+
if ( ! in_array( $line , $list ) )
41+
{
42+
$list[] = $line;
43+
$this->saveIgnores( $list );
44+
}
45+
exit;
46+
}
47+
48+
if ( str_starts_with( $arg , "--del-ignore=" ) )
49+
{
50+
$list = $this->loadIgnores();
51+
$line = substr( $arg , 13 );
52+
$dels = 0;
53+
while ( in_array( $line , $list ) )
54+
{
55+
$key = array_search( $line , $list );
56+
unset( $list[$key] );
57+
$dels++;
58+
}
59+
if ( $dels == 0 )
60+
print "Ignore mark not found.\n";
61+
else
62+
$this->saveIgnores( $list );
63+
exit;
64+
}
65+
66+
if ( $arg == "--disable-ignore" )
67+
{
68+
$this->showIgnore = false;
69+
unset( $argv[$key] );
70+
}
71+
}
72+
73+
$this->residualArgv = $argv;
74+
}
75+
76+
private function loadIgnores()
77+
{
78+
if ( ! file_exists( $this->filename ) )
79+
return [];
80+
$data = file_get_contents( $this->filename );
81+
return unserialize( gzdecode( $data ) );
82+
}
83+
84+
public function saveIgnores( $data )
85+
{
86+
$contents = gzencode( serialize( $data ) );
87+
file_put_contents( $this->filename , $contents );
88+
}
89+
90+
public function shouldIgnore( OutputBuffer $output , string $filename , string $hashHeader , string $hashMatter )
91+
{
92+
$ret = false;
93+
94+
$prefix = "{$filename}:{$hashHeader}:";
95+
$ignore = "{$filename}:{$hashHeader}:{$hashMatter}";
96+
$marks = $this->loadIgnores();
97+
98+
// --add-ignore command
99+
100+
if ( in_array( $ignore , $marks ) )
101+
$ret = true; // is already ignored
102+
else //
103+
if ( $this->showIgnore ) // show add command
104+
$output->addFooter( " php {$this->argv0} --add-ignore=$ignore\n" );
105+
106+
// Remove valid ignores, leaves outdated ones for listing
107+
108+
while ( in_array( $ignore , $marks ) )
109+
{
110+
$key = array_search( $ignore , $marks );
111+
unset( $marks[$key] );
112+
}
113+
114+
// --del-ignore command
115+
116+
if ( $this->showIgnore ) // show del commands (for this file/prefix)
117+
foreach ( $marks as $mark )
118+
if ( $mark != null )
119+
if ( str_starts_with( $mark , $prefix ) )
120+
$output->addFooter( " php {$this->argv0} --del-ignore=$mark\n" );
121+
122+
return $ret;
123+
}
124+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php /*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) 1997-2025 The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt. |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected], so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: André L F S Bacci <ae php.net> |
14+
+----------------------------------------------------------------------+
15+
16+
# Description
17+
18+
Holds file related data for synq XML tools. */
19+
20+
require_once __DIR__ . '/all.php';
21+
22+
class SyncFileItem
23+
{
24+
public string $sourceDir;
25+
public string $targetDir;
26+
public string $file;
27+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php /*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) 1997-2025 The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt. |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected], so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: André L F S Bacci <ae php.net> |
14+
+----------------------------------------------------------------------+
15+
16+
# Description
17+
18+
Generates (and caches) the list of files with TranslatedOk status. */
19+
20+
require_once __DIR__ . '/all.php';
21+
22+
class SyncFileList
23+
{
24+
static function load()
25+
{
26+
$file = __DIR__ . "/../../../temp/lang";
27+
if ( ! file_exists( $file ) )
28+
{
29+
fwrite( STDERR , "Language file not found, run 'doc-base/configure.php'.\n" );
30+
exit();
31+
}
32+
33+
$lang = trim( file_get_contents( $file ) );
34+
$cache = __DIR__ . "/../../../temp/$lang.oklist";
35+
36+
if ( file_exists( $cache ) )
37+
{
38+
$data = file_get_contents( $cache );
39+
return unserialize( gzdecode( $data ) );
40+
}
41+
42+
require_once __DIR__ . '/../lib/all.php';
43+
44+
$revcheck = new RevcheckRun( 'en' , $lang );
45+
$revdata = $revcheck->revData;
46+
$list = [];
47+
48+
foreach( $revdata->fileDetail as $file )
49+
{
50+
if ( $file->status != RevcheckStatus::TranslatedOk )
51+
continue;
52+
53+
$item = new SyncFileItem();
54+
$item->sourceDir = $revcheck->sourceDir;
55+
$item->targetDir = $revcheck->targetDir;
56+
$item->file = $file->path . '/' . $file->name;
57+
$list[] = $item;
58+
}
59+
60+
$contents = gzencode( serialize( $list ) );
61+
file_put_contents( $cache , $contents );
62+
63+
return $list;
64+
}
65+
}

0 commit comments

Comments
 (0)