blob: 708d95067545a39ac98aca42f36aafbcad93abc2 [file] [log] [blame]
droy81777472007-11-28 21:35:58 +00001<?php
2/*******************************************************************************
droyb9c2cb12008-10-08 13:58:14 +00003 * Copyright (c) 2007,2008 Eclipse Foundation 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
droy81777472007-11-28 21:35:58 +000016*******************************************************************************/
atoulme12882d52009-01-26 18:39:17 +000017require(dirname(__FILE__) . "/../system/language.class.php");
18require(dirname(__FILE__) . "/../system/release_train.class.php");
atoulme425b6c82008-11-24 14:00:27 +000019
atoulme12882d52009-01-26 18:39:17 +000020require(dirname(__FILE__) . "/../string/string.class.php");
droy81777472007-11-28 21:35:58 +000021
22class File {
23 public $errStrs;
24
25 public $file_id = 0;
26 public $project_id = '';
droy3bb0c962008-01-17 20:45:01 +000027 public $version = '';
droy81777472007-11-28 21:35:58 +000028 public $name = '';
29 public $is_active = 0;
droyb9c2cb12008-10-08 13:58:14 +000030 public $plugin_id = '';
droy81777472007-11-28 21:35:58 +000031
32
33 function save() {
34 $rValue = false;
droyaead6582008-02-25 19:47:15 +000035 if($this->name != "" && $this->project_id != "" && $this->version != "") {
atoulme3e5e9342009-01-23 17:34:30 +000036 global $dbh;
droy81777472007-11-28 21:35:58 +000037
droy81777472007-11-28 21:35:58 +000038 if($this->file_id == 0) {
droy3bb0c962008-01-17 20:45:01 +000039 $this->file_id = $this->getFileID($this->name, $this->project_id, $this->version);
droy81777472007-11-28 21:35:58 +000040 }
41
42 $sql = "INSERT INTO";
43 $where = "";
44 if($this->file_id > 0) {
45 $sql = "UPDATE";
atoulme3e5e9342009-01-23 17:34:30 +000046 $where = " WHERE file_id = " . sqlSanitize($this->file_id, $dbh);
droy81777472007-11-28 21:35:58 +000047 }
48
droy3e7f3a82007-11-29 19:35:51 +000049 $Event = new EventLog("files", "file_id", $this->file_id, $sql);
50
droy81777472007-11-28 21:35:58 +000051 $sql .= " files
atoulme3e5e9342009-01-23 17:34:30 +000052 SET file_id = " . sqlSanitize($this->file_id, $dbh) . ",
53 project_id = " . returnQuotedString(sqlSanitize($this->project_id, $dbh)) . ",
54 version = " . returnQuotedString(sqlSanitize($this->version, $dbh)) . ",
55 name = " . returnQuotedString(sqlSanitize($this->name, $dbh)) . ",
56 plugin_id = " . returnQuotedString(sqlSanitize($this->plugin_id, $dbh)) . ",
atoulme2e3c33c2009-02-19 14:53:15 +000057 is_active = " . $this->is_active . $where;
droy81777472007-11-28 21:35:58 +000058 if(mysql_query($sql, $dbh)) {
59 if($this->file_id == 0) {
60 $this->file_id = mysql_insert_id($dbh);
droy3e7f3a82007-11-29 19:35:51 +000061 $Event->key_value = $this->file_id;
droy81777472007-11-28 21:35:58 +000062 }
63 $rValue = true;
droy3e7f3a82007-11-29 19:35:51 +000064 $Event->add();
droy81777472007-11-28 21:35:58 +000065 }
66 else {
kitlo2e780dc2010-01-12 17:58:32 +000067 echo $sql . "\n";
droy81777472007-11-28 21:35:58 +000068 $GLOBALS['g_ERRSTRS'][1] = mysql_error();
69 }
70 }
droyaead6582008-02-25 19:47:15 +000071 else {
72 echo "ERROR: One missing:Name: " . $this->name . "Project: " . $this->project_id . "Version: " . $this->version;
73 }
droy81777472007-11-28 21:35:58 +000074 return $rValue;
75 }
76
atoulmebe3eae82008-12-05 16:38:20 +000077 static function getFileID($_name, $_project_id, $_version) {
kitloff50b952009-03-12 12:46:59 +000078 $rValue = 0;
atoulmebe3eae82008-12-05 16:38:20 +000079 if($_name != "" && $_project_id != "" && $_version != "") {
atoulme3e5e9342009-01-23 17:34:30 +000080 global $dbh;
droy81777472007-11-28 21:35:58 +000081
82 $sql = "SELECT file_id
83 FROM
84 files
atoulme3e5e9342009-01-23 17:34:30 +000085 WHERE name = " . returnQuotedString(sqlSanitize($_name, $dbh)) . "
86 AND project_id = " . returnQuotedString(sqlSanitize($_project_id, $dbh)) . "
87 AND version = '" . sqlSanitize($_version, $dbh) . "'";
droy81777472007-11-28 21:35:58 +000088
89 $result = mysql_query($sql, $dbh);
90 if($result && mysql_num_rows($result) > 0) {
91 $myrow = mysql_fetch_assoc($result);
92 $rValue = $myrow['file_id'];
93 }
94 }
95 return $rValue;
96 }
97
98 function parseProperties($_content) {
99 $rValue = "";
100 if($_content != "") {
droy37f2bc52007-11-29 16:32:21 +0000101
atoulme3e5e9342009-01-23 17:34:30 +0000102 global $User;
kitlo2e780dc2010-01-12 17:58:32 +0000103
104 # find all current active strings for this properties file
105 global $dbh;
106 $strings = array();
107 $sql = "SELECT * from strings WHERE is_active = 1 AND file_id = $this->file_id";
108 $rs_strings = mysql_query($sql, $dbh);
109 while ($myrow_strings = mysql_fetch_assoc($rs_strings)) {
110 $string = new String();
111 $string->string_id = $myrow_strings['string_id'];
112 $string->file_id = $myrow_strings['file_id'];
113 $string->name = $myrow_strings['name'];
114 $string->value = $myrow_strings['value'];
115 $string->userid = $myrow_strings['userid'];
116 $string->created_on = $myrow_strings['created_on'];
117 $string->is_active = $myrow_strings['is_active'];
118 $strings[$string->string_id] = $string;
119 }
120
121 # import existing strings. $String->save() will deal with merges
droy4f4c4072008-01-22 16:12:45 +0000122 $previous_line = "";
123 $lines = explode("\n", $_content);
kitlo8c31bd82009-11-09 23:52:05 +0000124 $non_translatable = FALSE;
droy81777472007-11-28 21:35:58 +0000125 foreach($lines as $line) {
kitlo8c31bd82009-11-09 23:52:05 +0000126 if(strlen($line) > 0 && $line[0] != "!" && $line[0] != ";") {
droye97d6fe2008-06-16 19:31:15 +0000127 # Bug 235553 - don't trim the space at the end of a line!
128 # $line = trim($line);
droy3bb0c962008-01-17 20:45:01 +0000129
kitlo8c31bd82009-11-09 23:52:05 +0000130 if($line[0] == "#") {
131 $tokens = preg_split("/[\s]+/", $line);
kitlo27e80be2010-01-13 13:03:55 +0000132 if (sizeof($tokens) > 2 && $tokens[2] == "NON-TRANSLATABLE") {
kitlo8c31bd82009-11-09 23:52:05 +0000133 if($tokens[1] == "START")
134 $non_translatable = TRUE;
135 elseif($tokens[1] == "END")
136 $non_translatable = FALSE;
droy81777472007-11-28 21:35:58 +0000137 }
kitlo8c31bd82009-11-09 23:52:05 +0000138 }
139 elseif($non_translatable == FALSE) {
140 # Does line end with a \ ?
141 if(preg_match("/\\\\$/", $line)) {
142 # Line ends with \
143
144 # strip the backslash
145 $previous_line .= $line . "\n";
146 }
147 else {
148 if($previous_line != "") {
149 $line = $previous_line . $line;
150 $previous_line = "";
droy4f4c4072008-01-22 16:12:45 +0000151 }
kitlo8c31bd82009-11-09 23:52:05 +0000152
153 $tags = explode("=", $line, 2);
154 if(count($tags) > 1) {
155 if($rValue != "") {
156 $rValue .= ",";
157 }
158 $tags[0] = trim($tags[0]);
159 # Bug 235553 - don't trim the space at the end of a line!
160 # Bug 258749 - use ltrim() to remove spaces at the beginning of value string
161 $tags[1] = ltrim($tags[1]);
162
163 $rValue .= $tags[0];
164
165 $String = new String();
166 $String->file_id = $this->file_id;
167 $String->name = $tags[0];
168 $String->value = $tags[1];
169 $String->userid = $User->userid;
170 $String->created_on = getCURDATE();
171 $String->is_active = 1;
172 $String->save();
kitlo2e780dc2010-01-12 17:58:32 +0000173
174 # remove the string from the list
175 unset($strings[$String->string_id]);
kitlo8c31bd82009-11-09 23:52:05 +0000176 }
droy4f4c4072008-01-22 16:12:45 +0000177 }
droy81777472007-11-28 21:35:58 +0000178 }
179 }
180 }
kitlo2e780dc2010-01-12 17:58:32 +0000181
182 # remove strings that are no longer in the properties file
183 foreach ($strings as $string) {
kitlo910d17f2010-02-12 17:17:44 +0000184 $string->is_active = 0;
185 if (!$string->save()) {
kitlo2e780dc2010-01-12 17:58:32 +0000186 echo "***ERROR: Cannot deactivate string $string->name in file $string->file_id\n";
187 }
droy37f2bc52007-11-29 16:32:21 +0000188 }
droy81777472007-11-28 21:35:58 +0000189 }
190 return $rValue;
191 }
kitlo2e780dc2010-01-12 17:58:32 +0000192
atoulme425b6c82008-11-24 14:00:27 +0000193 /**
194 * Returns the fragment relative path.
195 */
196 function findFragmentRelativePath() {
197 # strip useless CVS structure before the plugin name (bug 221675 c14):
198 $pattern = '/^([a-zA-Z0-9\/_-])+\/([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)(.*)\.properties$/i';
199 $replace = '${2}.${3}${4}.properties';
200 $path = preg_replace($pattern, $replace, $this->name);
201
202 # strip source folder (bug 221675) (org.eclipse.plugin/source_folder/org/eclipse/plugin)
203 $pattern = '/^([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)\.([a-zA-Z0-9\._-]+)(.*)\/(\1)([\.\/])(\2)([\.\/])(.*)\.properties$/i';
204 $replace = '${1}.${2}.${3}/${5}${6}${7}${8}${9}.properties';
205 $path = preg_replace($pattern, $replace, $path);
206
207 return $path;
208 }
209
210 /*
211 * Convert the filename to *_lang.properties, e.g., foo_fr.properties
212 */
atoulmecefb0242008-11-24 16:00:31 +0000213 function appendLangCode($language_iso, $filename = null) {
atoulmedfee65f2008-11-24 14:59:26 +0000214 if (!$filename) {
atoulme786dc552008-11-24 15:50:18 +0000215 $filename = $this->findFragmentRelativePath();
atoulmedfee65f2008-11-24 14:59:26 +0000216 }
atoulme425b6c82008-11-24 14:00:27 +0000217 if (preg_match( "/^(.*)\.properties$/", $filename, $matches)) {
218 $filename = $matches[1] . '_' . $language_iso . '.properties';
219 }
220 return $filename;
221 }
222
223 /**
224 * returns a hash that contains a mapping from the translation keys to the translation values.
225 */
226 function strings4PropertiesFile($language) {
227 $result = array();
228 if (strcmp($language->iso, "en_AA") == 0) {
229 $sql = "SELECT string_id, name, value FROM strings WHERE file_id = " . $this->file_id .
230 " AND is_active AND non_translatable = 0";
231 $strings_result = mysql_query($sql);
232 while (($strings_row = mysql_fetch_assoc($strings_result)) != null) {
233 $result[$strings_row['name']] = $this->project_id . $strings_row['string_id'] . ":" . $strings_row['value'];
atoulmedfee65f2008-11-24 14:59:26 +0000234 }
atoulme425b6c82008-11-24 14:00:27 +0000235 } else {
236 $sql = "SELECT
237 strings.name AS 'key',
238 strings.value AS orig,
239 translations.value AS trans
240 FROM strings, translations
241 WHERE strings.string_id = translations.string_id
242 AND strings.file_id = " . $this->file_id . "
243 AND strings.is_active
244 AND strings.non_translatable = 0
245 AND translations.language_id = " . $language->id . "
246 AND translations.is_active";
247 $strings_result = mysql_query($sql);
248 while (($strings_row = mysql_fetch_assoc($strings_result)) != null) {
249 $result[$strings_row['key']] = $strings_row['trans'];
250 }
251 }
252 return $result;
253 }
droy81777472007-11-28 21:35:58 +0000254}
droyb9c2cb12008-10-08 13:58:14 +0000255?>