-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathupdate_language.php
executable file
·297 lines (274 loc) · 8.73 KB
/
update_language.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
#!/usr/bin/env php
<?php
/**
* Copyright 2013 by Schmooze Com, Inc.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* @author andrew ! nagy => the159 ! com
*
* options:
* run with --help for options
*
*/
require_once('libraries/freepbx.php');
require("libraries/translation.class.php");
$freepbx_conf = freepbx::getFreePBXConfig();
//TODO Maybe move this inside of the function
if (is_array($freepbx_conf) && !empty($freepbx_conf)) {
foreach($freepbx_conf as $key => $value) {
if (isset($value) && $value != '') {
$vars[$key] = $value;
}
}
}
$mode = !empty($vars['mode']) ? $vars['mode'] : 'ssh';
$vars['repo_directory'] = !empty($vars['repo_directory']) ? $vars['repo_directory'] : dirname(dirname(__FILE__));
$help = array(
array('-m', 'The module to update'),
array('-u', 'Update i18n folder of module'),
array('-p', 'Package language of module (Update all mo files)'),
array('-s', 'Only update lanaguge is master is on supported release, eg -s 14.0'),
array('-a', 'All modules in the repo folder')
);
$longopts = array(
"help",
"module:",
"package",
"supportedlock:",
"all",
"update"
);
$options = getopt("m:s:pau",$longopts);
if(empty($options) || isset($options['help'])) {
freepbx::showHelp('update_language.php',$help);
exit(0);
}
$module = !empty($options['module']) ? $options['module'] : (!empty($options['m']) ? $options['m'] : "");
$package = isset($options['package']) ? true : (isset($options['p']) ? true : false);
$update = isset($options['update']) ? true : (isset($options['u']) ? true : false);
$supportedLock = !empty($options['supportedlock']) ? $options['supportedlock'] : (!empty($options['s']) ? $options['s'] : null);
$allModules = isset($options['all']) ? true : (isset($options['a']) ? true : false);
if(!$allModules) {
if(empty($module)) {
die('Undefined Module!');
}
if(!file_exists($vars['repo_directory'].'/'.$module)) {
die('Cant find '.$vars['repo_directory'].'/'.$module);
}
$repodir = $vars['repo_directory'].'/'.$module;
}
switch(true) {
case $package:
if(!$allModules) {
generateMO($repodir);
} else {
foreach(glob($vars['repo_directory']."/*", GLOB_ONLYDIR) as $dir) {
generateMO($dir);
}
}
break;
case $update:
if(!$allModules) {
updateLanguage($repodir, $supportedLock);
} else {
foreach(glob($vars['repo_directory']."/*", GLOB_ONLYDIR) as $dir) {
updateLanguage($dir, $supportedLock);
}
}
break;
default:
freepbx::showHelp('update_language.php',$help);
exit(0);
break;
}
function generateMO($repodir) {
FreePBX::refreshRepo($repodir);
try {
$repo = Git::open($repodir);
} catch(\Exception $e) {
freepbx::out($e->getMessage());
return;
}
freepbx::out("\tProcessing localizations...");
freepbx::outn("\t\tUpdating localization...");
$translation = new Translation($repodir);
if(!preg_match('/(core|framework)$/i',$repodir)) {
//if no i18n folder then make an english one!
if(!file_exists($repodir.'/i18n')) {
$translation->makeLanguage("en_US");
}
//pray that this works..
$translation->update_i18n();
freepbx::out("Done");
foreach(glob($repodir.'/i18n/*',GLOB_ONLYDIR) as $langDir) {
$lang = basename($langDir);
freepbx::outn("\t\tUpdating individual localization for ".$lang);
$o = $translation->merge_i18n($lang);
freepbx::out($o);
}
} elseif(preg_match('/framework$/i',$repodir)) {
$translation->update_i18n_amp();
foreach(glob($repodir.'/amp_conf/htdocs/admin/i18n/*',GLOB_ONLYDIR) as $langDir) {
$lang = basename($langDir);
freepbx::outn("\t\tUpdating individual localization for ".$lang);
$o = $translation->merge_i18n_amp($lang);
freepbx::out($o);
}
freepbx::out("Done");
} else {
freepbx::out("Core is done through framework");
}
}
function updateLanguage($repodir,$supported=null) {
FreePBX::refreshRepo($repodir);
try {
$repo = Git::open($repodir);
} catch(\Exception $e) {
freepbx::out($e->getMessage());
return;
}
$moduleMasterXmlString = $repo->show('origin/master','module.xml');
$masterXML = simplexml_load_string($moduleMasterXmlString);
$activeb = $repo->active_branch();
$sver = (string)$masterXML->supported->version;
if(!empty($supported) && $sver !== $supported) {
freepbx::out("Locked supported branch not equal to $supported [$sver !== $supported]");
return;
}
$rbranches = $repo->list_remote_branches();
foreach($rbranches as $branch) {
if(!preg_match("/^origin\/release\/(.*)/",$branch,$bmatch)) {
continue;
}
try{
$xml = $repo->show('refs/remotes/'.$branch,'module.xml');
} catch (Exception $e) {
//no module xml here...nothing to see move along and try next branch
freepbx::out("No Module.xml, skipping");
continue;
}
//load our xml string into our common parser
//we only get back three of the tags, rawname, version and supported
$bxml = freepbx::check_xml_string($xml);
if($bxml[2]['version'] == $sver) {
$mver = $bmatch[1];
break;
}
}
if(empty($mver)) {
freepbx::out("Could not find supported branch to work with!");
die();
}
$repo->checkout("release/".$mver);
freepbx::outn("\t\tMerging master into this branch...");
$stashable = $repo->add_stash();
$repo->fetch();
try {
$merged = $repo->pull('origin','master');
} catch(\Exception $e) {
$merged = false;
}
if(!$merged) {
freepbx::out("\t\tMerge from master to this branch failed");
freepbx::out("Module " . $module . " will not be tagged!");
return;
}
freepbx::out("Done");
if($stashable) {
$repo->apply_stash();
$repo->drop_stash();
}
freepbx::out("\tProcessing localizations...");
freepbx::outn("\t\tUpdating master localization...");
$translation = new Translation($repodir);
if(!preg_match('/(core|framework)$/i',$repodir)) {
//if no i18n folder then make an english one!
if(!file_exists($repodir.'/i18n')) {
$translation->makeLanguage("en_US");
}
//pray that this works..
$translation->update_i18n();
freepbx::out("Done");
foreach(glob($repodir.'/i18n/*',GLOB_ONLYDIR) as $langDir) {
$lang = basename($langDir);
freepbx::outn("\t\tUpdating individual localization for ".$lang);
$o = $translation->merge_i18n($lang);
freepbx::out($o);
}
} elseif(preg_match('/framework$/i',$repodir)) {
$translation->update_i18n_amp();
foreach(glob($repodir.'/amp_conf/htdocs/admin/i18n/*',GLOB_ONLYDIR) as $langDir) {
$lang = basename($langDir);
freepbx::outn("\t\tUpdating individual localization for ".$lang);
$o = $translation->merge_i18n_amp($lang);
freepbx::out($o);
}
freepbx::out("Done");
} else {
freepbx::out("Core is done through framework");
}
freepbx::outn("\tChecking for Modified or New files...");
$status = $repo->status();
$commitable = false;
if(empty($status)) {
freepbx::out("No Modified or New Files");
} else {
freepbx::out("Found ".count($status['modified'])." Modified files and ".count($status['untracked'])." New files");
$commitable = true;
}
if($commitable) {
freepbx::outn("\t\tCheckin Outstanding Changes...");
//-A will do more than ., it will add any unstaged files...
try {
$repo->add('-A');
} catch (Exception $e) {
freepbx::out($e->getMessage());
freepbx::out("Module " . $module . " will not be tagged!");
return;
}
freepbx::out("Done");
freepbx::outn("\t\tAdding Commit Message...");
//Commit with old commit message from before, but call it tag instead of commit.
try {
$repo->commit('[Automatic Language Updates]');
} catch (Exception $e) {
freepbx::out($e->getMessage());
freepbx::out("Problem Committing");
return;
}
freepbx::out("Done");
}
freepbx::outn("\t\tPushing to origin...");
//push branch and tag to remote
//TODO: check to make sure the author/committer isn't 'root'
try {
$repo->push("origin", "release/".$mver);
} catch (Exception $e) {
freepbx::out($e->getMessage());
freepbx::out("Module " . $module . " will not be pushed!");
return;
}
freepbx::out("Done");
freepbx::outn("\tMaster is the same supported release as this branch. Merging this release into master...");
$repo->checkout("master");
$merged = $repo->pull("origin","release/".$mver);
if(!$merged) {
freepbx::out("\t\tMerge from release/".$mver." into master failed");
freepbx::out("Module " . $module . " will not be tagged!");
return;
}
$repo->push("origin", "master");
$repo->checkout("release/".$mver);
freepbx::out("Done");
freepbx::outn("\tChecking you back into ".$activeb."...");
$repo->checkout($activeb);
freepbx::out("Done");
}