forked from abantecart/testing_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage_xml_comparer.php
executable file
·215 lines (177 loc) · 6.15 KB
/
language_xml_comparer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
* This script gets english language xml-files and compares with language extensions.
* Use for testing and fixing of language extensions!
* Just set START_DIR absolute path and run it via browser
* */
define('START_DIR', '');
#######################################
## DO NOT TOUCH CODE BELOW
#######################################
$admin_lang_files = get_all_files_dirs(START_DIR . 'admin/language/');
$sf_lang_files = get_all_files_dirs(START_DIR . 'storefront/language/');
if (!$admin_lang_files || !$sf_lang_files){
exit('no one language file found!');
}
$not_exists = array ();
/* *
* ADMIN SECTION
* */
$extension_dirs = glob(START_DIR . 'extensions/*', GLOB_ONLYDIR);
foreach ($extension_dirs as $extdir){
$extension_name = basename($extdir);
//check is extension have type "language"
$config = simplexml_load_file(START_DIR . 'extensions/' . $extension_name . '/config.xml');
if (!$config){
continue;
}
if ((string)$config->type != 'language'){
continue;
}
//detect folder with language files (language name)
$ldirs = glob(START_DIR . 'extensions/' . $extension_name . '/admin/language/*', GLOB_ONLYDIR);
$language_dir = '';
foreach ($ldirs as $ldir){
if (strpos($ldir, 'english') !== false){
continue;
} else{
$language_dir = basename($ldir);
break;
}
}
if (!$language_dir){
echo $extension_name . ' extension was skipped. cannot recognize folder with xml-files inside admin/language directory.' . "\n";
continue;
}
foreach ($admin_lang_files as $core_filename){
if (pathinfo($core_filename, PATHINFO_EXTENSION) != 'xml'){
continue;
}
$relative_path = str_replace(START_DIR . 'admin/language/english/', '', $core_filename);
if (pathinfo($relative_path, PATHINFO_FILENAME) . '.' . pathinfo($relative_path, PATHINFO_EXTENSION) == 'english.xml'){
$relative_path = str_replace('english.xml', $language_dir . '.xml', $relative_path);
}
$lang_file = START_DIR . 'extensions/' . $extension_name . '/admin/language/' . $language_dir . '/' . $relative_path;
echo compare($core_filename, $lang_file);
}
foreach ($sf_lang_files as $core_filename){
if (pathinfo($core_filename, PATHINFO_EXTENSION) != 'xml'){
continue;
}
$relative_path = str_replace(START_DIR . 'storefront/language/english/', '', $core_filename);
if (pathinfo($relative_path, PATHINFO_FILENAME) . '.' . pathinfo($relative_path, PATHINFO_EXTENSION) == 'english.xml'){
$relative_path = str_replace('english.xml', $language_dir . '.xml', $relative_path);
}
$lang_file = START_DIR . 'extensions/' . $extension_name . '/storefront/language/' . $language_dir . '/' . $relative_path;
echo compare($core_filename, $lang_file);
}
}
echo '<ol>';
foreach ($not_exists as $line){
echo '<li>' . $line . '</li>';
}
echo '</ol>';
exit;
// for future use. TODO:// needs to compare definitions inside extensions
/*
* EXTENSIONS
* */
$dirs = glob(START_DIR . 'extensions/*', GLOB_ONLYDIR);
foreach ($dirs as $extdir){
$extension_name = pathinfo($extdir, PATHINFO_BASENAME);
$files = glob($extdir . '/admin/language/english/' . $extension_name . '/*');
foreach ($files as $file){
$file2 = pathinfo($file, PATHINFO_BASENAME);
$file2 = START_DIR . 'extensions/' . $extension_name . '/admin/language/spanish/' . $extension_name . '/' . $file2;
echo compare($file, $file2);
}
$files = glob($extdir . '/storefront/language/english/' . $extension_name . '/*');
foreach ($files as $file){
$file2 = pathinfo($file, PATHINFO_BASENAME);
$file2 = START_DIR . 'extensions/' . $extension_name . '/storefront/language/spanish/' . $extension_name . '/' . $file2;
echo compare($file, $file2);
}
}
/*******************************
* functions
**********************/
function compare($file1, $file2){
global $not_exists;
$output = '';
if (!file_exists($file2)){
$not_exists[] = '<p style="color: #8b0000;"> ' . str_replace(START_DIR, '', $file2) . ' is does not exists. Tried to copy.</p>';
//try to copy
if (!is_dir(dirname($file2))){
mkdir(dirname($file2), 0777);
}
copy($file1, $file2);
return false;
}
$xml1 = simplexml_load_file($file1);
if (!$xml1){
exit($file1 . " is corrupted!");
}
$xml2 = simplexml_load_file($file2);
if (!$xml2){
exit($file2 . " is corrupted!");
}
$keys1 = $keys2 = $values1 = $values2 = array ();
foreach ($xml1->definition as $def){
$keys1[] = (string)$def->key;
$values1[(string)$def->key] = (string)$def->value;
}
foreach ($xml2->definition as $def){
$keys2[] = (string)$def->key;
$values2[(string)$def->key] = (string)$def->value;
}
$diff = array_diff($keys1, $keys2);
if ($diff){
$output .= "</br></br>Keys that not presents in <b>" . str_replace(START_DIR, '', $file2) . "</b> are:<br/>";
foreach ($diff as $key){
$slice = $xml1->xpath("definition/key[text()='" . $key . "']/parent::*");
$definition = $xml2->addChild('definition');
$definition->addChild('key', (string)$key);
$v = $definition->addChild('value');
addCData($v, '????' . (string)$slice[0]->value);
$output .= $key . '</br>';
}
}
if ($output){
$dom = new DOMDocument("1.0");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml2->asXML());
$string_xml = $dom->saveXML();
$string_xml = str_replace(' ', "\t", $string_xml);
file_put_contents($file2, $string_xml);
$output .= "<br>";
}
//check non-translated items
foreach ($keys1 as $key){
if ($values1[$key] == $values2[$key]){
$output .= "</br></br>Keys that seems not translated in <b>" . str_replace(START_DIR, '', $file2) . "</b> are:<br/>";
$output .= $key . '</br>';
}
}
return $output;
}
function addCData(&$xml_node, $cdata_text){
$node = dom_import_simplexml($xml_node);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($cdata_text));
}
function get_all_files_dirs($start_dir){
$iter = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($start_dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);
$paths = array ($start_dir);
foreach ($iter as $path => $dir){
if (pathinfo($path, PATHINFO_EXTENSION) != 'xml'){
continue;
}
$paths[] = $path;
}
return $paths;
}