|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\MoTranslator; |
|
6
|
|
|
|
|
7
|
|
|
use PhpMyAdmin\MoTranslator\Cache\CacheInterface; |
|
8
|
|
|
|
|
9
|
|
|
use function is_readable; |
|
10
|
|
|
use function strcmp; |
|
11
|
|
|
|
|
12
|
|
|
final class MoParser |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* None error. |
|
16
|
|
|
*/ |
|
17
|
|
|
public const ERROR_NONE = 0; |
|
18
|
|
|
/** |
|
19
|
|
|
* File does not exist. |
|
20
|
|
|
*/ |
|
21
|
|
|
public const ERROR_DOES_NOT_EXIST = 1; |
|
22
|
|
|
/** |
|
23
|
|
|
* File has bad magic number. |
|
24
|
|
|
*/ |
|
25
|
|
|
public const ERROR_BAD_MAGIC = 2; |
|
26
|
|
|
/** |
|
27
|
|
|
* Error while reading file, probably too short. |
|
28
|
|
|
*/ |
|
29
|
|
|
public const ERROR_READING = 3; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Big endian mo file magic bytes. |
|
33
|
|
|
*/ |
|
34
|
|
|
public const MAGIC_BE = "\x95\x04\x12\xde"; |
|
35
|
|
|
/** |
|
36
|
|
|
* Little endian mo file magic bytes. |
|
37
|
|
|
*/ |
|
38
|
|
|
public const MAGIC_LE = "\xde\x12\x04\x95"; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Parse error code (0 if no error). |
|
42
|
|
|
* |
|
43
|
|
|
* @var int |
|
44
|
|
|
*/ |
|
45
|
|
|
public $error = self::ERROR_NONE; |
|
46
|
|
|
|
|
47
|
|
|
/** @var string|null */ |
|
48
|
|
|
private $filename; |
|
49
|
|
|
|
|
50
|
|
|
public function __construct(?string $filename) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->filename = $filename; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Parses .mo file and stores results to `$cache` |
|
57
|
|
|
*/ |
|
58
|
|
|
public function parseIntoCache(CacheInterface $cache): void |
|
59
|
|
|
{ |
|
60
|
|
|
if ($this->filename === null) { |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (! is_readable($this->filename)) { |
|
65
|
|
|
$this->error = self::ERROR_DOES_NOT_EXIST; |
|
66
|
|
|
|
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$stream = new StringReader($this->filename); |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
|
|
$magic = $stream->read(0, 4); |
|
74
|
|
|
if (strcmp($magic, self::MAGIC_LE) === 0) { |
|
75
|
|
|
$unpack = 'V'; |
|
76
|
|
|
} elseif (strcmp($magic, self::MAGIC_BE) === 0) { |
|
77
|
|
|
$unpack = 'N'; |
|
78
|
|
|
} else { |
|
79
|
|
|
$this->error = self::ERROR_BAD_MAGIC; |
|
80
|
|
|
|
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/* Parse header */ |
|
85
|
|
|
$total = $stream->readint($unpack, 8); |
|
86
|
|
|
$originals = $stream->readint($unpack, 12); |
|
87
|
|
|
$translations = $stream->readint($unpack, 16); |
|
88
|
|
|
|
|
89
|
|
|
/* get original and translations tables */ |
|
90
|
|
|
$totalTimesTwo = (int) ($total * 2);// Fix for issue #36 on ARM |
|
91
|
|
|
$tableOriginals = $stream->readintarray($unpack, $originals, $totalTimesTwo); |
|
92
|
|
|
$tableTranslations = $stream->readintarray($unpack, $translations, $totalTimesTwo); |
|
93
|
|
|
|
|
94
|
|
|
/* read all strings to the cache */ |
|
95
|
|
|
for ($i = 0; $i < $total; ++$i) { |
|
96
|
|
|
$iTimesTwo = $i * 2; |
|
97
|
|
|
$iPlusOne = $iTimesTwo + 1; |
|
98
|
|
|
$iPlusTwo = $iTimesTwo + 2; |
|
99
|
|
|
$original = $stream->read($tableOriginals[$iPlusTwo], $tableOriginals[$iPlusOne]); |
|
100
|
|
|
$translation = $stream->read($tableTranslations[$iPlusTwo], $tableTranslations[$iPlusOne]); |
|
101
|
|
|
$cache->set($original, $translation); |
|
102
|
|
|
} |
|
103
|
|
|
} catch (ReaderException $e) { |
|
104
|
|
|
$this->error = self::ERROR_READING; |
|
105
|
|
|
|
|
106
|
|
|
return; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|