blob: 910dffb83cfff5d447a227001175eed92e37d92e [file] [log] [blame]
droy81777472007-11-28 21:35:58 +00001<?php
2/*******************************************************************************
Kit Lo6d440cc2013-03-02 19:56:45 -05003 * Copyright (c) 2007-2013 Eclipse Foundation, IBM Corporation and others.
droy81777472007-11-28 21:35:58 +00004 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Eclipse Foundation - initial API and implementation
droyb9c2cb12008-10-08 13:58:14 +000011 * Antoine Toulmé - Bug 248917
kitloff50b952009-03-12 12:46:59 +000012 * Kit Lo (IBM) - patch, bug 266250, Map file processor not running properly on live server
kitlo9207be32009-04-05 04:58:23 +000013 * Kit Lo (IBM) - patch, bug 258749, Keep spaces at the end of value string
kitlo8c31bd82009-11-09 23:52:05 +000014 * Kit Lo (IBM) - patch, bug 226378, Non-translatable strings or files should not be presented to user for translation
kitlo2e780dc2010-01-12 17:58:32 +000015 * Kit Lo (IBM) - Bug 299402, Extract properties files from Eclipse project update sites for translation
Kit Lo6d440cc2013-03-02 19:56:45 -050016 * Kit Lo (IBM) - [402215] Extract Orion JavaScript files for translation
Kit Lo178cac02013-07-22 11:11:41 -040017 * Kit Lo (IBM) - [413459] Received "Cannot deactivate string" messages during process_project_source_locations.php
Kit Lo6d440cc2013-03-02 19:56:45 -050018 *******************************************************************************/
19
20require_once("/home/data/httpd/babel.eclipse.org/html/json_encode.php");
atoulme12882d52009-01-26 18:39:17 +000021require(dirname(__FILE__) . "/../system/language.class.php");
22require(dirname(__FILE__) . "/../system/release_train.class.php");
atoulme425b6c82008-11-24 14:00:27 +000023
atoulme12882d52009-01-26 18:39:17 +000024require(dirname(__FILE__) . "/../string/string.class.php");
droy81777472007-11-28 21:35:58 +000025
26class File {
27 public $errStrs;
28
29 public $file_id = 0;
30 public $project_id = '';
droy3bb0c962008-01-17 20:45:01 +000031 public $version = '';
droy81777472007-11-28 21:35:58 +000032 public $name = '';
33 public $is_active = 0;
droyb9c2cb12008-10-08 13:58:14 +000034 public $plugin_id = '';
droy81777472007-11-28 21:35:58 +000035
droy81777472007-11-28 21:35:58 +000036 function save() {
37 $rValue = false;
droyaead6582008-02-25 19:47:15 +000038 if($this->name != "" && $this->project_id != "" && $this->version != "") {
atoulme3e5e9342009-01-23 17:34:30 +000039 global $dbh;
droy81777472007-11-28 21:35:58 +000040
droy81777472007-11-28 21:35:58 +000041 if($this->file_id == 0) {
droy3bb0c962008-01-17 20:45:01 +000042 $this->file_id = $this->getFileID($this->name, $this->project_id, $this->version);
droy81777472007-11-28 21:35:58 +000043 }
44
45 $sql = "INSERT INTO";
46 $where = "";
47 if($this->file_id > 0) {
48 $sql = "UPDATE";
atoulme3e5e9342009-01-23 17:34:30 +000049 $where = " WHERE file_id = " . sqlSanitize($this->file_id, $dbh);
droy81777472007-11-28 21:35:58 +000050 }
51
droy3e7f3a82007-11-29 19:35:51 +000052 $Event = new EventLog("files", "file_id", $this->file_id, $sql);
53
droy81777472007-11-28 21:35:58 +000054 $sql .= " files
atoulme3e5e9342009-01-23 17:34:30 +000055 SET file_id = " . sqlSanitize($this->file_id, $dbh) . ",
56 project_id = " . returnQuotedString(sqlSanitize($this->project_id, $dbh)) . ",
57 version = " . returnQuotedString(sqlSanitize($this->version, $dbh)) . ",
58 name = " . returnQuotedString(sqlSanitize($this->name, $dbh)) . ",
59 plugin_id = " . returnQuotedString(sqlSanitize($this->plugin_id, $dbh)) . ",
atoulme2e3c33c2009-02-19 14:53:15 +000060 is_active = " . $this->is_active . $where;
droy81777472007-11-28 21:35:58 +000061 if(mysql_query($sql, $dbh)) {
62 if($this->file_id == 0) {
63 $this->file_id = mysql_insert_id($dbh);
droy3e7f3a82007-11-29 19:35:51 +000064 $Event->key_value = $this->file_id;
droy81777472007-11-28 21:35:58 +000065 }
66 $rValue = true;
droy3e7f3a82007-11-29 19:35:51 +000067 $Event->add();
droy81777472007-11-28 21:35:58 +000068 }
69 else {
kitlo2e780dc2010-01-12 17:58:32 +000070 echo $sql . "\n";
droy81777472007-11-28 21:35:58 +000071 $GLOBALS['g_ERRSTRS'][1] = mysql_error();
72 }
73 }
droyaead6582008-02-25 19:47:15 +000074 else {
75 echo "ERROR: One missing:Name: " . $this->name . "Project: " . $this->project_id . "Version: " . $this->version;
76 }
droy81777472007-11-28 21:35:58 +000077 return $rValue;
78 }
79
atoulmebe3eae82008-12-05 16:38:20 +000080 static function getFileID($_name, $_project_id, $_version) {
kitloff50b952009-03-12 12:46:59 +000081 $rValue = 0;
atoulmebe3eae82008-12-05 16:38:20 +000082 if($_name != "" && $_project_id != "" && $_version != "") {
atoulme3e5e9342009-01-23 17:34:30 +000083 global $dbh;
droy81777472007-11-28 21:35:58 +000084
85 $sql = "SELECT file_id
86 FROM
87 files
atoulme3e5e9342009-01-23 17:34:30 +000088 WHERE name = " . returnQuotedString(sqlSanitize($_name, $dbh)) . "
89 AND project_id = " . returnQuotedString(sqlSanitize($_project_id, $dbh)) . "
90 AND version = '" . sqlSanitize($_version, $dbh) . "'";
droy81777472007-11-28 21:35:58 +000091
92 $result = mysql_query($sql, $dbh);
93 if($result && mysql_num_rows($result) > 0) {
94 $myrow = mysql_fetch_assoc($result);
95 $rValue = $myrow['file_id'];
96 }
97 }
98 return $rValue;
99 }
100
101 function parseProperties($_content) {
102 $rValue = "";
103 if($_content != "") {
droy37f2bc52007-11-29 16:32:21 +0000104
atoulme3e5e9342009-01-23 17:34:30 +0000105 global $User;
kitlo2e780dc2010-01-12 17:58:32 +0000106
107 # find all current active strings for this properties file
108 global $dbh;
109 $strings = array();
110 $sql = "SELECT * from strings WHERE is_active = 1 AND file_id = $this->file_id";
111 $rs_strings = mysql_query($sql, $dbh);
112 while ($myrow_strings = mysql_fetch_assoc($rs_strings)) {
113 $string = new String();
114 $string->string_id = $myrow_strings['string_id'];
115 $string->file_id = $myrow_strings['file_id'];
116 $string->name = $myrow_strings['name'];
117 $string->value = $myrow_strings['value'];
118 $string->userid = $myrow_strings['userid'];
119 $string->created_on = $myrow_strings['created_on'];
120 $string->is_active = $myrow_strings['is_active'];
121 $strings[$string->string_id] = $string;
122 }
123
Kit Lo6d440cc2013-03-02 19:56:45 -0500124 # import existing strings, $String->save() will deal with merges
droy4f4c4072008-01-22 16:12:45 +0000125 $previous_line = "";
126 $lines = explode("\n", $_content);
kitlo8c31bd82009-11-09 23:52:05 +0000127 $non_translatable = FALSE;
droy81777472007-11-28 21:35:58 +0000128 foreach($lines as $line) {
kitlo8c31bd82009-11-09 23:52:05 +0000129 if(strlen($line) > 0 && $line[0] != "!" && $line[0] != ";") {
droye97d6fe2008-06-16 19:31:15 +0000130 # Bug 235553 - don't trim the space at the end of a line!
131 # $line = trim($line);
droy3bb0c962008-01-17 20:45:01 +0000132
kitlo8c31bd82009-11-09 23:52:05 +0000133 if($line[0] == "#") {
134 $tokens = preg_split("/[\s]+/", $line);
kitlo27e80be2010-01-13 13:03:55 +0000135 if (sizeof($tokens) > 2 && $tokens[2] == "NON-TRANSLATABLE") {
kitlo8c31bd82009-11-09 23:52:05 +0000136 if($tokens[1] == "START")
137 $non_translatable = TRUE;
138 elseif($tokens[1] == "END")
139 $non_translatable = FALSE;
droy81777472007-11-28 21:35:58 +0000140 }
kitlo8c31bd82009-11-09 23:52:05 +0000141 }
142 elseif($non_translatable == FALSE) {
143 # Does line end with a \ ?
144 if(preg_match("/\\\\$/", $line)) {
145 # Line ends with \
146
147 # strip the backslash
148 $previous_line .= $line . "\n";
149 }
150 else {
151 if($previous_line != "") {
152 $line = $previous_line . $line;
153 $previous_line = "";
droy4f4c4072008-01-22 16:12:45 +0000154 }
kitlo8c31bd82009-11-09 23:52:05 +0000155
156 $tags = explode("=", $line, 2);
157 if(count($tags) > 1) {
158 if($rValue != "") {
159 $rValue .= ",";
160 }
161 $tags[0] = trim($tags[0]);
162 # Bug 235553 - don't trim the space at the end of a line!
163 # Bug 258749 - use ltrim() to remove spaces at the beginning of value string
164 $tags[1] = ltrim($tags[1]);
165
166 $rValue .= $tags[0];
167
168 $String = new String();
169 $String->file_id = $this->file_id;
170 $String->name = $tags[0];
171 $String->value = $tags[1];
172 $String->userid = $User->userid;
173 $String->created_on = getCURDATE();
174 $String->is_active = 1;
175 $String->save();
kitlo2e780dc2010-01-12 17:58:32 +0000176
177 # remove the string from the list
178 unset($strings[$String->string_id]);
kitlo8c31bd82009-11-09 23:52:05 +0000179 }
droy4f4c4072008-01-22 16:12:45 +0000180 }
droy81777472007-11-28 21:35:58 +0000181 }
182 }
183 }
kitlo2e780dc2010-01-12 17:58:32 +0000184
185 # remove strings that are no longer in the properties file
186 foreach ($strings as $string) {
kitlo910d17f2010-02-12 17:17:44 +0000187 $string->is_active = 0;
188 if (!$string->save()) {
kitlo2e780dc2010-01-12 17:58:32 +0000189 echo "***ERROR: Cannot deactivate string $string->name in file $string->file_id\n";
190 }
droy37f2bc52007-11-29 16:32:21 +0000191 }
droy81777472007-11-28 21:35:58 +0000192 }
193 return $rValue;
194 }
kitlo2e780dc2010-01-12 17:58:32 +0000195
Kit Lo6d440cc2013-03-02 19:56:45 -0500196 function parseJs($_content) {
197 if($_content != "") {
198
199 global $User;
200
201 # find all current active strings for this properties file
202 global $dbh;
203 $strings = array();
204 $sql = "SELECT * from strings WHERE is_active = 1 AND file_id = $this->file_id";
205 $rs_strings = mysql_query($sql, $dbh);
206 while ($myrow_strings = mysql_fetch_assoc($rs_strings)) {
207 $string = new String();
208 $string->string_id = $myrow_strings['string_id'];
209 $string->file_id = $myrow_strings['file_id'];
210 $string->name = $myrow_strings['name'];
211 $string->value = $myrow_strings['value'];
212 $string->userid = $myrow_strings['userid'];
213 $string->created_on = $myrow_strings['created_on'];
214 $string->is_active = $myrow_strings['is_active'];
215 $strings[$string->string_id] = $string;
216 }
217
218 # import existing strings, $String->save() will deal with merges
219 $file_contents = preg_replace("/NON-NLS-(.*)/", "", $_content);
220 $file_contents = preg_replace("/\\/\\/\\$/", "", $file_contents);
221 $file_contents = preg_replace("/((.*?(\n))+.*?)define\(/", "define(", $file_contents);
222 $file_contents = preg_replace("/define\(((.*?(\n))+.*?)\)\;/", "$1", $file_contents);
223 $jsons = new Services_JSON();
224 $lines = $jsons->decode($file_contents);
225 foreach($lines as $key => $value) {
kitlo840aadc2014-06-15 03:18:54 -0400226 # escape newlines and tabs
227 $value = preg_replace("/\\n/", "\\\\n", $value);
228 $value = preg_replace("/\\t/", "\\\\t", $value);
229
Kit Lo6d440cc2013-03-02 19:56:45 -0500230 $String = new String();
231 $String->file_id = $this->file_id;
232 $String->name = $key;
233 $String->value = $value;
234 $String->userid = $User->userid;
235 $String->created_on = getCURDATE();
236 $String->is_active = 1;
237 $String->saveJs();
238
239 # remove the string from the list
240 unset($strings[$String->string_id]);
241 }
242
243 # remove strings that are no longer in the properties file
244 foreach ($strings as $string) {
245 $string->is_active = 0;
Kit Lo178cac02013-07-22 11:11:41 -0400246 if (!$string->saveJs()) {
Kit Lo6d440cc2013-03-02 19:56:45 -0500247 echo "***ERROR: Cannot deactivate string $string->name in file $string->file_id\n";
248 }
249 }
250 }
251 }
252
atoulme425b6c82008-11-24 14:00:27 +0000253 /**
254 * Returns the fragment relative path.
255 */
256 function findFragmentRelativePath() {
257 # strip useless CVS structure before the plugin name (bug 221675 c14):
258 $pattern = '/^([a-zA-Z0-9\/_-])+\/([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)(.*)\.properties$/i';
259 $replace = '${2}.${3}${4}.properties';
260 $path = preg_replace($pattern, $replace, $this->name);
261
262 # strip source folder (bug 221675) (org.eclipse.plugin/source_folder/org/eclipse/plugin)
263 $pattern = '/^([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)\.([a-zA-Z0-9\._-]+)(.*)\/(\1)([\.\/])(\2)([\.\/])(.*)\.properties$/i';
264 $replace = '${1}.${2}.${3}/${5}${6}${7}${8}${9}.properties';
265 $path = preg_replace($pattern, $replace, $path);
266
267 return $path;
268 }
269
270 /*
271 * Convert the filename to *_lang.properties, e.g., foo_fr.properties
272 */
atoulmecefb0242008-11-24 16:00:31 +0000273 function appendLangCode($language_iso, $filename = null) {
atoulmedfee65f2008-11-24 14:59:26 +0000274 if (!$filename) {
atoulme786dc552008-11-24 15:50:18 +0000275 $filename = $this->findFragmentRelativePath();
atoulmedfee65f2008-11-24 14:59:26 +0000276 }
atoulme425b6c82008-11-24 14:00:27 +0000277 if (preg_match( "/^(.*)\.properties$/", $filename, $matches)) {
278 $filename = $matches[1] . '_' . $language_iso . '.properties';
279 }
280 return $filename;
281 }
282
283 /**
284 * returns a hash that contains a mapping from the translation keys to the translation values.
285 */
286 function strings4PropertiesFile($language) {
287 $result = array();
288 if (strcmp($language->iso, "en_AA") == 0) {
289 $sql = "SELECT string_id, name, value FROM strings WHERE file_id = " . $this->file_id .
290 " AND is_active AND non_translatable = 0";
291 $strings_result = mysql_query($sql);
292 while (($strings_row = mysql_fetch_assoc($strings_result)) != null) {
293 $result[$strings_row['name']] = $this->project_id . $strings_row['string_id'] . ":" . $strings_row['value'];
atoulmedfee65f2008-11-24 14:59:26 +0000294 }
atoulme425b6c82008-11-24 14:00:27 +0000295 } else {
296 $sql = "SELECT
297 strings.name AS 'key',
298 strings.value AS orig,
299 translations.value AS trans
300 FROM strings, translations
301 WHERE strings.string_id = translations.string_id
302 AND strings.file_id = " . $this->file_id . "
303 AND strings.is_active
304 AND strings.non_translatable = 0
305 AND translations.language_id = " . $language->id . "
306 AND translations.is_active";
307 $strings_result = mysql_query($sql);
308 while (($strings_row = mysql_fetch_assoc($strings_result)) != null) {
309 $result[$strings_row['key']] = $strings_row['trans'];
310 }
311 }
312 return $result;
313 }
droy81777472007-11-28 21:35:58 +0000314}
droyb9c2cb12008-10-08 13:58:14 +0000315?>