-
Notifications
You must be signed in to change notification settings - Fork 1
/
languageCleanup.php
204 lines (185 loc) · 7.22 KB
/
languageCleanup.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
<?PHP
/*
----------------------------------
------ Created: 030121 ------
------ Austin Best ------
----------------------------------
*/
// This will NOT report uninitialized variables
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$frontendFiles = array('js');
$backendFiles = array('cs');
$startTime = microtime(true);
define('APP', $_GET['application']);
define('TYPE', $_GET['type']);
//-- PATHS
define('LIDARR_BACKEND_ROOT', 'C:\\Repositories\\Lidarr\\src');
define('LIDARR_FRONTEND_ROOT', 'C:\\Repositories\\Lidarr\\frontend\\src');
define('LIDARR_LANGUAGE_ROOT', 'C:\\Repositories\\Lidarr\\src\\NzbDrone.Core\\Localization\\Core\\');
define('PROWLARR_BACKEND_ROOT', 'C:\\Repositories\\Prowlarr\\src');
define('PROWLARR_FRONTEND_ROOT', 'C:\\Repositories\\Prowlarr\\frontend\\src');
define('PROWLARR_LANGUAGE_ROOT', 'C:\\Repositories\\Prowlarr\\src\\NzbDrone.Core\\Localization\\Core\\');
define('RADARR_BACKEND_ROOT', 'C:\\Repositories\\Radarr\\src');
define('RADARR_FRONTEND_ROOT', 'C:\\Repositories\\Radarr\\frontend\\src');
define('RADARR_LANGUAGE_ROOT', 'C:\\Repositories\\Radarr\\src\\NzbDrone.Core\\Localization\\Core\\');
define('READARR_BACKEND_ROOT', 'C:\\Repositories\\Readarr\\src');
define('READARR_FRONTEND_ROOT', 'C:\\Repositories\\Readarr\\frontend\\src');
define('READARR_LANGUAGE_ROOT', 'C:\\Repositories\\Readarr\\src\\NzbDrone.Core\\Localization\\Core\\');
define('SONARR_BACKEND_ROOT', 'C:\\Repositories\\Sonarr\\src');
define('SONARR_FRONTEND_ROOT', 'C:\\Repositories\\Sonarr\\frontend\\src');
define('SONARR_LANGUAGE_ROOT', 'C:\\Repositories\\Sonarr\\src\\NzbDrone.Core\\Localization\\Core\\');
switch (APP) {
case 'lidarr';
define('BACKEND_ROOT', LIDARR_BACKEND_ROOT);
define('FRONTEND_ROOT', LIDARR_FRONTEND_ROOT);
define('LANGUAGE_ROOT', LIDARR_LANGUAGE_ROOT);
break;
case 'prowlarr';
define('BACKEND_ROOT', PROWLARR_BACKEND_ROOT);
define('FRONTEND_ROOT', PROWLARR_FRONTEND_ROOT);
define('LANGUAGE_ROOT', PROWLARR_LANGUAGE_ROOT);
break;
case 'radarr';
define('BACKEND_ROOT', RADARR_BACKEND_ROOT);
define('FRONTEND_ROOT', RADARR_FRONTEND_ROOT);
define('LANGUAGE_ROOT', RADARR_LANGUAGE_ROOT);
break;
case 'readarr';
define('BACKEND_ROOT', READARR_BACKEND_ROOT);
define('FRONTEND_ROOT', READARR_FRONTEND_ROOT);
define('LANGUAGE_ROOT', READARR_LANGUAGE_ROOT);
break;
case 'sonarr';
define('BACKEND_ROOT', SONARR_BACKEND_ROOT);
define('FRONTEND_ROOT', SONARR_FRONTEND_ROOT);
define('LANGUAGE_ROOT', SONARR_LANGUAGE_ROOT);
break;
}
//-- FULL STOP IF NO APP WAS PICKED
if (!file_exists(LANGUAGE_ROOT . DIRECTORY_SEPARATOR . 'en.json') || !APP || !TYPE) {
exit();
}
$lang = json_decode(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', file_get_contents(LANGUAGE_ROOT . DIRECTORY_SEPARATOR . 'en.json')), true);
$langKeys = array_keys($lang);
$originalCount = count($langKeys);
$codebase = array(
'front' => array(
'files' => recursive_read(FRONTEND_ROOT),
'regex_u' => "/translate\(\'%s\'/i",
'regex_m' => "/translate\('(.*?)'/mi",
'ext' => $frontendFiles,
'fileCount' => 0,
),
'back' => array(
'files' => recursive_read(BACKEND_ROOT, false, true),
'regex_u' => '/GetLocalizedString\(\"%s\"\)/mi',
'regex_m' => '/GetLocalizedString\("(.*?)"/mi',
'ext' => $backendFiles,
'fileCount' => 0,
),
);
switch (TYPE) {
case 'unused':
foreach ($codebase as $where => $details) {
foreach ($details['files'] as $filePath) {
//-- ALL KEYS FOUND
if (!$langKeys) {
break;
}
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
//-- SKIP EXTENSIONS
if (in_array($extension, $details['ext'])) {
$codebase[$where]['fileCount']++;
$file = file_get_contents($filePath);
foreach ($langKeys as $index => $key) {
//-- KEY ALREADY FOUND
if (!$langKeys[$index]) {
continue;
}
preg_match(sprintf($details['regex_u'], strtolower($key)), $file, $matches);
if ($matches) {
//-- KEY FOUND, REMOVE IT
unset($langKeys[$index]);
}
}
}
}
}
//-- APPLY REMOVALS TO ENGLISH
$removeKeys = array_values($langKeys);
$english = json_decode(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', file_get_contents(LANGUAGE_ROOT . DIRECTORY_SEPARATOR . 'en.json')), true);
foreach ($removeKeys as $key) {
if ($english[$key]) {
$removed++;
unset($english[$key]);
}
}
//-- WRITE THE CHANGES TO THE FILE
$json = json_encode($english, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
//-- DROP IT FROM 4 SPACES TO 2 FOR *arr
$json = preg_replace('/^( +?)\\1(?=[^ ])/m', '$1', $json);
$handle = fopen(LANGUAGE_ROOT . DIRECTORY_SEPARATOR . 'en.json', 'w');
fwrite($handle, $json, strlen($json));
fclose($handle);
$results = array(
'frontendScanned' => $codebase['front']['fileCount'],
'backendScanned' => $codebase['back']['fileCount'],
'keyTotal' => $originalCount,
'unusedTotal' => count($langKeys),
'runtime' => number_format(microtime(true) - $startTime, 2),
'unused' => $langKeys,
'removed' => $removed
);
break;
case 'missing':
foreach ($codebase as $where => $details) {
foreach ($details['files'] as $filePath) {
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
//-- SKIP EXTENSIONS
if (in_array($extension, $details['ext'])) {
$codebase[$where]['fileCount']++;
$file = file_get_contents($filePath);
preg_match_all($details['regex_m'], $file, $matches);
if (!empty($matches[1])) {
foreach ($matches[1] as $checkExistingKey) {
if (!$lang[$checkExistingKey]) {
$codebaseKeys[$checkExistingKey][] = $filePath;
}
}
}
}
}
}
$results = array(
'frontendScanned' => $codebase['front']['fileCount'],
'backendScanned' => $codebase['back']['fileCount'],
'missingTotal' => count($codebaseKeys),
'runtime' => number_format(microtime(true) - $startTime, 2),
'missing' => $codebaseKeys
);
break;
default:
exit();
break;
}
header('Content-Type: application/json');
echo json_encode($results, JSON_PRETTY_PRINT);
function recursive_read($directory, $entries_array = array(), $backend = false)
{
if (is_dir($directory)) {
$handle = opendir($directory);
while (false !== ($entry = readdir($handle))) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry = $directory . DIRECTORY_SEPARATOR . $entry;
if (is_dir($entry) && (!$backend || ($backend && strpos($entry, 'NzbDrone.') !== false))) {
$entries_array = recursive_read($entry, $entries_array, $backend);
} else {
$entries_array[] = $entry;
}
}
closedir($handle);
}
return $entries_array;
}