droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 1 | <?php |
| 2 | /******************************************************************************* |
Kit Lo | 6d440cc | 2013-03-02 19:56:45 -0500 | [diff] [blame] | 3 | * Copyright (c) 2007-2013 Eclipse Foundation, IBM Corporation and others. |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 4 | * 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 |
droy | b9c2cb1 | 2008-10-08 13:58:14 +0000 | [diff] [blame] | 11 | * Antoine Toulmé - Bug 248917 |
kitlo | ff50b95 | 2009-03-12 12:46:59 +0000 | [diff] [blame] | 12 | * Kit Lo (IBM) - patch, bug 266250, Map file processor not running properly on live server |
kitlo | 9207be3 | 2009-04-05 04:58:23 +0000 | [diff] [blame] | 13 | * Kit Lo (IBM) - patch, bug 258749, Keep spaces at the end of value string |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 14 | * Kit Lo (IBM) - patch, bug 226378, Non-translatable strings or files should not be presented to user for translation |
kitlo | 2e780dc | 2010-01-12 17:58:32 +0000 | [diff] [blame] | 15 | * Kit Lo (IBM) - Bug 299402, Extract properties files from Eclipse project update sites for translation |
Kit Lo | 6d440cc | 2013-03-02 19:56:45 -0500 | [diff] [blame] | 16 | * Kit Lo (IBM) - [402215] Extract Orion JavaScript files for translation |
Kit Lo | 178cac0 | 2013-07-22 11:11:41 -0400 | [diff] [blame] | 17 | * Kit Lo (IBM) - [413459] Received "Cannot deactivate string" messages during process_project_source_locations.php |
Kit Lo | 6d440cc | 2013-03-02 19:56:45 -0500 | [diff] [blame] | 18 | *******************************************************************************/ |
| 19 | |
| 20 | require_once("/home/data/httpd/babel.eclipse.org/html/json_encode.php"); |
atoulme | 12882d5 | 2009-01-26 18:39:17 +0000 | [diff] [blame] | 21 | require(dirname(__FILE__) . "/../system/language.class.php"); |
| 22 | require(dirname(__FILE__) . "/../system/release_train.class.php"); |
atoulme | 425b6c8 | 2008-11-24 14:00:27 +0000 | [diff] [blame] | 23 | |
atoulme | 12882d5 | 2009-01-26 18:39:17 +0000 | [diff] [blame] | 24 | require(dirname(__FILE__) . "/../string/string.class.php"); |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 25 | |
| 26 | class File { |
| 27 | public $errStrs; |
| 28 | |
| 29 | public $file_id = 0; |
| 30 | public $project_id = ''; |
droy | 3bb0c96 | 2008-01-17 20:45:01 +0000 | [diff] [blame] | 31 | public $version = ''; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 32 | public $name = ''; |
| 33 | public $is_active = 0; |
droy | b9c2cb1 | 2008-10-08 13:58:14 +0000 | [diff] [blame] | 34 | public $plugin_id = ''; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 35 | |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 36 | function save() { |
| 37 | $rValue = false; |
droy | aead658 | 2008-02-25 19:47:15 +0000 | [diff] [blame] | 38 | if($this->name != "" && $this->project_id != "" && $this->version != "") { |
atoulme | 3e5e934 | 2009-01-23 17:34:30 +0000 | [diff] [blame] | 39 | global $dbh; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 40 | |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 41 | if($this->file_id == 0) { |
droy | 3bb0c96 | 2008-01-17 20:45:01 +0000 | [diff] [blame] | 42 | $this->file_id = $this->getFileID($this->name, $this->project_id, $this->version); |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | $sql = "INSERT INTO"; |
| 46 | $where = ""; |
| 47 | if($this->file_id > 0) { |
| 48 | $sql = "UPDATE"; |
atoulme | 3e5e934 | 2009-01-23 17:34:30 +0000 | [diff] [blame] | 49 | $where = " WHERE file_id = " . sqlSanitize($this->file_id, $dbh); |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 50 | } |
| 51 | |
droy | 3e7f3a8 | 2007-11-29 19:35:51 +0000 | [diff] [blame] | 52 | $Event = new EventLog("files", "file_id", $this->file_id, $sql); |
| 53 | |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 54 | $sql .= " files |
atoulme | 3e5e934 | 2009-01-23 17:34:30 +0000 | [diff] [blame] | 55 | 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)) . ", |
atoulme | 2e3c33c | 2009-02-19 14:53:15 +0000 | [diff] [blame] | 60 | is_active = " . $this->is_active . $where; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 61 | if(mysql_query($sql, $dbh)) { |
| 62 | if($this->file_id == 0) { |
| 63 | $this->file_id = mysql_insert_id($dbh); |
droy | 3e7f3a8 | 2007-11-29 19:35:51 +0000 | [diff] [blame] | 64 | $Event->key_value = $this->file_id; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 65 | } |
| 66 | $rValue = true; |
droy | 3e7f3a8 | 2007-11-29 19:35:51 +0000 | [diff] [blame] | 67 | $Event->add(); |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 68 | } |
| 69 | else { |
kitlo | 2e780dc | 2010-01-12 17:58:32 +0000 | [diff] [blame] | 70 | echo $sql . "\n"; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 71 | $GLOBALS['g_ERRSTRS'][1] = mysql_error(); |
| 72 | } |
| 73 | } |
droy | aead658 | 2008-02-25 19:47:15 +0000 | [diff] [blame] | 74 | else { |
| 75 | echo "ERROR: One missing:Name: " . $this->name . "Project: " . $this->project_id . "Version: " . $this->version; |
| 76 | } |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 77 | return $rValue; |
| 78 | } |
| 79 | |
atoulme | be3eae8 | 2008-12-05 16:38:20 +0000 | [diff] [blame] | 80 | static function getFileID($_name, $_project_id, $_version) { |
kitlo | ff50b95 | 2009-03-12 12:46:59 +0000 | [diff] [blame] | 81 | $rValue = 0; |
atoulme | be3eae8 | 2008-12-05 16:38:20 +0000 | [diff] [blame] | 82 | if($_name != "" && $_project_id != "" && $_version != "") { |
atoulme | 3e5e934 | 2009-01-23 17:34:30 +0000 | [diff] [blame] | 83 | global $dbh; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 84 | |
| 85 | $sql = "SELECT file_id |
| 86 | FROM |
| 87 | files |
atoulme | 3e5e934 | 2009-01-23 17:34:30 +0000 | [diff] [blame] | 88 | WHERE name = " . returnQuotedString(sqlSanitize($_name, $dbh)) . " |
| 89 | AND project_id = " . returnQuotedString(sqlSanitize($_project_id, $dbh)) . " |
| 90 | AND version = '" . sqlSanitize($_version, $dbh) . "'"; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 91 | |
| 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 != "") { |
droy | 37f2bc5 | 2007-11-29 16:32:21 +0000 | [diff] [blame] | 104 | |
atoulme | 3e5e934 | 2009-01-23 17:34:30 +0000 | [diff] [blame] | 105 | global $User; |
kitlo | 2e780dc | 2010-01-12 17:58:32 +0000 | [diff] [blame] | 106 | |
| 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 Lo | 6d440cc | 2013-03-02 19:56:45 -0500 | [diff] [blame] | 124 | # import existing strings, $String->save() will deal with merges |
droy | 4f4c407 | 2008-01-22 16:12:45 +0000 | [diff] [blame] | 125 | $previous_line = ""; |
| 126 | $lines = explode("\n", $_content); |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 127 | $non_translatable = FALSE; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 128 | foreach($lines as $line) { |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 129 | if(strlen($line) > 0 && $line[0] != "!" && $line[0] != ";") { |
droy | e97d6fe | 2008-06-16 19:31:15 +0000 | [diff] [blame] | 130 | # Bug 235553 - don't trim the space at the end of a line! |
| 131 | # $line = trim($line); |
droy | 3bb0c96 | 2008-01-17 20:45:01 +0000 | [diff] [blame] | 132 | |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 133 | if($line[0] == "#") { |
| 134 | $tokens = preg_split("/[\s]+/", $line); |
kitlo | 27e80be | 2010-01-13 13:03:55 +0000 | [diff] [blame] | 135 | if (sizeof($tokens) > 2 && $tokens[2] == "NON-TRANSLATABLE") { |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 136 | if($tokens[1] == "START") |
| 137 | $non_translatable = TRUE; |
| 138 | elseif($tokens[1] == "END") |
| 139 | $non_translatable = FALSE; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 140 | } |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 141 | } |
| 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 = ""; |
droy | 4f4c407 | 2008-01-22 16:12:45 +0000 | [diff] [blame] | 154 | } |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 155 | |
| 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(); |
kitlo | 2e780dc | 2010-01-12 17:58:32 +0000 | [diff] [blame] | 176 | |
| 177 | # remove the string from the list |
| 178 | unset($strings[$String->string_id]); |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 179 | } |
droy | 4f4c407 | 2008-01-22 16:12:45 +0000 | [diff] [blame] | 180 | } |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
kitlo | 2e780dc | 2010-01-12 17:58:32 +0000 | [diff] [blame] | 184 | |
| 185 | # remove strings that are no longer in the properties file |
| 186 | foreach ($strings as $string) { |
kitlo | 910d17f | 2010-02-12 17:17:44 +0000 | [diff] [blame] | 187 | $string->is_active = 0; |
| 188 | if (!$string->save()) { |
kitlo | 2e780dc | 2010-01-12 17:58:32 +0000 | [diff] [blame] | 189 | echo "***ERROR: Cannot deactivate string $string->name in file $string->file_id\n"; |
| 190 | } |
droy | 37f2bc5 | 2007-11-29 16:32:21 +0000 | [diff] [blame] | 191 | } |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 192 | } |
| 193 | return $rValue; |
| 194 | } |
kitlo | 2e780dc | 2010-01-12 17:58:32 +0000 | [diff] [blame] | 195 | |
Kit Lo | 6d440cc | 2013-03-02 19:56:45 -0500 | [diff] [blame] | 196 | 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) { |
kitlo | 840aadc | 2014-06-15 03:18:54 -0400 | [diff] [blame] | 226 | # escape newlines and tabs |
| 227 | $value = preg_replace("/\\n/", "\\\\n", $value); |
| 228 | $value = preg_replace("/\\t/", "\\\\t", $value);
|
| 229 | |
Kit Lo | 6d440cc | 2013-03-02 19:56:45 -0500 | [diff] [blame] | 230 | $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 Lo | 178cac0 | 2013-07-22 11:11:41 -0400 | [diff] [blame] | 246 | if (!$string->saveJs()) { |
Kit Lo | 6d440cc | 2013-03-02 19:56:45 -0500 | [diff] [blame] | 247 | echo "***ERROR: Cannot deactivate string $string->name in file $string->file_id\n"; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
atoulme | 425b6c8 | 2008-11-24 14:00:27 +0000 | [diff] [blame] | 253 | /** |
| 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 | */ |
atoulme | cefb024 | 2008-11-24 16:00:31 +0000 | [diff] [blame] | 273 | function appendLangCode($language_iso, $filename = null) { |
atoulme | dfee65f | 2008-11-24 14:59:26 +0000 | [diff] [blame] | 274 | if (!$filename) { |
atoulme | 786dc55 | 2008-11-24 15:50:18 +0000 | [diff] [blame] | 275 | $filename = $this->findFragmentRelativePath(); |
atoulme | dfee65f | 2008-11-24 14:59:26 +0000 | [diff] [blame] | 276 | } |
atoulme | 425b6c8 | 2008-11-24 14:00:27 +0000 | [diff] [blame] | 277 | 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']; |
atoulme | dfee65f | 2008-11-24 14:59:26 +0000 | [diff] [blame] | 294 | } |
atoulme | 425b6c8 | 2008-11-24 14:00:27 +0000 | [diff] [blame] | 295 | } 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 | } |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 314 | } |
droy | b9c2cb1 | 2008-10-08 13:58:14 +0000 | [diff] [blame] | 315 | ?> |