blob: b882f401f22ee1c316428ce86893c6133065b1bd [file] [log] [blame]
droy81777472007-11-28 21:35:58 +00001<?php
2/*******************************************************************************
droydc4457d2019-09-25 15:26:15 -04003 * Copyright (c) 2007-2019 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
droydc4457d2019-09-25 15:26:15 -040018 * Denis Roy (Eclipse Foundation) - Bug 550544 - Babel server is not ready for PHP 7
Kit Lo6d440cc2013-03-02 19:56:45 -050019 *******************************************************************************/
20
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;
kitlo2c8d8a92018-04-19 13:25:09 -040061 if(mysqli_query($dbh, $sql)) {
droy81777472007-11-28 21:35:58 +000062 if($this->file_id == 0) {
kitlo1d027092018-04-19 17:44:07 -040063 $this->file_id = mysqli_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";
kitloa59efea2018-05-11 12:40:40 -040071 $GLOBALS['g_ERRSTRS'][1] = mysqli_error($dbh);
droy81777472007-11-28 21:35:58 +000072 }
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
kitlo2c8d8a92018-04-19 13:25:09 -040092 $result = mysqli_query($dbh, $sql);
kitlo1d027092018-04-19 17:44:07 -040093 if($result && mysqli_num_rows($result) > 0) {
94 $myrow = mysqli_fetch_assoc($result);
droy81777472007-11-28 21:35:58 +000095 $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";
kitlo2c8d8a92018-04-19 13:25:09 -0400111 $rs_strings = mysqli_query($dbh, $sql);
kitlo1d027092018-04-19 17:44:07 -0400112 while ($myrow_strings = mysqli_fetch_assoc($rs_strings)) {
droydc4457d2019-09-25 15:26:15 -0400113 $string = new BabelString();
kitlo2e780dc2010-01-12 17:58:32 +0000114 $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
droydc4457d2019-09-25 15:26:15 -0400168 $String = new BabelString();
kitlo8c31bd82009-11-09 23:52:05 +0000169 $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";
kitlo2c8d8a92018-04-19 13:25:09 -0400205 $rs_strings = mysqli_query($dbh, $sql);
kitlo1d027092018-04-19 17:44:07 -0400206 while ($myrow_strings = mysqli_fetch_assoc($rs_strings)) {
droydc4457d2019-09-25 15:26:15 -0400207 $string = new BabelString();
Kit Lo6d440cc2013-03-02 19:56:45 -0500208 $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);
droydc4457d2019-09-25 15:26:15 -0400223 $lines = $json_decode($file_contents, true, 10);
Kit Lo6d440cc2013-03-02 19:56:45 -0500224 foreach($lines as $key => $value) {
kitlo840aadc2014-06-15 03:18:54 -0400225 # escape newlines and tabs
226 $value = preg_replace("/\\n/", "\\\\n", $value);
227 $value = preg_replace("/\\t/", "\\\\t", $value);
228
droydc4457d2019-09-25 15:26:15 -0400229 $String = new BabelString();
Kit Lo6d440cc2013-03-02 19:56:45 -0500230 $String->file_id = $this->file_id;
231 $String->name = $key;
232 $String->value = $value;
233 $String->userid = $User->userid;
234 $String->created_on = getCURDATE();
235 $String->is_active = 1;
236 $String->saveJs();
237
238 # remove the string from the list
239 unset($strings[$String->string_id]);
240 }
241
242 # remove strings that are no longer in the properties file
243 foreach ($strings as $string) {
244 $string->is_active = 0;
Kit Lo178cac02013-07-22 11:11:41 -0400245 if (!$string->saveJs()) {
Kit Lo6d440cc2013-03-02 19:56:45 -0500246 echo "***ERROR: Cannot deactivate string $string->name in file $string->file_id\n";
247 }
248 }
249 }
250 }
251
atoulme425b6c82008-11-24 14:00:27 +0000252 /**
253 * Returns the fragment relative path.
254 */
255 function findFragmentRelativePath() {
256 # strip useless CVS structure before the plugin name (bug 221675 c14):
257 $pattern = '/^([a-zA-Z0-9\/_-])+\/([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)(.*)\.properties$/i';
258 $replace = '${2}.${3}${4}.properties';
259 $path = preg_replace($pattern, $replace, $this->name);
260
261 # strip source folder (bug 221675) (org.eclipse.plugin/source_folder/org/eclipse/plugin)
262 $pattern = '/^([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)\.([a-zA-Z0-9\._-]+)(.*)\/(\1)([\.\/])(\2)([\.\/])(.*)\.properties$/i';
263 $replace = '${1}.${2}.${3}/${5}${6}${7}${8}${9}.properties';
264 $path = preg_replace($pattern, $replace, $path);
265
266 return $path;
267 }
268
269 /*
270 * Convert the filename to *_lang.properties, e.g., foo_fr.properties
271 */
atoulmecefb0242008-11-24 16:00:31 +0000272 function appendLangCode($language_iso, $filename = null) {
atoulmedfee65f2008-11-24 14:59:26 +0000273 if (!$filename) {
atoulme786dc552008-11-24 15:50:18 +0000274 $filename = $this->findFragmentRelativePath();
atoulmedfee65f2008-11-24 14:59:26 +0000275 }
atoulme425b6c82008-11-24 14:00:27 +0000276 if (preg_match( "/^(.*)\.properties$/", $filename, $matches)) {
277 $filename = $matches[1] . '_' . $language_iso . '.properties';
278 }
279 return $filename;
280 }
281
282 /**
283 * returns a hash that contains a mapping from the translation keys to the translation values.
284 */
285 function strings4PropertiesFile($language) {
286 $result = array();
287 if (strcmp($language->iso, "en_AA") == 0) {
288 $sql = "SELECT string_id, name, value FROM strings WHERE file_id = " . $this->file_id .
289 " AND is_active AND non_translatable = 0";
kitlo1d027092018-04-19 17:44:07 -0400290 $strings_result = mysqli_query($dbh, $sql);
291 while (($strings_row = mysqli_fetch_assoc($strings_result)) != null) {
atoulme425b6c82008-11-24 14:00:27 +0000292 $result[$strings_row['name']] = $this->project_id . $strings_row['string_id'] . ":" . $strings_row['value'];
atoulmedfee65f2008-11-24 14:59:26 +0000293 }
atoulme425b6c82008-11-24 14:00:27 +0000294 } else {
295 $sql = "SELECT
296 strings.name AS 'key',
297 strings.value AS orig,
298 translations.value AS trans
299 FROM strings, translations
300 WHERE strings.string_id = translations.string_id
301 AND strings.file_id = " . $this->file_id . "
302 AND strings.is_active
303 AND strings.non_translatable = 0
304 AND translations.language_id = " . $language->id . "
305 AND translations.is_active";
kitlo1d027092018-04-19 17:44:07 -0400306 $strings_result = mysqli_query($dbh, $sql);
307 while (($strings_row = mysqli_fetch_assoc($strings_result)) != null) {
atoulme425b6c82008-11-24 14:00:27 +0000308 $result[$strings_row['key']] = $strings_row['trans'];
309 }
310 }
311 return $result;
312 }
droy81777472007-11-28 21:35:58 +0000313}
droyb9c2cb12008-10-08 13:58:14 +0000314?>