forked from grobmeier/simplepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOParser.php
448 lines (402 loc) · 12 KB
/
POParser.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
function debug($text) {
echo "<pre>";
print_r($text);
echo "</pre>";
}
interface PoMsgStore {
public function write( $msg, $isHeader );
public function read();
public function clean( $msgid_keys );
}
class TempPoMsgStore implements PoMsgStore {
private $msgs;
private $deleted;
function write($msg,$isHeader) {
$this->msgs[] = $msg;
}
function read() {
return $this->msgs;
}
function clean($msgid_keys) {
return $this->deleted;
}
}
class DBPoMsgStore implements PoMsgStore {
private $filled_keys = array();
public function init( $catalogue_name ){
$q = new Query();
$catalogue = $q->sql("SELECT * FROM {catalogues} WHERE name = ?",
$catalogue_name)->fetch();
if (!$catalogue) {
$q->sql("INSERT INTO {catalogues} (name) VALUES (?)",
$catalogue_name)->execute();
$this->catalogue_id = $q->insertId();
} else {
$this->catalogue_id = $catalogue['id'];
}
#Get keys to prevent them to be erased
$keys = $q->sql("SELECT msgid FROM {messages}
WHERE catalogue_id=? AND msgstr != ''",
$this->catalogue_id)
->fetchCol();
#Hashmap for perfs
if ( is_array($keys) ) {
foreach($keys as $key) {
$this->filled_keys[$key] = $key;
}
}
}
public function write( $msg, $isHeader ){
$q = new Query();
$msg['is_obsolete'] = !!$msg['is_obsolete'] ? 1 : 0;
$msg['is_header'] = $isHeader ? 1 : 0;
#TODO put this optional. Keys can be erased if option is on.
# Give priority to simplepo for already translated keys.
if ( !isset( $this->filled_keys[$msg["msgid"]] ) ) {
$q->sql("DELETE FROM {messages}
WHERE catalogue_id=? AND BINARY msgid= BINARY ? AND msgstr = ''",
$this->catalogue_id,$msg["msgid"])
->execute();
#FIXME Need to be checked: was the previous delete done? if yes go on else stop.
$allow_empty_fields = array('translator-comments', 'extracted-comments', 'reference', 'flags', 'is_obsolete', 'previous-untranslated-string');
foreach($allow_empty_fields as $field) {
if (!isset($msg[$field])) {
$msg[$field] = '';
}
}
$q->sql("INSERT INTO {messages}
(catalogue_id, msgid, msgstr, comments, extracted_comments,
reference,flags, is_obsolete, previous_untranslated_string,
is_header) VALUES (?, ?, ?, ?, ?, ?, ?, ?,?,?)",
$this->catalogue_id , $msg["msgid"], $msg["msgstr"],
$msg["translator-comments"], $msg["extracted-comments"],
$msg["reference"], $msg["flags"], $msg["is_obsolete"],
$msg["previous-untranslated-string"],$msg['is_header'])
->execute();
}
}
public function read(){
$q = new Query();
return $q->sql("SELECT * FROM {messages} WHERE catalogue_id = ? ORDER BY is_header DESC,is_obsolete,id",
$this->catalogue_id)->fetchAll();
}
public function clean($keys){
$todel = "";
$unused = "";
$used = "";
$i = 0;
# Get database keys
$q = new Query();
$msgs = $q->sql("SELECT msgid,msgstr FROM {messages} WHERE catalogue_id = ?", $this->catalogue_id)->fetchAll();
# Compare database and file keys
if ( is_array($msgs) ) {
foreach ( $msgs as $msg ) {
if ( $msg["msgid"] == 'login_account' ) {
//var_dump($msg["msgid"]);
//var_dump($keys);
}
#TODO handle special char on msgid (like " or ,)
if ( !isset( $keys['"'.$msg["msgid"].'"'] ) ) {
if ( empty( $msg["msgstr"] ) ) {
# delete unused key
$todel .= ($todel=="") ? '"'.$msg["msgid"].'"' : ',"'.$msg["msgid"].'"';
} else {
# mark obsolete unused key
$unused .= ($unused=="") ? '"'.$msg["msgid"].'"' : ',"'.$msg["msgid"].'"';
}
$i++;
} else {
$used .= ($used=="") ? '"'.$msg["msgid"].'"' : ',"'.$msg["msgid"].'"';
}
}
}
if ( !empty($todel) ) {
$q->sql("DELETE FROM {messages}
WHERE catalogue_id=? AND msgid IN ($todel) AND msgstr = ''",
$this->catalogue_id)
->execute();
}
if ( !empty($unused) ) {
$q->sql("UPDATE {messages}
SET is_obsolete = 1 WHERE catalogue_id=? AND msgid IN ($unused)",
$this->catalogue_id,$unused)
->execute();
}
if ( !empty($used) ) {
$q->sql("UPDATE {messages}
SET is_obsolete = 0 WHERE catalogue_id=? AND msgid IN ($used)",
$this->catalogue_id,$used)
->execute();
}
return $i;
}
}
class POParser{
public $fileHandle;
protected $context = array();
public $entryStore;
protected $match_expressions = array(
array(
'type' => 'translator-comments',
're_match' => '/(^# )|(^#$)/',
're_capture' => '/#\s*(.*)$/',
),
array(
'type' => 'extracted-comments',
're_match' => '/^#. /',
're_capture' => '/#.\s+(.*)$/',
),
array(
'type' => 'reference',
're_match' => '/^#: /',
're_capture' => '/#:\s+(.*)$/',
),
array(
'type' => 'flags',
're_match' => '/^#, /',
're_capture' => '/#,\s+(.*)$/',
),
array(
'type' => 'previous-untranslated-string',
're_match' => '/^#\| /',
're_capture' => '/#\|\s+(.*)$/',
),
array(
'type' => 'msgid',
're_match' => '/^msgid /',
're_capture' => '/msgid\s+(".*")/',
),
array(
'type' => 'msgstr',
're_match' => '/^msgstr /',
're_capture' => '/msgstr\s+(".*")/',
),
array(
'type' => 'string',
're_match' => '/^\s*"/',
're_capture' => '/^\s*(".*")/',
),
array(
'type' => 'obsolete-msgid',
're_match' => '/^#~\s+msgid /',
're_capture' => '/#~\s+msgid\s+(".*")/',
),
array(
'type' => 'obsolete-msgstr',
're_match' => '/^#~\s+msgstr /',
're_capture' => '/#~\s+msgstr\s+(".*")/',
),
array(
'type' => 'obsolete-string',
're_match' => '/^#~\s+\s*"/',
're_capture' => '/^#~\s+(".*")/',
),
array(
'type' => 'empty',
're_match' => '/^$/',
're_capture' => '/^()$/'
)
);
public function __construct($entryStore){
$this->entryStore = $entryStore;
}
public function writePoFileToStream($fh) {
$entries = $this->entryStore->read();
foreach($entries as $entry) {
fwrite( $fh, $this->convertEntryToString($entry) );
}
}
public function parseEntriesFromStream($fh) {
$this->lineNumber = 0;
$entry_count = 0;
$entry_lines = array();
while( ($line = fgets($fh)) !== false ) {
$this->lineNumber++;
$line = $this->parseLine($line);
if ( $line["type"] != "empty" ){
$entry_lines[] = $line;
}
else {
$entry = $this->reduceLines($entry_lines);
$this->saveEntry( $entry, $entry_count++ );
$entry_lines = array();
}
}
if ( $entry_lines ){
$entry = $this->reduceLines($entry_lines);
$this->saveEntry( $entry, $entry_count++ );
}
}
public function cleanDatabase($fh) {
$this->lineNumber = 0;
$entry_count = 0;
$deleted = 0;
$msgid_keys = array();
# First, build a map with all the file keyid
while( ($line = fgets($fh)) !== false ) {
$this->lineNumber++;
$line = $this->parseLine($line);
if ( $line["type"] == "msgid" ){
$msgid_keys[$line["value"]] = $line["value"];
if ($msgid_keys[$line["value"]] == "login_account") {
var_dump("$msgid_keys");
}
}
}
# Now test each database keyid and delete or mark it obsolete.
$deleted = $this->entryStore->clean($msgid_keys);
return $deleted;
}
function parseLine( $line ){
$this->line_count++;
$line_object = array();
foreach($this->match_expressions as $m) {
if(preg_match($m['re_match'],$line) ) {
preg_match($m['re_capture'],$line,$matches);
$line_object['value'] = $matches[1];
$line_object['type'] = $m['type'];
}
}
// didn't match anything
if(!$line_object) {
throw new Exception( sprintf("unrecognized line fomat at line: %d",$this->line_count) );
}
return $line_object;
}
public function decodeStringFormat( $str ){
if ( substr($str, 0, 1) == '"' && substr($str, -1,1) == '"' ){
$result = substr($str, 1, -1);
$translations = array("\\\\"=>"\\", "\\n"=>"\n",'\\"'=>'"');
$result = strtr($result, $translations);
} else {
throw new Exception("Invalid PO string (should be surrounded by quotes)\n$str\n");
}
return $result;
}
/**
* translates
* Hello"
* World
* to
* ""
* "Hello\"\n"
* "World"
*/
public function encodeStringFormat($message_string){
// translate the characters to escapted versions.
$translations = array("\n"=>"\\n",'"'=>'\\"',"\\"=>"\\\\");
$result = strtr($message_string, $translations);
// put the \n's at the end of the lines.
$result = str_replace("\\n","\\n\n",$result);
// wrap text so po files can be edited nicely.
$lines = explode("\n",$result);
foreach($lines as &$l) {
$l = $this->wordwrap($l,78);
}
$result = implode("\n",$lines);
// if there are mutiple lines, lets prefix everything with a "" like the gettext tools
if(strpos($result,"\n"))
$result = "\n" . $result;
// wrap each line in quotes
$result = $this->addPrefixToLines('"',$result);
$result = $this->addSuffixToLines('"',$result);
return $result;
}
public function addPrefixToLines($prefix,$text) {
$text = explode("\n",$text);
foreach($text as &$line) {
$line = $prefix . $line;
}
return implode("\n",$text);
}
public function addSuffixToLines($suffix,$text) {
$text = explode("\n",$text);
foreach($text as &$line) {
$line = $line . $suffix;
}
return implode("\n",$text);
}
public function wordwrap($text,$max_len=75) {
$result = "";
$ll=0;
$words = explode(" ",$text);
foreach($words as $w) {
$lw = mb_strlen($w,'UTF-8');
if ( $ll + $lw + 1 < $max_len) {
$result .= $w ." ";
$ll += $lw + 1;
}
else {
$result .= "\n" . $w . " ";
$ll = $lw + 1;
}
}
$result = substr($result,0,-1);
return $result;
}
public function saveEntry( $entry, $entry_count ){
$this->entryStore->write($entry, $entry_count == 0 );
}
public function reduceLines( $entry_lines ){
$entry = array();
$context = "";
$is_obsolete = false;
foreach ( $entry_lines as $line ) {
// convert the obsolete types into normal type, and mark as obsolete;
if (substr($line['type'],0,9) == "obsolete-") {
$is_obsolete = true;
$line['type'] = substr($line['type'],9);
preg_match('/".*"/',$line['value'],$m);
$line['value'] = $m[0];
}
if($line['type'] == "string") {
if($context == "msgid" || $context == "msgstr"){
$entry[ $context ][] = $this->decodeStringFormat( $line['value'] );
} else{
throw new Exception("String in invalid position: " . $line["value"]);
}
} else {
$context = $line["type"];
if( $line["type"] == "msgid" || $line["type"] == "msgstr" )
$entry[$line["type"]][] = $this->decodeStringFormat( $line["value"] );
else
$entry[$line["type"]][] = $line["value"];
}
}
foreach($entry as $k=>&$v){
if( in_array($k,array('msgid',"msgstr")) ){
$v = implode('',$v);
} else{
$v = implode("\n",$v);
}
}
$entry['is_obsolete'] = $is_obsolete;
return $entry;
}
public function convertEntryToString( $entry ){
$prefixes = array(
"comments"=>"# ",
"extracted_comments"=>"#. ",
"reference"=>"#: ",
"flags"=>"#, ",
"previous_untranslated_string"=>"#| "
);
$msg = "";
foreach ( $entry as $k=>$v ){
if($v && isset($prefixes[$k])) {
$msg .= $this->addPrefixToLines($prefixes[$k],$v) . "\n";
}
}
$msgid = 'msgid ' . $this->encodeStringFormat($entry['msgid']);
$msgstr = 'msgstr ' . $this->encodeStringFormat($entry['msgstr']);
if($entry['is_obsolete']) {
$msgid = $this->addPrefixToLines('#~ ',$msgid);
$msgstr = $this->addPrefixToLines('#~ ',$msgstr);
}
$msg .= $msgid . "\n";
$msg .= $msgstr . "\n";
$msg .= "\n";
return $msg;
}
}