droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 1 | <?php |
| 2 | /******************************************************************************* |
droy | b9c2cb1 | 2008-10-08 13:58:14 +0000 | [diff] [blame] | 3 | * Copyright (c) 2007,2008 Eclipse Foundation 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 |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 16 | *******************************************************************************/ |
atoulme | 12882d5 | 2009-01-26 18:39:17 +0000 | [diff] [blame] | 17 | require(dirname(__FILE__) . "/../system/language.class.php"); |
| 18 | require(dirname(__FILE__) . "/../system/release_train.class.php"); |
atoulme | 425b6c8 | 2008-11-24 14:00:27 +0000 | [diff] [blame] | 19 | |
atoulme | 12882d5 | 2009-01-26 18:39:17 +0000 | [diff] [blame] | 20 | require(dirname(__FILE__) . "/../string/string.class.php"); |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 21 | |
| 22 | class File { |
| 23 | public $errStrs; |
| 24 | |
| 25 | public $file_id = 0; |
| 26 | public $project_id = ''; |
droy | 3bb0c96 | 2008-01-17 20:45:01 +0000 | [diff] [blame] | 27 | public $version = ''; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 28 | public $name = ''; |
| 29 | public $is_active = 0; |
droy | b9c2cb1 | 2008-10-08 13:58:14 +0000 | [diff] [blame] | 30 | public $plugin_id = ''; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 31 | |
| 32 | |
| 33 | function save() { |
| 34 | $rValue = false; |
droy | aead658 | 2008-02-25 19:47:15 +0000 | [diff] [blame] | 35 | if($this->name != "" && $this->project_id != "" && $this->version != "") { |
atoulme | 3e5e934 | 2009-01-23 17:34:30 +0000 | [diff] [blame] | 36 | global $dbh; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 37 | |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 38 | if($this->file_id == 0) { |
droy | 3bb0c96 | 2008-01-17 20:45:01 +0000 | [diff] [blame] | 39 | $this->file_id = $this->getFileID($this->name, $this->project_id, $this->version); |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | $sql = "INSERT INTO"; |
| 43 | $where = ""; |
| 44 | if($this->file_id > 0) { |
| 45 | $sql = "UPDATE"; |
atoulme | 3e5e934 | 2009-01-23 17:34:30 +0000 | [diff] [blame] | 46 | $where = " WHERE file_id = " . sqlSanitize($this->file_id, $dbh); |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 47 | } |
| 48 | |
droy | 3e7f3a8 | 2007-11-29 19:35:51 +0000 | [diff] [blame] | 49 | $Event = new EventLog("files", "file_id", $this->file_id, $sql); |
| 50 | |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 51 | $sql .= " files |
atoulme | 3e5e934 | 2009-01-23 17:34:30 +0000 | [diff] [blame] | 52 | 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)) . ", |
atoulme | 2e3c33c | 2009-02-19 14:53:15 +0000 | [diff] [blame] | 57 | is_active = " . $this->is_active . $where; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 58 | if(mysql_query($sql, $dbh)) { |
| 59 | if($this->file_id == 0) { |
| 60 | $this->file_id = mysql_insert_id($dbh); |
droy | 3e7f3a8 | 2007-11-29 19:35:51 +0000 | [diff] [blame] | 61 | $Event->key_value = $this->file_id; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 62 | } |
| 63 | $rValue = true; |
droy | 3e7f3a8 | 2007-11-29 19:35:51 +0000 | [diff] [blame] | 64 | $Event->add(); |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 65 | } |
| 66 | else { |
kitlo | 2e780dc | 2010-01-12 17:58:32 +0000 | [diff] [blame] | 67 | echo $sql . "\n"; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 68 | $GLOBALS['g_ERRSTRS'][1] = mysql_error(); |
| 69 | } |
| 70 | } |
droy | aead658 | 2008-02-25 19:47:15 +0000 | [diff] [blame] | 71 | else { |
| 72 | echo "ERROR: One missing:Name: " . $this->name . "Project: " . $this->project_id . "Version: " . $this->version; |
| 73 | } |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 74 | return $rValue; |
| 75 | } |
| 76 | |
atoulme | be3eae8 | 2008-12-05 16:38:20 +0000 | [diff] [blame] | 77 | static function getFileID($_name, $_project_id, $_version) { |
kitlo | ff50b95 | 2009-03-12 12:46:59 +0000 | [diff] [blame] | 78 | $rValue = 0; |
atoulme | be3eae8 | 2008-12-05 16:38:20 +0000 | [diff] [blame] | 79 | if($_name != "" && $_project_id != "" && $_version != "") { |
atoulme | 3e5e934 | 2009-01-23 17:34:30 +0000 | [diff] [blame] | 80 | global $dbh; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 81 | |
| 82 | $sql = "SELECT file_id |
| 83 | FROM |
| 84 | files |
atoulme | 3e5e934 | 2009-01-23 17:34:30 +0000 | [diff] [blame] | 85 | WHERE name = " . returnQuotedString(sqlSanitize($_name, $dbh)) . " |
| 86 | AND project_id = " . returnQuotedString(sqlSanitize($_project_id, $dbh)) . " |
| 87 | AND version = '" . sqlSanitize($_version, $dbh) . "'"; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 88 | |
| 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 != "") { |
droy | 37f2bc5 | 2007-11-29 16:32:21 +0000 | [diff] [blame] | 101 | |
atoulme | 3e5e934 | 2009-01-23 17:34:30 +0000 | [diff] [blame] | 102 | global $User; |
kitlo | 2e780dc | 2010-01-12 17:58:32 +0000 | [diff] [blame] | 103 | |
| 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 |
droy | 4f4c407 | 2008-01-22 16:12:45 +0000 | [diff] [blame] | 122 | $previous_line = ""; |
| 123 | $lines = explode("\n", $_content); |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 124 | $non_translatable = FALSE; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 125 | foreach($lines as $line) { |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 126 | if(strlen($line) > 0 && $line[0] != "!" && $line[0] != ";") { |
droy | e97d6fe | 2008-06-16 19:31:15 +0000 | [diff] [blame] | 127 | # Bug 235553 - don't trim the space at the end of a line! |
| 128 | # $line = trim($line); |
droy | 3bb0c96 | 2008-01-17 20:45:01 +0000 | [diff] [blame] | 129 | |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 130 | if($line[0] == "#") { |
| 131 | $tokens = preg_split("/[\s]+/", $line); |
kitlo | 27e80be | 2010-01-13 13:03:55 +0000 | [diff] [blame] | 132 | if (sizeof($tokens) > 2 && $tokens[2] == "NON-TRANSLATABLE") { |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 133 | if($tokens[1] == "START") |
| 134 | $non_translatable = TRUE; |
| 135 | elseif($tokens[1] == "END") |
| 136 | $non_translatable = FALSE; |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 137 | } |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 138 | } |
| 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 = ""; |
droy | 4f4c407 | 2008-01-22 16:12:45 +0000 | [diff] [blame] | 151 | } |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 152 | |
| 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(); |
kitlo | 2e780dc | 2010-01-12 17:58:32 +0000 | [diff] [blame] | 173 | |
| 174 | # remove the string from the list |
| 175 | unset($strings[$String->string_id]); |
kitlo | 8c31bd8 | 2009-11-09 23:52:05 +0000 | [diff] [blame] | 176 | } |
droy | 4f4c407 | 2008-01-22 16:12:45 +0000 | [diff] [blame] | 177 | } |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |
kitlo | 2e780dc | 2010-01-12 17:58:32 +0000 | [diff] [blame] | 181 | |
| 182 | # remove strings that are no longer in the properties file |
| 183 | foreach ($strings as $string) { |
kitlo | 910d17f | 2010-02-12 17:17:44 +0000 | [diff] [blame] | 184 | $string->is_active = 0; |
| 185 | if (!$string->save()) { |
kitlo | 2e780dc | 2010-01-12 17:58:32 +0000 | [diff] [blame] | 186 | echo "***ERROR: Cannot deactivate string $string->name in file $string->file_id\n"; |
| 187 | } |
droy | 37f2bc5 | 2007-11-29 16:32:21 +0000 | [diff] [blame] | 188 | } |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 189 | } |
| 190 | return $rValue; |
| 191 | } |
kitlo | 2e780dc | 2010-01-12 17:58:32 +0000 | [diff] [blame] | 192 | |
atoulme | 425b6c8 | 2008-11-24 14:00:27 +0000 | [diff] [blame] | 193 | /** |
| 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 | */ |
atoulme | cefb024 | 2008-11-24 16:00:31 +0000 | [diff] [blame] | 213 | function appendLangCode($language_iso, $filename = null) { |
atoulme | dfee65f | 2008-11-24 14:59:26 +0000 | [diff] [blame] | 214 | if (!$filename) { |
atoulme | 786dc55 | 2008-11-24 15:50:18 +0000 | [diff] [blame] | 215 | $filename = $this->findFragmentRelativePath(); |
atoulme | dfee65f | 2008-11-24 14:59:26 +0000 | [diff] [blame] | 216 | } |
atoulme | 425b6c8 | 2008-11-24 14:00:27 +0000 | [diff] [blame] | 217 | 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']; |
atoulme | dfee65f | 2008-11-24 14:59:26 +0000 | [diff] [blame] | 234 | } |
atoulme | 425b6c8 | 2008-11-24 14:00:27 +0000 | [diff] [blame] | 235 | } 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 | } |
droy | 8177747 | 2007-11-28 21:35:58 +0000 | [diff] [blame] | 254 | } |
droy | b9c2cb1 | 2008-10-08 13:58:14 +0000 | [diff] [blame] | 255 | ?> |