-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlangdump.php
executable file
·239 lines (188 loc) · 5.41 KB
/
langdump.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/php
<?php
/*
This file is part of Erben
A tool for making full e-books from pages digitized by Czech National Library
Copyright (C) 2014 Czech Pirate Party
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (empty($_SERVER['argv'])) {
die("Run this script from command line.\n");
}
define('S_IFMT', 0xf000);
define('S_IFDIR', 0x4000);
define('S_IFREG', 0x8000);
/**
* Create escape sequences for double-quoted PHP strings
*/
function qqescape($str) {
return addcslashes($str, "\0..\x1f\\\$\"");
}
function slurp($file, array &$ret) {
$data = file_get_contents($file);
if (empty($data)) {
return;
}
preg_match_all('/\\btr\\b\\s*\\(\\s*("(?:\\\\.|[^\\\\"]*)*"|\'(?:\\\\.|[^\'\\\\]*)*\')\\s*(.)/mu', $data, $matches, PREG_SET_ORDER);
foreach ($matches as $call) {
$msg = $call[1];
# Message is not complete, skip
if ($call[2] != ')' && $call[2] != ',') {
continue;
}
if ($msg[0] == '"') {
# Message contains variable expansion, skip
if (!preg_match('/^(?:\\\\.|[^\\\\$]*)*$/', $msg, $m2)) {
continue;
}
$msg = preg_replace('/((?:[^\\\\]|\\G)(?:\\\\\\\\)*)(\\\\[^\\\\"nrtvef$x0-7])/', '$1\\$2', $msg);
} else {
$msg = preg_replace('/((?:[^\\\\]|\\G)(?:\\\\\\\\)*)(\\\\[^\\\\\'])/', '$1\\$2', $msg);
}
$msg = stripcslashes(substr($msg, 1, strlen($msg) - 2));
if ($call[2] == ')') {
$ret[$msg] = null;
} else {
$ret[$msg] = array('0' => null);
}
}
}
function dirwalk($path, &$ret) {
$stat = stat($path);
if (empty($stat)) {
return;
}
$type = $stat['mode'] & S_IFMT;
if ($type == S_IFREG && preg_match('/\.php$/iu', $path)) {
slurp($path, $ret);
} else if ($type == S_IFDIR) {
$ents = scandir($path);
if (empty($ents)) {
return;
}
foreach ($ents as $item) {
if ($item[0] != '.') {
dirwalk($path . '/' . $item, $ret);
}
}
}
}
function dump_msgfile($file, array $msglist) {
$messages = array();
$prologue = '';
$ns = 'FIXME';
if (!empty($file)) {
$prologue = @file_get_contents($file);
# Fill $messages with existing translations
if (!empty($prologue)) {
include $file;
}
if (preg_match('#([^\\\\/]*)\.php#iu', $file, $matches)) {
$ns = $matches[1];
}
}
if (empty($prologue)) {
$prologue = <<<EOF
<?php
/*
This file is part of Erben
A tool for making full e-books from pages digitized by Czech National Library
Copyright (C) 2014 Czech Pirate Party
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Lang\\$ns;
function plural(\$num) {
return '0';
}
# Any changes after the first appearance of "messages" variable other than
# translations will be lost during file update.
EOF;
} else {
$prologue = preg_replace('/\\$messages\\b.*/su', '', $prologue);
}
foreach ($messages as $key => $trans) {
if (array_key_exists($key, $msglist)) {
$msglist[$key] = $trans;
}
}
$msgdef = '';
foreach ($msglist as $key => $value) {
$msgdef .= '$messages["' . qqescape($key) . '"]';
if (is_null($value)) {
$msgdef .= " = null";
} else if (is_array($value)) {
foreach ($value as $key => $val) {
$msgdef .= '["' . qqescape($key) . '"] = ';
if (is_null($val)) {
$msgdef .= "null";
} else {
$msgdef .= '"' . qqescape($val) . '"';
}
}
} else {
$msgdef .= ' = "' . qqescape($value) . '"';
}
$msgdef .= ";\n";
}
if (!empty($file)) {
file_put_contents($file, "$prologue$msgdef");
} else {
echo "$prologue$msgdef";
}
}
$cmdopts = array_slice($_SERVER['argv'], 1);
$pathlist = array();
$outfiles = array();
$msglist = array();
reset($cmdopts);
for ($val = current($cmdopts); !is_null(key($cmdopts)); $val = next($cmdopts)) {
if (empty($val)) {
continue;
} else if ($val[0] != '-') {
$pathlist[] = $val;
} else if ($val == '-o') {
$arg = next($cmdopts);
if (is_null($arg) || strlen($arg) == 0) {
die("Invalid or missing argument for option -o\n");
}
$outfiles[] = $arg;
} else {
die("Invalid option $val\n");
}
}
foreach ($pathlist as $file) {
dirwalk($file, $msglist);
}
ksort($msglist);
if (empty($outfiles)) {
dump_msgfile(null, $msglist);
}
foreach ($outfiles as $file) {
dump_msgfile($file, $msglist);
}
/*
$msgdef = '';
foreach ($msglist as $key => $value) {
$msgdef .= '$messages["' . qqescape($key) . '"] = ';
$msgdef .= (is_null($value) ? "null" : '"' . qqescape($value) . '"') . ";\n";
}
echo $msgdef;
*/