-
Notifications
You must be signed in to change notification settings - Fork 561
/
update-plugins.php
113 lines (89 loc) · 3.42 KB
/
update-plugins.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
<?php
/*
+---------------------------------------------------------------------------+
| Revive Adserver |
| http://www.revive-adserver.com |
| |
| Copyright: See the COPYRIGHT.txt file. |
| License: GPLv2 or later, see the LICENSE.txt file. |
+---------------------------------------------------------------------------+
*/
require __DIR__ . '/../../lib/vendor/autoload.php';
chdir(__DIR__ . '/../..');
$finder = new \Symfony\Component\Finder\Finder();
$preg = '#(?:->|::)translate\((["\'])(.*?)\1\)#x';
$pregName = '#->nameEnglish *= *(["\'])(.*?)\1#x';
$files = $finder
->in('plugins_repo')
->exclude('openXDeveloperToolbox')
->name('*.php')
->contains($preg)
->getIterator();
$trans = [];
foreach ($files as $file) {
$contents = $file->getContents();
$path = explode(DIRECTORY_SEPARATOR, $file->getPathname());
if ('plugins' === $path[2]) {
$poFilePath = "plugins_repo/{$path[1]}/plugins/etc/{$path[4]}/_lang/po/en.po";
} elseif ('www' === $path[2] && 'admin' === $path[3] && 'plugins' === $path[4]) {
$poFilePath = "plugins_repo/{$path[1]}/plugins/etc/{$path[5]}/_lang/po/en.po";
} else {
die("Unsupported path: " . $file->getPathname() . "\n");
}
if (!is_writable($poFilePath)) {
die("Unwritable file: {$poFilePath}\n");
}
if (!isset($trans[$poFilePath])) {
$trans[$poFilePath] = [];
}
foreach (explode("\n", $contents) as $n => $ref) {
if (preg_match($preg, $ref, $m) || preg_match($pregName, $ref, $m)) {
if (!isset($trans[$poFilePath][$m[2]])) {
$trans[$poFilePath][$m[2]] = [];
}
$trans[$poFilePath][$m[2]][] = implode(':', [
str_replace(DIRECTORY_SEPARATOR, '/', $file->getPathname()),
$n + 1,
]);
}
}
}
foreach ($trans as $file => $msgs) {
$po = <<<EOF
msgid ""
msgstr ""
"Project-Id-Version: Revive Adserver: {$file}\\n"
"Last-Translator: Revive Adserver Team <noreply@revive-adserver.com>\\n"
"Language-Team: English\\n"
"Language: en_GB\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: (n != 1)\\n"
EOF;
// Sort references by file path and line number
$msgs = array_map(function ($references) {
usort($references, function ($a, $b) {
[$a_file, $a_line] = explode(':', $a);
[$b_file, $b_line] = explode(':', $b);
return $a_file !== $b_file ? $a_file <=> $b_file : $a_line <=> $b_line;
});
return $references;
}, $msgs);
// Sort messages by first reference
uasort($msgs, function ($a, $b) {
[$a_file, $a_line] = explode(':', $a[0]);
[$b_file, $b_line] = explode(':', $b[0]);
return $a_file !== $b_file ? $a_file <=> $b_file : $a_line <=> $b_line;
});
foreach ($msgs as $msgid => $references) {
$po .= "\n";
foreach ($references as $ref) {
$po .= "#: {$ref}\n";
}
$id = str_replace('\\n', "\\n\"\n\"", addcslashes($msgid, "\0..\37\"\\"));
$po .= "msgid \"{$id}\"\nmsgstr \"{$id}\"\n";
}
$po = str_replace("\r", "", $po);
file_put_contents($file, $po);
}